feat: add report review publish flow

This commit is contained in:
wushumin
2026-06-04 12:08:16 +08:00
parent 9dfd5976ed
commit 55c357f2c2
14 changed files with 624 additions and 52 deletions

View File

@@ -434,8 +434,6 @@ class AppraisalTasksController
Db::rollback();
return api_error('请先提交鉴定结论生成报告草稿', 422);
}
$publish = $this->publishReportRecord($report, $request, false);
(new FulfillmentFlowService())->markReportPublished((int)$task['order_id'], $request);
Db::commit();
} catch (\InvalidArgumentException $e) {
Db::rollback();
@@ -445,14 +443,20 @@ class AppraisalTasksController
return api_error($e->getMessage(), $e->getCode() ?: 404);
} catch (\Throwable $e) {
Db::rollback();
return api_error('验真吊牌绑定或报告发布失败', 500, ['detail' => $e->getMessage()]);
return api_error('验真吊牌绑定失败', 500, ['detail' => $e->getMessage()]);
}
return api_success([
'id' => $id,
'material_tag' => $tag,
'report' => $publish,
], '验真吊牌已绑定,报告已发布');
'report' => [
'id' => (int)$report['id'],
'report_status' => (string)$report['report_status'],
'publish_time' => (string)($report['publish_time'] ?? ''),
'verify_url' => '',
'report_page_url' => '',
],
], '验真吊牌已绑定,报告待管理员发布');
}
public function saveZhongjianReport(Request $request)
@@ -571,8 +575,8 @@ class AppraisalTasksController
}
$this->saveTaskKeyPoints($savedResultId, $keyPoints, $now);
$this->createOrUpdateReportDraft((int)$task['order_id'], $task, $resultPayload, $now);
$report = $this->findLatestAppraisalReport((int)$task['order_id']);
$draftChange = $this->createOrUpdateReportDraft((int)$task['order_id'], $task, $resultPayload, $now);
$report = $draftChange['report'];
if (!$report) {
Db::rollback();
return api_error('中检报告草稿生成失败', 500);
@@ -612,16 +616,22 @@ class AppraisalTasksController
}
$tag = (new MaterialTagService())->bindTagToReportByTask($id, $qrInput, $request);
$publish = $this->publishReportRecord($freshReport, $request, false);
(new FulfillmentFlowService())->markReportPublished((int)$task['order_id'], $request);
$this->insertReportLog((int)$freshReport['id'], $draftChange['action'], $draftChange['before'], $freshReport, $request, '报告已提交,待管理员发布');
$this->insertReportLog((int)$freshReport['id'], 'submit', $draftChange['before'], $freshReport, $request, '鉴定师提交报告');
Db::commit();
return api_success([
'id' => $id,
'material_tag' => $tag,
'report' => $publish,
], '验真吊牌已绑定,报告已发布');
'report' => [
'id' => (int)$freshReport['id'],
'report_status' => (string)$freshReport['report_status'],
'publish_time' => (string)($freshReport['publish_time'] ?? ''),
'verify_url' => '',
'report_page_url' => '',
],
], '报告已提交,待管理员发布');
} catch (\Throwable $e) {
Db::rollback();
return api_error('中检报告录入失败', 500, ['detail' => $e->getMessage()]);
@@ -820,15 +830,16 @@ class AppraisalTasksController
'created_at' => $now,
]);
$this->createOrUpdateReportDraft((int)$task['order_id'], $task, $payload, $now);
$report = $this->findLatestAppraisalReport((int)$task['order_id']);
$draftChange = $this->createOrUpdateReportDraft((int)$task['order_id'], $task, $payload, $now);
$report = $draftChange['report'];
if (!$report) {
Db::rollback();
return api_error('报告草稿生成失败', 500);
}
$tag = (new MaterialTagService())->bindTagToReportByTask($id, $qrInput, $request);
$publish = $this->publishReportRecord($report, $request, false);
(new FulfillmentFlowService())->markReportPublished((int)$task['order_id'], $request);
$freshReport = $this->findLatestAppraisalReport((int)$task['order_id']) ?: $report;
$this->insertReportLog((int)$freshReport['id'], $draftChange['action'], $draftChange['before'], $freshReport, $request, '报告已提交,待管理员发布');
$this->insertReportLog((int)$freshReport['id'], 'submit', $draftChange['before'], $freshReport, $request, '鉴定师提交报告');
Db::commit();
(new EnterpriseWebhookService())->recordOrderEvent((int)$task['order_id'], 'appraisal_finished', [
@@ -839,8 +850,14 @@ class AppraisalTasksController
return api_success([
'id' => $id,
'material_tag' => $tag,
'report' => $publish,
], '验真吊牌已绑定,报告已发布');
'report' => [
'id' => (int)$freshReport['id'],
'report_status' => (string)$freshReport['report_status'],
'publish_time' => (string)($freshReport['publish_time'] ?? ''),
'verify_url' => '',
'report_page_url' => '',
],
], '报告已提交,待管理员发布');
} catch (\Throwable $e) {
Db::rollback();
return api_error('结论保存失败', 500, [
@@ -1467,6 +1484,15 @@ class AppraisalTasksController
$stage = (string)($task['task_stage'] ?? '');
$submittedAt = (string)($task['submitted_at'] ?? '');
$orderStatus = (string)($task['order_status'] ?? '');
$reportStatus = $report ? (string)($report['report_status'] ?? '') : '';
if ($reportStatus === 'published') {
return 'completed';
}
if (in_array($reportStatus, ['draft', 'pending_publish', 'updated', 'rejected'], true)) {
return 'processing';
}
if (
$submittedAt !== ''
@@ -1504,6 +1530,7 @@ class AppraisalTasksController
'draft' => '草稿中',
'pending_publish' => '待发布',
'published' => '已发布',
'rejected' => '已驳回',
'updated' => '已更新',
'invalid' => '已作废',
default => $status,
@@ -1804,7 +1831,7 @@ class AppraisalTasksController
return (string)Db::name($table)->where('id', $id)->value($field);
}
private function createOrUpdateReportDraft(int $orderId, array $task, array $resultPayload, string $now): void
private function createOrUpdateReportDraft(int $orderId, array $task, array $resultPayload, string $now): array
{
$report = Db::name('reports')->where('order_id', $orderId)->order('id', 'desc')->find();
$order = Db::name('orders')->where('id', $orderId)->find();
@@ -1842,17 +1869,27 @@ class AppraisalTasksController
'report_title' => $task['service_provider'] === 'zhongjian' ? '中检鉴定报告' : '安心验鉴定报告',
'report_status' => 'pending_publish',
'publish_time' => null,
'invalid_reason' => '',
'reject_reason' => '',
'rejected_by' => null,
'rejected_by_name' => '',
'rejected_at' => null,
'updated_at' => $now,
];
if ($report) {
$beforeReport = $report;
$reportData['report_version'] = (int)($report['report_version'] ?? 1) + 1;
Db::name('reports')->where('id', $report['id'])->update($reportData);
$reportId = (int)$report['id'];
$logAction = 'update_draft';
} else {
$reportData['report_no'] = 'AXY-R-' . date('Ymd') . '-' . mt_rand(1000, 9999);
$reportData['report_version'] = 1;
$reportData['created_at'] = $now;
$reportId = (int)Db::name('reports')->insertGetId($reportData);
$beforeReport = [];
$logAction = 'create_draft';
}
$contentPayload = [
@@ -1892,6 +1929,12 @@ class AppraisalTasksController
$contentPayload['created_at'] = $now;
Db::name('report_contents')->insert($contentPayload);
}
return [
'report' => Db::name('reports')->where('id', $reportId)->find() ?: [],
'before' => $beforeReport,
'action' => $logAction,
];
}
private function buildAppraisalSnapshot(string $serviceProvider, string $fallbackTime, ?array $firstReviewTask, ?array $finalReviewTask): array
@@ -1915,6 +1958,41 @@ class AppraisalTasksController
];
}
private function insertReportLog(int $reportId, string $action, array $before, array $after, Request $request, string $remark = ''): void
{
if ($reportId <= 0) {
return;
}
Db::name('report_logs')->insert([
'report_id' => $reportId,
'action' => $action,
'operator_id' => (int)$request->header('x-admin-id', 0) ?: null,
'operator_name' => trim((string)$request->header('x-admin-name', '')),
'before_data' => $before ? json_encode($this->reportLogSnapshot($before), JSON_UNESCAPED_UNICODE) : null,
'after_data' => $after ? json_encode($this->reportLogSnapshot($after), JSON_UNESCAPED_UNICODE) : null,
'remark' => mb_substr($remark, 0, 255),
'created_at' => date('Y-m-d H:i:s'),
]);
}
private function reportLogSnapshot(array $report): array
{
return [
'id' => (int)($report['id'] ?? 0),
'report_no' => (string)($report['report_no'] ?? ''),
'order_id' => (int)($report['order_id'] ?? 0),
'report_status' => (string)($report['report_status'] ?? ''),
'report_version' => (int)($report['report_version'] ?? 0),
'publish_time' => (string)($report['publish_time'] ?? ''),
'zhongjian_report_no' => (string)($report['zhongjian_report_no'] ?? ''),
'invalid_reason' => (string)($report['invalid_reason'] ?? ''),
'reject_reason' => (string)($report['reject_reason'] ?? ''),
'rejected_by_name' => (string)($report['rejected_by_name'] ?? ''),
'rejected_at' => (string)($report['rejected_at'] ?? ''),
];
}
private function normalizeAssigneeName(?string $value): string
{
$name = trim((string)$value);