feat: enhance reconciliation job processing and reporting features

- Updated ReconcileItemIndexController to include admin user validation and improved transfer order handling.
- Refactored ReconcileJobStoreController to ensure date range handling is more precise with start and end of day adjustments.
- Enhanced various report controllers to include currency code in responses for better financial clarity.
- Introduced new methods in AdminReconcileJobService for wallet transfer job creation and improved item scanning logic.
- Added wallet lookup idempotent path configuration for integration services to enhance API interactions.
This commit is contained in:
2026-06-16 13:50:55 +08:00
parent 606ed6817e
commit 1e7b2b31ee
21 changed files with 886 additions and 71 deletions

View File

@@ -10,12 +10,17 @@ use Illuminate\Http\Request;
use App\Models\ReconcileItem;
use App\Services\AuditLogger;
use Illuminate\Support\Facades\DB;
use App\Services\Wallet\WalletTransferReconcileDetector;
/**
* 对账任务:落库 `reconcile_jobs` / `reconcile_items`(阶段 7;差异引擎可后续替换)
* 对账任务:落库 `reconcile_jobs` / `reconcile_items`
*/
final class AdminReconcileJobService
{
public function __construct(
private readonly WalletTransferReconcileDetector $detector,
) {}
/**
* @param list<array{side_a_ref?: ?string, side_b_ref?: ?string, difference_amount?: int, status?: string}>|null $items
*/
@@ -26,7 +31,12 @@ final class AdminReconcileJobService
?Carbon $periodStart,
?Carbon $periodEnd,
?array $items,
?int $playerId = null,
): ReconcileJob {
if ($reconcileType === 'wallet_transfer' && $items === null) {
return $this->createWalletTransferScanJob($admin, $request, $periodStart, $periodEnd, $playerId);
}
return DB::transaction(function () use ($admin, $request, $reconcileType, $periodStart, $periodEnd, $items): ReconcileJob {
$jobNo = 'REC'.now()->format('YmdHis').strtoupper(Str::random(4));
@@ -81,4 +91,44 @@ final class AdminReconcileJobService
return $job->fresh();
});
}
private function createWalletTransferScanJob(
AdminUser $admin,
Request $request,
?Carbon $periodStart,
?Carbon $periodEnd,
?int $playerId,
): ReconcileJob {
$periodEnd ??= now()->endOfDay();
$periodStart ??= $periodEnd->copy()->startOfDay();
[$items] = $this->detector->scanItems(
$periodStart,
$periodEnd,
limit: 1000,
staleMinutes: 15,
playerId: $playerId,
includeMainSiteCheck: true,
);
$job = $this->detector->persistJob($items, $periodStart, $periodEnd, (int) $admin->getKey());
AuditLogger::recordForAdmin(
$admin,
$request,
'reconcile_jobs',
'create',
'reconcile_job',
(string) $job->getKey(),
null,
[
'job_no' => $job->job_no,
'reconcile_type' => 'wallet_transfer',
'item_count' => count($items),
'player_id' => $playerId,
],
);
return $job->fresh();
}
}

View File

