- 在多个控制器中引入 ApiMessage,替换原有的 ApiResponse 错误处理逻辑,确保错误信息的一致性与可读性。 - 更新错误返回信息,使用更具语义的键值,提升 API 的可维护性与用户体验。 - 适配相关控制器的请求参数,确保在处理错误时能够正确返回相应的错误信息。
55 lines
1.7 KiB
PHP
55 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
|
|
|
use App\Models\Draw;
|
|
use App\Lottery\ErrorCode;
|
|
use App\Support\ApiResponse;
|
|
use App\Support\ApiMessage;
|
|
use App\Lottery\DrawStatus;
|
|
use Illuminate\Http\JsonResponse;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Services\Draw\DrawRngRunner;
|
|
|
|
final class DrawRngRunController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly DrawRngRunner $rng,
|
|
) {}
|
|
|
|
public function __invoke(Draw $draw): JsonResponse
|
|
{
|
|
try {
|
|
$batch = DB::transaction(function () use ($draw) {
|
|
/** @var Draw $locked */
|
|
$locked = Draw::query()->whereKey($draw->id)->lockForUpdate()->firstOrFail();
|
|
if ($locked->status !== DrawStatus::Closed->value || (int) $locked->settle_version > 0) {
|
|
throw new \RuntimeException('draw_not_runnable');
|
|
}
|
|
if (! (bool) $locked->is_reopened && $locked->resultBatches()->exists()) {
|
|
throw new \RuntimeException('draw_not_runnable');
|
|
}
|
|
|
|
return $this->rng->executeLocked($locked);
|
|
});
|
|
} catch (\RuntimeException $e) {
|
|
return ApiMessage::runtimeErrorResponse(request(), $e);
|
|
}
|
|
|
|
$draw->refresh();
|
|
|
|
return ApiResponse::success([
|
|
'draw_no' => $draw->draw_no,
|
|
'status' => $draw->status,
|
|
'batch' => [
|
|
'id' => (int) $batch->id,
|
|
'result_version' => (int) $batch->result_version,
|
|
'source_type' => $batch->source_type,
|
|
'status' => $batch->status,
|
|
'items_count' => $batch->items()->count(),
|
|
],
|
|
]);
|
|
}
|
|
}
|