123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- <?php
- namespace app\common\Service;
- use app\common\repository\OrderActionRepository;
- use app\common\Enum\OrderActionEnum;
- use think\Exception;
- use think\Collection;
- /**
- * 订单操作记录服务类
- * 业务逻辑层,处理复杂的业务规则和流程
- */
- class OrderActionService
- {
- protected $repository;
- public function __construct()
- {
- $this->repository = new OrderActionRepository();
- }
- /**
- * 添加订单操作记录
- * @param string $order_sn 订单编号
- * @param string $operator 操作人
- * @param string $memo 备注
- * @return bool
- */
- public function push($order_sn, $operator, $memo)
- {
- return $this->addAction([
- 'order_sn' => $order_sn,
- 'operator' => $operator,
- 'memo' => $memo,
- 'action_type' => OrderActionEnum::ACTION_MODIFY,
- 'user_type' => OrderActionEnum::USER_TYPE_ADMIN,
- 'operator_type' => OrderActionEnum::OPERATOR_TYPE_ADMIN,
- ]);
- }
- /**
- * 静态方法兼容(向后兼容)
- */
- public static function staticPush($order_sn, $operator, $memo)
- {
- $service = new self();
- return $service->push($order_sn, $operator, $memo);
- }
- /**
- * 添加订单操作记录(核心方法)
- * @param array $data 操作记录数据
- * @return bool
- */
- public function addAction($data = [])
- {
- // 验证必要字段
- if (empty($data['order_sn'])) {
- return false;
- }
- // 验证操作类型
- if (!empty($data['action_type']) && !OrderActionEnum::isValidActionType($data['action_type'])) {
- return false;
- }
- // 验证用户类型
- if (!empty($data['user_type']) && !OrderActionEnum::isValidUserType($data['user_type'])) {
- return false;
- }
- // 构建操作数据
- $actionData = $this->buildActionData($data);
- // 通过仓库层保存数据
- return $this->repository->create($actionData);
- }
- /**
- * 添加用户操作记录
- * @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 function addUserAction($order_sn, $action_type, $operator, $memo = '', $user_id = 0, $extra = [])
- {
- return $this->addAction([
- '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' => $extra,
- ]);
- }
- public function addInspectionAction($order_sn, $action_type, $operator, $memo = '', $user_id = 0, $extra = []){
- return $this->addAction([
- 'order_sn' => $order_sn,
- 'action_type' => $action_type,
- 'operator' => $operator,
- 'memo' => $memo,
- 'user_type' => OrderActionEnum::USER_TYPE_INSPECTION,
- 'operator_type' => OrderActionEnum::OPERATOR_TYPE_INSPECTION,
- 'user_id' => $user_id,
- 'extra_data' => $extra,
- ]);
- }
- /**
- * 添加管理员操作记录
- * @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 function addAdminAction($order_sn, $action_type, $operator, $memo = '', $admin_id = 0, $extra = [])
- {
- return $this->addAction([
- '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' => $extra,
- ]);
- }
- /**
- * 添加系统操作记录
- * @param string $order_sn 订单编号
- * @param string $action_type 操作类型
- * @param string $memo 备注
- * @param array $extra 额外数据
- * @return bool
- */
- public function addSystemAction($order_sn, $action_type, $memo = '', $extra = [])
- {
- return $this->addAction([
- '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' => $extra,
- ]);
- }
- /**
- * 批量添加操作记录
- * @param array $batch_data 批量数据
- * @return bool
- */
- public function addBatchActions($batch_data)
- {
- if (empty($batch_data)) {
- return false;
- }
- $processedData = [];
- foreach ($batch_data as $data) {
- if (empty($data['order_sn'])) {
- continue;
- }
- $processedData[] = $this->buildActionData($data);
- }
- return $this->repository->createBatch($processedData);
- }
- /**
- * 获取订单操作记录
- * @param string $order_sn 订单编号
- * @param string $action_type 操作类型(可选)
- * @param string $user_type 用户类型(可选)
- * @param int $limit 限制数量
- * @return Collection
- */
- public function getOrderActions($order_sn, $action_type = '', $user_type = '', $limit = 0)
- {
- return $this->repository->findByOrderSn($order_sn, $action_type, $user_type, $limit);
- }
- /**
- * 获取最后一次操作记录
- * @param string $order_sn 订单编号
- * @param string $action_type 操作类型(可选)
- * @return mixed
- */
- public function getLastAction($order_sn, $action_type = '')
- {
- return $this->repository->findLatestByOrderSn($order_sn, $action_type);
- }
- /**
- * 检查操作是否存在
- * @param string $order_sn 订单编号
- * @param string $action_type 操作类型
- * @return bool
- */
- public function hasAction($order_sn, $action_type)
- {
- return $this->repository->existsByActionType($order_sn, $action_type);
- }
- /**
- * 获取操作统计数据
- * @param string $order_sn 订单编号
- * @return array
- */
- public function getActionStats($order_sn)
- {
- $stats = $this->repository->getStatisticsData($order_sn);
-
- // 添加业务逻辑处理
- if ($stats['total_count'] > 0) {
- $stats['first_action'] = end($stats['actions_data']);
- $stats['last_action'] = $stats['actions_data'][0];
-
- // 计算活跃度
- $stats['activity_score'] = $this->calculateActivityScore($stats);
- }
- return $stats;
- }
- /**
- * 生成操作时间线
- * @param string $order_sn 订单编号
- * @param bool $include_system 是否包含系统操作
- * @return array
- */
- public function getActionTimeline($order_sn, $include_system = true)
- {
- return $this->repository->getTimelineData($order_sn, $include_system);
- }
- /**
- * 记录订单状态变化
- * @param string $order_sn 订单编号
- * @param string $old_status 旧状态
- * @param string $new_status 新状态
- * @param string $operator 操作人
- * @param string $user_type 用户类型
- * @param int $user_id 用户ID
- * @param string $memo 备注
- * @return bool
- */
- public function recordStatusChange($order_sn, $old_status, $new_status, $operator, $user_type = OrderActionEnum::USER_TYPE_ADMIN, $user_id = 0, $memo = '')
- {
- $actionType = $this->getActionTypeByStatus($new_status);
- $defaultMemo = "订单状态从 {$old_status} 变更为 {$new_status}";
-
- return $this->addAction([
- 'order_sn' => $order_sn,
- 'action_type' => $actionType,
- 'operator' => $operator,
- 'memo' => $memo ?: $defaultMemo,
- 'user_type' => $user_type,
- 'user_id' => $user_id,
- 'extra_data' => [
- 'old_status' => $old_status,
- 'new_status' => $new_status,
- ],
- ]);
- }
- /**
- * 获取操作次数统计
- * @param string $order_sn 订单编号
- * @param string $action_type 操作类型
- * @return int
- */
- public function getActionCount($order_sn, $action_type = '')
- {
- return $this->repository->countByOrderSn($order_sn, $action_type);
- }
- /**
- * 获取用户操作历史
- * @param int $user_id 用户ID
- * @param string $user_type 用户类型
- * @param int $limit 限制数量
- * @return Collection
- */
- public function getUserActionHistory($user_id, $user_type = OrderActionEnum::USER_TYPE_CUSTOMER, $limit = 10)
- {
- return $this->repository->findByUser($user_id, $user_type, $limit);
- }
- /**
- * 清理过期操作记录
- * @param int $days 保留天数
- * @return bool
- */
- public function cleanExpiredActions($days = 90)
- {
- $expiredTime = time() - ($days * 24 * 3600);
- return $this->repository->deleteExpired($expiredTime);
- }
- /**
- * 获取高优先级操作记录
- * @param string $order_sn 订单编号
- * @return Collection
- */
- public function getHighPriorityActions($order_sn)
- {
- return $this->repository->findHighPriorityByOrderSn($order_sn);
- }
- /**
- * 验证操作记录数据
- * @param array $data 数据
- * @return array [bool, string] [是否有效, 错误信息]
- */
- public function validateActionData($data)
- {
- if (empty($data['order_sn'])) {
- return [false, '订单编号不能为空'];
- }
- if (!empty($data['action_type']) && !OrderActionEnum::isValidActionType($data['action_type'])) {
- return [false, '无效的操作类型'];
- }
- if (!empty($data['user_type']) && !OrderActionEnum::isValidUserType($data['user_type'])) {
- return [false, '无效的用户类型'];
- }
- if (!empty($data['priority']) && !OrderActionEnum::isValidPriority($data['priority'])) {
- return [false, '无效的优先级'];
- }
- return [true, ''];
- }
- /**
- * 构建操作数据
- * @param array $data 原始数据
- * @return array 处理后的数据
- */
- protected function buildActionData($data)
- {
- $actionData = [
- 'order_sn' => $data['order_sn'],
- 'operator' => $data['operator'] ?? 'unknown',
- 'memo' => $data['memo'] ?? '',
- 'action_type' => $data['action_type'] ?? OrderActionEnum::ACTION_MODIFY,
- 'user_type' => $data['user_type'] ?? OrderActionEnum::USER_TYPE_ADMIN,
- 'operator_type' => $data['operator_type'] ?? OrderActionEnum::OPERATOR_TYPE_ADMIN,
- 'priority' => $data['priority'] ?? OrderActionEnum::getDefaultPriority($data['action_type'] ?? OrderActionEnum::ACTION_MODIFY),
- 'user_id' => $data['user_id'] ?? 0,
- 'ip' => $data['ip'] ?? request()->ip(),
- 'user_agent' => $data['user_agent'] ?? request()->server('HTTP_USER_AGENT', ''),
- 'extra_data' => is_array($data['extra_data'] ?? '') ? json_encode($data['extra_data']) : ($data['extra_data'] ?? ''),
- ];
- // 如果没有指定操作员类型,根据用户类型自动设置
- if (empty($data['operator_type'])) {
- $actionData['operator_type'] = OrderActionEnum::getDefaultOperatorType($actionData['user_type']);
- }
- return $actionData;
- }
- /**
- * 根据状态获取操作类型
- * @param string $status 状态
- * @return string
- */
- protected function getActionTypeByStatus($status)
- {
- $statusMap = [
- 'created' => OrderActionEnum::ACTION_CREATE,
- 'paid' => OrderActionEnum::ACTION_PAY,
- 'shipped' => OrderActionEnum::ACTION_SHIP,
- 'received' => OrderActionEnum::ACTION_RECEIVE,
- 'cancelled' => OrderActionEnum::ACTION_CANCEL,
- 'refunded' => OrderActionEnum::ACTION_REFUND,
- 'completed' => OrderActionEnum::ACTION_COMPLETE,
- ];
- return $statusMap[$status] ?? OrderActionEnum::ACTION_MODIFY;
- }
- /**
- * 计算活跃度评分
- * @param array $stats 统计数据
- * @return float
- */
- protected function calculateActivityScore($stats)
- {
- $baseScore = $stats['total_count'] * 10;
- $userScore = $stats['user_count'] * 5;
- $adminScore = $stats['admin_count'] * 3;
- $systemScore = $stats['system_count'] * 1;
- return ($baseScore + $userScore + $adminScore + $systemScore) / 100;
- }
- // 静态方法兼容(向后兼容)
- public static function staticAddAction($data = [])
- {
- $service = new self();
- return $service->addAction($data);
- }
- public static function staticAddUserAction($order_sn, $action_type, $operator, $memo = '', $user_id = 0, $extra = [])
- {
- $service = new self();
- return $service->addUserAction($order_sn, $action_type, $operator, $memo, $user_id, $extra);
- }
- public static function staticAddAdminAction($order_sn, $action_type, $operator, $memo = '', $admin_id = 0, $extra = [])
- {
- $service = new self();
- return $service->addAdminAction($order_sn, $action_type, $operator, $memo, $admin_id, $extra);
- }
- public static function staticAddSystemAction($order_sn, $action_type, $memo = '', $extra = [])
- {
- $service = new self();
- return $service->addSystemAction($order_sn, $action_type, $memo, $extra);
- }
- public static function staticAddBatchActions($batch_data)
- {
- $service = new self();
- return $service->addBatchActions($batch_data);
- }
- public static function staticGetOrderActions($order_sn, $action_type = '', $user_type = '', $limit = 0)
- {
- $service = new self();
- return $service->getOrderActions($order_sn, $action_type, $user_type, $limit);
- }
- public static function staticGetLastAction($order_sn, $action_type = '')
- {
- $service = new self();
- return $service->getLastAction($order_sn, $action_type);
- }
- public static function staticHasAction($order_sn, $action_type)
- {
- $service = new self();
- return $service->hasAction($order_sn, $action_type);
- }
- public static function staticGetActionStats($order_sn)
- {
- $service = new self();
- return $service->getActionStats($order_sn);
- }
- public static function staticGetActionTimeline($order_sn, $include_system = true)
- {
- $service = new self();
- return $service->getActionTimeline($order_sn, $include_system);
- }
- public static function staticRecordStatusChange($order_sn, $old_status, $new_status, $operator, $user_type = OrderActionEnum::USER_TYPE_ADMIN, $user_id = 0, $memo = '')
- {
- $service = new self();
- return $service->recordStatusChange($order_sn, $old_status, $new_status, $operator, $user_type, $user_id, $memo);
- }
- public static function staticGetActionCount($order_sn, $action_type = '')
- {
- $service = new self();
- return $service->getActionCount($order_sn, $action_type);
- }
- public static function staticGetUserActionHistory($user_id, $user_type = OrderActionEnum::USER_TYPE_CUSTOMER, $limit = 10)
- {
- $service = new self();
- return $service->getUserActionHistory($user_id, $user_type, $limit);
- }
- public static function staticCleanExpiredActions($days = 90)
- {
- $service = new self();
- return $service->cleanExpiredActions($days);
- }
- public static function staticGetHighPriorityActions($order_sn)
- {
- $service = new self();
- return $service->getHighPriorityActions($order_sn);
- }
- public static function staticValidateActionData($data)
- {
- $service = new self();
- return $service->validateActionData($data);
- }
- }
|