@@ -46,9 +46,9 @@ final class AdminReportQueryService
$fromRaw = trim((string) ($filters['date_from'] ?? ''));
$toRaw = trim((string) ($filters['date_to'] ?? ''));
// 未传日期时按历史全量范围导出/查询,避免默认“仅今天”导致空数据
// 未传日期时默认近 30 个自然日(与后台报表中心默认筛选一致)
if ($fromRaw === '' && $toRaw === '') {
return $this->lifetimeBusinessDateBounds();
return $this->defaultReportDateRange();
}
$dateFrom = $fromRaw !== '' ? $fromRaw : $toRaw;
@@ -99,6 +99,19 @@ final class AdminReportQueryService
return ['date_from' => $from, 'date_to' => $to];
}
/**
* @return array{date_from: string, date_to: string}
*/
public function defaultReportDateRange(): array
{
$today = now()->toDateString();
return [
'date_from' => now()->subDays(29)->toDateString(),
'date_to' => $today,
];
}
/**
* @return array{date_from: string, date_to: string}
*/
@@ -298,7 +311,8 @@ final class AdminReportQueryService
$context = $this->normalizeScope($scope);
$currencyQuery = DB::table('ticket_orders as o')
->join('draws as d', 'd.id', '=', 'o.draw_id')
->whereBetween('d.business_date', [$dateFrom, $dateTo]);
->whereDate('d.business_date', '>=', $dateFrom)
->whereDate('d.business_date', '<=', $dateTo);
AdminDataScope::applyToTicketOrdersViaPlayer($currencyQuery, $context?->admin, 'o');
$currencyCode = (string) ($currencyQuery->orderByDesc('o.id')->value('o.currency_code') ?? '');
@@ -335,10 +349,12 @@ final class AdminReportQueryService
AdminDataScope::applyToTicketOrdersViaPlayer($payoutSub, $context?->admin, 'o');
return DB::table('draws as d')
->whereBetween('d.business_date', [$dateFrom, $dateTo])
->whereDate('d.business_date', '>=', $dateFrom)
->whereDate('d.business_date', '<=', $dateTo)
->leftJoinSub($betSub, 'b', 'b.draw_id', '=', 'd.id')
->leftJoinSub($payoutSub, 'p', 'p.draw_id', '=', 'd.id')
->groupBy('d.business_date')
->havingRaw('COALESCE(SUM(b.total_bet_minor), 0) > 0 OR COALESCE(SUM(p.total_payout_minor), 0) > 0')
->orderBy('d.business_date')
->get([
'd.business_date',
@@ -349,7 +365,7 @@ final class AdminReportQueryService
->map(static function (object $row): array {
$businessDate = $row->business_date instanceof Carbon
? $row->business_date->format('Y-m-d')
: (string) $row->business_date;
: substr((string) $row->business_date, 0, 10);
return [
'business_date' => $businessDate,
@@ -665,6 +681,7 @@ final class AdminReportQueryService
$context = $this->normalizeScope($scope);
$query = DB::table('ticket_items as ti')
->join('ticket_orders as o', 'o.id', '=', 'ti.order_id')
->join('draws as d', 'd.id', '=', 'o.draw_id')
->leftJoin('players as p', 'p.id', '=', 'ti.player_id')
->leftJoin('agent_nodes as an', 'an.id', '=', 'p.agent_node_id')
->selectRaw('ti.player_id')
@@ -675,8 +692,8 @@ final class AdminReportQueryService
->selectRaw('SUM(ti.actual_deduct_amount) as total_bet_minor')
->selectRaw('SUM(ti.win_amount + ti.jackpot_win_amount) as total_payout_minor')
->selectRaw('SUM(ti.actual_deduct_amount) - SUM(ti.win_amount + ti.jackpot_win_amount) as net_win_loss_minor')
->whereDate('o.created_at', '>=', $dateFrom)
->whereDate('o.created_at', '<=', $dateTo)
->whereDate('d.business_date', '>=', $dateFrom)
->whereDate('d.business_date', '<=', $dateTo)
->groupBy('ti.player_id', 'p.username', 'p.agent_node_id', 'an.code', 'an.name')
->orderByDesc('net_win_loss_minor');
@@ -700,13 +717,14 @@ final class AdminReportQueryService
$context = $this->normalizeScope($scope);
$query = DB::table('ticket_items as ti')
->join('ticket_orders as o', 'o.id', '=', 'ti.order_id')
->join('draws as d', 'd.id', '=', 'o.draw_id')
->selectRaw('ti.play_code')
->selectRaw('ti.dimension')
->selectRaw('SUM(ti.actual_deduct_amount) as total_bet_minor')
->selectRaw('SUM(ti.win_amount + ti.jackpot_win_amount) as total_payout_minor')
->selectRaw('SUM(ti.actual_deduct_amount) - SUM(ti.win_amount + ti.jackpot_win_amount) as approx_house_gross_minor')
->whereDate('o.created_at', '>=', $dateFrom)
->whereDate('o.created_at', '<=', $dateTo)
->whereDate('d.business_date', '>=', $dateFrom)
->whereDate('d.business_date', '<=', $dateTo)
->groupBy('ti.play_code', 'ti.dimension')
->orderBy('ti.play_code')
->orderBy('ti.dimension');
@@ -726,12 +744,13 @@ final class AdminReportQueryService
$context = $this->normalizeScope($scope);
$query = DB::table('ticket_items as ti')
->join('ticket_orders as o', 'o.id', '=', 'ti.order_id')
->join('draws as d', 'd.id', '=', 'o.draw_id')
->selectRaw('ti.play_code')
->selectRaw('SUM(ti.total_bet_amount - ti.actual_deduct_amount) as total_rebate_minor')
->selectRaw('COUNT(DISTINCT o.id) as order_count')
->selectRaw('COUNT(ti.id) as ticket_item_count')
->whereDate('o.created_at', '>=', $dateFrom)
->whereDate('o.created_at', '<=', $dateTo)
->whereDate('d.business_date', '>=', $dateFrom)
->whereDate('d.business_date', '<=', $dateTo)
->groupBy('ti.play_code')
->orderBy('ti.play_code');