Files
lotteryLaravel/app/Services/Draw/DrawPendingResultBatchDiscardService.php
kang b13776b480 feat: 添加删除待审核开奖批次功能及相关错误信息
- 在 AdminAuthorizationRegistry 中新增删除待审核开奖批次的权限定义。
- 更新 API 路由以支持删除待审核开奖批次的请求。
- 在多语言文件中添加相关错误信息,确保用户在删除操作中获得清晰的反馈。
- 增加测试用例,验证管理员能够成功删除待审核的开奖批次并返回正确状态。
2026-06-01 15:37:33 +08:00

63 lines
2.2 KiB
PHP

<?php
namespace App\Services\Draw;
use App\Models\Draw;
use App\Models\DrawResultBatch;
use App\Lottery\DrawStatus;
use Illuminate\Support\Facades\DB;
use App\Lottery\DrawResultBatchStatus;
use App\Models\SettlementBatch;
/**
* 删除待审核开奖批次,便于作废草稿后重新录入或重新 RNG。
*/
final class DrawPendingResultBatchDiscardService
{
public function discard(Draw $draw, DrawResultBatch $batch): Draw
{
return DB::transaction(function () use ($draw, $batch): Draw {
/** @var DrawResultBatch $lockedBatch */
$lockedBatch = DrawResultBatch::query()->whereKey($batch->id)->lockForUpdate()->firstOrFail();
if ((int) $lockedBatch->draw_id !== (int) $draw->id) {
throw new \RuntimeException('batch_draw_mismatch');
}
if ($lockedBatch->status !== DrawResultBatchStatus::PendingReview->value) {
throw new \RuntimeException('batch_not_pending_review');
}
if (SettlementBatch::query()->where('result_batch_id', $lockedBatch->id)->exists()) {
throw new \RuntimeException('batch_linked_to_settlement');
}
/** @var Draw $lockedDraw */
$lockedDraw = Draw::query()->whereKey($draw->id)->lockForUpdate()->firstOrFail();
$lockedBatch->delete();
if ($lockedDraw->status === DrawStatus::Review->value) {
$stillPending = DrawResultBatch::query()
->where('draw_id', $lockedDraw->id)
->where('status', DrawResultBatchStatus::PendingReview->value)
->exists();
if (! $stillPending) {
$hasPublished = DrawResultBatch::query()
->where('draw_id', $lockedDraw->id)
->where('status', DrawResultBatchStatus::Published->value)
->exists();
$lockedDraw->forceFill([
'status' => DrawStatus::Closed->value,
'result_source' => $hasPublished ? $lockedDraw->result_source : null,
])->save();
}
}
return $lockedDraw->refresh();
});
}
}