OrderActionRepository.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace app\common\repository;
  3. use app\common\model\OrderAction;
  4. use app\common\Enum\OrderActionEnum;
  5. use think\Collection;
  6. use think\Exception;
  7. /**
  8. * 订单操作记录仓库类
  9. * 数据访问抽象层,隐藏具体数据存储实现
  10. */
  11. class OrderActionRepository
  12. {
  13. protected $model;
  14. public function __construct()
  15. {
  16. $this->model = new OrderAction();
  17. }
  18. /**
  19. * 创建操作记录
  20. * @param array $data 操作数据
  21. * @return bool
  22. */
  23. public function create(array $data): bool
  24. {
  25. try {
  26. return $this->model->create($data) ? true : false;
  27. } catch (Exception $e) {
  28. return false;
  29. }
  30. }
  31. /**
  32. * 批量创建操作记录
  33. * @param array $dataList 批量数据
  34. * @return bool
  35. */
  36. public function createBatch(array $dataList): bool
  37. {
  38. try {
  39. return $this->model->insertBatch($dataList);
  40. } catch (Exception $e) {
  41. return false;
  42. }
  43. }
  44. /**
  45. * 根据订单编号查询操作记录
  46. * @param string $orderSn 订单编号
  47. * @param string $actionType 操作类型(可选)
  48. * @param string $userType 用户类型(可选)
  49. * @param int $limit 限制数量(可选)
  50. * @return Collection
  51. */
  52. public function findByOrderSn(string $orderSn, string $actionType = '', string $userType = '', int $limit = 0): Collection
  53. {
  54. $where = ['order_sn' => $orderSn];
  55. if ($actionType) {
  56. $where['action_type'] = $actionType;
  57. }
  58. if ($userType) {
  59. $where['user_type'] = $userType;
  60. }
  61. $query = $this->model->where($where)->order('createtime', 'desc');
  62. if ($limit > 0) {
  63. $query = $query->limit($limit);
  64. }
  65. return $query->select();
  66. }
  67. /**
  68. * 获取最新的操作记录
  69. * @param string $orderSn 订单编号
  70. * @param string $actionType 操作类型(可选)
  71. * @return OrderAction|null
  72. */
  73. public function findLatestByOrderSn(string $orderSn, string $actionType = ''): ?OrderAction
  74. {
  75. $where = ['order_sn' => $orderSn];
  76. if ($actionType) {
  77. $where['action_type'] = $actionType;
  78. }
  79. return $this->model->where($where)->order('createtime', 'desc')->find();
  80. }
  81. /**
  82. * 检查特定操作类型是否存在
  83. * @param string $orderSn 订单编号
  84. * @param string $actionType 操作类型
  85. * @return bool
  86. */
  87. public function existsByActionType(string $orderSn, string $actionType): bool
  88. {
  89. return $this->model->where([
  90. 'order_sn' => $orderSn,
  91. 'action_type' => $actionType
  92. ])->count() > 0;
  93. }
  94. /**
  95. * 统计操作记录数量
  96. * @param string $orderSn 订单编号
  97. * @param string $actionType 操作类型(可选)
  98. * @param string $userType 用户类型(可选)
  99. * @return int
  100. */
  101. public function countByOrderSn(string $orderSn, string $actionType = '', string $userType = ''): int
  102. {
  103. $where = ['order_sn' => $orderSn];
  104. if ($actionType) {
  105. $where['action_type'] = $actionType;
  106. }
  107. if ($userType) {
  108. $where['user_type'] = $userType;
  109. }
  110. return $this->model->where($where)->count();
  111. }
  112. /**
  113. * 获取用户操作历史
  114. * @param int $userId 用户ID
  115. * @param string $userType 用户类型
  116. * @param int $limit 限制数量
  117. * @return Collection
  118. */
  119. public function findByUser(int $userId, string $userType = OrderActionEnum::USER_TYPE_CUSTOMER, int $limit = 10): Collection
  120. {
  121. return $this->model->where([
  122. 'user_id' => $userId,
  123. 'user_type' => $userType
  124. ])->order('createtime', 'desc')
  125. ->limit($limit)
  126. ->select();
  127. }
  128. /**
  129. * 获取高优先级操作记录
  130. * @param string $orderSn 订单编号
  131. * @return Collection
  132. */
  133. public function findHighPriorityByOrderSn(string $orderSn): Collection
  134. {
  135. return $this->model->where([
  136. 'order_sn' => $orderSn,
  137. 'priority' => [
  138. 'in',
  139. [OrderActionEnum::PRIORITY_HIGH, OrderActionEnum::PRIORITY_URGENT]
  140. ]
  141. ])->order('createtime', 'desc')
  142. ->select();
  143. }
  144. /**
  145. * 删除过期记录
  146. * @param int $expiredTime 过期时间戳
  147. * @return bool
  148. */
  149. public function deleteExpired(int $expiredTime): bool
  150. {
  151. try {
  152. return $this->model->where('createtime', '<', $expiredTime)->delete() !== false;
  153. } catch (Exception $e) {
  154. return false;
  155. }
  156. }
  157. /**
  158. * 获取操作记录用于统计
  159. * @param string $orderSn 订单编号
  160. * @return array
  161. */
  162. public function getStatisticsData(string $orderSn): array
  163. {
  164. $actions = $this->findByOrderSn($orderSn);
  165. $stats = [
  166. 'total_count' => 0,
  167. 'user_count' => 0,
  168. 'admin_count' => 0,
  169. 'system_count' => 0,
  170. 'action_types' => [],
  171. 'priority_distribution' => [],
  172. 'actions_data' => []
  173. ];
  174. if ($actions->isEmpty()) {
  175. return $stats;
  176. }
  177. $stats['total_count'] = $actions->count();
  178. $stats['actions_data'] = $actions->toArray();
  179. foreach ($actions as $action) {
  180. // 统计用户类型
  181. switch ($action['user_type']) {
  182. case OrderActionEnum::USER_TYPE_CUSTOMER:
  183. $stats['user_count']++;
  184. break;
  185. case OrderActionEnum::USER_TYPE_ADMIN:
  186. $stats['admin_count']++;
  187. break;
  188. case OrderActionEnum::USER_TYPE_SYSTEM:
  189. $stats['system_count']++;
  190. break;
  191. }
  192. // 统计操作类型
  193. $actionType = $action['action_type'];
  194. if (!isset($stats['action_types'][$actionType])) {
  195. $stats['action_types'][$actionType] = 0;
  196. }
  197. $stats['action_types'][$actionType]++;
  198. // 统计优先级分布
  199. $priority = $action['priority'];
  200. if (!isset($stats['priority_distribution'][$priority])) {
  201. $stats['priority_distribution'][$priority] = 0;
  202. }
  203. $stats['priority_distribution'][$priority]++;
  204. }
  205. return $stats;
  206. }
  207. /**
  208. * 获取订单操作时间线数据
  209. * @param string $orderSn 订单编号
  210. * @param bool $includeSystem 是否包含系统操作
  211. * @return array
  212. */
  213. public function getTimelineData(string $orderSn, bool $includeSystem = true): array
  214. {
  215. $query = $this->model->where(['order_sn' => $orderSn]);
  216. if (!$includeSystem) {
  217. $query = $query->where('user_type', '<>', OrderActionEnum::USER_TYPE_SYSTEM);
  218. }
  219. $actions = $query->order('createtime', 'desc')->select();
  220. $timeline = [];
  221. foreach ($actions as $action) {
  222. $timeline[] = [
  223. 'id' => $action['id'],
  224. 'time' => $action['createtime'],
  225. 'time_text' => $action['createtime_text'],
  226. 'operator' => $action['operator'],
  227. 'action_type' => $action['action_type'],
  228. 'action_text' => $action['action_type_text'],
  229. 'user_type' => $action['user_type'],
  230. 'user_text' => $action['user_type_text'],
  231. 'memo' => $action['memo'],
  232. 'priority' => $action['priority'],
  233. 'priority_text' => $action['priority_text'],
  234. 'extra_data' => $action['extra_data'],
  235. ];
  236. }
  237. return $timeline;
  238. }
  239. /**
  240. * 按时间范围查询操作记录
  241. * @param string $orderSn 订单编号
  242. * @param int $startTime 开始时间
  243. * @param int $endTime 结束时间
  244. * @return Collection
  245. */
  246. public function findByTimeRange(string $orderSn, int $startTime, int $endTime): Collection
  247. {
  248. return $this->model->where([
  249. 'order_sn' => $orderSn,
  250. 'createtime' => ['between', [$startTime, $endTime]]
  251. ])->order('createtime', 'desc')->select();
  252. }
  253. /**
  254. * 按优先级查询操作记录
  255. * @param string $orderSn 订单编号
  256. * @param int $priority 优先级
  257. * @return Collection
  258. */
  259. public function findByPriority(string $orderSn, int $priority): Collection
  260. {
  261. return $this->model->where([
  262. 'order_sn' => $orderSn,
  263. 'priority' => $priority
  264. ])->order('createtime', 'desc')->select();
  265. }
  266. }