feat: 添加删除待审核开奖批次功能及相关错误信息
- 在 AdminAuthorizationRegistry 中新增删除待审核开奖批次的权限定义。 - 更新 API 路由以支持删除待审核开奖批次的请求。 - 在多语言文件中添加相关错误信息,确保用户在删除操作中获得清晰的反馈。 - 增加测试用例,验证管理员能够成功删除待审核的开奖批次并返回正确状态。
This commit is contained in:
62
app/Services/Draw/DrawPendingResultBatchDiscardService.php
Normal file
62
app/Services/Draw/DrawPendingResultBatchDiscardService.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?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();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user