| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?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\BusinessCardCustomerLog;
- use addons\BusinessCard\common\models\BusinessCardPosition;
- use addons\BusinessCard\common\models\BusinessCardStatistics;
- use addons\BusinessCard\common\models\BusinessCardUser;
- use common\models\goods\Goods;
- use api\controllers\OnAuthController;
- use common\models\statistics\GoodsStatistics;
- use common\enums\StatusEnum;
- use Yii;
- class RadarController extends OnAuthController
- {
- public $modelClass = '';
- public $department_id = null;
- public $card_user = null;
- /**
- * 主管可以查看部门下全部职位,BOSS可以查看全部部门
- * @return void|mixed
- */
- public function beforeAction($action) {
- parent::beforeAction($action);
- $department_id = Yii::$app->request->get('department_id', 0);
- $data = [];
- $card_user = BusinessCardUser::getOne([['=', 'user_id', $this->user_id], ['=', 'status', StatusEnum::ENABLED]]);
- if (empty($card_user)) $data = ['code' => 1, 'msg' => '用户不存在'];
- if ($card_user->role_type == 3) $data = ['code' => 1, 'msg' => '无权查看'];
- if ($department_id && $card_user->role_type != 1 && $card_user->department_id != $department_id) $data = ['code' => 1, 'msg' => '无权查看该部门'];
- if (!empty($data)) {
- Yii::$app->controller->asJson($data)->send();
- return;
- }
- if ($department_id == 0) {
- if ($card_user->role_type == 2) {
- $this->department_id = [$card_user->department_id];
- } else {
- $this->department_id = BusinessCardPosition::find()->select(['id'])->where(['mall_id' => $this->mall_id, 'parent_id' => 0, 'status' => StatusEnum::ENABLED])->column();
- }
- } else {
- $this->department_id = [$card_user->department_id];
- }
- $this->card_user = $card_user;
- return $action;
- }
- /**
- * 获取部门
- * @author hpei
- * @return array
- */
- public function actionDepartment() {
- if ($this->card_user->role_type == 2) {
- $department_list = BusinessCardPosition::lists(['select' => ['name', 'id'], 'where' => [['in', 'id', $this->department_id]]]);
- } else {
- $department_list = BusinessCardPosition::getDepartment($this->mall_id);
- }
- return $this->success('操作成功', $department_list);
- }
- /**
- * 数据概况
- * @author hpei
- * @param int date
- * @return array|null
- */
- public function actionStatistics() {
- if (isset(Yii::$app->request->isGet)) {
- if (Yii::$app->request->isGet) {
- $date = Yii::$app->request->get('date', '');
- $ids = BusinessCardUser::find()->select('user_id')->where(['department_id' => $this->department_id])->column();
- $data = BusinessCardStatistics::survey($date, $ids);
- return $this->success('操作成功', $data);
- }
- }
- }
- /**
- * 成交转化率
- * @author hpei
- * @return array
- */
- public function actionConversion() {
- if (isset(Yii::$app->request->isGet)) {
- $param = [
- 'where' => [['in', 'department_id', $this->department_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]],
- 'group' => 'customer_type',
- 'index' => 'customer_type',
- 'select' => ['count(*) count', 'customer_type']
- ];
- $res = BusinessCardUser::conversion($param);
- return $this->success('操作成功', $res);
- }
- }
- /**
- * 商城订单交易金额统计
- * @author hpei
- * @param int date
- * @return array
- */
- public function actionOrderStatistics() {
- if (isset(Yii::$app->request->isGet)) {
- $date = Yii::$app->request->get('date', 7);
- $start_time = strtotime("-$date day 0:0:0");
- $end_time = $start_time + ($date - 1) * 86400 + 86399;
- $user_ids = BusinessCardUser::getDepartmentUid($this->department_id);
- //初始化
- $data = [];
- for ($i = 0; $i <= $date -1; $i++) {
- $newdate = date('m-d', $start_time + ($i * 86400));
- $data[$newdate] = ['order_num' => 0, 'order_money' => 0, 'date' => $newdate];
- }
- if (empty($user_ids)) return $this->success('操作成功', array_values($data));
- $params = [
- 'where' => [['=', 'mall_id', $this->mall_id], ['in', 'user_id', $user_ids], ['>', 'created_at', $start_time], ['<=', 'created_at', $end_time]],
- 'select' => ['order_num', 'order_money', 'created_at'],
- 'order' => 'created_at asc',
- ];
- $list = BusinessCardStatistics::lists($params);
- if (!empty($list)) {
- array_walk($list, function($val) use(&$data) {
- $date = date('m-d', $val['created_at']);
- if (isset($data[$date])) {
- $data[$date]['order_money'] += $val['order_money'];
- $data[$date]['order_num'] += $val['order_num'];
- }
- });
- }
- return $this->success('操作成功', array_values($data));
- }
- }
- /**
- * 新增客户数
- * @author hpei
- * @param int date
- * @return array
- */
- public function actionCustomerNumber() {
- if (isset(Yii::$app->request->isGet)) {
- $date = Yii::$app->request->get('date', 7);
- $start_time = strtotime("-$date day 0:0:0");
- $end_time = $start_time + ($date - 1) * 86400 + 86399;
- $params = [
- 'where' => [['in', 'department_id', $this->department_id], ['=', 'mall_id', $this->mall_id], ['>', 'created_at', $start_time], ['<=', 'created_at', $end_time]],
- 'select' => ['user_id', 'created_at'],
- 'ordre' => 'created_at asc',
- ];
- //初始化
- $data = [];
- for ($i = 0; $i <= $date -1; $i++) {
- $newdate = date('m-d', $start_time + ($i * 86400));
- $data[$newdate] = ['num' => 0, 'date' => $newdate];
- }
- $list = BusinessCardUser::lists($params);
- if (!empty($list)) {
- array_walk($list, function($val) use(&$data) {
- $date = date('m-d', $val['created_at']);
- if (isset($data[$date])) $data[$date]['num'] += 1;
- });
- }
- return $this->success('操作成功', array_values($data));
- }
- }
- /**
- * 跟进客户
- * @author hpei
- * @param int date
- * @return array
- */
- public function actionFollowCustomer() {
- if (Yii::$app->request->isGet) {
- $date = Yii::$app->request->get('date', 7);
- $start_time = strtotime("-$date day 0:0:0");
- $end_time = $start_time + ($date - 1) * 86400 + 86399;
- $params = [
- 'where' => [['in', 'department_id', $this->department_id], ['=', 'mall_id', $this->mall_id], ['=', 'customer_type', CardEnums::CUSTOMER_TYPE_ORDER], ['>', 'created_at', $start_time], ['<=', 'created_at', $end_time]],
- 'select' => ['user_id', 'created_at'],
- 'ordre' => 'created_at asc',
- ];
- $list = BusinessCardUser::lists($params);
- //初始化
- $data = [];
- for ($i = 0; $i <= $date -1; $i++) {
- $newdate = date('m-d', $start_time + ($i * 86400));
- $data[$newdate] = ['num' => 0, 'date' => $newdate];
- }
- if (!empty($list)) {
- array_walk($list, function($val) use(&$data) {
- $date = date('m-d', $val['created_at']);
- if (isset($data[$date])) $data[$date]['num'] += 1;
- });
- }
- return $this->success('操作成功', array_values($data));
- }
- }
- /**
- * 客户兴趣
- * @author hpei
- * @return array company:公司 product:产品 my:我的
- */
- public function actionCustomerInterest() {
- if (Yii::$app->request->isGet) {
- $user_ids = BusinessCardUser::getDepartmentUid($this->department_id);
- if (empty($user_ids)) return $this->success('操作成功', ['company' => 0, 'product' => 0, 'my' => 0]);
- $total = BusinessCardCustomerLog::count(['where' => [['in', 'user_id', $user_ids], ['in', 'log_type', array_values(array_keys(CardEnums::getLogTypeMap()))]]]);
- if ($total == 0) return $this->success('操作成功', ['company' => 0, 'product' => 0, 'my' => 0]);
- //对公司感兴趣=查看首页,对产品感兴趣=查看商品,我的=查看名片
- $params = [
- 'where' => [['in', 'user_id', $user_ids], ['in', 'log_type', [CardEnums::LOG_TYPE_INDEX, CardEnums::LOG_TYPE_GOODS, CardEnums::LOG_TYPE_CARD]]],
- 'group' => 'log_type',
- 'index' => 'log_type',
- 'select' => ['count(*) count', 'log_type']
- ];
- $list = BusinessCardCustomerLog::lists($params);
- $data['company'] = isset($list[CardEnums::LOG_TYPE_INDEX]) ? number_format($list[CardEnums::LOG_TYPE_INDEX]['count'] / $total, 2) : 0;
- $data['product'] = isset($list[CardEnums::LOG_TYPE_GOODS]) ? number_format($list[CardEnums::LOG_TYPE_GOODS]['count'] / $total, 2) : 0;
- $data['my'] = isset($list[CardEnums::LOG_TYPE_CARD]) ? number_format($list[CardEnums::LOG_TYPE_CARD]['count'] / $total, 2) : 0;
- return $this->success('操作成功', $data);
- }
- }
- /**
- * 用户活跃度
- * @author hpei
- * @param int date
- * @return array
- */
- public function actionCustomerActive() {
- if (Yii::$app->request->isGet) {
- $date = Yii::$app->request->get('date', 7);
- $start_time = strtotime("-$date day 0:0:0");
- $end_time = $start_time + ($date - 1) * 86400 + 86399;
- $user_ids = BusinessCardUser::getDepartmentUid($this->department_id);
- $data = [];
- for ($i = 0; $i <= $date -1; $i++) {
- $newdate = date('m-d', $start_time + ($i * 86400));
- $data[$newdate] = ['num' => 0, 'date' => $newdate];
- }
- if (empty($user_ids)) return $this->success('操作成功', $data);
- $params = [
- 'where' => [['in', 'user_id', $user_ids], ['in', 'log_type', array_values(array_keys(CardEnums::getLogTypeMap()))], ['>', 'created_at', $start_time], ['<=', 'created_at', $end_time]],
- 'select' => ['user_id', 'created_at']
- ];
- $list = BusinessCardCustomerLog::lists($params);
- if (!empty($list)) {
- array_walk($list, function($val) use(&$data) {
- $date = date('m-d', $val['created_at']);
- if (isset($data[$date])) $data[$date]['num'] += 1;
- });
- }
- return $this->success('操作成功', array_values($data));
- }
- }
- /**
- * 销售排行
- * @author hpei
- * @param int type 1=客户人数,2=线索数量,3=订单数量,4=交易金额
- * @return array
- */
- public function actionSaleRank() {
- if (Yii::$app->request->isGet) {
- $type = Yii::$app->request->get('type', 1);
- if ($type == 1) {
- $params = [
- 'where' => [['=', 'mall_id', $this->mall_id], ['in', 'department_id', $this->department_id]],
- 'group' => 'parent_id',
- 'order' => 'total desc',
- 'limit' => 10,
- 'select' => ['count(*) total', 'parent_id'],
- 'with' => ['parent' => function($query) {
- $query->select(['avatar_url', 'nickname', 'id']);
- }]
- ];
- $list = BusinessCardUser::listPage($params);
- } else {
- switch($type) {
- case '2':
- $field = 'clue_num';
- break;
- case '3':
- $field = 'order_num';
- break;
- case '4':
- $field = 'order_money';
- break;
- }
- $user_ids = BusinessCardUser::getDepartmentUid($this->department_id);
- $params = [
- 'where' => [['=', 'mall_id', $this->mall_id], ['in', 'user_id', $user_ids]],
- 'group' => 'user_id',
- 'order' => 'total desc',
- 'limit' => 10,
- 'select' => ["sum($field) total", 'user_id'],
- 'with' => ['user' => function($query) {
- $query->select(['avatar_url', 'nickname', 'id']);
- }]
- ];
- $list = BusinessCardStatistics::listPage($params);
- }
- if (!empty($list['list'])) {
- foreach ($list['list'] as &$val) {
- $val['avatar_url'] = isset($val['parent']) ? $val['parent']['avatar_url'] : $val['user']['avatar_url'];
- $val['nickname'] = isset($val['parent']) ? $val['parent']['nickname'] : $val['user']['nickname'];
- unset($val['parent']);
- unset($val['user']);
- }
- }
- return $this->success('操作成功', $list);
- }
- }
- /**
- * 热度排行
- * @author hpei
- * @param int type 1=商品热度,2=名片热度
- * @param int date
- */
- public function actionHotRank() {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($get, [
- [['date', 'type'], 'number'],
- [['date', 'type'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $date = Yii::$app->request->get('date', 7);
- $type = Yii::$app->request->get('type', 1);
- $start_time = strtotime("-$date day 0:0:0");
- $end_time = $start_time + ($date - 1) * 86400 + 86399;
- $params = [
- 'where' => [['>', 'created_at', $start_time], ['<=', 'created_at', $end_time], ['=', 'mall_id', $this->mall_id]],
- 'order' => 'total desc',
- 'group' => 'item_id',
- 'select' => ['count(*) total', 'item_id']
- ];
- switch($type) {
- case '1':
- array_push($params['where'], ['=', 'log_type', CardEnums::LOG_TYPE_GOODS]);
- $list = BusinessCardCustomerLog::listPage($params);
- if (!empty($list['list'])) {
- $ids = array_column($list['list'], 'item_id');
- $goods_statistics = GoodsStatistics::lists(['where' => [['in', 'goods_id', $ids]], 'group' => 'goods_id',
- 'index' => 'goods_id', 'select' => ['goods_id', 'sum(sales_num) sales_num']]);
- foreach ($list['list'] as &$val) {
- $val['sales_num'] = isset($goods_statistics[$val['item_id']]) ? $goods_statistics[$val['item_id']]['sales_num'] : 0;
- }
- $item_list = Goods::lists(['where' => [['in', 'id', $ids]], 'select' => ['id', 'goods_name', 'cover_pic', 'price'], 'index' => 'id']);
- }
- break;
- case '2':
- array_push($params['where'], ['=', 'log_type', CardEnums::LOG_TYPE_CARD]);
- $list = BusinessCardCustomerLog::listPage($params);
- if (!empty($list)) {
- $ids = array_column($list['list'], 'item_id');
- $item_param = [
- 'where' =>
- [['in', 'id', $ids]],
- 'select' => ['id', 'avatar_url', 'nickname', 'position_id'],
- 'with' => ['position' => function($qurey){
- $qurey->select(['name','id']);
- }],
- ];
- $item_list = BusinessCard::lists($item_param);
- if (!empty($item_list)) $item_list = array_column($item_list, null, 'id');
- }
- break;
- }
- if (!empty($item_list) && !empty($list)) {
- array_walk($list['list'], function(&$val) use ($item_list) {
- if (isset($item_list[$val['item_id']])) {
- $val = array_merge($val, $item_list[$val['item_id']]);
- }
- });
- }
- return $this->success('操作成功', $list);
- }
- }
- }
|