123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php
- namespace app\common\Service\Order;
- use app\common\model\OrderAction as OrderActionModel;
- use app\common\Enum\OrderActionEnum;
- use think\Exception;
- /**
- * 订单操作记录服务
- */
- class OrderActionService
- {
- /**
- * 添加订单操作记录(兼容老接口)
- *
- * @param string $order_sn 订单编号
- * @param string $operator 操作人
- * @param string $memo 备注
- * @return bool
- */
- public static function push($order_sn, $operator, $memo)
- {
- return OrderActionModel::push($order_sn, $operator, $memo);
- }
- /**
- * 记录用户操作
- *
- * @param string $order_sn 订单编号
- * @param string $action_type 操作类型
- * @param string $operator 操作人
- * @param string $memo 备注
- * @param int $user_id 用户ID
- * @param array $extra 额外数据
- * @return bool
- */
- public static function recordUserAction($order_sn, $action_type, $operator, $memo = '', $user_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_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;
- }
- }
|