- 在 `ReportJobStoreRequest` 中新增 `draw_id`、`draw_no` 和 `normalized_number` 参数的验证规则。 - 更新 `AdminReportJobService` 以支持动态生成输出路径后缀,确保导出文件名包含相关信息。 - 在 `AdminReportQueryService` 中新增多个报表类型的处理逻辑,包括 `draw_profit_summary` 和 `sold_out_number_report`。 - 添加相应的测试用例,确保新功能的正确性和稳定性。
98 lines
3.0 KiB
PHP
98 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use App\Models\AdminUser;
|
|
use App\Models\ReportJob;
|
|
use App\Services\AuditLogger;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* 报表导出任务:落库 `report_jobs`(同步生成,可后续接队列)。
|
|
*/
|
|
final class AdminReportJobService
|
|
{
|
|
public function __construct(
|
|
private readonly AdminReportQueryService $queryService,
|
|
) {}
|
|
|
|
/**
|
|
* @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 {
|
|
$params = $this->extractReportParameters($request, $filterJson);
|
|
$range = $this->queryService->resolveDateRange($params);
|
|
$dateFrom = $range['date_from'];
|
|
$dateTo = $range['date_to'];
|
|
|
|
$jobNo = 'RPT'.now()->format('YmdHis').strtoupper(Str::random(4));
|
|
$exportFormat = $exportFormat === 'xlsx' ? 'xlsx' : 'csv';
|
|
|
|
$pathSuffix = $this->queryService->resolveOutputPathSuffix($reportType, $params, $dateFrom, $dateTo);
|
|
|
|
$job = ReportJob::query()->create([
|
|
'job_no' => $jobNo,
|
|
'admin_user_id' => (int) $admin->getKey(),
|
|
'report_type' => $reportType,
|
|
'export_format' => $exportFormat,
|
|
'filter_json' => $params,
|
|
'status' => 'completed',
|
|
'output_path' => 'reports/'.$this->queryService->reportLabel($reportType).'_'.$pathSuffix.'.'.$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;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return list<array<int, string|int|float|null>>
|
|
*/
|
|
public function reportRows(string $reportType, ?array $filterJson): array
|
|
{
|
|
return $this->queryService->reportRows($reportType, $filterJson);
|
|
}
|
|
|
|
public function reportLabel(string $reportType): string
|
|
{
|
|
return $this->queryService->reportLabel($reportType);
|
|
}
|
|
|
|
/**
|
|
* @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 [];
|
|
}
|
|
}
|