56 lines
1.7 KiB
PHP
56 lines
1.7 KiB
PHP
<?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));
|
||
|
||
$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/'.$jobNo.'.'.$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;
|
||
});
|
||
}
|
||
}
|