feat: 增强抽奖管理功能,支持手动创建、更新和删除期号
- 新增 API 路由和控制器,允许管理员手动创建、更新和删除抽奖期号。 - 更新抽奖调度逻辑,确保在抽奖时间和封盘时间的管理上更加灵活。 - 添加多语言支持的错误信息,提升用户体验。 - 更新测试用例,确保新功能的正确性和稳定性。
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||||
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\Draw\DrawDestroyService;
|
||||
|
||||
final class AdminDrawBatchDestroyController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DrawDestroyService $service,
|
||||
) {}
|
||||
|
||||
public function __invoke(Request $request): JsonResponse
|
||||
{
|
||||
$drawIds = $request->input('draw_ids', []);
|
||||
|
||||
if (!is_array($drawIds) || empty($drawIds)) {
|
||||
return ApiResponse::error(trans('api.invalid_params'), ErrorCode::ClientHttpError->value, [], 400);
|
||||
}
|
||||
|
||||
$results = [
|
||||
'success' => [],
|
||||
'failed' => [],
|
||||
];
|
||||
|
||||
foreach ($drawIds as $drawId) {
|
||||
try {
|
||||
$draw = \App\Models\Draw::findOrFail($drawId);
|
||||
$this->service->destroy($draw);
|
||||
$results['success'][] = $drawId;
|
||||
} catch (\RuntimeException $e) {
|
||||
$results['failed'][] = [
|
||||
'id' => $drawId,
|
||||
'reason' => match ($e->getMessage()) {
|
||||
'draw_not_deletable' => trans('api.draw_not_deletable'),
|
||||
'draw_has_bets' => trans('api.draw_has_bets'),
|
||||
'draw_result_exists' => trans('api.draw_result_exists'),
|
||||
default => trans('api.client_error'),
|
||||
},
|
||||
];
|
||||
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
|
||||
$results['failed'][] = [
|
||||
'id' => $drawId,
|
||||
'reason' => trans('api.draw_not_found'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return ApiResponse::success($results);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||||
|
||||
use App\Models\Draw;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\Draw\DrawDestroyService;
|
||||
|
||||
final class AdminDrawDestroyController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DrawDestroyService $service,
|
||||
) {}
|
||||
|
||||
public function __invoke(Draw $draw): JsonResponse
|
||||
{
|
||||
try {
|
||||
$this->service->destroy($draw);
|
||||
} catch (\RuntimeException $e) {
|
||||
$message = match ($e->getMessage()) {
|
||||
'draw_not_deletable' => trans('api.draw_not_deletable'),
|
||||
'draw_has_bets' => trans('api.draw_has_bets'),
|
||||
'draw_result_exists' => trans('api.draw_result_exists'),
|
||||
default => trans('api.client_error'),
|
||||
};
|
||||
|
||||
return ApiResponse::error($message, ErrorCode::ClientHttpError->value, ['reason' => $e->getMessage()], 409);
|
||||
}
|
||||
|
||||
return ApiResponse::success(['deleted' => true]);
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,14 @@ final class AdminDrawIndexController extends Controller
|
||||
/** @var LengthAwarePaginator $paginator */
|
||||
$paginator = $q->paginate($p['perPage'], ['*'], 'page', $p['page']);
|
||||
|
||||
return AdminApiList::json($paginator, fn (Draw $row) => $this->row($row));
|
||||
return AdminApiList::jsonWith($paginator, fn (Draw $row) => $this->row($row), [
|
||||
'schedule' => [
|
||||
'timezone' => (string) config('lottery.draw.timezone', 'UTC'),
|
||||
'interval_minutes' => (int) config('lottery.draw.interval_minutes', 5),
|
||||
'betting_window_seconds' => (int) config('lottery.draw.betting_window_seconds', 270),
|
||||
'close_before_draw_seconds' => (int) config('lottery.draw.close_before_draw_seconds', 30),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||||
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\Draw\DrawManualCreateService;
|
||||
use App\Http\Requests\Admin\DrawStoreRequest;
|
||||
|
||||
final class AdminDrawStoreController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DrawManualCreateService $service,
|
||||
) {}
|
||||
|
||||
public function __invoke(DrawStoreRequest $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
$draw = $this->service->create($request->validated());
|
||||
} catch (\RuntimeException $e) {
|
||||
$message = match ($e->getMessage()) {
|
||||
'draw_no_exists' => trans('api.draw_no_exists'),
|
||||
'draw_timeline_invalid' => trans('api.draw_timeline_invalid'),
|
||||
default => trans('api.client_error'),
|
||||
};
|
||||
|
||||
return ApiResponse::error($message, ErrorCode::ClientHttpError->value, ['reason' => $e->getMessage()], 409);
|
||||
}
|
||||
|
||||
return ApiResponse::success([
|
||||
'id' => (int) $draw->id,
|
||||
'draw_no' => $draw->draw_no,
|
||||
'business_date' => (string) $draw->business_date,
|
||||
'sequence_no' => (int) $draw->sequence_no,
|
||||
'status' => $draw->status,
|
||||
'start_time' => $draw->start_time?->toIso8601String(),
|
||||
'close_time' => $draw->close_time?->toIso8601String(),
|
||||
'draw_time' => $draw->draw_time?->toIso8601String(),
|
||||
])->setStatusCode(201);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||||
|
||||
use App\Models\Draw;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\DrawStoreRequest;
|
||||
use App\Services\Draw\DrawManualUpdateService;
|
||||
|
||||
final class AdminDrawUpdateController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DrawManualUpdateService $service,
|
||||
) {}
|
||||
|
||||
public function __invoke(DrawStoreRequest $request, Draw $draw): JsonResponse
|
||||
{
|
||||
try {
|
||||
$updated = $this->service->update($draw, $request->validated());
|
||||
} catch (\RuntimeException $e) {
|
||||
$message = match ($e->getMessage()) {
|
||||
'draw_no_exists' => trans('api.draw_no_exists'),
|
||||
'draw_timeline_invalid' => trans('api.draw_timeline_invalid'),
|
||||
'draw_not_editable' => trans('api.draw_not_editable'),
|
||||
'draw_has_bets' => trans('api.draw_has_bets'),
|
||||
'draw_result_exists' => trans('api.draw_result_exists'),
|
||||
default => trans('api.client_error'),
|
||||
};
|
||||
|
||||
return ApiResponse::error($message, ErrorCode::ClientHttpError->value, ['reason' => $e->getMessage()], 409);
|
||||
}
|
||||
|
||||
return ApiResponse::success([
|
||||
'id' => (int) $updated->id,
|
||||
'draw_no' => $updated->draw_no,
|
||||
'business_date' => (string) $updated->business_date,
|
||||
'sequence_no' => (int) $updated->sequence_no,
|
||||
'status' => $updated->status,
|
||||
'start_time' => $updated->start_time?->toIso8601String(),
|
||||
'close_time' => $updated->close_time?->toIso8601String(),
|
||||
'draw_time' => $updated->draw_time?->toIso8601String(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user