115 lines
3.7 KiB
PHP
115 lines
3.7 KiB
PHP
<?php
|
|
namespace app\admin\controller;
|
|
|
|
use support\Request;
|
|
use app\common\model\Order;
|
|
use app\common\model\Report;
|
|
use app\common\service\OrderFlowService;
|
|
use Illuminate\Database\Capsule\Manager as DB;
|
|
|
|
class ReportController
|
|
{
|
|
public function list(Request $request)
|
|
{
|
|
$page = max(1, intval($request->get('page', 1)));
|
|
$pageSize = min(50, max(1, intval($request->get('page_size', 10))));
|
|
|
|
$query = Report::with(['order', 'inspector']);
|
|
|
|
if ($reportNo = $request->get('report_no')) {
|
|
$query->where('report_no', 'like', "%{$reportNo}%");
|
|
}
|
|
|
|
$total = $query->count();
|
|
$items = $query->orderBy('id', 'desc')
|
|
->offset(($page - 1) * $pageSize)
|
|
->limit($pageSize)
|
|
->get();
|
|
|
|
return jsonResponse([
|
|
'items' => $items,
|
|
'total' => $total,
|
|
'page' => $page,
|
|
'page_size' => $pageSize
|
|
]);
|
|
}
|
|
|
|
public function detail(Request $request)
|
|
{
|
|
$id = (int)$request->get('id');
|
|
$report = Report::with(['order.logs', 'inspector'])->find($id);
|
|
|
|
if (!$report) {
|
|
return jsonResponse(null, '报告不存在', 404);
|
|
}
|
|
|
|
return jsonResponse($report);
|
|
}
|
|
|
|
public function create(Request $request)
|
|
{
|
|
$orderId = (int)$request->post('order_id');
|
|
$conclusion = trim($request->post('conclusion', '')); // REAL, FAKE, DOUBT
|
|
$level = trim($request->post('level', ''));
|
|
$flawsJson = $request->post('flaws_json', []);
|
|
$imagesJson = $request->post('images_json', []);
|
|
|
|
if (!in_array($conclusion, ['REAL', 'FAKE', 'DOUBT'])) {
|
|
return jsonResponse(null, '鉴定结论不合法', 400);
|
|
}
|
|
|
|
if (empty($imagesJson)) {
|
|
return jsonResponse(null, '必须上传证据图片', 400);
|
|
}
|
|
|
|
$order = Order::find($orderId);
|
|
if (!$order) {
|
|
return jsonResponse(null, '订单不存在', 404);
|
|
}
|
|
|
|
if ($order->status !== 'inspecting') {
|
|
return jsonResponse(null, '订单当前状态不可出具报告', 400);
|
|
}
|
|
|
|
if (Report::where('order_id', $orderId)->exists()) {
|
|
return jsonResponse(null, '该订单已出具报告,不可重复出具', 400);
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
try {
|
|
$reportNo = 'R' . date('YmdHis') . rand(1000, 9999);
|
|
$verifyCode = bin2hex(random_bytes(8)); // 16字符防伪码
|
|
|
|
$report = Report::create([
|
|
'report_no' => $reportNo,
|
|
'order_id' => $orderId,
|
|
'conclusion' => $conclusion,
|
|
'level' => $level,
|
|
'flaws_json' => $flawsJson,
|
|
'images_json' => $imagesJson,
|
|
'inspector_id' => $request->admin->id,
|
|
'verify_code' => $verifyCode
|
|
]);
|
|
|
|
// 扭转订单状态
|
|
$order->status = 'finished';
|
|
$order->save();
|
|
|
|
$conclusionMap = [
|
|
'REAL' => '正品',
|
|
'FAKE' => '仿品',
|
|
'DOUBT' => '存疑'
|
|
];
|
|
$conclusionText = $conclusionMap[$conclusion] ?? '未知';
|
|
|
|
OrderFlowService::addLog($orderId, 'report_generated', '报告已出具', "鉴定结论:{$conclusionText}", 'admin', $request->admin->id);
|
|
|
|
DB::commit();
|
|
return jsonResponse($report, '报告出具成功');
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return jsonResponse(null, '出具报告失败: ' . $e->getMessage(), 500);
|
|
}
|
|
}
|
|
}
|