RadarController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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\BusinessCardCustomerLog;
  6. use addons\BusinessCard\common\models\BusinessCardPosition;
  7. use addons\BusinessCard\common\models\BusinessCardStatistics;
  8. use addons\BusinessCard\common\models\BusinessCardUser;
  9. use common\models\goods\Goods;
  10. use api\controllers\OnAuthController;
  11. use common\models\statistics\GoodsStatistics;
  12. use common\enums\StatusEnum;
  13. use Yii;
  14. class RadarController extends OnAuthController
  15. {
  16. public $modelClass = '';
  17. public $department_id = null;
  18. public $card_user = null;
  19. /**
  20. * 主管可以查看部门下全部职位,BOSS可以查看全部部门
  21. * @return void|mixed
  22. */
  23. public function beforeAction($action) {
  24. parent::beforeAction($action);
  25. $department_id = Yii::$app->request->get('department_id', 0);
  26. $data = [];
  27. $card_user = BusinessCardUser::getOne([['=', 'user_id', $this->user_id], ['=', 'status', StatusEnum::ENABLED]]);
  28. if (empty($card_user)) $data = ['code' => 1, 'msg' => '用户不存在'];
  29. if ($card_user->role_type == 3) $data = ['code' => 1, 'msg' => '无权查看'];
  30. if ($department_id && $card_user->role_type != 1 && $card_user->department_id != $department_id) $data = ['code' => 1, 'msg' => '无权查看该部门'];
  31. if (!empty($data)) {
  32. Yii::$app->controller->asJson($data)->send();
  33. return;
  34. }
  35. if ($department_id == 0) {
  36. if ($card_user->role_type == 2) {
  37. $this->department_id = [$card_user->department_id];
  38. } else {
  39. $this->department_id = BusinessCardPosition::find()->select(['id'])->where(['mall_id' => $this->mall_id, 'parent_id' => 0, 'status' => StatusEnum::ENABLED])->column();
  40. }
  41. } else {
  42. $this->department_id = [$card_user->department_id];
  43. }
  44. $this->card_user = $card_user;
  45. return $action;
  46. }
  47. /**
  48. * 获取部门
  49. * @author hpei
  50. * @return array
  51. */
  52. public function actionDepartment() {
  53. if ($this->card_user->role_type == 2) {
  54. $department_list = BusinessCardPosition::lists(['select' => ['name', 'id'], 'where' => [['in', 'id', $this->department_id]]]);
  55. } else {
  56. $department_list = BusinessCardPosition::getDepartment($this->mall_id);
  57. }
  58. return $this->success('操作成功', $department_list);
  59. }
  60. /**
  61. * 数据概况
  62. * @author hpei
  63. * @param int date
  64. * @return array|null
  65. */
  66. public function actionStatistics() {
  67. if (isset(Yii::$app->request->isGet)) {
  68. if (Yii::$app->request->isGet) {
  69. $date = Yii::$app->request->get('date', '');
  70. $ids = BusinessCardUser::find()->select('user_id')->where(['department_id' => $this->department_id])->column();
  71. $data = BusinessCardStatistics::survey($date, $ids);
  72. return $this->success('操作成功', $data);
  73. }
  74. }
  75. }
  76. /**
  77. * 成交转化率
  78. * @author hpei
  79. * @return array
  80. */
  81. public function actionConversion() {
  82. if (isset(Yii::$app->request->isGet)) {
  83. $param = [
  84. 'where' => [['in', 'department_id', $this->department_id], ['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]],
  85. 'group' => 'customer_type',
  86. 'index' => 'customer_type',
  87. 'select' => ['count(*) count', 'customer_type']
  88. ];
  89. $res = BusinessCardUser::conversion($param);
  90. return $this->success('操作成功', $res);
  91. }
  92. }
  93. /**
  94. * 商城订单交易金额统计
  95. * @author hpei
  96. * @param int date
  97. * @return array
  98. */
  99. public function actionOrderStatistics() {
  100. if (isset(Yii::$app->request->isGet)) {
  101. $date = Yii::$app->request->get('date', 7);
  102. $start_time = strtotime("-$date day 0:0:0");
  103. $end_time = $start_time + ($date - 1) * 86400 + 86399;
  104. $user_ids = BusinessCardUser::getDepartmentUid($this->department_id);
  105. //初始化
  106. $data = [];
  107. for ($i = 0; $i <= $date -1; $i++) {
  108. $newdate = date('m-d', $start_time + ($i * 86400));
  109. $data[$newdate] = ['order_num' => 0, 'order_money' => 0, 'date' => $newdate];
  110. }
  111. if (empty($user_ids)) return $this->success('操作成功', array_values($data));
  112. $params = [
  113. 'where' => [['=', 'mall_id', $this->mall_id], ['in', 'user_id', $user_ids], ['>', 'created_at', $start_time], ['<=', 'created_at', $end_time]],
  114. 'select' => ['order_num', 'order_money', 'created_at'],
  115. 'order' => 'created_at asc',
  116. ];
  117. $list = BusinessCardStatistics::lists($params);
  118. if (!empty($list)) {
  119. array_walk($list, function($val) use(&$data) {
  120. $date = date('m-d', $val['created_at']);
  121. if (isset($data[$date])) {
  122. $data[$date]['order_money'] += $val['order_money'];
  123. $data[$date]['order_num'] += $val['order_num'];
  124. }
  125. });
  126. }
  127. return $this->success('操作成功', array_values($data));
  128. }
  129. }
  130. /**
  131. * 新增客户数
  132. * @author hpei
  133. * @param int date
  134. * @return array
  135. */
  136. public function actionCustomerNumber() {
  137. if (isset(Yii::$app->request->isGet)) {
  138. $date = Yii::$app->request->get('date', 7);
  139. $start_time = strtotime("-$date day 0:0:0");
  140. $end_time = $start_time + ($date - 1) * 86400 + 86399;
  141. $params = [
  142. 'where' => [['in', 'department_id', $this->department_id], ['=', 'mall_id', $this->mall_id], ['>', 'created_at', $start_time], ['<=', 'created_at', $end_time]],
  143. 'select' => ['user_id', 'created_at'],
  144. 'ordre' => 'created_at asc',
  145. ];
  146. //初始化
  147. $data = [];
  148. for ($i = 0; $i <= $date -1; $i++) {
  149. $newdate = date('m-d', $start_time + ($i * 86400));
  150. $data[$newdate] = ['num' => 0, 'date' => $newdate];
  151. }
  152. $list = BusinessCardUser::lists($params);
  153. if (!empty($list)) {
  154. array_walk($list, function($val) use(&$data) {
  155. $date = date('m-d', $val['created_at']);
  156. if (isset($data[$date])) $data[$date]['num'] += 1;
  157. });
  158. }
  159. return $this->success('操作成功', array_values($data));
  160. }
  161. }
  162. /**
  163. * 跟进客户
  164. * @author hpei
  165. * @param int date
  166. * @return array
  167. */
  168. public function actionFollowCustomer() {
  169. if (Yii::$app->request->isGet) {
  170. $date = Yii::$app->request->get('date', 7);
  171. $start_time = strtotime("-$date day 0:0:0");
  172. $end_time = $start_time + ($date - 1) * 86400 + 86399;
  173. $params = [
  174. '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]],
  175. 'select' => ['user_id', 'created_at'],
  176. 'ordre' => 'created_at asc',
  177. ];
  178. $list = BusinessCardUser::lists($params);
  179. //初始化
  180. $data = [];
  181. for ($i = 0; $i <= $date -1; $i++) {
  182. $newdate = date('m-d', $start_time + ($i * 86400));
  183. $data[$newdate] = ['num' => 0, 'date' => $newdate];
  184. }
  185. if (!empty($list)) {
  186. array_walk($list, function($val) use(&$data) {
  187. $date = date('m-d', $val['created_at']);
  188. if (isset($data[$date])) $data[$date]['num'] += 1;
  189. });
  190. }
  191. return $this->success('操作成功', array_values($data));
  192. }
  193. }
  194. /**
  195. * 客户兴趣
  196. * @author hpei
  197. * @return array company:公司 product:产品 my:我的
  198. */
  199. public function actionCustomerInterest() {
  200. if (Yii::$app->request->isGet) {
  201. $user_ids = BusinessCardUser::getDepartmentUid($this->department_id);
  202. if (empty($user_ids)) return $this->success('操作成功', ['company' => 0, 'product' => 0, 'my' => 0]);
  203. $total = BusinessCardCustomerLog::count(['where' => [['in', 'user_id', $user_ids], ['in', 'log_type', array_values(array_keys(CardEnums::getLogTypeMap()))]]]);
  204. if ($total == 0) return $this->success('操作成功', ['company' => 0, 'product' => 0, 'my' => 0]);
  205. //对公司感兴趣=查看首页,对产品感兴趣=查看商品,我的=查看名片
  206. $params = [
  207. 'where' => [['in', 'user_id', $user_ids], ['in', 'log_type', [CardEnums::LOG_TYPE_INDEX, CardEnums::LOG_TYPE_GOODS, CardEnums::LOG_TYPE_CARD]]],
  208. 'group' => 'log_type',
  209. 'index' => 'log_type',
  210. 'select' => ['count(*) count', 'log_type']
  211. ];
  212. $list = BusinessCardCustomerLog::lists($params);
  213. $data['company'] = isset($list[CardEnums::LOG_TYPE_INDEX]) ? number_format($list[CardEnums::LOG_TYPE_INDEX]['count'] / $total, 2) : 0;
  214. $data['product'] = isset($list[CardEnums::LOG_TYPE_GOODS]) ? number_format($list[CardEnums::LOG_TYPE_GOODS]['count'] / $total, 2) : 0;
  215. $data['my'] = isset($list[CardEnums::LOG_TYPE_CARD]) ? number_format($list[CardEnums::LOG_TYPE_CARD]['count'] / $total, 2) : 0;
  216. return $this->success('操作成功', $data);
  217. }
  218. }
  219. /**
  220. * 用户活跃度
  221. * @author hpei
  222. * @param int date
  223. * @return array
  224. */
  225. public function actionCustomerActive() {
  226. if (Yii::$app->request->isGet) {
  227. $date = Yii::$app->request->get('date', 7);
  228. $start_time = strtotime("-$date day 0:0:0");
  229. $end_time = $start_time + ($date - 1) * 86400 + 86399;
  230. $user_ids = BusinessCardUser::getDepartmentUid($this->department_id);
  231. $data = [];
  232. for ($i = 0; $i <= $date -1; $i++) {
  233. $newdate = date('m-d', $start_time + ($i * 86400));
  234. $data[$newdate] = ['num' => 0, 'date' => $newdate];
  235. }
  236. if (empty($user_ids)) return $this->success('操作成功', $data);
  237. $params = [
  238. 'where' => [['in', 'user_id', $user_ids], ['in', 'log_type', array_values(array_keys(CardEnums::getLogTypeMap()))], ['>', 'created_at', $start_time], ['<=', 'created_at', $end_time]],
  239. 'select' => ['user_id', 'created_at']
  240. ];
  241. $list = BusinessCardCustomerLog::lists($params);
  242. if (!empty($list)) {
  243. array_walk($list, function($val) use(&$data) {
  244. $date = date('m-d', $val['created_at']);
  245. if (isset($data[$date])) $data[$date]['num'] += 1;
  246. });
  247. }
  248. return $this->success('操作成功', array_values($data));
  249. }
  250. }
  251. /**
  252. * 销售排行
  253. * @author hpei
  254. * @param int type 1=客户人数,2=线索数量,3=订单数量,4=交易金额
  255. * @return array
  256. */
  257. public function actionSaleRank() {
  258. if (Yii::$app->request->isGet) {
  259. $type = Yii::$app->request->get('type', 1);
  260. if ($type == 1) {
  261. $params = [
  262. 'where' => [['=', 'mall_id', $this->mall_id], ['in', 'department_id', $this->department_id]],
  263. 'group' => 'parent_id',
  264. 'order' => 'total desc',
  265. 'limit' => 10,
  266. 'select' => ['count(*) total', 'parent_id'],
  267. 'with' => ['parent' => function($query) {
  268. $query->select(['avatar_url', 'nickname', 'id']);
  269. }]
  270. ];
  271. $list = BusinessCardUser::listPage($params);
  272. } else {
  273. switch($type) {
  274. case '2':
  275. $field = 'clue_num';
  276. break;
  277. case '3':
  278. $field = 'order_num';
  279. break;
  280. case '4':
  281. $field = 'order_money';
  282. break;
  283. }
  284. $user_ids = BusinessCardUser::getDepartmentUid($this->department_id);
  285. $params = [
  286. 'where' => [['=', 'mall_id', $this->mall_id], ['in', 'user_id', $user_ids]],
  287. 'group' => 'user_id',
  288. 'order' => 'total desc',
  289. 'limit' => 10,
  290. 'select' => ["sum($field) total", 'user_id'],
  291. 'with' => ['user' => function($query) {
  292. $query->select(['avatar_url', 'nickname', 'id']);
  293. }]
  294. ];
  295. $list = BusinessCardStatistics::listPage($params);
  296. }
  297. if (!empty($list['list'])) {
  298. foreach ($list['list'] as &$val) {
  299. $val['avatar_url'] = isset($val['parent']) ? $val['parent']['avatar_url'] : $val['user']['avatar_url'];
  300. $val['nickname'] = isset($val['parent']) ? $val['parent']['nickname'] : $val['user']['nickname'];
  301. unset($val['parent']);
  302. unset($val['user']);
  303. }
  304. }
  305. return $this->success('操作成功', $list);
  306. }
  307. }
  308. /**
  309. * 热度排行
  310. * @author hpei
  311. * @param int type 1=商品热度,2=名片热度
  312. * @param int date
  313. */
  314. public function actionHotRank() {
  315. if (Yii::$app->request->isGet) {
  316. $get = Yii::$app->request->get();
  317. $res = Yii::$app->services->validate->validate($get, [
  318. [['date', 'type'], 'number'],
  319. [['date', 'type'], 'safe'],
  320. ]);
  321. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  322. $date = Yii::$app->request->get('date', 7);
  323. $type = Yii::$app->request->get('type', 1);
  324. $start_time = strtotime("-$date day 0:0:0");
  325. $end_time = $start_time + ($date - 1) * 86400 + 86399;
  326. $params = [
  327. 'where' => [['>', 'created_at', $start_time], ['<=', 'created_at', $end_time], ['=', 'mall_id', $this->mall_id]],
  328. 'order' => 'total desc',
  329. 'group' => 'item_id',
  330. 'select' => ['count(*) total', 'item_id']
  331. ];
  332. switch($type) {
  333. case '1':
  334. array_push($params['where'], ['=', 'log_type', CardEnums::LOG_TYPE_GOODS]);
  335. $list = BusinessCardCustomerLog::listPage($params);
  336. if (!empty($list['list'])) {
  337. $ids = array_column($list['list'], 'item_id');
  338. $goods_statistics = GoodsStatistics::lists(['where' => [['in', 'goods_id', $ids]], 'group' => 'goods_id',
  339. 'index' => 'goods_id', 'select' => ['goods_id', 'sum(sales_num) sales_num']]);
  340. foreach ($list['list'] as &$val) {
  341. $val['sales_num'] = isset($goods_statistics[$val['item_id']]) ? $goods_statistics[$val['item_id']]['sales_num'] : 0;
  342. }
  343. $item_list = Goods::lists(['where' => [['in', 'id', $ids]], 'select' => ['id', 'goods_name', 'cover_pic', 'price'], 'index' => 'id']);
  344. }
  345. break;
  346. case '2':
  347. array_push($params['where'], ['=', 'log_type', CardEnums::LOG_TYPE_CARD]);
  348. $list = BusinessCardCustomerLog::listPage($params);
  349. if (!empty($list)) {
  350. $ids = array_column($list['list'], 'item_id');
  351. $item_param = [
  352. 'where' =>
  353. [['in', 'id', $ids]],
  354. 'select' => ['id', 'avatar_url', 'nickname', 'position_id'],
  355. 'with' => ['position' => function($qurey){
  356. $qurey->select(['name','id']);
  357. }],
  358. ];
  359. $item_list = BusinessCard::lists($item_param);
  360. if (!empty($item_list)) $item_list = array_column($item_list, null, 'id');
  361. }
  362. break;
  363. }
  364. if (!empty($item_list) && !empty($list)) {
  365. array_walk($list['list'], function(&$val) use ($item_list) {
  366. if (isset($item_list[$val['item_id']])) {
  367. $val = array_merge($val, $item_list[$val['item_id']]);
  368. }
  369. });
  370. }
  371. return $this->success('操作成功', $list);
  372. }
  373. }
  374. }