完善百家乐买单补偿自动领取与聊天室播报

This commit is contained in:
2026-04-11 23:43:07 +08:00
parent e43dceab2c
commit f4a632a9c1
3 changed files with 268 additions and 1 deletions
+97
View File
@@ -0,0 +1,97 @@
<?php
/**
* 文件功能:AI小班长自动领取百家乐买单活动补偿任务
*
* 当买单活动进入“可领取”状态后,异步为 AI小班长检查并领取
* 本次活动中累计的补偿金币,确保金币入账和流水日志统一走正式服务。
*/
namespace App\Jobs;
use App\Models\BaccaratLossCoverEvent;
use App\Models\BaccaratLossCoverRecord;
use App\Models\User;
use App\Services\BaccaratLossCoverService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Log;
class AiClaimBaccaratLossCoverJob implements ShouldQueue
{
use Queueable;
/**
* 最大重试次数。
*/
public int $tries = 3;
/**
* @param int $eventId 需要自动领取补偿的活动 ID
*/
public function __construct(
public readonly int $eventId,
) {}
/**
* 执行 AI 小班长自动领取补偿逻辑。
*/
public function handle(BaccaratLossCoverService $lossCoverService): void
{
$event = BaccaratLossCoverEvent::query()->find($this->eventId);
if (! $event) {
Log::channel('daily')->warning('AI小班长自动领取买单补偿失败:活动不存在', [
'event_id' => $this->eventId,
]);
return;
}
// 只有活动进入可领取状态后,才允许自动发起补偿领取。
if (! $event->isClaimable()) {
Log::channel('daily')->info('AI小班长自动领取买单补偿跳过:活动暂不可领取', [
'event_id' => $event->id,
'status' => $event->status,
]);
return;
}
$aiUser = User::query()->where('username', 'AI小班长')->first();
if (! $aiUser) {
Log::channel('daily')->warning('AI小班长自动领取买单补偿失败:未找到 AI 用户', [
'event_id' => $event->id,
]);
return;
}
$record = BaccaratLossCoverRecord::query()
->where('event_id', $event->id)
->where('user_id', $aiUser->id)
->first();
// 没有补偿记录或本次没有待领取金额时,直接记日志后结束。
if (! $record || $record->compensation_amount <= 0 || $record->claim_status !== 'pending') {
Log::channel('daily')->info('AI小班长自动领取买单补偿跳过:暂无待领取补偿', [
'event_id' => $event->id,
'user_id' => $aiUser->id,
'claim_status' => $record?->claim_status,
'compensation_amount' => $record?->compensation_amount,
]);
return;
}
$claimResult = $lossCoverService->claim($event, $aiUser);
// 统一记录自动领取结果,便于后续核对 AI 补偿发放情况。
Log::channel('daily')->info('AI小班长自动领取买单补偿结果', [
'event_id' => $event->id,
'user_id' => $aiUser->id,
'ok' => $claimResult['ok'],
'message' => $claimResult['message'],
'amount' => $claimResult['amount'] ?? 0,
]);
}
}