CustomerController.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <?php
  2. namespace addons\BusinessCard\api\modules\v1\controllers;
  3. use addons\BusinessCard\common\enums\CardEnums;
  4. use addons\BusinessCard\common\logic\Statistics;
  5. use addons\BusinessCard\common\models\BusinessCard;
  6. use addons\BusinessCard\common\models\BusinessCardBuriedPoint;
  7. use addons\BusinessCard\common\models\BusinessCardCustomerLog;
  8. use addons\BusinessCard\common\models\BusinessCardFollowLog;
  9. use addons\BusinessCard\common\models\BusinessCardMaterials;
  10. use addons\BusinessCard\common\models\BusinessCardMaterialsBrowseLog;
  11. use addons\BusinessCard\common\models\BusinessCardOrder;
  12. use addons\BusinessCard\common\models\BusinessCardPosition;
  13. use addons\BusinessCard\common\models\BusinessCardSecretUserScore;
  14. use addons\BusinessCard\common\models\BusinessCardStatistics;
  15. use addons\BusinessCard\common\models\BusinessCardUser;
  16. use common\models\common\BuyingPointDay;
  17. use common\models\goods\Goods;
  18. use common\models\order\Order;
  19. use common\models\order\OrderDetail;
  20. use common\models\user\User;
  21. use api\controllers\OnAuthController;
  22. use common\enums\StatusEnum;
  23. use common\models\user\UserLabel;
  24. use common\models\user\UserLabelRelationship;
  25. use common\models\user\UserPartitionRelationship;
  26. use Yii;
  27. use yii\data\Pagination;
  28. class CustomerController extends OnAuthController
  29. {
  30. public $modelClass = '';
  31. public $card = '';
  32. /**
  33. * 提示创建名片
  34. */
  35. public function beforeAction($action) {
  36. parent::beforeAction($action);
  37. $card = BusinessCard::getOne([['=', 'user_id', $this->user_id], ['=', 'status', StatusEnum::ENABLED]], true);
  38. if (empty($card)) {
  39. Yii::$app->controller->asJson(['code' => 3, 'msg' => '请先创建名片'])->send();
  40. return;
  41. }
  42. $this->card = $card;
  43. return $action;
  44. }
  45. /**
  46. * 客户列表&我的线索&新增线索
  47. * @author hpei
  48. * @param int $customer_type 0:全部 1:潜在客户 2:意向客户 3:比较客户 4:待成交客户 5:已成交客户
  49. * @param int $deep 1:直推 2:间推
  50. * @param int $date 新增线索传15,其他不用传
  51. * @param int user_id 查询团队列表传
  52. * @param string $keyword
  53. * @return array
  54. */
  55. public function actionCustomerList() {
  56. $get = Yii::$app->request->get();
  57. $where[] = ['=' ,'mall_id', $this->mall_id];
  58. $where[] = ['=' , 'status', StatusEnum::ENABLED];
  59. if (isset($get['customer_type']) && !empty($get['customer_type'])) $where[] = ['=', 'customer_type', $get['customer_type']];
  60. $ids = [];
  61. $customer_type = CardEnums::getCustomerTypeMap();
  62. if (isset($get['date']) && !empty($get['date'])) {
  63. $date = $get['date'];
  64. $start_time = strtotime("-$date day 0:0:0");
  65. $end_time = $start_time + ($date - 1) * 86400 + 86399;
  66. $where[] = ['>', 'created_at', $start_time];
  67. $where[] = ['<=', 'created_at', $end_time];
  68. }
  69. if (isset($get['keyword']) && !empty($get['keyword'])) {
  70. $userModel = User::find();
  71. $userParam['where'] = [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED], ['like', 'nickname', $get['keyword']]];
  72. User::paramPack($userParam, $userModel);
  73. $ids = $userModel->select(['id'])->column();
  74. if (empty($ids)) $this->success('操作成功', ['list' => [], 'direct' => 0, 'second' => 0, 'customer_list' => $customer_type]);
  75. }
  76. $deep = $get['deep'] ? $get['deep'] : 1;
  77. if ($deep > 2) return $this->error('层级异常');
  78. $uid = isset($get['user_id']) ? $get['user_id'] : $this->user_id;
  79. $direct_ids = UserPartitionRelationship::getChildIdArr($uid, 1);
  80. $second_ids = UserPartitionRelationship::getChildIdArr($uid, 2);
  81. $direct_ids = array_merge($ids, $direct_ids);
  82. $second_ids = array_merge($ids, $second_ids);
  83. $direct_count = BusinessCardUser::count(['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED], ['in', 'user_id', $direct_ids]]]);
  84. $second_count = BusinessCardUser::count(['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED], ['in', 'user_id', $second_ids]]]);
  85. $where[] = $deep == 1 ? ['in', 'user_id', $direct_ids] : ['in', 'user_id', $second_ids];
  86. $params = [
  87. 'where' => $where,
  88. 'select' => ['user_id', 'customer_type', 'created_at'],
  89. 'order' => 'id desc',
  90. 'limit' => 10,
  91. 'with' => ['user' => function($query) {$query->select(['id', 'nickname', 'mobile', 'avatar_url']);}]
  92. ];
  93. $list = BusinessCardUser::listPage($params);
  94. if (!empty($list['list'])) {
  95. foreach ($list['list'] as &$val) {
  96. $val['nickname'] = $val['user']['nickname'];
  97. $val['avatar_url'] = $val['user']['avatar_url'];
  98. $val['mobile'] = $val['user']['mobile'];
  99. unset($val['user']);
  100. }
  101. }
  102. $list['customer_type'] = $customer_type;//客户类型
  103. $list['direct_count'] = $direct_count;//直推数量
  104. $list['second_count'] = $second_count;//间推数量
  105. return $this->success('操作成功', $list);
  106. }
  107. /**
  108. * 我的客户展示数据
  109. * @author hpei
  110. * @param string $keyword
  111. * @return array team_count:团队人数 order_num:订单数 order_money:订单金额 deal_count:成交客户数 follow_customer:跟进中 deal_customer:成交 count:粉丝总数
  112. */
  113. public function actionCustomerStatistics() {
  114. if (Yii::$app->request->isGet) {
  115. //从关系链中取智能卡片的下级用户
  116. $ids = UserPartitionRelationship::getChildIdArr($this->user_id);
  117. if (empty($ids)) {
  118. $team_count = 0;
  119. } else {
  120. $team_count = BusinessCardUser::find()->where(['user_id' => $ids, 'mall_id' => $this->mall_id])->count();
  121. }
  122. $params = [
  123. 'where' => [['in', 'user_id', $ids], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]],
  124. 'group' => 'customer_type',
  125. 'index' => 'customer_type',
  126. 'select' => ['count(*) count', 'customer_type']
  127. ];
  128. $data = BusinessCardUser::conversion($params);
  129. $data['team_count'] = $team_count;
  130. $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');
  131. $data['order_num'] = empty($statistics['order_num']) ? 0 : $statistics[0]['order_num'];
  132. $data['order_money'] = empty($statistics['order_money']) ? 0 : $statistics[0]['order_money'];
  133. $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();
  134. return $this->success('操作成功', $data);
  135. }
  136. }
  137. /**
  138. * 修改客户类型
  139. * @author hpei
  140. * @param int customer_type
  141. * @param int $user_id
  142. * @return mixed
  143. */
  144. public function actionSetCustomerType() {
  145. if (Yii::$app->request->isPost) {
  146. $post = Yii::$app->request->post();
  147. $res = Yii::$app->services->validate->validate($post, [
  148. [['user_id', 'customer_type'], 'required'],
  149. [['user_id', 'customer_type'], 'integer'],
  150. [['customer_type'], 'in', 'range' => array_values(array_keys(CardEnums::getCustomerTypeMap()))]
  151. ]);
  152. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  153. $post['mall_id'] = $this->mall_id;
  154. $post['parent_id'] = $this->user_id;
  155. try {
  156. $res = BusinessCardUser::setCustomerData($post);
  157. if ($res == false) return $this->error(BusinessCardUser::getStaticError());
  158. } catch (\Exception $e) {
  159. return $this->error($e->getMessage());
  160. }
  161. return $this->success('操作成功');
  162. }
  163. }
  164. /**
  165. * 客户信息
  166. * @author hpei
  167. * @param int $user_id
  168. * @param array $customer_info
  169. * @return mixed
  170. */
  171. public function actionCustomerDetail() {
  172. if (Yii::$app->request->isGet) {
  173. $user_id = Yii::$app->request->get('user_id', 0);
  174. $where = [['=', 'user_id', $user_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
  175. $select = ['user_id', 'tags', 'customer_nickname', 'customer_sex', 'customer_mobile', 'customer_email', 'customer_address', 'customer_birthday', 'customer_remark'];
  176. $customer_info = BusinessCardUser::getOne($where, true, ['user' => function($query){$query->select(['id', 'nickname', 'avatar_url']);}], false, $select);
  177. if (!empty($customer_info)) {
  178. $customer_info['nickname'] = $customer_info['user']['nickname'];
  179. $customer_info['avatar_url'] = $customer_info['user']['avatar_url'];
  180. unset($customer_info['user']);
  181. $customer_info['tags'] = empty($customer_info['tags']) ? [] : explode(',', $customer_info['tags']);
  182. }
  183. return $this->success('操作成功', $customer_info);
  184. } else {
  185. $post = Yii::$app->request->post();
  186. $res = Yii::$app->services->validate->validate($post, [
  187. [['user_id'], 'required'],
  188. [['user_id'], 'integer'],
  189. [['customer_nickname', 'customer_mobile', 'customer_email', 'customer_address', 'customer_birthday', 'customer_remark'], 'default', 'value' => ''],
  190. [['customer_sex'], 'default', 'value' => 1]
  191. ]);
  192. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  193. $post['mall_id'] = $this->mall_id;
  194. $post['parent_id'] = $this->user_id;
  195. try {
  196. $res = BusinessCardUser::setCustomerData($post);
  197. if ($res == false) return $this->error(BusinessCardUser::getStaticError());
  198. } catch (\Exception $e) {
  199. return $this->error($e->getMessage());
  200. }
  201. return $this->success('操作成功');
  202. }
  203. }
  204. /**
  205. * AI分析
  206. *
  207. */
  208. public function actionAiAnalysis() {
  209. }
  210. public function actionUserInfo() {
  211. if (Yii::$app->request->isGet) {
  212. $data['nickname'] = $this->user->nickname;
  213. $data['avatar_url'] = $this->user->avatar_url;
  214. $data['user_id'] = $this->user->id;
  215. $data['card_id'] = $this->card['id'];
  216. $position_id = BusinessCardUser::find()->select(['position_id'])->where(['user_id' => $this->user_id])->column();
  217. if (empty($position_id)) {
  218. $data['position_name'] = '';
  219. } else {
  220. $data['position_name'] = BusinessCardPosition::find()->select(['name'])->where(['id' => $position_id])->column()[0];
  221. }
  222. $setting = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic');
  223. $data['share_desc'] = isset($setting['share_desc']) ? $setting['share_desc'] : '';
  224. $data['share_title'] = isset($setting['share_title']) ? $setting['share_title'] : '';
  225. $data['logo'] = isset($setting['logo']) ? $setting['logo'] : '';
  226. return $this->success('操作成功', $data);
  227. }
  228. }
  229. /**
  230. * 跟进记录
  231. * @author hpei
  232. * @param int $user_id
  233. * @return array|null
  234. */
  235. public function actionFollowRecord() {
  236. if (Yii::$app->request->isGet) {
  237. $user_id = Yii::$app->request->get('user_id', 0);
  238. $params = [
  239. 'where' => [['=', 'mall_id', $this->mall_id], ['=', 'user_id', $user_id]],
  240. 'select' => ['remark', 'user_id', 'operate_id', 'created_at'],
  241. 'order' => 'id desc',
  242. 'limit' => 10
  243. ];
  244. $list = BusinessCardFollowLog::listPage($params);
  245. if (!empty($list['list'])) {
  246. $ids = array_unique(array_column($list['list'], 'operate_id'));
  247. $user_list = User::find()->where(['id' => $ids])->select(['nickname', 'avatar_url', 'id'])->indexBy('id')->all();
  248. array_walk($list['list'], function(&$val) use($user_list) {
  249. $val['nickname'] = isset($user_list[$val['operate_id']]) ? $user_list[$val['operate_id']]['nickname'] : '';
  250. $val['avatar_url'] = isset($user_list[$val['operate_id']]) ? $user_list[$val['operate_id']]['avatar_url'] : '';
  251. });
  252. }
  253. return $this->success('操作成功', $list);
  254. } else {
  255. $post = Yii::$app->request->post();
  256. $res = Yii::$app->services->validate->validate($post, [
  257. [['operate_id', 'remark'], 'required'],
  258. [['operate_id'], 'integer']
  259. ]);
  260. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  261. if (BusinessCardUser::count(['where' => [['=', 'user_id', $this->user_id], ['=', 'parent_id', $post['operate_id']]]]) <= 0) return $this->error('只能添加下级的记录');
  262. $data = ['user_id' => $this->user_id, 'mall_id' => $this->mall_id, 'operate_id' => $post['operate_id'], 'log_type' => 2, 'remark' => $post['remark']];
  263. BusinessCardFollowLog::setData($data);
  264. $this->success('操作成功');
  265. }
  266. }
  267. /**
  268. * 我的名片统计
  269. * @author hpei
  270. * @param int date
  271. * @return array|null
  272. */
  273. public function actionStatistics() {
  274. if (Yii::$app->request->isGet) {
  275. $date = Yii::$app->request->get('date', '');
  276. $ids = UserPartitionRelationship::getChildIdArr($this->user_id);
  277. $data = BusinessCardStatistics::survey($date, $ids);
  278. return $this->success('操作成功', $data);
  279. }
  280. }
  281. /**
  282. * 授权手机号(废弃)
  283. * @author hpei
  284. * @param int mobile
  285. * @param int card_id
  286. * @return mixed
  287. */
  288. public function actionBindPhone() {
  289. if (Yii::$app->request->isPost) {
  290. $post = Yii::$app->request->post();
  291. $res = Yii::$app->services->validate->validate($post, [
  292. [['mobile', 'card_id'], 'required'],
  293. [['mobile', 'card_id'], 'integer'],
  294. ]);
  295. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  296. try {
  297. $bind_user = User::getUser([['=', 'mobile', $post['mobile']], ['=', 'mall_id', $this->mall_id]]);//绑定者,当前登录账号
  298. if (empty($bind_user)) return $this->error('手机号未注册');
  299. if ($bind_user->id != $this->user_id) return $this->error('该号码不是登录手机号');
  300. $card = BusinessCard::getOne(['where' => ['=', 'id', $post['card_id']], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]]);
  301. if (empty($card)) return $this->error('卡片不存在');
  302. $parent = UserPartitionRelationship::getParentIdArr($bind_user->id);
  303. if (!in_array($card->user_id, $parent)) return $this->error('您不存在对方关系链当中');
  304. $is_extis_user = BusinessCardUser::find()->where(['user_id' => $bind_user->id, 'parent_id' => $card->user_id, 'mall_id' => $this->mall_id])->count();
  305. if ($is_extis_user >= 1) return $this->error('已有绑定的上级');
  306. if ($card->user_id == $bind_user->id || $this->user_id == $card->user_id) return $this->error('不允许绑定自己');
  307. $insert_data = [
  308. 'mall_id' => $this->mall_id,
  309. 'tags' => $card->tags,
  310. 'user_id' => $bind_user->id,
  311. 'parent_id' => $card->user_id,
  312. 'department_id' => $card->department_id,
  313. 'position_id' => $card->position_id,
  314. 'is_clue' => $bind_user->parent_id == $card->user_id ? 1 : 0
  315. ];
  316. $res = BusinessCardUser::setData($insert_data);
  317. if ($res === false) return $this->error(BusinessCardUser::getStaticError());
  318. } catch (\Exception $e) {
  319. return $this->error($e->getMessage());
  320. }
  321. Statistics::handler(['mall_id' => $this->mall_id, 'item_id' => $bind_user->id, 'user_id' => $card->user_id, 'log_type' => CardEnums::LOG_TYPE_USER]);
  322. return $this->success('操作成功', $res);
  323. }
  324. }
  325. /**
  326. * 复制手机号
  327. * @author hpei
  328. * @param int card_id
  329. * @return void
  330. */
  331. public function actionCopyPhone() {
  332. if (Yii::$app->request->isGet) {
  333. $card_id = Yii::$app->request->get('card_id', 0);
  334. $card = BusinessCard::getOne(['where' => ['=', 'id', $card_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]]);
  335. if (empty($card)) return $this->error('卡片不存在');
  336. Statistics::handler(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'item_id' => $card->user_id, 'log_type' => CardEnums::LOG_TYPE_PHONE]);
  337. return $this->success('操作成功');
  338. }
  339. }
  340. /**
  341. * 转发名片
  342. * @author hpei
  343. * @param int card_id
  344. * @return void
  345. */
  346. public function actionShareCard() {
  347. if (Yii::$app->request->isGet) {
  348. $card_id = Yii::$app->request->get('card_id', 0);
  349. $card = BusinessCard::getOne(['where' => ['=', 'id', $card_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]]);
  350. if (empty($card)) return $this->error('卡片不存在');
  351. Statistics::handler(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'item_id' => $card->id, 'log_type' => CardEnums::LOG_TYPE_TURN]);
  352. return $this->success('操作成功');
  353. }
  354. }
  355. /**
  356. * 客户信息
  357. * @return mixed|null
  358. */
  359. public function actionInfo()
  360. {
  361. $get = Yii::$app->request->get();
  362. $res = Yii::$app->services->validate->validate($get, [
  363. [['user_id'], 'required'],
  364. [['user_id'], 'integer'],
  365. ]);
  366. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  367. $label_ids = UserLabelRelationship::find()
  368. ->where(['user_id' => $get['user_id']])
  369. ->andWhere(['mall_id' => $this->mall_id])
  370. ->andWhere(['status' => StatusEnum::ENABLED])
  371. ->orderBy('id asc')
  372. ->select('label_id')
  373. ->column();
  374. $ret['labels'] = [];
  375. if (!empty($label_ids)) {
  376. $ret['labels'] = UserLabel::find()->where(['id' => $label_ids])->andWhere(['mall_id' => $this->mall_id])
  377. ->select('name')->column();
  378. }
  379. return $this->success('获取成功', $ret);
  380. }
  381. /**
  382. * 客户行为
  383. * @return mixed|null
  384. */
  385. public function actionBehavior()
  386. {
  387. $get = Yii::$app->request->get();
  388. $res = Yii::$app->services->validate->validate($get, [
  389. [['user_id'], 'required'],
  390. [['user_id', 'page', 'limit'], 'integer'],
  391. ]);
  392. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  393. $log_type_enum = array_keys(CardEnums::getLogTypeMap());
  394. // 角色需要处理一下
  395. $carduser = BusinessCardUser::getOne([['mall_id' => $this->mall_id,'user_id' => $this->user_id,'status' =>StatusEnum::ENABLED]],true);
  396. if($carduser['role_type'] == CardEnums::ROLE_TYPE_STAFF || !$carduser['role_type']){ // 员工角色
  397. $user_ids = BusinessCardUser::find()->select("user_id")
  398. ->where(['mall_id' => $this->mall_id])->andWhere(['=', 'parent_id', $this->user_id])->asArray()->column();
  399. $card_data = BusinessCard::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['=', 'user_id', $user_ids])->asArray()->column();
  400. $card_data[] = $this->card['id'];
  401. }else if($carduser['role_type'] == CardEnums::ROLE_TYPE_LEADER){ // 主管角色
  402. $user_ids = BusinessCardUser::find()->select("user_id")
  403. ->where(['mall_id' => $this->mall_id])->andWhere(['=', 'department_id', $carduser['department_id']])->asArray()->column();
  404. $card_data = BusinessCard::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['=', 'user_id', $user_ids])->asArray()->column();
  405. $card_data[] = $this->card['id'];
  406. }
  407. $query = BusinessCardCustomerLog::find()
  408. ->where(['mall_id' => $this->mall_id])
  409. ->andWhere(['user_id' => $get['user_id']])
  410. ->andWhere(['log_type' => $log_type_enum])
  411. ->select('id, log_type, created_at, updated_at, user_id, item_id, material_id as object_id, material_times, material_log_id');
  412. $queryOrder = BusinessCardOrder::find()
  413. ->where(['mall_id' => $this->mall_id])
  414. ->andWhere(['user_id' => $get['user_id']])
  415. ->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');
  416. $queryPoint = BusinessCardBuriedPoint::find()
  417. ->where(['mall_id' => $this->mall_id])
  418. ->andWhere(['user_id' => $get['user_id']])
  419. ->select('id, log_type, created_at, updated_at, user_id, item_id, object_id, status as material_times, status as material_log_id');
  420. if (!empty($card_data)) {
  421. $card_data = array_unique($card_data);
  422. $query->andWhere(['item_id' => $card_data]);
  423. $queryPoint->andWhere(['item_id' => $card_data]);
  424. }
  425. $query->union($queryOrder)->union($queryPoint);
  426. $ret['page_size'] = $get['limit'] ?? 10;
  427. $ret['page'] = max($get['page'] ?? 1, 1);
  428. $ret['count'] = $query->count();
  429. $pages = new Pagination(['totalCount' => $ret['count']]);
  430. $pages->pageSizeParam = "limit";
  431. $pages->defaultPageSize = $ret['page_size'];
  432. $pages->setPageSize($ret['page_size']);
  433. $ret['pagination'] = $pages;
  434. $ret['page_count'] = (!empty($ret['count'] ) && $ret['count'] > 0) ? ceil($ret['count'] / $ret['page_size']) : 1;
  435. $offset = ($ret['page']-1) * $ret['page_size'];
  436. $sql = $query->createCommand()->getRawSql();
  437. $ret['list'] = Yii::$app->db->createCommand("$sql ORDER BY updated_at DESC LIMIT $offset, {$ret['page_size']}")->queryAll();
  438. // 获取当前用户手机信息
  439. $ret['user'] = User::getOne([
  440. ['id' => $get['user_id']],
  441. ['mall_id' => $this->mall_id]
  442. ], true, [], null, 'nickname, username, mobile, avatar_url');
  443. // if (!empty($ret['list'])) {
  444. // $get_date = [];
  445. // $get_date_key = [];
  446. // foreach ($ret['list'] as $k => $v) {
  447. // $ret['list'][$k]['date'] = date('Y年m月d日',$v['created_at']);
  448. // $ret['list'][$k]['time'] = date('H:i',$v['created_at']);
  449. // $ret['list'][$k]['remark'] = CardEnums::getLogTypeMapV1($v['log_type']);
  450. // $get_date[$ret['list'][$k]['date']][] = $ret['list'][$k];
  451. // }
  452. // if(!empty($get_date)){
  453. // foreach($get_date as $key=>$value){
  454. // $get_date_key[] = $value;
  455. // }
  456. // }
  457. // $ret['list'] = $get_date_key;
  458. // }
  459. if (!empty($ret['list'])) {
  460. $user_ids = array_column($ret['list'], 'user_id');
  461. $user_list = User::find()->where(['id' => $user_ids])->select(['nickname', 'avatar_url','mobile', 'id'])->indexBy('id')->asArray()->all();
  462. $material_times_arr = [CardEnums::LOG_TYPE_CARD];
  463. $get_date = [];
  464. $get_date_key = [];
  465. foreach ($ret['list'] as &$val) {
  466. if (!empty($user_list[$val['user_id']])) {
  467. $val['nickname'] = $user_list[$val['user_id']]['nickname'];
  468. $val['avatar_url'] = $user_list[$val['user_id']]['avatar_url'];
  469. $val['mobile'] = $user_list[$val['user_id']]['mobile'];
  470. }
  471. $val['date'] = date('Y年m月d日',$val['created_at']);
  472. $val['time'] = date('H:i',$val['created_at']);
  473. if (!empty($val['object_id'])) {
  474. switch ($val['log_type']) {
  475. case CardEnums::LOG_TYPE_BURIED_POINT_GOODS_DETAIL:
  476. // 查看商品
  477. $val['remark'] = '<span>查看</span>了<span>商品</span>[<span>' .
  478. Goods::find()->where(['id' => $val['object_id']])->select('goods_name')->scalar() . '</span>],请抓住机会吧';
  479. break;
  480. case CardEnums::LOG_TYPE_ORDER_TYPE:
  481. // 下单
  482. // 获取商品名称
  483. $goods_names = OrderDetail::find()
  484. ->where(['mall_id' => $this->mall_id])
  485. ->andWhere(['order_id' => $val['object_id']])
  486. ->andWhere(['status' => StatusEnum::ENABLED])
  487. ->select('goods_name')
  488. ->column();
  489. $goods_name = !empty($goods_names[0]) ? "[$goods_names[0]]" : '';
  490. $goods_cnt = count($goods_names);
  491. if ($goods_cnt > 1) {
  492. $goods_name = "$goods_name 等, 共 [$goods_cnt] 款";
  493. }
  494. $val['remark'] = "<span>购买</span>了商品<span>$goods_name</span>,请抓住机会吧";
  495. break;
  496. default:
  497. // 查看素材
  498. $material_name = BusinessCardMaterials::getOne([['=','id',$val['object_id']]],true,'','','title');
  499. $material_view_time = BusinessCardMaterialsBrowseLog::getOne([['=','id',$val['material_log_id']]],true,'','','view_seconds');
  500. $times = $val['material_times']==1?'首次':'第'.$val['material_times'].'次';
  501. $val['remark'] = "<span>$times</span>查看了<span>素材《{$material_name['title']}》</span>,本次TA观看了".BusinessCardCustomerLog::sec2Time($material_view_time['view_seconds']);
  502. // $val['remark'] = $times.'查看素材《'.$material_name['title'].'》,本次TA观看了'.BusinessCardCustomerLog::sec2Time($material_view_time['view_seconds']);
  503. }
  504. } else {
  505. // 判断是否为本人/非本人那么就需要获取名片名称
  506. if ($this->card['id'] != $val['item_id']) {
  507. $nickname = BusinessCard::find()->where(['id' => $val['item_id']])->select('nickname')->scalar();
  508. }
  509. $val['remark'] = CardEnums::getLogTypeMapV2($val['log_type'],
  510. in_array($val['log_type'], $material_times_arr) ? $val['material_times'] : null, !empty($nickname) ? "[$nickname]" : "您");
  511. }
  512. $val['remark'] = str_replace('<span>', '<span style="color: #db0505">', $val['remark']);
  513. $val['user'] = $ret['user'];
  514. $get_date[$val['date']][] = $val;
  515. }
  516. if($get_date){
  517. foreach($get_date as $value){
  518. $get_date_key[] = $value;
  519. }
  520. $ret['list'] = $get_date_key;
  521. }
  522. }
  523. return $this->success('获取成功', $ret);
  524. }
  525. /**
  526. * 客户分析
  527. * @return mixed|null
  528. */
  529. public function actionAnalyze()
  530. {
  531. $get = Yii::$app->request->get();
  532. $res = Yii::$app->services->validate->validate($get, [
  533. [['user_id'], 'required'],
  534. [['range'], 'in', 'range' => [0, 1, 7, 30]],
  535. [['user_id', 'range'], 'integer'], // 0, 1, 7, 30
  536. ], [
  537. 'user_id' => '客户',
  538. 'range' => '区间'
  539. ]);
  540. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  541. $user_id = $get['user_id'];
  542. $range = intval($get['range']);
  543. $log = BusinessCardCustomerLog::getOne([
  544. ['mall_id' => $this->mall_id],
  545. ['item_id' => $this->card['id']],
  546. ['user_id' => $user_id],
  547. ], true, [], null, 'id, created_at');
  548. if (empty($log)) return $this->error('当前用户并未查看过你的名片');
  549. $created_at = 0;
  550. switch ($range) {
  551. case 1:
  552. $created_at = strtotime(date('Y-m-d'));
  553. break;
  554. case 7:
  555. $created_at = time() - (7 * 86400);
  556. break;
  557. case 30:
  558. $created_at = time() - (30 * 86400);
  559. break;
  560. }
  561. // 查看名片
  562. // 查看资料
  563. // 查看商品
  564. // 分享名片
  565. $ret['browse_card'] = BusinessCardCustomerLog::find()
  566. ->where(['mall_id' => $this->mall_id])
  567. ->andWhere(['user_id' => $user_id])
  568. ->andWhere(['>=', 'created_at', $created_at])
  569. ->andWhere(['log_type' => CardEnums::LOG_TYPE_CARD])
  570. ->count() ?? 0;
  571. $ret['browse_material'] = BusinessCardCustomerLog::find()
  572. ->where(['mall_id' => $this->mall_id])
  573. ->andWhere(['user_id' => $user_id])
  574. ->andWhere(['>=', 'created_at', $created_at])
  575. ->andWhere(['log_type' => CardEnums::LOG_TYPE_MATEREAL_DETAIL])
  576. ->count() ?? 0;
  577. $ret['browse_goods'] = BusinessCardBuriedPoint::find()
  578. ->where(['mall_id' => $this->mall_id])
  579. ->andWhere(['user_id' => $user_id])
  580. ->andWhere(['>=', 'created_at', $created_at])
  581. ->andWhere(['log_type' => CardEnums::LOG_TYPE_BURIED_POINT_GOODS_DETAIL])
  582. ->count() ?? 0;
  583. $ret['share_card'] = BusinessCardCustomerLog::find()
  584. ->where(['mall_id' => $this->mall_id])
  585. ->andWhere(['user_id' => $user_id])
  586. ->andWhere(['>=', 'created_at', $created_at])
  587. ->andWhere(['log_type' => CardEnums::LOG_TYPE_TURN])
  588. ->count() ?? 0;
  589. /*
  590. // 下单数量
  591. $ret['order_cnt'] = BusinessCardOrder::find()
  592. ->where(['mall_id' => $this->mall_id])
  593. ->andWhere(['user_id' => $user_id])
  594. ->andWhere(['>=', 'created_at', $created_at])
  595. ->count() ?? 0;
  596. // 支付金额
  597. $ret['pay_money'] = BusinessCardOrder::find()
  598. ->where(['mall_id' => $this->mall_id])
  599. ->andWhere(['user_id' => $user_id])
  600. ->andWhere(['>=', 'created_at', $created_at])
  601. ->sum('pay_amount') ?? 0;
  602. // 浏览人数
  603. $ret['browse_people_num'] = BusinessCardCustomerLog::find()
  604. ->where(['mall_id' => $this->mall_id])
  605. ->andWhere(['user_id' => $user_id])
  606. ->andWhere(['log_type' => CardEnums::LOG_TYPE_CARD])
  607. ->groupBy('item_id')
  608. ->count() ?? 0;
  609. // 访问次数 进入首页 + 商品详情
  610. $ret['browse_num'] = BusinessCardBuriedPoint::find()
  611. ->where(['mall_id' => $this->mall_id])
  612. ->andWhere(['user_id' => $user_id])
  613. ->andWhere(['>=', 'created_at', $created_at])
  614. // ->andWhere(['log_type' => CardEnums::LOG_TYPE_BURIED_POINT_HOME])
  615. ->count() ?? 0;
  616. $ret['browse_num'] += BusinessCardCustomerLog::find()
  617. ->where(['mall_id' => $this->mall_id])
  618. ->andWhere(['user_id' => $user_id])
  619. ->andWhere(['>=', 'created_at', $created_at])
  620. ->andWhere(['log_type' => [CardEnums::LOG_TYPE_INDEX, CardEnums::LOG_TYPE_GOODS, CardEnums::LOG_TYPE_IMAGE,
  621. CardEnums::LOG_TYPE_GOODS, CardEnums::LOG_TYPE_CARD, CardEnums::LOG_TYPE_VIDEO, CardEnums::LOG_TYPE_DYNAMIC,
  622. CardEnums::LOG_TYPE_MATEREAL, CardEnums::LOG_TYPE_MATEREAL_DETAIL]])
  623. ->count() ?? 0;
  624. */
  625. // 分数
  626. $ret['analyze'] = [
  627. 'score' => BusinessCardSecretUserScore::getScoreByUserId($this->mall_id, $user_id),
  628. ];
  629. return $this->success('获取成功', $ret);
  630. }
  631. }