| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679 |
- <?php
- namespace addons\BusinessCard\api\modules\v1\controllers;
- use addons\BusinessCard\common\enums\CardEnums;
- use addons\BusinessCard\common\logic\Statistics;
- use addons\BusinessCard\common\models\BusinessCard;
- use addons\BusinessCard\common\models\BusinessCardBuriedPoint;
- use addons\BusinessCard\common\models\BusinessCardCustomerLog;
- use addons\BusinessCard\common\models\BusinessCardFollowLog;
- use addons\BusinessCard\common\models\BusinessCardMaterials;
- use addons\BusinessCard\common\models\BusinessCardMaterialsBrowseLog;
- use addons\BusinessCard\common\models\BusinessCardOrder;
- use addons\BusinessCard\common\models\BusinessCardPosition;
- use addons\BusinessCard\common\models\BusinessCardSecretUserScore;
- use addons\BusinessCard\common\models\BusinessCardStatistics;
- use addons\BusinessCard\common\models\BusinessCardUser;
- use common\models\common\BuyingPointDay;
- use common\models\goods\Goods;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use api\controllers\OnAuthController;
- use common\enums\StatusEnum;
- use common\models\user\UserLabel;
- use common\models\user\UserLabelRelationship;
- use common\models\user\UserPartitionRelationship;
- use Yii;
- use yii\data\Pagination;
- class CustomerController extends OnAuthController
- {
- public $modelClass = '';
- public $card = '';
- /**
- * 提示创建名片
- */
- public function beforeAction($action) {
- parent::beforeAction($action);
- $card = BusinessCard::getOne([['=', 'user_id', $this->user_id], ['=', 'status', StatusEnum::ENABLED]], true);
- if (empty($card)) {
- Yii::$app->controller->asJson(['code' => 3, 'msg' => '请先创建名片'])->send();
- return;
- }
- $this->card = $card;
- return $action;
- }
- /**
- * 客户列表&我的线索&新增线索
- * @author hpei
- * @param int $customer_type 0:全部 1:潜在客户 2:意向客户 3:比较客户 4:待成交客户 5:已成交客户
- * @param int $deep 1:直推 2:间推
- * @param int $date 新增线索传15,其他不用传
- * @param int user_id 查询团队列表传
- * @param string $keyword
- * @return array
- */
- public function actionCustomerList() {
- $get = Yii::$app->request->get();
- $where[] = ['=' ,'mall_id', $this->mall_id];
- $where[] = ['=' , 'status', StatusEnum::ENABLED];
- if (isset($get['customer_type']) && !empty($get['customer_type'])) $where[] = ['=', 'customer_type', $get['customer_type']];
- $ids = [];
- $customer_type = CardEnums::getCustomerTypeMap();
- if (isset($get['date']) && !empty($get['date'])) {
- $date = $get['date'];
- $start_time = strtotime("-$date day 0:0:0");
- $end_time = $start_time + ($date - 1) * 86400 + 86399;
- $where[] = ['>', 'created_at', $start_time];
- $where[] = ['<=', 'created_at', $end_time];
- }
- if (isset($get['keyword']) && !empty($get['keyword'])) {
- $userModel = User::find();
- $userParam['where'] = [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED], ['like', 'nickname', $get['keyword']]];
- User::paramPack($userParam, $userModel);
- $ids = $userModel->select(['id'])->column();
- if (empty($ids)) $this->success('操作成功', ['list' => [], 'direct' => 0, 'second' => 0, 'customer_list' => $customer_type]);
- }
- $deep = $get['deep'] ? $get['deep'] : 1;
- if ($deep > 2) return $this->error('层级异常');
- $uid = isset($get['user_id']) ? $get['user_id'] : $this->user_id;
- $direct_ids = UserPartitionRelationship::getChildIdArr($uid, 1);
- $second_ids = UserPartitionRelationship::getChildIdArr($uid, 2);
- $direct_ids = array_merge($ids, $direct_ids);
- $second_ids = array_merge($ids, $second_ids);
- $direct_count = BusinessCardUser::count(['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED], ['in', 'user_id', $direct_ids]]]);
- $second_count = BusinessCardUser::count(['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED], ['in', 'user_id', $second_ids]]]);
- $where[] = $deep == 1 ? ['in', 'user_id', $direct_ids] : ['in', 'user_id', $second_ids];
- $params = [
- 'where' => $where,
- 'select' => ['user_id', 'customer_type', 'created_at'],
- 'order' => 'id desc',
- 'limit' => 10,
- 'with' => ['user' => function($query) {$query->select(['id', 'nickname', 'mobile', 'avatar_url']);}]
- ];
- $list = BusinessCardUser::listPage($params);
- if (!empty($list['list'])) {
- foreach ($list['list'] as &$val) {
- $val['nickname'] = $val['user']['nickname'];
- $val['avatar_url'] = $val['user']['avatar_url'];
- $val['mobile'] = $val['user']['mobile'];
- unset($val['user']);
- }
- }
- $list['customer_type'] = $customer_type;//客户类型
- $list['direct_count'] = $direct_count;//直推数量
- $list['second_count'] = $second_count;//间推数量
- return $this->success('操作成功', $list);
- }
- /**
- * 我的客户展示数据
- * @author hpei
- * @param string $keyword
- * @return array team_count:团队人数 order_num:订单数 order_money:订单金额 deal_count:成交客户数 follow_customer:跟进中 deal_customer:成交 count:粉丝总数
- */
- public function actionCustomerStatistics() {
- if (Yii::$app->request->isGet) {
- //从关系链中取智能卡片的下级用户
- $ids = UserPartitionRelationship::getChildIdArr($this->user_id);
- if (empty($ids)) {
- $team_count = 0;
- } else {
- $team_count = BusinessCardUser::find()->where(['user_id' => $ids, 'mall_id' => $this->mall_id])->count();
- }
- $params = [
- 'where' => [['in', 'user_id', $ids], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]],
- 'group' => 'customer_type',
- 'index' => 'customer_type',
- 'select' => ['count(*) count', 'customer_type']
- ];
- $data = BusinessCardUser::conversion($params);
- $data['team_count'] = $team_count;
- $statistics = BusinessCardStatistics::getList([['user_id' => $this->user_id, 'mall_id' => $this->mall_id]], ['sum(order_num) order_num', 'sum(order_money) order_money'], 'user_id');
- $data['order_num'] = empty($statistics['order_num']) ? 0 : $statistics[0]['order_num'];
- $data['order_money'] = empty($statistics['order_money']) ? 0 : $statistics[0]['order_money'];
- $data['deal_count'] = BusinessCardUser::find()->where(['mall_id' => $this->mall_id, 'parent_id' => $this->user_id, 'customer_type' => CardEnums::CUSTOMER_TYPE_MONEY, 'status' => StatusEnum::ENABLED])->count();
- return $this->success('操作成功', $data);
- }
- }
- /**
- * 修改客户类型
- * @author hpei
- * @param int customer_type
- * @param int $user_id
- * @return mixed
- */
- public function actionSetCustomerType() {
- if (Yii::$app->request->isPost) {
- $post = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($post, [
- [['user_id', 'customer_type'], 'required'],
- [['user_id', 'customer_type'], 'integer'],
- [['customer_type'], 'in', 'range' => array_values(array_keys(CardEnums::getCustomerTypeMap()))]
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $post['mall_id'] = $this->mall_id;
- $post['parent_id'] = $this->user_id;
- try {
- $res = BusinessCardUser::setCustomerData($post);
- if ($res == false) return $this->error(BusinessCardUser::getStaticError());
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('操作成功');
- }
- }
- /**
- * 客户信息
- * @author hpei
- * @param int $user_id
- * @param array $customer_info
- * @return mixed
- */
- public function actionCustomerDetail() {
- if (Yii::$app->request->isGet) {
- $user_id = Yii::$app->request->get('user_id', 0);
- $where = [['=', 'user_id', $user_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
- $select = ['user_id', 'tags', 'customer_nickname', 'customer_sex', 'customer_mobile', 'customer_email', 'customer_address', 'customer_birthday', 'customer_remark'];
- $customer_info = BusinessCardUser::getOne($where, true, ['user' => function($query){$query->select(['id', 'nickname', 'avatar_url']);}], false, $select);
- if (!empty($customer_info)) {
- $customer_info['nickname'] = $customer_info['user']['nickname'];
- $customer_info['avatar_url'] = $customer_info['user']['avatar_url'];
- unset($customer_info['user']);
- $customer_info['tags'] = empty($customer_info['tags']) ? [] : explode(',', $customer_info['tags']);
- }
- return $this->success('操作成功', $customer_info);
- } else {
- $post = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($post, [
- [['user_id'], 'required'],
- [['user_id'], 'integer'],
- [['customer_nickname', 'customer_mobile', 'customer_email', 'customer_address', 'customer_birthday', 'customer_remark'], 'default', 'value' => ''],
- [['customer_sex'], 'default', 'value' => 1]
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $post['mall_id'] = $this->mall_id;
- $post['parent_id'] = $this->user_id;
- try {
- $res = BusinessCardUser::setCustomerData($post);
- if ($res == false) return $this->error(BusinessCardUser::getStaticError());
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('操作成功');
- }
- }
- /**
- * AI分析
- *
- */
- public function actionAiAnalysis() {
- }
- public function actionUserInfo() {
- if (Yii::$app->request->isGet) {
- $data['nickname'] = $this->user->nickname;
- $data['avatar_url'] = $this->user->avatar_url;
- $data['user_id'] = $this->user->id;
- $data['card_id'] = $this->card['id'];
- $position_id = BusinessCardUser::find()->select(['position_id'])->where(['user_id' => $this->user_id])->column();
- if (empty($position_id)) {
- $data['position_name'] = '';
- } else {
- $data['position_name'] = BusinessCardPosition::find()->select(['name'])->where(['id' => $position_id])->column()[0];
- }
- $setting = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic');
- $data['share_desc'] = isset($setting['share_desc']) ? $setting['share_desc'] : '';
- $data['share_title'] = isset($setting['share_title']) ? $setting['share_title'] : '';
- $data['logo'] = isset($setting['logo']) ? $setting['logo'] : '';
- return $this->success('操作成功', $data);
- }
- }
- /**
- * 跟进记录
- * @author hpei
- * @param int $user_id
- * @return array|null
- */
- public function actionFollowRecord() {
- if (Yii::$app->request->isGet) {
- $user_id = Yii::$app->request->get('user_id', 0);
- $params = [
- 'where' => [['=', 'mall_id', $this->mall_id], ['=', 'user_id', $user_id]],
- 'select' => ['remark', 'user_id', 'operate_id', 'created_at'],
- 'order' => 'id desc',
- 'limit' => 10
- ];
- $list = BusinessCardFollowLog::listPage($params);
- if (!empty($list['list'])) {
- $ids = array_unique(array_column($list['list'], 'operate_id'));
- $user_list = User::find()->where(['id' => $ids])->select(['nickname', 'avatar_url', 'id'])->indexBy('id')->all();
- array_walk($list['list'], function(&$val) use($user_list) {
- $val['nickname'] = isset($user_list[$val['operate_id']]) ? $user_list[$val['operate_id']]['nickname'] : '';
- $val['avatar_url'] = isset($user_list[$val['operate_id']]) ? $user_list[$val['operate_id']]['avatar_url'] : '';
- });
- }
- return $this->success('操作成功', $list);
- } else {
- $post = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($post, [
- [['operate_id', 'remark'], 'required'],
- [['operate_id'], 'integer']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if (BusinessCardUser::count(['where' => [['=', 'user_id', $this->user_id], ['=', 'parent_id', $post['operate_id']]]]) <= 0) return $this->error('只能添加下级的记录');
- $data = ['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'operate_id' => $post['operate_id'], 'log_type' => 2, 'remark' => $post['remark']];
- BusinessCardFollowLog::setData($data);
- $this->success('操作成功');
- }
- }
- /**
- * 我的名片统计
- * @author hpei
- * @param int date
- * @return array|null
- */
- public function actionStatistics() {
- if (Yii::$app->request->isGet) {
- $date = Yii::$app->request->get('date', '');
- $ids = UserPartitionRelationship::getChildIdArr($this->user_id);
- $data = BusinessCardStatistics::survey($date, $ids);
- return $this->success('操作成功', $data);
- }
- }
- /**
- * 授权手机号(废弃)
- * @author hpei
- * @param int mobile
- * @param int card_id
- * @return mixed
- */
- public function actionBindPhone() {
- if (Yii::$app->request->isPost) {
- $post = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($post, [
- [['mobile', 'card_id'], 'required'],
- [['mobile', 'card_id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $bind_user = User::getUser([['=', 'mobile', $post['mobile']], ['=', 'mall_id', $this->mall_id]]);//绑定者,当前登录账号
- if (empty($bind_user)) return $this->error('手机号未注册');
- if ($bind_user->id != $this->user_id) return $this->error('该号码不是登录手机号');
- $card = BusinessCard::getOne(['where' => ['=', 'id', $post['card_id']], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]]);
- if (empty($card)) return $this->error('卡片不存在');
- $parent = UserPartitionRelationship::getParentIdArr($bind_user->id);
- if (!in_array($card->user_id, $parent)) return $this->error('您不存在对方关系链当中');
- $is_extis_user = BusinessCardUser::find()->where(['user_id' => $bind_user->id, 'parent_id' => $card->user_id, 'mall_id' => $this->mall_id])->count();
- if ($is_extis_user >= 1) return $this->error('已有绑定的上级');
- if ($card->user_id == $bind_user->id || $this->user_id == $card->user_id) return $this->error('不允许绑定自己');
- $insert_data = [
- 'mall_id' => $this->mall_id,
- 'tags' => $card->tags,
- 'user_id' => $bind_user->id,
- 'parent_id' => $card->user_id,
- 'department_id' => $card->department_id,
- 'position_id' => $card->position_id,
- 'is_clue' => $bind_user->parent_id == $card->user_id ? 1 : 0
- ];
- $res = BusinessCardUser::setData($insert_data);
- if ($res === false) return $this->error(BusinessCardUser::getStaticError());
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- Statistics::handler(['mall_id' => $this->mall_id, 'item_id' => $bind_user->id, 'user_id' => $card->user_id, 'log_type' => CardEnums::LOG_TYPE_USER]);
- return $this->success('操作成功', $res);
- }
- }
- /**
- * 复制手机号
- * @author hpei
- * @param int card_id
- * @return void
- */
- public function actionCopyPhone() {
- if (Yii::$app->request->isGet) {
- $card_id = Yii::$app->request->get('card_id', 0);
- $card = BusinessCard::getOne(['where' => ['=', 'id', $card_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]]);
- if (empty($card)) return $this->error('卡片不存在');
- Statistics::handler(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'item_id' => $card->user_id, 'log_type' => CardEnums::LOG_TYPE_PHONE]);
- return $this->success('操作成功');
- }
- }
- /**
- * 转发名片
- * @author hpei
- * @param int card_id
- * @return void
- */
- public function actionShareCard() {
- if (Yii::$app->request->isGet) {
- $card_id = Yii::$app->request->get('card_id', 0);
- $card = BusinessCard::getOne(['where' => ['=', 'id', $card_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]]);
- if (empty($card)) return $this->error('卡片不存在');
- Statistics::handler(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'item_id' => $card->id, 'log_type' => CardEnums::LOG_TYPE_TURN]);
- return $this->success('操作成功');
- }
- }
- /**
- * 客户信息
- * @return mixed|null
- */
- public function actionInfo()
- {
- $get = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($get, [
- [['user_id'], 'required'],
- [['user_id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $label_ids = UserLabelRelationship::find()
- ->where(['user_id' => $get['user_id']])
- ->andWhere(['mall_id' => $this->mall_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->orderBy('id asc')
- ->select('label_id')
- ->column();
- $ret['labels'] = [];
- if (!empty($label_ids)) {
- $ret['labels'] = UserLabel::find()->where(['id' => $label_ids])->andWhere(['mall_id' => $this->mall_id])
- ->select('name')->column();
- }
- return $this->success('获取成功', $ret);
- }
- /**
- * 客户行为
- * @return mixed|null
- */
- public function actionBehavior()
- {
- $get = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($get, [
- [['user_id'], 'required'],
- [['user_id', 'page', 'limit'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $log_type_enum = array_keys(CardEnums::getLogTypeMap());
- // 角色需要处理一下
- $carduser = BusinessCardUser::getOne([['mall_id' => $this->mall_id,'user_id' => $this->user_id,'status' =>StatusEnum::ENABLED]],true);
- if($carduser['role_type'] == CardEnums::ROLE_TYPE_STAFF || !$carduser['role_type']){ // 员工角色
- $user_ids = BusinessCardUser::find()->select("user_id")
- ->where(['mall_id' => $this->mall_id])->andWhere(['=', 'parent_id', $this->user_id])->asArray()->column();
- $card_data = BusinessCard::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['=', 'user_id', $user_ids])->asArray()->column();
- $card_data[] = $this->card['id'];
- }else if($carduser['role_type'] == CardEnums::ROLE_TYPE_LEADER){ // 主管角色
- $user_ids = BusinessCardUser::find()->select("user_id")
- ->where(['mall_id' => $this->mall_id])->andWhere(['=', 'department_id', $carduser['department_id']])->asArray()->column();
- $card_data = BusinessCard::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['=', 'user_id', $user_ids])->asArray()->column();
- $card_data[] = $this->card['id'];
- }
- $query = BusinessCardCustomerLog::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['user_id' => $get['user_id']])
- ->andWhere(['log_type' => $log_type_enum])
- ->select('id, log_type, created_at, updated_at, user_id, item_id, material_id as object_id, material_times, material_log_id');
- $queryOrder = BusinessCardOrder::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['user_id' => $get['user_id']])
- ->select('id, log_type, created_at, updated_at, user_id, item_id, order_id as object_id, status as material_times, status as material_log_id');
- $queryPoint = BusinessCardBuriedPoint::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['user_id' => $get['user_id']])
- ->select('id, log_type, created_at, updated_at, user_id, item_id, object_id, status as material_times, status as material_log_id');
- if (!empty($card_data)) {
- $card_data = array_unique($card_data);
- $query->andWhere(['item_id' => $card_data]);
- $queryPoint->andWhere(['item_id' => $card_data]);
- }
- $query->union($queryOrder)->union($queryPoint);
- $ret['page_size'] = $get['limit'] ?? 10;
- $ret['page'] = max($get['page'] ?? 1, 1);
- $ret['count'] = $query->count();
- $pages = new Pagination(['totalCount' => $ret['count']]);
- $pages->pageSizeParam = "limit";
- $pages->defaultPageSize = $ret['page_size'];
- $pages->setPageSize($ret['page_size']);
- $ret['pagination'] = $pages;
- $ret['page_count'] = (!empty($ret['count'] ) && $ret['count'] > 0) ? ceil($ret['count'] / $ret['page_size']) : 1;
- $offset = ($ret['page']-1) * $ret['page_size'];
- $sql = $query->createCommand()->getRawSql();
- $ret['list'] = Yii::$app->db->createCommand("$sql ORDER BY updated_at DESC LIMIT $offset, {$ret['page_size']}")->queryAll();
- // 获取当前用户手机信息
- $ret['user'] = User::getOne([
- ['id' => $get['user_id']],
- ['mall_id' => $this->mall_id]
- ], true, [], null, 'nickname, username, mobile, avatar_url');
- // if (!empty($ret['list'])) {
- // $get_date = [];
- // $get_date_key = [];
- // foreach ($ret['list'] as $k => $v) {
- // $ret['list'][$k]['date'] = date('Y年m月d日',$v['created_at']);
- // $ret['list'][$k]['time'] = date('H:i',$v['created_at']);
- // $ret['list'][$k]['remark'] = CardEnums::getLogTypeMapV1($v['log_type']);
- // $get_date[$ret['list'][$k]['date']][] = $ret['list'][$k];
- // }
- // if(!empty($get_date)){
- // foreach($get_date as $key=>$value){
- // $get_date_key[] = $value;
- // }
- // }
- // $ret['list'] = $get_date_key;
- // }
- if (!empty($ret['list'])) {
- $user_ids = array_column($ret['list'], 'user_id');
- $user_list = User::find()->where(['id' => $user_ids])->select(['nickname', 'avatar_url','mobile', 'id'])->indexBy('id')->asArray()->all();
- $material_times_arr = [CardEnums::LOG_TYPE_CARD];
- $get_date = [];
- $get_date_key = [];
- foreach ($ret['list'] as &$val) {
- if (!empty($user_list[$val['user_id']])) {
- $val['nickname'] = $user_list[$val['user_id']]['nickname'];
- $val['avatar_url'] = $user_list[$val['user_id']]['avatar_url'];
- $val['mobile'] = $user_list[$val['user_id']]['mobile'];
- }
- $val['date'] = date('Y年m月d日',$val['created_at']);
- $val['time'] = date('H:i',$val['created_at']);
- if (!empty($val['object_id'])) {
- switch ($val['log_type']) {
- case CardEnums::LOG_TYPE_BURIED_POINT_GOODS_DETAIL:
- // 查看商品
- $val['remark'] = '<span>查看</span>了<span>商品</span>[<span>' .
- Goods::find()->where(['id' => $val['object_id']])->select('goods_name')->scalar() . '</span>],请抓住机会吧';
- break;
- case CardEnums::LOG_TYPE_ORDER_TYPE:
- // 下单
- // 获取商品名称
- $goods_names = OrderDetail::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['order_id' => $val['object_id']])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->select('goods_name')
- ->column();
- $goods_name = !empty($goods_names[0]) ? "[$goods_names[0]]" : '';
- $goods_cnt = count($goods_names);
- if ($goods_cnt > 1) {
- $goods_name = "$goods_name 等, 共 [$goods_cnt] 款";
- }
- $val['remark'] = "<span>购买</span>了商品<span>$goods_name</span>,请抓住机会吧";
- break;
- default:
- // 查看素材
- $material_name = BusinessCardMaterials::getOne([['=','id',$val['object_id']]],true,'','','title');
- $material_view_time = BusinessCardMaterialsBrowseLog::getOne([['=','id',$val['material_log_id']]],true,'','','view_seconds');
- $times = $val['material_times']==1?'首次':'第'.$val['material_times'].'次';
- $val['remark'] = "<span>$times</span>查看了<span>素材《{$material_name['title']}》</span>,本次TA观看了".BusinessCardCustomerLog::sec2Time($material_view_time['view_seconds']);
- // $val['remark'] = $times.'查看素材《'.$material_name['title'].'》,本次TA观看了'.BusinessCardCustomerLog::sec2Time($material_view_time['view_seconds']);
- }
- } else {
- // 判断是否为本人/非本人那么就需要获取名片名称
- if ($this->card['id'] != $val['item_id']) {
- $nickname = BusinessCard::find()->where(['id' => $val['item_id']])->select('nickname')->scalar();
- }
- $val['remark'] = CardEnums::getLogTypeMapV2($val['log_type'],
- in_array($val['log_type'], $material_times_arr) ? $val['material_times'] : null, !empty($nickname) ? "[$nickname]" : "您");
- }
- $val['remark'] = str_replace('<span>', '<span style="color: #db0505">', $val['remark']);
- $val['user'] = $ret['user'];
- $get_date[$val['date']][] = $val;
- }
- if($get_date){
- foreach($get_date as $value){
- $get_date_key[] = $value;
- }
- $ret['list'] = $get_date_key;
- }
- }
- return $this->success('获取成功', $ret);
- }
- /**
- * 客户分析
- * @return mixed|null
- */
- public function actionAnalyze()
- {
- $get = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($get, [
- [['user_id'], 'required'],
- [['range'], 'in', 'range' => [0, 1, 7, 30]],
- [['user_id', 'range'], 'integer'], // 0, 1, 7, 30
- ], [
- 'user_id' => '客户',
- 'range' => '区间'
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $user_id = $get['user_id'];
- $range = intval($get['range']);
- $log = BusinessCardCustomerLog::getOne([
- ['mall_id' => $this->mall_id],
- ['item_id' => $this->card['id']],
- ['user_id' => $user_id],
- ], true, [], null, 'id, created_at');
- if (empty($log)) return $this->error('当前用户并未查看过你的名片');
- $created_at = 0;
- switch ($range) {
- case 1:
- $created_at = strtotime(date('Y-m-d'));
- break;
- case 7:
- $created_at = time() - (7 * 86400);
- break;
- case 30:
- $created_at = time() - (30 * 86400);
- break;
- }
- // 查看名片
- // 查看资料
- // 查看商品
- // 分享名片
- $ret['browse_card'] = BusinessCardCustomerLog::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['>=', 'created_at', $created_at])
- ->andWhere(['log_type' => CardEnums::LOG_TYPE_CARD])
- ->count() ?? 0;
- $ret['browse_material'] = BusinessCardCustomerLog::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['>=', 'created_at', $created_at])
- ->andWhere(['log_type' => CardEnums::LOG_TYPE_MATEREAL_DETAIL])
- ->count() ?? 0;
- $ret['browse_goods'] = BusinessCardBuriedPoint::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['>=', 'created_at', $created_at])
- ->andWhere(['log_type' => CardEnums::LOG_TYPE_BURIED_POINT_GOODS_DETAIL])
- ->count() ?? 0;
- $ret['share_card'] = BusinessCardCustomerLog::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['>=', 'created_at', $created_at])
- ->andWhere(['log_type' => CardEnums::LOG_TYPE_TURN])
- ->count() ?? 0;
- /*
- // 下单数量
- $ret['order_cnt'] = BusinessCardOrder::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['>=', 'created_at', $created_at])
- ->count() ?? 0;
- // 支付金额
- $ret['pay_money'] = BusinessCardOrder::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['>=', 'created_at', $created_at])
- ->sum('pay_amount') ?? 0;
- // 浏览人数
- $ret['browse_people_num'] = BusinessCardCustomerLog::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['log_type' => CardEnums::LOG_TYPE_CARD])
- ->groupBy('item_id')
- ->count() ?? 0;
- // 访问次数 进入首页 + 商品详情
- $ret['browse_num'] = BusinessCardBuriedPoint::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['>=', 'created_at', $created_at])
- // ->andWhere(['log_type' => CardEnums::LOG_TYPE_BURIED_POINT_HOME])
- ->count() ?? 0;
- $ret['browse_num'] += BusinessCardCustomerLog::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['>=', 'created_at', $created_at])
- ->andWhere(['log_type' => [CardEnums::LOG_TYPE_INDEX, CardEnums::LOG_TYPE_GOODS, CardEnums::LOG_TYPE_IMAGE,
- CardEnums::LOG_TYPE_GOODS, CardEnums::LOG_TYPE_CARD, CardEnums::LOG_TYPE_VIDEO, CardEnums::LOG_TYPE_DYNAMIC,
- CardEnums::LOG_TYPE_MATEREAL, CardEnums::LOG_TYPE_MATEREAL_DETAIL]])
- ->count() ?? 0;
- */
- // 分数
- $ret['analyze'] = [
- 'score' => BusinessCardSecretUserScore::getScoreByUserId($this->mall_id, $user_id),
- ];
- return $this->success('获取成功', $ret);
- }
- }
|