RecordController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. namespace addons\BusinessCard\api\modules\v1\controllers;
  3. use addons\BusinessCard\common\enums\CardEnums;
  4. use addons\BusinessCard\common\models\BusinessCard;
  5. use addons\BusinessCard\common\models\BusinessCardBuriedPoint;
  6. use addons\BusinessCard\common\models\BusinessCardCustomerLog;
  7. use addons\BusinessCard\common\models\BusinessCardMaterials;
  8. use addons\BusinessCard\common\models\BusinessCardMaterialsBrowseLog;
  9. use addons\BusinessCard\common\models\BusinessCardOrder;
  10. use addons\BusinessCard\common\models\BusinessCardUser;
  11. use common\models\goods\Goods;
  12. use common\models\order\OrderDetail;
  13. use common\models\user\UserPartitionRelationship;
  14. use common\models\statistics\GoodsStatistics;
  15. use api\controllers\OnAuthController;
  16. use common\models\user\User;
  17. use common\enums\StatusEnum;
  18. use Yii;
  19. use yii\data\Pagination;
  20. class RecordController extends OnAuthController
  21. {
  22. public $modelClass = '';
  23. public $card = '';
  24. /**
  25. * 提示创建名片
  26. */
  27. public function beforeAction($action) {
  28. parent::beforeAction($action);
  29. $card = BusinessCard::getOne([['=', 'user_id', $this->user_id], ['=', 'status', StatusEnum::ENABLED]], true);
  30. if (empty($card)) {
  31. Yii::$app->controller->asJson(['code' => 3, 'msg' => '请先创建名片'])->send();
  32. return;
  33. }
  34. $this->card = $card;
  35. return $action;
  36. }
  37. /**
  38. * 用户轨迹(下级)
  39. * @author hpei
  40. * @param int date
  41. * @return mixed
  42. */
  43. public function actionTrajectory() {
  44. if (Yii::$app->request->isGet) {
  45. $date = Yii::$app->request->get('date', '');
  46. if(!empty($date)) {
  47. $start_time = strtotime("-$date day 0:0:0");
  48. $end_time = $start_time + ($date - 1) * 86400 + 86399;
  49. $where[] = ['>', 'created_at', $start_time];
  50. $where[] = ['<=', 'created_at', $end_time];
  51. }
  52. $child = UserPartitionRelationship::getChildIdArr($this->user_id);
  53. $where[] = ['=', 'mall_id', $this->mall_id];
  54. $where[] = ['in', 'user_id', $child];
  55. $where[] = ['=', 'status', StatusEnum::ENABLED];
  56. $params = [
  57. 'where' => $where,
  58. 'select' => ['count(*) count', 'log_type'],
  59. 'group' => 'log_type',
  60. 'index' => 'log_type'
  61. ];
  62. $model = BusinessCardCustomerLog::find();
  63. BusinessCardCustomerLog::paramPack($params, $model);
  64. $list = $model->asArray()->all();
  65. $log_type_enum = array_keys(CardEnums::getLogTypeMap());
  66. $data = [];
  67. array_walk($log_type_enum, function($log_type) use($list, &$data) {
  68. $data[] = ['type' => $log_type, 'count' => isset($list[$log_type]) ? $list[$log_type]['count'] : 0];
  69. });
  70. return $this->success('操作成功', $data);
  71. }
  72. }
  73. /**
  74. * 商品热度排行(下级)
  75. * @author hpei
  76. * @return mixed
  77. */
  78. public function actionGoodsBrowseRank() {
  79. if (Yii::$app->request->isGet) {
  80. $child = UserPartitionRelationship::getChildIdArr($this->user_id);
  81. $goods_list = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic.goods_list');
  82. $goods_list = array_column(json_decode($goods_list, true), null, 'goods_id');
  83. $goods_ids = array_keys($goods_list);
  84. $params = [
  85. 'where' => [['=', 'log_type', CardEnums::LOG_TYPE_GOODS], ['=', 'mall_id', $this->mall_id], ['in', 'item_id', $goods_ids], ['in', 'user_id', $child]],
  86. 'group' => 'goods_id',
  87. 'select' => ['count(*) browse_num', 'item_id goods_id'],
  88. 'order' => 'browse_num desc'
  89. ];
  90. $model = BusinessCardCustomerLog::find();
  91. BusinessCardCustomerLog::paramPack($params, $model);
  92. $list = $model->asArray()->all();
  93. if (!empty($list)) {
  94. $goods_sales = GoodsStatistics::find()->where(['goods_id' => $goods_ids])->select(['sales_num'])->indexBy('goods_id')->column();
  95. array_walk($list, function(&$val) use($goods_list, $goods_sales) {
  96. if (isset($goods_list[$val['goods_id']])) {
  97. $val['goods_name'] = $goods_list[$val['goods_id']]['goods_name'];
  98. $val['cover_pic'] = $goods_list[$val['goods_id']]['cover_pic'];
  99. }
  100. $val['sales_num'] = isset($goods_sales[$val['goods_id']]) ? $goods_sales[$val['goods_id']] : 0;
  101. });
  102. }
  103. return $this->success('操作成功', $list);
  104. }
  105. }
  106. /**
  107. * 用户行为
  108. * @author hpei
  109. * @return mixed
  110. */
  111. public function actionBehavior() {
  112. if (Yii::$app->request->isGet) {
  113. //$child = UserPartitionRelationship::getChildIdArr($this->user_id);
  114. $log_type_enum = array_keys(CardEnums::getLogTypeMap());
  115. $where[] = ['=', 'mall_id', $this->mall_id];
  116. //$where[] = ['in', 'user_id', $child];
  117. $where[] = ['in', 'log_type', $log_type_enum];
  118. $where[] = ['!=', 'user_id', $this->user_id];
  119. $carduser = BusinessCardUser::getOne([['mall_id' => $this->mall_id,'user_id' => $this->user_id,'status' =>StatusEnum::ENABLED]],true);
  120. if($carduser['role_type'] == CardEnums::ROLE_TYPE_STAFF || !$carduser['role_type']){
  121. $user_ids = BusinessCardUser::find()->select("user_id")->where(['mall_id' => $this->mall_id])->andWhere(['=', 'parent_id', $this->user_id])->asArray()->column();
  122. $card_data = BusinessCard::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['=', 'user_id', $user_ids])->asArray()->column();
  123. if($card_data) $where[] = ['in', 'item_id', $card_data];
  124. }else if($carduser['role_type'] == CardEnums::ROLE_TYPE_LEADER){
  125. $user_ids = BusinessCardUser::find()->select("user_id")->where(['mall_id' => $this->mall_id])->andWhere(['=', 'department_id', $carduser['department_id']])->asArray()->column();
  126. $card_data = BusinessCard::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['=', 'user_id', $user_ids])->asArray()->column();
  127. if($card_data) $where[] = ['in', 'item_id', $card_data];
  128. }
  129. $params = [
  130. 'where' => $where,
  131. 'select' => ['log_type','id', 'item_id', 'user_id', 'created_at','material_id','material_log_id','material_times'],
  132. 'order' => 'id desc',
  133. 'limit' => 10
  134. ];
  135. //var_dump($params);
  136. $list = BusinessCardCustomerLog::listPage($params);
  137. if (!empty($list['list'])) {
  138. $user_ids = array_column($list['list'], 'user_id');
  139. $user_list = User::find()->where(['id' => $user_ids])->select(['nickname', 'avatar_url','mobile', 'id'])->indexBy('id')->asArray()->all();
  140. $goods_list = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic.goods_list');
  141. if($goods_list) $goods_list = array_column(json_decode($goods_list, true), null, 'goods_id');
  142. array_walk($list['list'], function(&$val) use ($user_list, $goods_list) {
  143. if ($user_list[$val['user_id']]) {
  144. $val['nickname'] = $user_list[$val['user_id']]['nickname'];
  145. $val['avatar_url'] = $user_list[$val['user_id']]['avatar_url'];
  146. $val['mobile'] = $user_list[$val['user_id']]['mobile'];
  147. }
  148. $val['remark'] = CardEnums::getLogTypeMap($val['log_type']);
  149. //查看素材记录
  150. if($val['item_id'] == '-1'){
  151. $material_name = BusinessCardMaterials::getOne([['=','id',$val['material_id']]],true,'','','title');
  152. $material_view_time = BusinessCardMaterialsBrowseLog::getOne([['=','id',$val['material_log_id']]],true,'','','view_seconds');
  153. $times = $val['material_times']==1?'首次':'第'.$val['material_times'].'次';;
  154. $val['remark'] = $times.'查看素材《'.$material_name['title'].'》,本次TA观看了'.BusinessCardCustomerLog::sec2Time($material_view_time['view_seconds']);
  155. }
  156. //查看商品记录
  157. if ($val['log_type'] == CardEnums::LOG_TYPE_GOODS) $val['remark'] .= '(' . $goods_list[$val['item_id']]['goods_name'] .')';
  158. });
  159. $get_date = [];
  160. $get_date_key = [];
  161. if($list['list']){
  162. foreach($list['list'] as $k =>$v){
  163. $list['list'][$k]['date'] = date('Y-m-d',$v['created_at']);
  164. $list['list'][$k]['time'] = date('H:i',$v['created_at']);
  165. $get_date[$list['list'][$k]['date']][] = $list['list'][$k];
  166. }
  167. if($get_date){
  168. foreach($get_date as $key=>$value){
  169. $get_date_key[] = $value;
  170. }
  171. }
  172. }
  173. $list['list'] = $get_date_key;
  174. }
  175. return $this->success('操作成功', $list);
  176. }
  177. }
  178. /**
  179. * 雷达 - 数据中心 - 成交率
  180. * @return mixed|null
  181. */
  182. public function actionTransactionRate()
  183. {
  184. $get = Yii::$app->request->get();
  185. $res = Yii::$app->services->validate->validate($get, [
  186. [['range'], 'in', 'range' => [0, 1, 7, 30]],
  187. [['range'], 'integer'], // 0, 1, 7, 30
  188. ], [
  189. 'range' => '区间'
  190. ]);
  191. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  192. $range = $get['range'];
  193. $created_at = 0;
  194. switch ($range) {
  195. case 1:
  196. $created_at = strtotime(date('Y-m-d'));
  197. break;
  198. case 7:
  199. $created_at = time() - (7 * 86400);
  200. break;
  201. case 30:
  202. $created_at = time() - (30 * 86400);
  203. break;
  204. }
  205. // 下单数量
  206. $ret['order_cnt'] = BusinessCardOrder::find()
  207. ->where(['mall_id' => $this->mall_id])
  208. ->andWhere(['item_id' => $this->card['id']])
  209. ->andWhere(['>=', 'created_at', $created_at])
  210. ->count() ?? 0;
  211. // 支付金额
  212. $ret['pay_money'] = BusinessCardOrder::find()
  213. ->where(['mall_id' => $this->mall_id])
  214. ->andWhere(['item_id' => $this->card['id']])
  215. ->andWhere(['>=', 'created_at', $created_at])
  216. ->sum('pay_amount') ?? 0;
  217. // 访问次数 所有记录的浏览
  218. $ret['browse_num'] = BusinessCardBuriedPoint::find()
  219. ->where(['mall_id' => $this->mall_id])
  220. ->andWhere(['item_id' => $this->card['id']])
  221. ->andWhere(['>=', 'created_at', $created_at])
  222. ->andWhere(['log_type' => [CardEnums::LOG_TYPE_BURIED_POINT_HOME, CardEnums::LOG_TYPE_CARD, CardEnums::LOG_TYPE_DYNAMIC, CardEnums::LOG_TYPE_MATEREAL_DETAIL]])
  223. ->count();
  224. $ret['browse_num'] += BusinessCardBuriedPoint::find()
  225. ->where(['mall_id' => $this->mall_id])
  226. ->andWhere(['item_id' => $this->card['id']])
  227. ->andWhere(['>=', 'created_at', $created_at])
  228. ->count() ?? 0;
  229. // 访问人数
  230. $ret['browse_people_num'] = BusinessCardBuriedPoint::find()
  231. ->where(['mall_id' => $this->mall_id])
  232. ->andWhere(['item_id' => $this->card['id']])
  233. ->andWhere(['>=', 'created_at', $created_at])
  234. ->andWhere(['log_type' => [CardEnums::LOG_TYPE_BURIED_POINT_HOME, CardEnums::LOG_TYPE_CARD, CardEnums::LOG_TYPE_DYNAMIC, CardEnums::LOG_TYPE_MATEREAL_DETAIL]])
  235. ->groupBy('user_id')
  236. ->count();
  237. $ret['browse_people_num'] += BusinessCardBuriedPoint::find()
  238. ->where(['mall_id' => $this->mall_id])
  239. ->andWhere(['item_id' => $this->card['id']])
  240. ->andWhere(['>=', 'created_at', $created_at])
  241. ->groupBy('user_id')
  242. ->count() ?? 0;
  243. // 商品浏览排行
  244. $ret['goods'] = BusinessCardBuriedPoint::find()
  245. ->where(['mall_id' => $this->mall_id])
  246. ->andWhere(['item_id' => $this->card['id']])
  247. ->andWhere(['status' => StatusEnum::ENABLED])
  248. ->andWhere(['log_type' => CardEnums::LOG_TYPE_BURIED_POINT_GOODS_DETAIL])
  249. ->groupBy('object_id')
  250. ->select(['id', 'object_id', 'COUNT(*) as browse_num'])
  251. ->orderBy('browse_num desc')
  252. ->limit(10)
  253. ->with(['goods' => function($query) {
  254. $query->select('id, price, cover_pic, goods_name, sales_num');
  255. }])
  256. ->asArray()
  257. ->all();
  258. if (!empty($ret['goods'])) {
  259. foreach ($ret['goods'] as &$item) {
  260. $item['goods']['browse_num'] = $item['browse_num'];
  261. $item = $item['goods'];
  262. }
  263. }
  264. return $this->success('获取成功', $ret);
  265. }
  266. /**
  267. * 行为分析
  268. * @return mixed|null
  269. */
  270. public function actionAnalyze()
  271. {
  272. $get = \Yii::$app->request->get();
  273. // 行为轨迹
  274. $log_type_enum = array_keys(CardEnums::getLogTypeMap());
  275. $where[] = ['=', 'mall_id', $this->mall_id];
  276. $where[] = ['in', 'log_type', $log_type_enum];
  277. $where[] = ['!=', 'user_id', $this->user_id];
  278. // $carduser = BusinessCardUser::getOne([['mall_id' => $this->mall_id,'user_id' => $this->user_id,'status' =>StatusEnum::ENABLED]],true);
  279. // if($carduser['role_type'] == CardEnums::ROLE_TYPE_STAFF || !$carduser['role_type']){ // 员工角色
  280. // $user_ids = BusinessCardUser::find()->select("user_id")
  281. // ->where(['mall_id' => $this->mall_id])->andWhere(['=', 'parent_id', $this->user_id])->asArray()->column();
  282. // $card_data = BusinessCard::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['=', 'user_id', $user_ids])->asArray()->column();
  283. // $card_data[] = $this->card['id'];
  284. // }else if($carduser['role_type'] == CardEnums::ROLE_TYPE_LEADER){ // 主管角色
  285. // $user_ids = BusinessCardUser::find()->select("user_id")
  286. // ->where(['mall_id' => $this->mall_id])->andWhere(['=', 'department_id', $carduser['department_id']])->asArray()->column();
  287. // $card_data = BusinessCard::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['=', 'user_id', $user_ids])->asArray()->column();
  288. // $card_data[] = $this->card['id'];
  289. // }
  290. $query = BusinessCardCustomerLog::find()
  291. ->where(['mall_id' => $this->mall_id])
  292. ->andWhere(['!=', 'user_id', $this->user_id])
  293. ->andWhere(['log_type' => $log_type_enum])
  294. ->andWhere(['item_id' => $this->card['id']])
  295. ->select('id, log_type, created_at, updated_at, user_id, item_id, material_id as object_id, material_times, material_log_id');
  296. $queryOrder = BusinessCardOrder::find()
  297. ->where(['mall_id' => $this->mall_id])
  298. ->andWhere(['!=', 'user_id', $this->user_id])
  299. ->andWhere(['item_id' => $this->card['id']])
  300. ->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');
  301. $queryPoint = BusinessCardBuriedPoint::find()
  302. ->where(['mall_id' => $this->mall_id])
  303. ->andWhere(['!=', 'user_id', $this->user_id])
  304. ->andWhere(['log_type' => $log_type_enum])
  305. ->andWhere(['item_id' => $this->card['id']])
  306. ->select('id, log_type, created_at, updated_at, user_id, item_id, object_id, status as material_times, status as material_log_id');
  307. if (!empty($card_data)) {
  308. $card_data = array_unique($card_data);
  309. $query->andWhere(['item_id' => $card_data]);
  310. $queryPoint->andWhere(['item_id' => $card_data]);
  311. }
  312. $query->union($queryPoint)->union($queryOrder);
  313. $ret['page_size'] = $get['limit'] ?? 10;
  314. $ret['page'] = max($get['page'] ?? 1, 1);
  315. $ret['count'] = $query->count();
  316. $pages = new Pagination(['totalCount' => $ret['count']]);
  317. $pages->pageSizeParam = "limit";
  318. $pages->defaultPageSize = $ret['page_size'];
  319. $pages->setPageSize($ret['page_size']);
  320. $ret['pagination'] = $pages;
  321. $ret['page_count'] = (!empty($ret['count'] ) && $ret['count'] > 0) ? ceil($ret['count'] / $ret['page_size']) : 1;
  322. $offset = ($ret['page']-1) * $ret['page_size'];
  323. $sql = $query->createCommand()->getRawSql();
  324. $ret['list'] = Yii::$app->db->createCommand("$sql ORDER BY updated_at DESC LIMIT $offset, {$ret['page_size']}")->queryAll();
  325. $ret['data'] = [
  326. 'share_num' => $this->card['share_num'],
  327. 'browse_num'=> $this->card['browse_num'],
  328. 'like_num' => $this->card['like_num'],
  329. ];
  330. if (!empty($ret['list'])) {
  331. $user_ids = array_column($ret['list'], 'user_id');
  332. $user_list = User::find()->where(['id' => $user_ids])->select(['nickname', 'avatar_url','mobile', 'id'])->indexBy('id')->asArray()->all();
  333. $material_times_arr = [CardEnums::LOG_TYPE_CARD];
  334. $get_date = [];
  335. $get_date_key = [];
  336. foreach ($ret['list'] as &$val) {
  337. if (!empty($user_list[$val['user_id']])) {
  338. $val['nickname'] = $user_list[$val['user_id']]['nickname'];
  339. $val['avatar_url'] = $user_list[$val['user_id']]['avatar_url'];
  340. $val['mobile'] = $user_list[$val['user_id']]['mobile'];
  341. }
  342. $val['date'] = date('Y年m月d日',$val['created_at']);
  343. $val['time'] = date('H:i',$val['created_at']);
  344. if (!empty($val['object_id'])) {
  345. switch ($val['log_type']) {
  346. case CardEnums::LOG_TYPE_BURIED_POINT_GOODS_DETAIL:
  347. // 查看商品
  348. $val['remark'] = '<span>查看</span>了<span>商品</span>[<span>' .
  349. Goods::find()->where(['id' => $val['object_id']])->select('goods_name')->scalar() . '</span>],请抓住机会吧';
  350. break;
  351. case CardEnums::LOG_TYPE_ORDER_TYPE:
  352. // 下单
  353. // 获取商品名称
  354. $goods_names = OrderDetail::find()
  355. ->where(['mall_id' => $this->mall_id])
  356. ->andWhere(['order_id' => $val['object_id']])
  357. ->andWhere(['status' => StatusEnum::ENABLED])
  358. ->select('goods_name')
  359. ->column();
  360. $goods_name = !empty($goods_names[0]) ? "[$goods_names[0]]" : '';
  361. $goods_cnt = count($goods_names);
  362. if ($goods_cnt > 1) {
  363. $goods_name = "$goods_name 等, 共 [$goods_cnt] 款";
  364. }
  365. $val['remark'] = "<span>购买</span>了商品<span>$goods_name</span>,请抓住机会吧";
  366. break;
  367. default:
  368. // 查看素材
  369. $material_name = BusinessCardMaterials::getOne([['=','id',$val['object_id']]],true,'','','title');
  370. $material_view_time = BusinessCardMaterialsBrowseLog::getOne([['=','id',$val['material_log_id']]],true,'','','view_seconds');
  371. $times = $val['material_times']==1?'首次':'第'.$val['material_times'].'次';;
  372. $val['remark'] = "<span>$times</span>查看了<span>素材《{$material_name['title']}》</span>,本次TA观看了".BusinessCardCustomerLog::sec2Time($material_view_time['view_seconds']);
  373. // $val['remark'] = $times.'查看素材《'.$material_name['title'].'》,本次TA观看了'.BusinessCardCustomerLog::sec2Time($material_view_time['view_seconds']);
  374. }
  375. } else {
  376. // 需要取数据
  377. if ($this->card['id'] != $val['item_id']) {
  378. $nickname = BusinessCard::find()->where(['id' => $val['item_id']])->select('nickname')->scalar();
  379. }
  380. $val['remark'] = CardEnums::getLogTypeMapV2($val['log_type'],
  381. in_array($val['log_type'], $material_times_arr) ? $val['material_times'] : null,
  382. !empty($nickname) ? "[$nickname]" : "您");
  383. }
  384. $val['remark'] = str_replace('<span>', '<span style="color: #db0505">', $val['remark']);
  385. $get_date[$val['date']][] = $val;
  386. }
  387. if($get_date){
  388. foreach($get_date as $value){
  389. $get_date_key[] = $value;
  390. }
  391. $ret['list'] = $get_date_key;
  392. }
  393. }
  394. return $this->success('获取成功', $ret);
  395. }
  396. }