feat: 添加结算功能,更新 TicketItem 模型以支持最新结算详情,增强 DrawTickService 以自动处理结算,更新 TicketWalletService 以支持派彩入账,扩展 API 路由以管理结算批次和奖池
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Ticket;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Draw;
|
||||
use App\Models\Player;
|
||||
use App\Models\TicketCombination;
|
||||
use App\Models\TicketItem;
|
||||
use App\Services\Draw\DrawResultViewService;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* `GET /api/v1/ticket/draws/{draw_no}/my-match` — 当期本人号码与已发布开奖 23 格的交集(用于开奖页高亮)。
|
||||
*/
|
||||
class TicketDrawMyMatchController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DrawResultViewService $drawResultView,
|
||||
) {}
|
||||
|
||||
public function __invoke(Request $request, string $draw_no): JsonResponse
|
||||
{
|
||||
/** @var Player $player */
|
||||
$player = $request->attributes->get('lottery_player');
|
||||
$draw_no = trim($draw_no);
|
||||
|
||||
$draw = Draw::query()->where('draw_no', $draw_no)->first();
|
||||
if ($draw === null || ! in_array($draw->status, DrawResultViewService::publishedDrawStatuses(), true)) {
|
||||
return ApiResponse::success([
|
||||
'draw_no' => $draw_no,
|
||||
'hit_numbers_4d' => [],
|
||||
'total_win_minor' => 0,
|
||||
'total_jackpot_win_minor' => 0,
|
||||
'has_bets' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
$payload = $this->drawResultView->summarizeDraw($draw);
|
||||
if ($payload === null) {
|
||||
return ApiResponse::success([
|
||||
'draw_no' => $draw_no,
|
||||
'hit_numbers_4d' => [],
|
||||
'total_win_minor' => 0,
|
||||
'total_jackpot_win_minor' => 0,
|
||||
'has_bets' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
$board = collect($payload['result_items'] ?? [])
|
||||
->pluck('number_4d')
|
||||
->filter()
|
||||
->map(fn ($n) => self::norm4d((string) $n))
|
||||
->unique()
|
||||
->flip();
|
||||
|
||||
$itemIds = TicketItem::query()
|
||||
->where('draw_id', $draw->id)
|
||||
->where('player_id', $player->id)
|
||||
->whereIn('status', ['success', 'settled_win', 'settled_lose'])
|
||||
->pluck('id');
|
||||
|
||||
$hasBets = $itemIds->isNotEmpty();
|
||||
|
||||
$hits = [];
|
||||
if ($hasBets) {
|
||||
$hits = TicketCombination::query()
|
||||
->whereIn('ticket_item_id', $itemIds)
|
||||
->pluck('number_4d')
|
||||
->map(fn ($n) => self::norm4d((string) $n))
|
||||
->filter(fn (string $n) => isset($board[$n]))
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
$sums = TicketItem::query()
|
||||
->where('draw_id', $draw->id)
|
||||
->where('player_id', $player->id)
|
||||
->whereIn('status', ['settled_win', 'settled_lose'])
|
||||
->selectRaw('coalesce(sum(win_amount),0) as sum_win, coalesce(sum(jackpot_win_amount),0) as sum_jackpot')
|
||||
->first();
|
||||
|
||||
return ApiResponse::success([
|
||||
'draw_no' => $draw_no,
|
||||
'hit_numbers_4d' => $hits,
|
||||
'total_win_minor' => (int) ($sums->sum_win ?? 0),
|
||||
'total_jackpot_win_minor' => (int) ($sums->sum_jackpot ?? 0),
|
||||
'has_bets' => $hasBets,
|
||||
]);
|
||||
}
|
||||
|
||||
private static function norm4d(string $n): string
|
||||
{
|
||||
$n = preg_replace('/\D/', '', $n) ?? '';
|
||||
|
||||
return str_pad(substr($n, -4), 4, '0', STR_PAD_LEFT);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user