| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- <?php
- namespace backend\modules\mall\services;
- use addons\Mch\common\models\MchAdmin;
- use addons\Store\common\models\StoreAdmin;
- use common\enums\AddonsEnum;
- use common\enums\OrderBehaviorLogsEnum;
- use common\enums\RefundStatusEnum;
- use common\enums\StatusEnum;
- use common\enums\UserBehaviorLogsEnum;
- use common\models\backend\Admin;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use common\models\order\OrderBehaviorLogs;
- use common\models\user\User;
- use common\models\user\UserBehaviorLogs;
- class LogsServices extends BaseService
- {
- /**
- * 获取用户日志
- *
- * @param array $params
- *
- * @return array
- */
- public function getUserLogs(array $params): array
- {
- $operatorUserType = $params['operator_user_type'] ?? 0;
- $operatorUserKeyword = $params['operator_user_keyword'] ?? 0;
- $operatorTargetUserKeyword = $params['operator_target_user_keyword'] ?? '';
- $behavior = $params['behavior'] ?? 0;
- $where = [['mall_id' => $this->mall_id]];
- if ($operatorUserType) {
- $where[] = ['operator_user_type' => $operatorUserType];
- }
- if ($operatorUserKeyword) {
- if ($operatorUserType == UserBehaviorLogsEnum::OPERATOR_USER_TYPE_ADMIN) {
- $adminFind = Admin::find();
- Admin::paramPack([
- 'where' => [
- ['mall_id' => [0, $this->mall_id]],
- [
- 'or',
- ['like', 'username', $operatorUserKeyword],
- ['like', 'account', $operatorUserKeyword]
- ]
- ],
- 'select' => 'id'
- ], $adminFind);
- $adminIdsBy = $adminFind->column();
- $where[] = ['operator_user_id' => $adminIdsBy];
- } elseif ($operatorUserType == UserBehaviorLogsEnum::OPERATOR_USER_TYPE_USER) {
- $userIdsByMobile = User::find()
- ->where(['like', 'mobile', $operatorUserKeyword])
- ->andWhere(['mall_id' => $this->mall_id])
- ->select('id')
- ->column();
- $where[] = ['operator_user_id' => array_merge([$operatorUserKeyword], $userIdsByMobile)];
- }
- }
- if ($operatorTargetUserKeyword) {
- $userIdsByOperatorTargetUserKeyword = User::find()
- ->where(['like', 'mobile', $operatorTargetUserKeyword])
- ->andWhere(['mall_id' => $this->mall_id])
- ->select('id')
- ->column();
- $where[] = [
- 'or',
- ['=', 'operator_target_user_id', $operatorTargetUserKeyword],
- ['in', 'operator_target_user_id', $userIdsByOperatorTargetUserKeyword]
- ];
- }
- if ($behavior) {
- $where[] = ['=', 'behavior', $behavior];
- }
- $userSelect = 'id, mobile, nickname, avatar_url';
- $with = [
- 'user' => function ($query) use ($userSelect) {
- $query->select($userSelect);
- }
- ];
- $order = 'id desc';
- $list = UserBehaviorLogs::listPage(compact('where', 'with', 'order'));
- $operatorUserIds = [];
- array_walk($list['list'], function ($item) use (&$operatorUserIds) {
- $operatorUserIds[$item['operator_user_type']][] = $item['operator_user_id'];
- });
- // 查询用户
- $userList = [];
- if (!empty($operatorUserIds[UserBehaviorLogsEnum::OPERATOR_USER_TYPE_USER])) {
- $userList = User::getUserList(
- [
- ['in', 'id', $operatorUserIds[UserBehaviorLogsEnum::OPERATOR_USER_TYPE_USER]]
- ],
- $userSelect,
- [],
- true
- );
- }
- // 查询后台管理员
- $adminList = [];
- if (!empty($operatorUserIds[UserBehaviorLogsEnum::OPERATOR_USER_TYPE_ADMIN])) {
- $adminList = Admin::lists([
- 'where' => [
- [
- 'id' => $operatorUserIds[UserBehaviorLogsEnum::OPERATOR_USER_TYPE_ADMIN],
- 'status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]
- ]
- ],
- 'select' => "id, account mobile, username nickname"
- ]);
- }
- $userList = array_column($userList, null, 'id');
- $adminList = array_column($adminList, null, 'id');
- array_walk($list['list'], function (&$item) use ($adminList, $userList) {
- $item['extra_info'] = json_decode($item['extra_info'], true);
- $item['behavior_text'] = UserBehaviorLogsEnum::getMapValueByKey(0, $item['behavior']);
- $item['operator_user_type_text'] = UserBehaviorLogsEnum::getMapValueByKey(1, $item['operator_user_type']);
- $item['created_at_text'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['ip_text'] = $item['ip'] ? long2ip($item['ip']) : '-';
- $item['operator_user'] = UserBehaviorLogsEnum::OPERATOR_USER_TYPE_USER == $item['operator_user_type'] ?
- ($userList[$item['operator_user_id']] ?? []) :
- ($adminList[$item['operator_user_id']] ?? []);
- });
- return $list;
- }
- /**
- * 获取用户日志
- *
- * @param array $params
- *
- * @return array
- */
- public function getOrderLogs(array $params): array
- {
- $operatorUserType = $params['operator_user_type'] ?? 0;
- $operatorUserKeyword = $params['operator_user_keyword'] ?? 0;
- $behavior = $params['behavior'] ?? 0;
- $orderId = $params['order_id'] ?? 0;
- $orderNo = $params['order_no'] ?? '';
- $createdAt = $params['created_at'] ?? [];
- $where = [['mall_id' => $this->mall_id]];
- if ($operatorUserType) {
- $where[] = ['operator_user_type' => $operatorUserType];
- }
- if ($operatorUserKeyword) {
- if ($operatorUserType == OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_ADMIN) {
- $adminFind = Admin::find();
- Admin::paramPack([
- 'where' => [
- ['mall_id' => [0, $this->mall_id]],
- [
- 'or',
- ['like', 'username', $operatorUserKeyword],
- ['like', 'account', $operatorUserKeyword]
- ]
- ],
- 'select' => 'id'
- ], $adminFind);
- $adminIdsBy = $adminFind->column();
- $where[] = ['operator_user_id' => $adminIdsBy];
- } elseif ($operatorUserType == OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_USER) {
- $userIdsByMobile = User::find()
- ->where(['like', 'mobile', $operatorUserKeyword])
- ->andWhere(['mall_id' => $this->mall_id])
- ->select('id')
- ->column();
- $where[] = ['operator_user_id' => array_merge([$operatorUserKeyword], $userIdsByMobile)];
- }
- }
- if ($behavior) {
- $where[] = ['=', 'behavior', $behavior];
- }
- if ($orderId) {
- $where[] = ['=', 'order_id', $orderId];
- }
- if ($orderNo) {
- $orderIds = Order::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['like', 'order_no', $orderNo])
- ->select('id')
- ->column();
- $where[] = ['in', 'order_id', $orderIds];
- }
- if ($createdAt) {
- $strToTime = array_map(function ($value) {
- return strtotime($value);
- }, $createdAt);
- if (!empty($strToTime)) {
- $where[] = ['between', 'created_at', $strToTime[0], $strToTime[1]];
- }
- }
- $with = [
- 'order' => function ($query) {
- $query->select('id, order_no');
- }
- ];
- $order = 'id desc';
- $list = OrderBehaviorLogs::listPage(compact('where', 'with', 'order'));
- $operatorUserIds = [];
- array_walk($list['list'], function ($item) use (&$operatorUserIds) {
- $operatorUserIds[$item['operator_user_type']][] = $item['operator_user_id'];
- });
- $userArr = $this->getOperatorOrderUserList($operatorUserIds);
- array_walk($list['list'], function (&$item) use ($userArr) {
- $item['extra_info'] = json_decode($item['extra_info'], true);
- $item['operator_user_type_text'] = OrderBehaviorLogsEnum::getMap(1)[$item['operator_user_type']] ?? '-';
- $item['created_at_text'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['ip_text'] = $item['ip'] ? long2ip($item['ip']) : '-';
- $item['operator_user'] = $userArr[$item['operator_user_type']][$item['operator_user_id']] ?? [];
- $item['behavior_text'] = $this->getBehaviorText($item);
- $item['desc'] = $this->getDescText($item);
- });
- return $list;
- }
- /**
- * 获取行为text
- *
- * @param array $item
- *
- * @return string
- */
- protected function getBehaviorText(array $item): string
- {
- if (!isset(OrderBehaviorLogsEnum::getMap()[$item['behavior']])) {
- return '';
- }
- $str = OrderBehaviorLogsEnum::getMap()[$item['behavior']];
- if (!empty($item['extra_info']['is_manual'])) {
- $str .= '(手动)';
- } elseif (!empty($item['extra_info']['is_expired'])) {
- $str .= '(超过售后期限,撤销售后订单)';
- } elseif (!empty($item['extra_info']['is_batch'])) {
- $str .= '(批量)';
- }
- return $str;
- }
- /**
- * 获取desc
- *
- * @param array $item
- *
- * @return string
- */
- protected function getDescText(array $item): string
- {
- $str = $item['behavior_text'] . ($item['desc'] ? '-' . $item['desc'] : '');
- if (!empty($item['extra_info']['step_status'])) {
- $str .= sprintf('-(%s)', RefundStatusEnum::getStepStatusText($item['extra_info']['step_status'])['desc']);
- }
- return $str;
- }
- /**
- * @param array $operatorUserIds
- *
- * @return array
- */
- protected function getOperatorOrderUserList(array $operatorUserIds): array
- {
- $userArr = [];
- // 查询用户
- if (!empty($operatorUserIds[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_USER])) {
- $userList = User::getUserList(
- [
- ['in', 'id', $operatorUserIds[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_USER]]
- ],
- 'id, mobile, nickname, avatar_url',
- [],
- true
- );
- $userArr[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_USER] = array_column($userList, null, 'id');
- }
- // 查询后台管理员
- if (!empty($operatorUserIds[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_ADMIN])) {
- $adminList = Admin::lists([
- 'where' => [
- [
- 'id' => $operatorUserIds[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_ADMIN],
- 'status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]
- ]
- ],
- 'select' => "id, account mobile, username nickname"
- ]);
- $userArr[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_ADMIN] = array_column($adminList, null, 'id');
- }
- // 查询门店管理员
- if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE) && !empty($operatorUserIds[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_STORE])) {
- $storeList = StoreAdmin::lists([
- 'where' => [
- [
- 'id' => $operatorUserIds[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_STORE],
- 'status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]
- ]
- ],
- 'select' => "id, account mobile, username nickname"
- ]);
- $userArr[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_STORE] = array_column($storeList, null, 'id');
- }
- // 查询多商户管理员
- if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_MCH) && !empty($operatorUserIds[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_MCH])) {
- $mchList = MchAdmin::lists([
- 'where' => [
- [
- 'id' => $operatorUserIds[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_MCH],
- 'status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]
- ]
- ],
- 'select' => "id, account mobile, username nickname"
- ]);
- $userArr[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_MCH] = array_column($mchList, null, 'id');
- }
- // 查询aiyunuo管理员
- if (MallAddons::isInstall($this->mall_id, 'Aiyunuo') && !empty($operatorUserIds[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_AIYUNUO])) {
- $aiyunuoList = Admin::lists([
- 'where' => [
- [
- 'id' => $operatorUserIds[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_AIYUNUO],
- 'status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]
- ]
- ],
- 'select' => "id, account mobile, username nickname"
- ]);
- $userArr[OrderBehaviorLogsEnum::OPERATOR_USER_TYPE_AIYUNUO] = array_column($aiyunuoList, null, 'id');
- }
- return $userArr;
- }
- }
|