OrderActionService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace app\common\Service\Order;
  3. use app\common\model\OrderAction as OrderActionModel;
  4. use app\common\Enum\OrderActionEnum;
  5. use think\Exception;
  6. /**
  7. * 订单操作记录服务
  8. */
  9. class OrderActionService
  10. {
  11. /**
  12. * 添加订单操作记录(兼容老接口)
  13. *
  14. * @param string $order_sn 订单编号
  15. * @param string $operator 操作人
  16. * @param string $memo 备注
  17. * @return bool
  18. */
  19. public static function push($order_sn, $operator, $memo)
  20. {
  21. return OrderActionModel::push($order_sn, $operator, $memo);
  22. }
  23. /**
  24. * 记录用户操作
  25. *
  26. * @param string $order_sn 订单编号
  27. * @param string $action_type 操作类型
  28. * @param string $operator 操作人
  29. * @param string $memo 备注
  30. * @param int $user_id 用户ID
  31. * @param array $extra 额外数据
  32. * @return bool
  33. */
  34. public static function recordUserAction($order_sn, $action_type, $operator, $memo = '', $user_id = 0, $extra = [])
  35. {
  36. // 验证操作类型
  37. if (!OrderActionEnum::isValidActionType($action_type)) {
  38. throw new Exception('无效的操作类型: ' . $action_type);
  39. }
  40. return OrderActionModel::create([
  41. 'order_sn' => $order_sn,
  42. 'action_type' => $action_type,
  43. 'operator' => $operator,
  44. 'memo' => $memo,
  45. 'user_type' => OrderActionEnum::USER_TYPE_CUSTOMER,
  46. 'operator_type' => OrderActionEnum::OPERATOR_TYPE_USER,
  47. 'user_id' => $user_id,
  48. 'extra_data' => json_encode($extra),
  49. 'priority' => OrderActionEnum::getDefaultPriority($action_type),
  50. 'ip' => request()->ip(),
  51. 'user_agent' => request()->server('HTTP_USER_AGENT', ''),
  52. ]) ? true : false;
  53. }
  54. /**
  55. * 记录管理员操作
  56. *
  57. * @param string $order_sn 订单编号
  58. * @param string $action_type 操作类型
  59. * @param string $operator 操作人
  60. * @param string $memo 备注
  61. * @param int $admin_id 管理员ID
  62. * @param array $extra 额外数据
  63. * @return bool
  64. */
  65. public static function recordAdminAction($order_sn, $action_type, $operator, $memo = '', $admin_id = 0, $extra = [])
  66. {
  67. // 验证操作类型
  68. if (!OrderActionEnum::isValidActionType($action_type)) {
  69. throw new Exception('无效的操作类型: ' . $action_type);
  70. }
  71. return OrderActionModel::create([
  72. 'order_sn' => $order_sn,
  73. 'action_type' => $action_type,
  74. 'operator' => $operator,
  75. 'memo' => $memo,
  76. 'user_type' => OrderActionEnum::USER_TYPE_ADMIN,
  77. 'operator_type' => OrderActionEnum::OPERATOR_TYPE_ADMIN,
  78. 'user_id' => $admin_id,
  79. 'extra_data' => json_encode($extra),
  80. 'priority' => OrderActionEnum::getDefaultPriority($action_type),
  81. 'ip' => request()->ip(),
  82. 'user_agent' => request()->server('HTTP_USER_AGENT', ''),
  83. ]) ? true : false;
  84. }
  85. /**
  86. * 记录系统操作
  87. *
  88. * @param string $order_sn 订单编号
  89. * @param string $action_type 操作类型
  90. * @param string $memo 备注
  91. * @param array $extra 额外数据
  92. * @return bool
  93. */
  94. public static function recordSystemAction($order_sn, $action_type, $memo = '', $extra = [])
  95. {
  96. // 验证操作类型
  97. if (!OrderActionEnum::isValidActionType($action_type)) {
  98. throw new Exception('无效的操作类型: ' . $action_type);
  99. }
  100. return OrderActionModel::create([
  101. 'order_sn' => $order_sn,
  102. 'action_type' => $action_type,
  103. 'operator' => 'system',
  104. 'memo' => $memo,
  105. 'user_type' => OrderActionEnum::USER_TYPE_SYSTEM,
  106. 'operator_type' => OrderActionEnum::OPERATOR_TYPE_SYSTEM,
  107. 'user_id' => 0,
  108. 'extra_data' => json_encode($extra),
  109. 'priority' => OrderActionEnum::getDefaultPriority($action_type),
  110. 'ip' => request()->ip(),
  111. 'user_agent' => request()->server('HTTP_USER_AGENT', ''),
  112. ]) ? true : false;
  113. }
  114. /**
  115. * 批量记录操作
  116. *
  117. * @param array $actions 操作记录数组
  118. * @return bool
  119. */
  120. public static function batchRecord($actions)
  121. {
  122. if (!is_array($actions) || empty($actions)) {
  123. return false;
  124. }
  125. $processedData = [];
  126. foreach ($actions as $action) {
  127. if (!isset($action['order_sn']) || !isset($action['action_type'])) {
  128. continue;
  129. }
  130. $userType = $action['user_type'] ?? OrderActionEnum::USER_TYPE_ADMIN;
  131. $operator = $action['operator'] ?? 'unknown';
  132. $memo = $action['memo'] ?? '';
  133. $userId = $action['user_id'] ?? 0;
  134. $extra = $action['extra'] ?? [];
  135. $processedData[] = [
  136. 'order_sn' => $action['order_sn'],
  137. 'action_type' => $action['action_type'],
  138. 'operator' => $operator,
  139. 'memo' => $memo,
  140. 'user_type' => $userType,
  141. 'operator_type' => OrderActionEnum::getDefaultOperatorType($userType),
  142. 'user_id' => $userId,
  143. 'extra_data' => json_encode($extra),
  144. 'priority' => OrderActionEnum::getDefaultPriority($action['action_type']),
  145. 'ip' => request()->ip(),
  146. 'user_agent' => request()->server('HTTP_USER_AGENT', ''),
  147. 'createtime' => time(),
  148. ];
  149. }
  150. return OrderActionModel::insertBatch($processedData);
  151. }
  152. /**
  153. * 获取订单操作记录
  154. *
  155. * @param string $order_sn 订单编号
  156. * @param string $action_type 操作类型(可选)
  157. * @param string $user_type 用户类型(可选)
  158. * @return \think\Collection
  159. */
  160. public static function getOrderActions($order_sn, $action_type = '', $user_type = '')
  161. {
  162. return OrderActionModel::getByOrderSn($order_sn, $action_type, $user_type);
  163. }
  164. /**
  165. * 获取最后一次操作记录
  166. *
  167. * @param string $order_sn 订单编号
  168. * @param string $action_type 操作类型(可选)
  169. * @return OrderActionModel|null
  170. */
  171. public static function getLastAction($order_sn, $action_type = '')
  172. {
  173. return OrderActionModel::getLatestByOrderSn($order_sn, $action_type);
  174. }
  175. /**
  176. * 检查操作是否存在
  177. *
  178. * @param string $order_sn 订单编号
  179. * @param string $action_type 操作类型
  180. * @return bool
  181. */
  182. public static function hasAction($order_sn, $action_type)
  183. {
  184. return OrderActionModel::hasActionType($order_sn, $action_type);
  185. }
  186. /**
  187. * 获取操作统计信息
  188. *
  189. * @param string $order_sn 订单编号
  190. * @return array
  191. */
  192. public static function getActionStats($order_sn)
  193. {
  194. $actions = self::getOrderActions($order_sn);
  195. $stats = [
  196. 'total_count' => $actions->count(),
  197. 'user_count' => 0,
  198. 'admin_count' => 0,
  199. 'system_count' => 0,
  200. 'action_types' => [],
  201. 'last_action' => null,
  202. 'first_action' => null,
  203. ];
  204. if ($actions->count() > 0) {
  205. $actionsArray = $actions->toArray();
  206. $stats['last_action'] = $actionsArray[0];
  207. $stats['first_action'] = $actionsArray[count($actionsArray) - 1];
  208. foreach ($actions as $action) {
  209. // 统计用户类型
  210. switch ($action['user_type']) {
  211. case OrderActionEnum::USER_TYPE_CUSTOMER:
  212. $stats['user_count']++;
  213. break;
  214. case OrderActionEnum::USER_TYPE_ADMIN:
  215. $stats['admin_count']++;
  216. break;
  217. case OrderActionEnum::USER_TYPE_SYSTEM:
  218. $stats['system_count']++;
  219. break;
  220. }
  221. // 统计操作类型
  222. $actionType = $action['action_type'];
  223. if (!isset($stats['action_types'][$actionType])) {
  224. $stats['action_types'][$actionType] = 0;
  225. }
  226. $stats['action_types'][$actionType]++;
  227. }
  228. }
  229. return $stats;
  230. }
  231. /**
  232. * 获取操作时间轴
  233. *
  234. * @param string $order_sn 订单编号
  235. * @return array
  236. */
  237. public static function getActionTimeline($order_sn)
  238. {
  239. $actions = self::getOrderActions($order_sn);
  240. $timeline = [];
  241. foreach ($actions as $action) {
  242. $timeline[] = [
  243. 'id' => $action['id'],
  244. 'action_type' => $action['action_type'],
  245. 'action_type_text' => $action['action_type_text'],
  246. 'operator' => $action['operator'],
  247. 'user_type' => $action['user_type'],
  248. 'user_type_text' => $action['user_type_text'],
  249. 'memo' => $action['memo'],
  250. 'createtime' => $action['createtime'],
  251. 'createtime_text' => $action['createtime_text'],
  252. 'priority' => $action['priority'],
  253. 'priority_text' => $action['priority_text'],
  254. ];
  255. }
  256. return $timeline;
  257. }
  258. /**
  259. * 根据订单状态变化自动记录操作
  260. *
  261. * @param string $order_sn 订单编号
  262. * @param int $old_status 旧状态
  263. * @param int $new_status 新状态
  264. * @param string $operator 操作人
  265. * @param string $user_type 用户类型
  266. * @param int $user_id 用户ID
  267. * @return bool
  268. */
  269. public static function recordStatusChange($order_sn, $old_status, $new_status, $operator, $user_type = OrderActionEnum::USER_TYPE_ADMIN, $user_id = 0)
  270. {
  271. // 状态映射到操作类型
  272. $statusActionMap = [
  273. 101 => OrderActionEnum::ACTION_CREATE, // 创建订单
  274. 201 => OrderActionEnum::ACTION_PAY, // 支付
  275. 301 => OrderActionEnum::ACTION_SHIP, // 发货
  276. 401 => OrderActionEnum::ACTION_RECEIVE, // 确认收货
  277. 501 => OrderActionEnum::ACTION_COMMENT, // 评价
  278. 102 => OrderActionEnum::ACTION_CANCEL, // 取消
  279. 103 => OrderActionEnum::ACTION_AUTO_CANCEL, // 自动取消
  280. 104 => OrderActionEnum::ACTION_ADMIN_CANCEL, // 管理员取消
  281. 202 => OrderActionEnum::ACTION_REFUND, // 退款
  282. 402 => OrderActionEnum::ACTION_AUTO_CONFIRM, // 自动确认
  283. ];
  284. $actionType = $statusActionMap[$new_status] ?? null;
  285. if (!$actionType) {
  286. return false;
  287. }
  288. $memo = "订单状态从 {$old_status} 变更为 {$new_status}";
  289. $extra = [
  290. 'old_status' => $old_status,
  291. 'new_status' => $new_status,
  292. 'change_time' => time(),
  293. ];
  294. switch ($user_type) {
  295. case OrderActionEnum::USER_TYPE_CUSTOMER:
  296. return self::recordUserAction($order_sn, $actionType, $operator, $memo, $user_id, $extra);
  297. case OrderActionEnum::USER_TYPE_ADMIN:
  298. return self::recordAdminAction($order_sn, $actionType, $operator, $memo, $user_id, $extra);
  299. case OrderActionEnum::USER_TYPE_SYSTEM:
  300. return self::recordSystemAction($order_sn, $actionType, $memo, $extra);
  301. }
  302. return false;
  303. }
  304. }