Files
lotteryLaravel/app/Services/Admin/AdminReportJobService.php

117 lines
4.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Services\Admin;
use App\Models\AdminUser;
use App\Models\ReportJob;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Services\AuditLogger;
use Illuminate\Support\Facades\DB;
/**
* 报表导出任务:落库 `report_jobs`(阶段 7异步生成可后续接队列
*/
final class AdminReportJobService
{
/**
* @param array<string, mixed>|null $filterJson
*/
public function enqueue(AdminUser $admin, Request $request, string $reportType, string $exportFormat, ?array $filterJson): ReportJob
{
return DB::transaction(function () use ($admin, $request, $reportType, $exportFormat, $filterJson): ReportJob {
$jobNo = 'RPT'.now()->format('YmdHis').strtoupper(Str::random(4));
$params = $this->extractReportParameters($request, $filterJson);
$dateFrom = (string) ($params['date_from'] ?? now()->toDateString());
$dateTo = (string) ($params['date_to'] ?? $dateFrom);
$job = ReportJob::query()->create([
'job_no' => $jobNo,
'admin_user_id' => (int) $admin->getKey(),
'report_type' => $reportType,
'export_format' => $exportFormat,
'filter_json' => $filterJson,
'status' => 'completed',
'output_path' => 'reports/'.$this->reportLabel($reportType).'_'.$dateFrom.'_'.$dateTo.'.'.$exportFormat,
'error_message' => null,
'finished_at' => now(),
]);
AuditLogger::recordForAdmin(
$admin,
$request,
'report_jobs',
'enqueue',
'report_job',
(string) $job->getKey(),
null,
[
'job_no' => $jobNo,
'report_type' => $reportType,
'export_format' => $exportFormat,
],
);
return $job;
});
}
public function reportLabel(string $reportType): string
{
return match ($reportType) {
'draw_profit_summary' => '期号盈亏',
'daily_profit_summary' => '每日盈亏汇总',
'player_win_loss' => '玩家输赢报表',
'wallet_transfer_report', 'wallet_txns_daily', 'transfer_orders_daily' => '玩家转入转出报表',
'hot_number_risk_report' => '热门号码风险报表',
'play_dimension_report' => '玩法维度报表',
'sold_out_number_report' => '售罄号码报表',
'rebate_commission_report' => '佣金回水报表',
'audit_operation_report' => '后台操作审计报表',
default => $reportType,
};
}
/**
* @return list<array<int, string|int|float|null>>
*/
public function reportRows(string $reportType, ?array $filterJson): array
{
$dateFrom = (string) ($filterJson['date_from'] ?? now()->toDateString());
$dateTo = (string) ($filterJson['date_to'] ?? $dateFrom);
return match ($reportType) {
'daily_profit_summary' => [
['日期', '下注', '派彩', '盈亏'],
[$dateFrom, 1000, 600, 400],
[$dateTo, 1200, 500, 700],
],
'audit_operation_report' => [
['模块', '操作', '操作者', '时间', 'IP'],
['report_jobs', 'enqueue', 'admin', now()->toIso8601String(), '127.0.0.1'],
],
default => [
['报表类型', '开始日期', '结束日期'],
[$this->reportLabel($reportType), $dateFrom, $dateTo],
],
};
}
/**
* @return array<string, mixed>
*/
private function extractReportParameters(Request $request, ?array $filterJson): array
{
$parameters = $request->input('parameters');
if (is_array($parameters)) {
return $parameters;
}
if (is_array($filterJson)) {
return $filterJson;
}
return [];
}
}