【发布时间】:2021-10-15 17:47:03
【问题描述】:
我有两张表和一张数据透视表。 结算表是这样的:
$table->ulid();
$table->string('track_id', 36);
$table->string('payee_user_id', 36);
$table->string('terminal_type', 36)->nullable();
$table->string('terminal_provider', 36)->nullable();
$table->string('terminal_psp', 36)->nullable();
$table->string('terminal_id', 36)->nullable();
$table->string('acceptor_id', 36)->nullable();
$table->string('terminal_no', 36)->nullable();
$table->string('account_bank_id', 36)->nullable();
$table->string('account_id', 36)->nullable();
$table->string('account_deposit', 36)->nullable();
$table->string('account_iban', 36)->nullable();
$table->integer('count')->nullable();
$table->decimal('amount_settle', 15, 2);
$table->decimal('amount_wage', 15, 2);
$table->timestamps(6);
事务表是这个
$table->ulid();
$table->string('payee_user_id', 36);
$table->string('type', 36);
$table->string('terminal_id', 36);
$table->string('terminal_no', 36);
$table->string('terminal_type', 36);
$table->string('acceptor_id', 36);
$table->string('terminal_psp', 36);
$table->string('terminal_provider', 36);
$table->decimal('amount_settle', 15, 2);
$table->decimal('amount_wage', 15, 2);
$table->string('account_id', 36)->nullable();;
$table->string('account_bank_id', 36)->nullable();;
$table->string('account_deposit', 26)->nullable();
$table->string('account_iban', 26)->nullable();
$table->string('wallet_id', 36)->nullable();
$table->timestamp('paid_at', 6);
settlements_transactions 表是这样的:
$table->string('transaction_id')->index();
$table->string('settlement_id')->index();
$table->foreign('transaction_id', 'fk_transaction_id')
->on('transactions')->references('id');
$table->foreign('settlement_id', 'fk_settlement_id')
->on('settlements')->references('id');
如果我之前没有保存,我将在结算表中插入事务组。
$transactions = DB::table('transactions')
->where('paid_at', '<', $date)
->where('terminal_type', Transaction::PRIVATE)
->where('terminal_provider', Transaction::SHAPARAK)
->distinct()->selectRaw(
'account_iban,
account_deposit,
account_bank_id,
payee_user_id,
account_id,
terminal_psp,
terminal_id,
terminal_no,
acceptor_id,
account_id,
SUM(amount_settle) as amount_settle,
SUM(amount_wage) as amount_wage,
COUNT(id) as count'
)->groupBy(
'payee_user_id',
'account_id',
'account_iban',
'terminal_psp',
'terminal_no',
'terminal_id',
'acceptor_id',
'account_id',
'account_deposit',
'account_bank_id',
)->orderBy('payee_user_id')
->orderBy('account_id')
->orderBy('terminal_psp')
->orderBy('terminal_id')
->orderBy('account_id')
->orderBy('account_deposit')
->orderBy('account_bank_id')
->orderBy('account_iban')
->orderBy('terminal_no')
->orderBy('acceptor_id')
->get();
foreach ($transactions as $transaction) {
$exist = DB::table('settlements')
->where('payee_user_id', $transaction->payee_user_id)
->where('account_id', $transaction->account_id)
->where('account_deposit', $transaction->account_deposit)
->where('terminal_id', $transaction->terminal_id)
->where('terminal_psp', $transaction->terminal_psp)
->where('account_bank_id', $transaction->account_bank_id)
->where('terminal_provider', Transaction::SHAPARAK)
->where('terminal_type', Transaction::PRIVATE)
->where('settlements.created_at', '>=', Carbon::today())
->join('settlement_statuses', 'settlement_statuses.settlement_id', 'settlements.id')
->applySettlementLastStatusSubJoin('settlement_statuses')
->applySettlementLastAccepted('settlements')
->exists();
if ($exist) {
continue;
}
//insert in settlements table
}
我不想检查循环中是否存在。如果结算表中不存在,我需要按结果获取交易组。
【问题讨论】: