| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- <?php
- namespace addons\BusinessCard\api\modules\v1\controllers;
- use addons\BusinessCard\common\enums\CardEnums;
- use addons\BusinessCard\common\models\BusinessCard;
- use addons\BusinessCard\common\models\BusinessCardBuriedPoint;
- use addons\BusinessCard\common\models\BusinessCardCustomerLog;
- use addons\BusinessCard\common\models\BusinessCardMaterials;
- use addons\BusinessCard\common\models\BusinessCardMaterialsBrowseLog;
- use addons\BusinessCard\common\models\BusinessCardOrder;
- use addons\BusinessCard\common\models\BusinessCardUser;
- use common\models\goods\Goods;
- use common\models\order\OrderDetail;
- use common\models\user\UserPartitionRelationship;
- use common\models\statistics\GoodsStatistics;
- use api\controllers\OnAuthController;
- use common\models\user\User;
- use common\enums\StatusEnum;
- use Yii;
- use yii\data\Pagination;
- class RecordController 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 date
- * @return mixed
- */
- public function actionTrajectory() {
- if (Yii::$app->request->isGet) {
- $date = Yii::$app->request->get('date', '');
- if(!empty($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];
- }
- $child = UserPartitionRelationship::getChildIdArr($this->user_id);
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['in', 'user_id', $child];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $params = [
- 'where' => $where,
- 'select' => ['count(*) count', 'log_type'],
- 'group' => 'log_type',
- 'index' => 'log_type'
- ];
- $model = BusinessCardCustomerLog::find();
- BusinessCardCustomerLog::paramPack($params, $model);
- $list = $model->asArray()->all();
- $log_type_enum = array_keys(CardEnums::getLogTypeMap());
- $data = [];
- array_walk($log_type_enum, function($log_type) use($list, &$data) {
- $data[] = ['type' => $log_type, 'count' => isset($list[$log_type]) ? $list[$log_type]['count'] : 0];
- });
- return $this->success('操作成功', $data);
- }
- }
- /**
- * 商品热度排行(下级)
- * @author hpei
- * @return mixed
- */
- public function actionGoodsBrowseRank() {
- if (Yii::$app->request->isGet) {
- $child = UserPartitionRelationship::getChildIdArr($this->user_id);
- $goods_list = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic.goods_list');
- $goods_list = array_column(json_decode($goods_list, true), null, 'goods_id');
- $goods_ids = array_keys($goods_list);
- $params = [
- 'where' => [['=', 'log_type', CardEnums::LOG_TYPE_GOODS], ['=', 'mall_id', $this->mall_id], ['in', 'item_id', $goods_ids], ['in', 'user_id', $child]],
- 'group' => 'goods_id',
- 'select' => ['count(*) browse_num', 'item_id goods_id'],
- 'order' => 'browse_num desc'
- ];
- $model = BusinessCardCustomerLog::find();
- BusinessCardCustomerLog::paramPack($params, $model);
- $list = $model->asArray()->all();
- if (!empty($list)) {
- $goods_sales = GoodsStatistics::find()->where(['goods_id' => $goods_ids])->select(['sales_num'])->indexBy('goods_id')->column();
- array_walk($list, function(&$val) use($goods_list, $goods_sales) {
- if (isset($goods_list[$val['goods_id']])) {
- $val['goods_name'] = $goods_list[$val['goods_id']]['goods_name'];
- $val['cover_pic'] = $goods_list[$val['goods_id']]['cover_pic'];
- }
- $val['sales_num'] = isset($goods_sales[$val['goods_id']]) ? $goods_sales[$val['goods_id']] : 0;
- });
- }
- return $this->success('操作成功', $list);
- }
- }
- /**
- * 用户行为
- * @author hpei
- * @return mixed
- */
- public function actionBehavior() {
- if (Yii::$app->request->isGet) {
- //$child = UserPartitionRelationship::getChildIdArr($this->user_id);
- $log_type_enum = array_keys(CardEnums::getLogTypeMap());
- $where[] = ['=', 'mall_id', $this->mall_id];
- //$where[] = ['in', 'user_id', $child];
- $where[] = ['in', 'log_type', $log_type_enum];
- $where[] = ['!=', 'user_id', $this->user_id];
- $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();
- if($card_data) $where[] = ['in', 'item_id', $card_data];
- }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();
- if($card_data) $where[] = ['in', 'item_id', $card_data];
- }
- $params = [
- 'where' => $where,
- 'select' => ['log_type','id', 'item_id', 'user_id', 'created_at','material_id','material_log_id','material_times'],
- 'order' => 'id desc',
- 'limit' => 10
- ];
- //var_dump($params);
- $list = BusinessCardCustomerLog::listPage($params);
- if (!empty($list['list'])) {
- $user_ids = array_column($list['list'], 'user_id');
- $user_list = User::find()->where(['id' => $user_ids])->select(['nickname', 'avatar_url','mobile', 'id'])->indexBy('id')->asArray()->all();
- $goods_list = Yii::$app->services->setting->addons->get($this->mall_id, CardEnums::ADDONS_NAME, 'basic.goods_list');
- if($goods_list) $goods_list = array_column(json_decode($goods_list, true), null, 'goods_id');
- array_walk($list['list'], function(&$val) use ($user_list, $goods_list) {
- if ($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['remark'] = CardEnums::getLogTypeMap($val['log_type']);
- //查看素材记录
- if($val['item_id'] == '-1'){
- $material_name = BusinessCardMaterials::getOne([['=','id',$val['material_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'] = $times.'查看素材《'.$material_name['title'].'》,本次TA观看了'.BusinessCardCustomerLog::sec2Time($material_view_time['view_seconds']);
- }
- //查看商品记录
- if ($val['log_type'] == CardEnums::LOG_TYPE_GOODS) $val['remark'] .= '(' . $goods_list[$val['item_id']]['goods_name'] .')';
- });
- $get_date = [];
- $get_date_key = [];
- if($list['list']){
- foreach($list['list'] as $k =>$v){
- $list['list'][$k]['date'] = date('Y-m-d',$v['created_at']);
- $list['list'][$k]['time'] = date('H:i',$v['created_at']);
- $get_date[$list['list'][$k]['date']][] = $list['list'][$k];
- }
- if($get_date){
- foreach($get_date as $key=>$value){
- $get_date_key[] = $value;
- }
- }
- }
- $list['list'] = $get_date_key;
- }
- return $this->success('操作成功', $list);
- }
- }
- /**
- * 雷达 - 数据中心 - 成交率
- * @return mixed|null
- */
- public function actionTransactionRate()
- {
- $get = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($get, [
- [['range'], 'in', 'range' => [0, 1, 7, 30]],
- [['range'], 'integer'], // 0, 1, 7, 30
- ], [
- 'range' => '区间'
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $range = $get['range'];
- $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['order_cnt'] = BusinessCardOrder::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['item_id' => $this->card['id']])
- ->andWhere(['>=', 'created_at', $created_at])
- ->count() ?? 0;
- // 支付金额
- $ret['pay_money'] = BusinessCardOrder::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['item_id' => $this->card['id']])
- ->andWhere(['>=', 'created_at', $created_at])
- ->sum('pay_amount') ?? 0;
- // 访问次数 所有记录的浏览
- $ret['browse_num'] = BusinessCardBuriedPoint::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['item_id' => $this->card['id']])
- ->andWhere(['>=', 'created_at', $created_at])
- ->andWhere(['log_type' => [CardEnums::LOG_TYPE_BURIED_POINT_HOME, CardEnums::LOG_TYPE_CARD, CardEnums::LOG_TYPE_DYNAMIC, CardEnums::LOG_TYPE_MATEREAL_DETAIL]])
- ->count();
- $ret['browse_num'] += BusinessCardBuriedPoint::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['item_id' => $this->card['id']])
- ->andWhere(['>=', 'created_at', $created_at])
- ->count() ?? 0;
- // 访问人数
- $ret['browse_people_num'] = BusinessCardBuriedPoint::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['item_id' => $this->card['id']])
- ->andWhere(['>=', 'created_at', $created_at])
- ->andWhere(['log_type' => [CardEnums::LOG_TYPE_BURIED_POINT_HOME, CardEnums::LOG_TYPE_CARD, CardEnums::LOG_TYPE_DYNAMIC, CardEnums::LOG_TYPE_MATEREAL_DETAIL]])
- ->groupBy('user_id')
- ->count();
- $ret['browse_people_num'] += BusinessCardBuriedPoint::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['item_id' => $this->card['id']])
- ->andWhere(['>=', 'created_at', $created_at])
- ->groupBy('user_id')
- ->count() ?? 0;
- // 商品浏览排行
- $ret['goods'] = BusinessCardBuriedPoint::find()
- ->where(['mall_id' => $this->mall_id])
- ->andWhere(['item_id' => $this->card['id']])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['log_type' => CardEnums::LOG_TYPE_BURIED_POINT_GOODS_DETAIL])
- ->groupBy('object_id')
- ->select(['id', 'object_id', 'COUNT(*) as browse_num'])
- ->orderBy('browse_num desc')
- ->limit(10)
- ->with(['goods' => function($query) {
- $query->select('id, price, cover_pic, goods_name, sales_num');
- }])
- ->asArray()
- ->all();
- if (!empty($ret['goods'])) {
- foreach ($ret['goods'] as &$item) {
- $item['goods']['browse_num'] = $item['browse_num'];
- $item = $item['goods'];
- }
- }
- return $this->success('获取成功', $ret);
- }
- /**
- * 行为分析
- * @return mixed|null
- */
- public function actionAnalyze()
- {
- $get = \Yii::$app->request->get();
- // 行为轨迹
- $log_type_enum = array_keys(CardEnums::getLogTypeMap());
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['in', 'log_type', $log_type_enum];
- $where[] = ['!=', 'user_id', $this->user_id];
- // $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', $this->user_id])
- ->andWhere(['log_type' => $log_type_enum])
- ->andWhere(['item_id' => $this->card['id']])
- ->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', $this->user_id])
- ->andWhere(['item_id' => $this->card['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', $this->user_id])
- ->andWhere(['log_type' => $log_type_enum])
- ->andWhere(['item_id' => $this->card['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($queryPoint)->union($queryOrder);
- $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['data'] = [
- 'share_num' => $this->card['share_num'],
- 'browse_num'=> $this->card['browse_num'],
- 'like_num' => $this->card['like_num'],
- ];
- 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']);
- $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);
- }
- }
|