123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- namespace app\common\repository;
- use app\common\model\OrderAction;
- use app\common\Enum\OrderActionEnum;
- use think\Collection;
- use think\Exception;
- /**
- * 订单操作记录仓库类
- * 数据访问抽象层,隐藏具体数据存储实现
- */
- class OrderActionRepository
- {
- protected $model;
- public function __construct()
- {
- $this->model = new OrderAction();
- }
- /**
- * 创建操作记录
- * @param array $data 操作数据
- * @return bool
- */
- public function create(array $data): bool
- {
- try {
- return $this->model->create($data) ? true : false;
- } catch (Exception $e) {
- return false;
- }
- }
- /**
- * 批量创建操作记录
- * @param array $dataList 批量数据
- * @return bool
- */
- public function createBatch(array $dataList): bool
- {
- try {
- return $this->model->insertBatch($dataList);
- } catch (Exception $e) {
- return false;
- }
- }
- /**
- * 根据订单编号查询操作记录
- * @param string $orderSn 订单编号
- * @param string $actionType 操作类型(可选)
- * @param string $userType 用户类型(可选)
- * @param int $limit 限制数量(可选)
- * @return Collection
- */
- public function findByOrderSn(string $orderSn, string $actionType = '', string $userType = '', int $limit = 0): Collection
- {
- $where = ['order_sn' => $orderSn];
-
- if ($actionType) {
- $where['action_type'] = $actionType;
- }
-
- if ($userType) {
- $where['user_type'] = $userType;
- }
- $query = $this->model->where($where)->order('createtime', 'desc');
-
- if ($limit > 0) {
- $query = $query->limit($limit);
- }
- return $query->select();
- }
- /**
- * 获取最新的操作记录
- * @param string $orderSn 订单编号
- * @param string $actionType 操作类型(可选)
- * @return OrderAction|null
- */
- public function findLatestByOrderSn(string $orderSn, string $actionType = ''): ?OrderAction
- {
- $where = ['order_sn' => $orderSn];
-
- if ($actionType) {
- $where['action_type'] = $actionType;
- }
- return $this->model->where($where)->order('createtime', 'desc')->find();
- }
- /**
- * 检查特定操作类型是否存在
- * @param string $orderSn 订单编号
- * @param string $actionType 操作类型
- * @return bool
- */
- public function existsByActionType(string $orderSn, string $actionType): bool
- {
- return $this->model->where([
- 'order_sn' => $orderSn,
- 'action_type' => $actionType
- ])->count() > 0;
- }
- /**
- * 统计操作记录数量
- * @param string $orderSn 订单编号
- * @param string $actionType 操作类型(可选)
- * @param string $userType 用户类型(可选)
- * @return int
- */
- public function countByOrderSn(string $orderSn, string $actionType = '', string $userType = ''): int
- {
- $where = ['order_sn' => $orderSn];
-
- if ($actionType) {
- $where['action_type'] = $actionType;
- }
-
- if ($userType) {
- $where['user_type'] = $userType;
- }
- return $this->model->where($where)->count();
- }
- /**
- * 获取用户操作历史
- * @param int $userId 用户ID
- * @param string $userType 用户类型
- * @param int $limit 限制数量
- * @return Collection
- */
- public function findByUser(int $userId, string $userType = OrderActionEnum::USER_TYPE_CUSTOMER, int $limit = 10): Collection
- {
- return $this->model->where([
- 'user_id' => $userId,
- 'user_type' => $userType
- ])->order('createtime', 'desc')
- ->limit($limit)
- ->select();
- }
- /**
- * 获取高优先级操作记录
- * @param string $orderSn 订单编号
- * @return Collection
- */
- public function findHighPriorityByOrderSn(string $orderSn): Collection
- {
- return $this->model->where([
- 'order_sn' => $orderSn,
- 'priority' => [
- 'in',
- [OrderActionEnum::PRIORITY_HIGH, OrderActionEnum::PRIORITY_URGENT]
- ]
- ])->order('createtime', 'desc')
- ->select();
- }
- /**
- * 删除过期记录
- * @param int $expiredTime 过期时间戳
- * @return bool
- */
- public function deleteExpired(int $expiredTime): bool
- {
- try {
- return $this->model->where('createtime', '<', $expiredTime)->delete() !== false;
- } catch (Exception $e) {
- return false;
- }
- }
- /**
- * 获取操作记录用于统计
- * @param string $orderSn 订单编号
- * @return array
- */
- public function getStatisticsData(string $orderSn): array
- {
- $actions = $this->findByOrderSn($orderSn);
-
- $stats = [
- 'total_count' => 0,
- 'user_count' => 0,
- 'admin_count' => 0,
- 'system_count' => 0,
- 'action_types' => [],
- 'priority_distribution' => [],
- 'actions_data' => []
- ];
- if ($actions->isEmpty()) {
- return $stats;
- }
- $stats['total_count'] = $actions->count();
- $stats['actions_data'] = $actions->toArray();
- 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]++;
- // 统计优先级分布
- $priority = $action['priority'];
- if (!isset($stats['priority_distribution'][$priority])) {
- $stats['priority_distribution'][$priority] = 0;
- }
- $stats['priority_distribution'][$priority]++;
- }
- return $stats;
- }
- /**
- * 获取订单操作时间线数据
- * @param string $orderSn 订单编号
- * @param bool $includeSystem 是否包含系统操作
- * @return array
- */
- public function getTimelineData(string $orderSn, bool $includeSystem = true): array
- {
- $query = $this->model->where(['order_sn' => $orderSn]);
-
- if (!$includeSystem) {
- $query = $query->where('user_type', '<>', OrderActionEnum::USER_TYPE_SYSTEM);
- }
-
- $actions = $query->order('createtime', 'desc')->select();
-
- $timeline = [];
- foreach ($actions as $action) {
- $timeline[] = [
- 'id' => $action['id'],
- 'time' => $action['createtime'],
- 'time_text' => $action['createtime_text'],
- 'operator' => $action['operator'],
- 'action_type' => $action['action_type'],
- 'action_text' => $action['action_type_text'],
- 'user_type' => $action['user_type'],
- 'user_text' => $action['user_type_text'],
- 'memo' => $action['memo'],
- 'priority' => $action['priority'],
- 'priority_text' => $action['priority_text'],
- 'extra_data' => $action['extra_data'],
- ];
- }
- return $timeline;
- }
- /**
- * 按时间范围查询操作记录
- * @param string $orderSn 订单编号
- * @param int $startTime 开始时间
- * @param int $endTime 结束时间
- * @return Collection
- */
- public function findByTimeRange(string $orderSn, int $startTime, int $endTime): Collection
- {
- return $this->model->where([
- 'order_sn' => $orderSn,
- 'createtime' => ['between', [$startTime, $endTime]]
- ])->order('createtime', 'desc')->select();
- }
- /**
- * 按优先级查询操作记录
- * @param string $orderSn 订单编号
- * @param int $priority 优先级
- * @return Collection
- */
- public function findByPriority(string $orderSn, int $priority): Collection
- {
- return $this->model->where([
- 'order_sn' => $orderSn,
- 'priority' => $priority
- ])->order('createtime', 'desc')->select();
- }
- }
|