$order_sn, 'action_type' => $action_type, 'operator' => $operator, 'memo' => $memo, 'user_type' => OrderActionEnum::USER_TYPE_CUSTOMER, 'operator_type' => OrderActionEnum::OPERATOR_TYPE_USER, 'user_id' => $user_id, 'extra_data' => json_encode($extra), 'priority' => OrderActionEnum::getDefaultPriority($action_type), 'ip' => request()->ip(), 'user_agent' => request()->server('HTTP_USER_AGENT', ''), ]) ? true : false; } /** * 记录管理员操作 * * @param string $order_sn 订单编号 * @param string $action_type 操作类型 * @param string $operator 操作人 * @param string $memo 备注 * @param int $admin_id 管理员ID * @param array $extra 额外数据 * @return bool */ public static function recordAdminAction($order_sn, $action_type, $operator, $memo = '', $admin_id = 0, $extra = []) { // 验证操作类型 if (!OrderActionEnum::isValidActionType($action_type)) { throw new Exception('无效的操作类型: ' . $action_type); } return OrderActionModel::create([ 'order_sn' => $order_sn, 'action_type' => $action_type, 'operator' => $operator, 'memo' => $memo, 'user_type' => OrderActionEnum::USER_TYPE_ADMIN, 'operator_type' => OrderActionEnum::OPERATOR_TYPE_ADMIN, 'user_id' => $admin_id, 'extra_data' => json_encode($extra), 'priority' => OrderActionEnum::getDefaultPriority($action_type), 'ip' => request()->ip(), 'user_agent' => request()->server('HTTP_USER_AGENT', ''), ]) ? true : false; } /** * 记录系统操作 * * @param string $order_sn 订单编号 * @param string $action_type 操作类型 * @param string $memo 备注 * @param array $extra 额外数据 * @return bool */ public static function recordSystemAction($order_sn, $action_type, $memo = '', $extra = []) { // 验证操作类型 if (!OrderActionEnum::isValidActionType($action_type)) { throw new Exception('无效的操作类型: ' . $action_type); } return OrderActionModel::create([ 'order_sn' => $order_sn, 'action_type' => $action_type, 'operator' => 'system', 'memo' => $memo, 'user_type' => OrderActionEnum::USER_TYPE_SYSTEM, 'operator_type' => OrderActionEnum::OPERATOR_TYPE_SYSTEM, 'user_id' => 0, 'extra_data' => json_encode($extra), 'priority' => OrderActionEnum::getDefaultPriority($action_type), 'ip' => request()->ip(), 'user_agent' => request()->server('HTTP_USER_AGENT', ''), ]) ? true : false; } /** * 批量记录操作 * * @param array $actions 操作记录数组 * @return bool */ public static function batchRecord($actions) { if (!is_array($actions) || empty($actions)) { return false; } $processedData = []; foreach ($actions as $action) { if (!isset($action['order_sn']) || !isset($action['action_type'])) { continue; } $userType = $action['user_type'] ?? OrderActionEnum::USER_TYPE_ADMIN; $operator = $action['operator'] ?? 'unknown'; $memo = $action['memo'] ?? ''; $userId = $action['user_id'] ?? 0; $extra = $action['extra'] ?? []; $processedData[] = [ 'order_sn' => $action['order_sn'], 'action_type' => $action['action_type'], 'operator' => $operator, 'memo' => $memo, 'user_type' => $userType, 'operator_type' => OrderActionEnum::getDefaultOperatorType($userType), 'user_id' => $userId, 'extra_data' => json_encode($extra), 'priority' => OrderActionEnum::getDefaultPriority($action['action_type']), 'ip' => request()->ip(), 'user_agent' => request()->server('HTTP_USER_AGENT', ''), 'createtime' => time(), ]; } return OrderActionModel::insertBatch($processedData); } /** * 获取订单操作记录 * * @param string $order_sn 订单编号 * @param string $action_type 操作类型(可选) * @param string $user_type 用户类型(可选) * @return \think\Collection */ public static function getOrderActions($order_sn, $action_type = '', $user_type = '') { return OrderActionModel::getByOrderSn($order_sn, $action_type, $user_type); } /** * 获取最后一次操作记录 * * @param string $order_sn 订单编号 * @param string $action_type 操作类型(可选) * @return OrderActionModel|null */ public static function getLastAction($order_sn, $action_type = '') { return OrderActionModel::getLatestByOrderSn($order_sn, $action_type); } /** * 检查操作是否存在 * * @param string $order_sn 订单编号 * @param string $action_type 操作类型 * @return bool */ public static function hasAction($order_sn, $action_type) { return OrderActionModel::hasActionType($order_sn, $action_type); } /** * 获取操作统计信息 * * @param string $order_sn 订单编号 * @return array */ public static function getActionStats($order_sn) { $actions = self::getOrderActions($order_sn); $stats = [ 'total_count' => $actions->count(), 'user_count' => 0, 'admin_count' => 0, 'system_count' => 0, 'action_types' => [], 'last_action' => null, 'first_action' => null, ]; if ($actions->count() > 0) { $actionsArray = $actions->toArray(); $stats['last_action'] = $actionsArray[0]; $stats['first_action'] = $actionsArray[count($actionsArray) - 1]; foreach ($actions as $action) { // 统计用户类型 switch ($action['user_type']) { case OrderActionEnum::USER_TYPE_CUSTOMER: $stats['user_count']++; break; case OrderActionEnum::USER_TYPE_ADMIN: $stats['admin_count']++; break; case OrderActionEnum::USER_TYPE_SYSTEM: $stats['system_count']++; break; } // 统计操作类型 $actionType = $action['action_type']; if (!isset($stats['action_types'][$actionType])) { $stats['action_types'][$actionType] = 0; } $stats['action_types'][$actionType]++; } } return $stats; } /** * 获取操作时间轴 * * @param string $order_sn 订单编号 * @return array */ public static function getActionTimeline($order_sn) { $actions = self::getOrderActions($order_sn); $timeline = []; foreach ($actions as $action) { $timeline[] = [ 'id' => $action['id'], 'action_type' => $action['action_type'], 'action_type_text' => $action['action_type_text'], 'operator' => $action['operator'], 'user_type' => $action['user_type'], 'user_type_text' => $action['user_type_text'], 'memo' => $action['memo'], 'createtime' => $action['createtime'], 'createtime_text' => $action['createtime_text'], 'priority' => $action['priority'], 'priority_text' => $action['priority_text'], ]; } return $timeline; } /** * 根据订单状态变化自动记录操作 * * @param string $order_sn 订单编号 * @param int $old_status 旧状态 * @param int $new_status 新状态 * @param string $operator 操作人 * @param string $user_type 用户类型 * @param int $user_id 用户ID * @return bool */ public static function recordStatusChange($order_sn, $old_status, $new_status, $operator, $user_type = OrderActionEnum::USER_TYPE_ADMIN, $user_id = 0) { // 状态映射到操作类型 $statusActionMap = [ 101 => OrderActionEnum::ACTION_CREATE, // 创建订单 201 => OrderActionEnum::ACTION_PAY, // 支付 301 => OrderActionEnum::ACTION_SHIP, // 发货 401 => OrderActionEnum::ACTION_RECEIVE, // 确认收货 501 => OrderActionEnum::ACTION_COMMENT, // 评价 102 => OrderActionEnum::ACTION_CANCEL, // 取消 103 => OrderActionEnum::ACTION_AUTO_CANCEL, // 自动取消 104 => OrderActionEnum::ACTION_ADMIN_CANCEL, // 管理员取消 202 => OrderActionEnum::ACTION_REFUND, // 退款 402 => OrderActionEnum::ACTION_AUTO_CONFIRM, // 自动确认 ]; $actionType = $statusActionMap[$new_status] ?? null; if (!$actionType) { return false; } $memo = "订单状态从 {$old_status} 变更为 {$new_status}"; $extra = [ 'old_status' => $old_status, 'new_status' => $new_status, 'change_time' => time(), ]; switch ($user_type) { case OrderActionEnum::USER_TYPE_CUSTOMER: return self::recordUserAction($order_sn, $actionType, $operator, $memo, $user_id, $extra); case OrderActionEnum::USER_TYPE_ADMIN: return self::recordAdminAction($order_sn, $actionType, $operator, $memo, $user_id, $extra); case OrderActionEnum::USER_TYPE_SYSTEM: return self::recordSystemAction($order_sn, $actionType, $memo, $extra); } return false; } }