feat: 切换 schema dump 基线并增强返点结算与管理校验

This commit is contained in:
2026-06-08 17:41:41 +08:00
parent 2d32f006c5
commit 8d5d7f5b17
130 changed files with 5746 additions and 6723 deletions

View File

@@ -5,6 +5,7 @@ namespace App\Services\AgentSettlement;
use App\Models\Player;
use App\Services\Player\PlayerCreditService;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
final class SettlementPaymentService
{
@@ -25,66 +26,72 @@ final class SettlementPaymentService
*/
public function recordPayment(int $billId, int $amount, int $adminUserId, array $meta = []): void
{
$bill = DB::table('settlement_bills')->where('id', $billId)->first();
if ($bill === null) {
throw new \InvalidArgumentException('bill_not_found');
}
DB::transaction(function () use ($billId, $amount, $adminUserId, $meta): void {
$bill = DB::table('settlement_bills')->where('id', $billId)->lockForUpdate()->first();
if ($bill === null) {
throw new \InvalidArgumentException('bill_not_found');
}
$this->billGuard->assertPeriodMutable($billId);
$this->billGuard->assertPayable($billId);
$this->billGuard->assertPeriodMutable($billId);
if (! in_array((string) $bill->status, ['confirmed', 'partial_paid', 'overdue'], true)) {
throw ValidationException::withMessages([
'bill' => ['not_payable'],
]);
}
$amount = min($amount, abs((int) $bill->unpaid_amount));
if ($amount <= 0) {
return;
}
$payAmount = min($amount, abs((int) $bill->unpaid_amount));
if ($payAmount <= 0) {
return;
}
[$payerType, $payerId, $payeeType, $payeeId] = $this->resolvePayerPayee($bill);
[$payerType, $payerId, $payeeType, $payeeId] = $this->resolvePayerPayee($bill);
DB::table('payment_records')->insert([
'settlement_bill_id' => $billId,
'payer_type' => $payerType,
'payer_id' => $payerId,
'payee_type' => $payeeType,
'payee_id' => $payeeId,
'amount' => $amount,
'method' => $meta['method'] ?? null,
'proof' => $meta['proof'] ?? null,
'remark' => $meta['remark'] ?? null,
'status' => 'confirmed',
'created_by' => $adminUserId,
'confirmed_by' => $adminUserId,
'confirmed_at' => now(),
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('payment_records')->insert([
'settlement_bill_id' => $billId,
'payer_type' => $payerType,
'payer_id' => $payerId,
'payee_type' => $payeeType,
'payee_id' => $payeeId,
'amount' => $payAmount,
'method' => $meta['method'] ?? null,
'proof' => $meta['proof'] ?? null,
'remark' => $meta['remark'] ?? null,
'status' => 'confirmed',
'created_by' => $adminUserId,
'confirmed_by' => $adminUserId,
'confirmed_at' => now(),
'created_at' => now(),
'updated_at' => now(),
]);
$newPaid = (int) $bill->paid_amount + $amount;
$newUnpaid = max(0, (int) $bill->unpaid_amount - $amount);
$status = $newUnpaid === 0 ? 'settled' : 'partial_paid';
$newPaid = (int) $bill->paid_amount + $payAmount;
$newUnpaid = max(0, (int) $bill->unpaid_amount - $payAmount);
$status = $newUnpaid === 0 ? 'settled' : 'partial_paid';
DB::table('settlement_bills')->where('id', $billId)->update([
'paid_amount' => $newPaid,
'unpaid_amount' => $newUnpaid,
'status' => $status,
'updated_at' => now(),
]);
DB::table('settlement_bills')->where('id', $billId)->update([
'paid_amount' => $newPaid,
'unpaid_amount' => $newUnpaid,
'status' => $status,
'updated_at' => now(),
]);
if ($bill->owner_type === 'player' && (int) $bill->owner_id > 0) {
$player = Player::query()->find((int) $bill->owner_id);
if ($player !== null) {
if ((int) $bill->net_amount > 0) {
$this->playerCreditService->releaseFromSettlement($player, $amount, $billId);
} elseif ((int) $bill->net_amount < 0) {
$this->playerCreditService->applySettlementPayout($player, $amount, $billId);
}
if ($bill->owner_type === 'player' && (int) $bill->owner_id > 0) {
$player = Player::query()->find((int) $bill->owner_id);
if ($player !== null) {
if ((int) $bill->net_amount > 0) {
$this->playerCreditService->releaseFromSettlement($player, $payAmount, $billId);
} elseif ((int) $bill->net_amount < 0) {
$this->playerCreditService->applySettlementPayout($player, $payAmount, $billId);
}
if ($status === 'settled') {
$this->periodCloseRebate->markRebatesSettledForBill($billId);
if ($status === 'settled') {
$this->periodCloseRebate->markRebatesSettledForBill($billId);
}
}
}
}
$this->periodCompletion->syncIfReady((int) $bill->settlement_period_id);
$this->periodCompletion->syncIfReady((int) $bill->settlement_period_id);
});
}
/**