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'] = '查看商品[' . Goods::find()->where(['id' => $val['object_id']])->select('goods_name')->scalar() . '],请抓住机会吧'; 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'] = "购买了商品$goods_name,请抓住机会吧"; 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'] = "$times查看了素材《{$material_name['title']}》,本次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('', '', $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); } }