Files
appraisal_center_api/app/common/service/OrderFlowService.php
wushumin 99173639c9 add
2026-04-16 13:04:15 +08:00

139 lines
4.4 KiB
PHP

<?php
namespace app\common\service;
use app\common\model\Order;
use app\common\model\OrderLog;
use Exception;
class OrderFlowService
{
/**
* 模拟创建订单事务
*/
public static function createOrder(array $params, int $userId)
{
$orderNo = 'AXY' . date('YmdHis') . rand(100, 999);
// 1. 创建主订单
$order = Order::create([
'order_no' => $orderNo,
'user_id' => $userId,
'category' => $params['category'] ?? '奢品包袋',
'service_type' => $params['service_type'] ?? '真伪鉴定',
'brand' => $params['brand'] ?? '未知品牌',
'model' => $params['model'] ?? '',
'remark' => $params['remark'] ?? '',
'is_fast' => $params['is_fast'] ?? 0,
'total_price' => $params['total_price'] ?? 49.00,
'status' => 'wait_pay',
]);
// 2. 写入初始流转日志
self::addLog($order->id, 'create', '订单已创建', '等待用户支付', 'user', $userId);
return $order;
}
/**
* 模拟支付成功
*/
public static function payOrder(Order $order)
{
if ($order->status !== 'wait_pay') {
return $order;
}
$order->status = 'shipping'; // 待寄送
$order->pay_time = date('Y-m-d H:i:s');
$order->pay_status = 'paid';
$order->save();
self::addLog($order->id, 'pay_success', '支付成功', '请尽快寄出物品', 'user', $order->user_id);
return $order;
}
/**
* 用户填写物流并发货
*/
public static function userShip(Order $order, string $expressCompany, string $expressNo)
{
if ($order->status !== 'shipping' && $order->status !== 'wait_receive') {
throw new Exception("当前状态不允许修改发货信息");
}
if ($order->status === 'wait_receive') {
if ($order->express_modify_count >= 1) {
throw new Exception("运单号仅允许修改1次");
}
$order->express_modify_count += 1;
$actionType = 'user_ship_modify';
$title = '修改物流信息';
} else {
$order->status = 'wait_receive'; // 等待平台收件
$actionType = 'user_ship';
$title = '物品已寄出';
}
$order->express_company = $expressCompany;
$order->express_no = $expressNo;
$order->save();
self::addLog($order->id, $actionType, $title, "物流公司: {$expressCompany}, 单号: {$expressNo}", 'user', $order->user_id);
return $order;
}
/**
* 平台确认收件并开始鉴定
*/
public static function adminReceive(Order $order, int $adminId)
{
if ($order->status !== 'wait_receive' && $order->status !== 'shipping') {
throw new Exception("当前状态不允许收件");
}
$order->status = 'inspecting';
$order->save();
self::addLog($order->id, 'admin_receive', '平台已收件', '物品已入库,即将开始鉴定', 'admin', $adminId);
return $order;
}
public static function adminReturnShip(Order $order, int $adminId, string $expressCompany, string $expressNo)
{
if ($order->status !== 'finished') {
throw new Exception("当前状态不允许回寄");
}
if ($expressCompany === '' || $expressNo === '') {
throw new Exception("回寄物流信息不完整");
}
$order->return_express_company = $expressCompany;
$order->return_express_no = $expressNo;
$order->return_ship_time = date('Y-m-d H:i:s');
$order->status = 'return_shipping';
$order->save();
self::addLog($order->id, 'return_ship', '已回寄', "物流公司: {$expressCompany}, 单号: {$expressNo}", 'admin', $adminId);
return $order;
}
/**
* 写入时间轴日志
*/
public static function addLog(int $orderId, string $actionType, string $title, string $desc = '', string $operatorType = 'system', int $operatorId = 0)
{
return OrderLog::create([
'order_id' => $orderId,
'action_type' => $actionType,
'title' => $title,
'description' => $desc,
'operator_type' => $operatorType,
'operator_id' => $operatorId
]);
}
}