fix(funds): 加固转账冲正、结算收付幂等与坏账核销
- 转入 main_site_timeout 等不可冲正场景直接拒绝,避免假结案 - payment_records / settlement_adjustments 增加 partial unique 索引 - 坏账核销行锁 + meta 幂等回放;补差单记录 result_bill_id - 新增 FundOperationsHardeningTest 覆盖关键路径
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Services\AgentSettlement;
|
||||
|
||||
use App\Support\DatabaseUniqueViolation;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
@@ -47,15 +49,9 @@ final class AgentSettlementBillAdjustmentService
|
||||
}
|
||||
|
||||
if ($idempotencyKey !== null && $idempotencyKey !== '') {
|
||||
$existing = DB::table('settlement_adjustments')
|
||||
->where('original_bill_id', $originalBillId)
|
||||
->where('idempotency_key', $idempotencyKey)
|
||||
->first();
|
||||
if ($existing !== null) {
|
||||
return (int) DB::table('settlement_bills')
|
||||
->where('reversed_bill_id', $originalBillId)
|
||||
->where('bill_type', 'adjustment')
|
||||
->value('id');
|
||||
$existingBillId = $this->findAdjustmentBillByIdempotency($originalBillId, $idempotencyKey);
|
||||
if ($existingBillId !== null) {
|
||||
return $existingBillId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,59 +59,89 @@ final class AgentSettlementBillAdjustmentService
|
||||
? $adjustmentType
|
||||
: 'adjustment';
|
||||
|
||||
return (int) DB::transaction(function () use ($original, $amount, $type, $reason, $adminUserId, $idempotencyKey): int {
|
||||
if ($idempotencyKey !== null && $idempotencyKey !== '') {
|
||||
$existing = DB::table('settlement_adjustments')
|
||||
->where('original_bill_id', (int) $original->id)
|
||||
->where('idempotency_key', $idempotencyKey)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
if ($existing !== null) {
|
||||
return (int) DB::table('settlement_bills')
|
||||
->where('reversed_bill_id', (int) $original->id)
|
||||
->where('bill_type', 'adjustment')
|
||||
->value('id');
|
||||
try {
|
||||
return (int) DB::transaction(function () use ($original, $amount, $type, $reason, $adminUserId, $idempotencyKey): int {
|
||||
return $this->insertAdjustmentBill($original, $amount, $type, $reason, $adminUserId, $idempotencyKey);
|
||||
});
|
||||
} catch (QueryException $e) {
|
||||
if (
|
||||
$idempotencyKey !== null
|
||||
&& $idempotencyKey !== ''
|
||||
&& DatabaseUniqueViolation::matches($e)
|
||||
) {
|
||||
$existingBillId = $this->findAdjustmentBillByIdempotency((int) $original->id, $idempotencyKey);
|
||||
if ($existingBillId !== null) {
|
||||
return $existingBillId;
|
||||
}
|
||||
}
|
||||
|
||||
$now = now();
|
||||
$newBillId = (int) DB::table('settlement_bills')->insertGetId([
|
||||
'settlement_period_id' => (int) $original->settlement_period_id,
|
||||
'bill_type' => $type,
|
||||
'owner_type' => (string) $original->owner_type,
|
||||
'owner_id' => (int) $original->owner_id,
|
||||
'counterparty_type' => (string) $original->counterparty_type,
|
||||
'counterparty_id' => (int) $original->counterparty_id,
|
||||
'gross_win_loss' => 0,
|
||||
'rebate_amount' => 0,
|
||||
'adjustment_amount' => $amount,
|
||||
'platform_rounding_adjustment' => 0,
|
||||
'net_amount' => $amount,
|
||||
'paid_amount' => 0,
|
||||
'unpaid_amount' => abs($amount),
|
||||
'status' => 'pending_confirm',
|
||||
'reversed_bill_id' => (int) $original->id,
|
||||
'meta_json' => json_encode([
|
||||
'original_bill_id' => (int) $original->id,
|
||||
'original_net_amount' => (int) $original->net_amount,
|
||||
]),
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
|
||||
DB::table('settlement_adjustments')->insert([
|
||||
'settlement_period_id' => (int) $original->settlement_period_id,
|
||||
'original_bill_id' => (int) $original->id,
|
||||
'adjustment_type' => $type,
|
||||
'amount' => $amount,
|
||||
'reason' => $reason,
|
||||
'idempotency_key' => $idempotencyKey,
|
||||
'created_by' => $adminUserId > 0 ? $adminUserId : null,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
|
||||
return $newBillId;
|
||||
});
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function insertAdjustmentBill(
|
||||
object $original,
|
||||
int $amount,
|
||||
string $type,
|
||||
?string $reason,
|
||||
int $adminUserId,
|
||||
?string $idempotencyKey,
|
||||
): int {
|
||||
if ($idempotencyKey !== null && $idempotencyKey !== '') {
|
||||
$existingBillId = $this->findAdjustmentBillByIdempotency((int) $original->id, $idempotencyKey);
|
||||
if ($existingBillId !== null) {
|
||||
return $existingBillId;
|
||||
}
|
||||
}
|
||||
|
||||
$now = now();
|
||||
$newBillId = (int) DB::table('settlement_bills')->insertGetId([
|
||||
'settlement_period_id' => (int) $original->settlement_period_id,
|
||||
'bill_type' => $type,
|
||||
'owner_type' => (string) $original->owner_type,
|
||||
'owner_id' => (int) $original->owner_id,
|
||||
'counterparty_type' => (string) $original->counterparty_type,
|
||||
'counterparty_id' => (int) $original->counterparty_id,
|
||||
'gross_win_loss' => 0,
|
||||
'rebate_amount' => 0,
|
||||
'adjustment_amount' => $amount,
|
||||
'platform_rounding_adjustment' => 0,
|
||||
'net_amount' => $amount,
|
||||
'paid_amount' => 0,
|
||||
'unpaid_amount' => abs($amount),
|
||||
'status' => 'pending_confirm',
|
||||
'reversed_bill_id' => (int) $original->id,
|
||||
'meta_json' => json_encode([
|
||||
'original_bill_id' => (int) $original->id,
|
||||
'original_net_amount' => (int) $original->net_amount,
|
||||
]),
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
|
||||
DB::table('settlement_adjustments')->insert([
|
||||
'settlement_period_id' => (int) $original->settlement_period_id,
|
||||
'original_bill_id' => (int) $original->id,
|
||||
'result_bill_id' => $newBillId,
|
||||
'adjustment_type' => $type,
|
||||
'amount' => $amount,
|
||||
'reason' => $reason,
|
||||
'idempotency_key' => $idempotencyKey,
|
||||
'created_by' => $adminUserId > 0 ? $adminUserId : null,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
|
||||
return $newBillId;
|
||||
}
|
||||
|
||||
private function findAdjustmentBillByIdempotency(int $originalBillId, string $idempotencyKey): ?int
|
||||
{
|
||||
$resultBillId = DB::table('settlement_adjustments')
|
||||
->where('original_bill_id', $originalBillId)
|
||||
->where('idempotency_key', $idempotencyKey)
|
||||
->value('result_bill_id');
|
||||
|
||||
return $resultBillId !== null ? (int) $resultBillId : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user