Customer.php 47KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 商业智能-客户分析
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\bi\controller;
  8. use app\admin\controller\ApiCommon;
  9. use app\bi\logic\BiCustomerLogic;
  10. use app\bi\model\Customer as CustomerModel;
  11. use app\admin\model\User as UserModel;
  12. use app\bi\traits\SortTrait;
  13. use think\Hook;
  14. use think\Request;
  15. use think\Db;
  16. use app\bi\logic\ExcelLogic;
  17. class Customer extends ApiCommon
  18. {
  19. use SortTrait;
  20. /**
  21. * 用于判断权限
  22. * @permission 无限制
  23. * @allow 登录用户可访问
  24. * @other 其他根据系统设置
  25. **/
  26. public function _initialize()
  27. {
  28. $action = [
  29. 'permission' => [''],
  30. 'allow' => [
  31. 'statistics',
  32. 'total',
  33. 'recordtimes',
  34. 'recordlist',
  35. 'recordmode',
  36. 'conversion',
  37. 'conversioninfo',
  38. 'pool',
  39. 'poollist',
  40. 'usercycle',
  41. 'usercyclelist',
  42. 'productcycle',
  43. 'addresscycle',
  44. 'addressanalyse',
  45. 'portrait',
  46. 'customersatisfaction',
  47. 'productsatisfaction',
  48. 'excelexport'
  49. ]
  50. ];
  51. Hook::listen('check_auth', $action);
  52. $request = Request::instance();
  53. $a = strtolower($request->action());
  54. if (!in_array($a, $action['permission'])) {
  55. parent::_initialize();
  56. }
  57. // if (!checkPerByAction('bi', 'customer', 'read')) {
  58. // header('Content-Type:application/json; charset=utf-8');
  59. // exit(json_encode(['code' => 102, 'error' => '无权操作']));
  60. // }
  61. }
  62. /**
  63. * 员工客户分析
  64. * @param
  65. * @return
  66. * @author Michael_xu
  67. */
  68. public function statistics($param='')
  69. {
  70. $customerModel = new \app\crm\model\Customer();
  71. if($param['excel_type']!=1){
  72. $param = $this->param;
  73. }
  74. # 排序参数
  75. $sortField = !empty($param['sort_field']) ? $param['sort_field'] : '';
  76. $sortValue = !empty($param['sort_value']) ? $param['sort_value'] : '';
  77. unset($param['sort_field']);
  78. unset($param['sort_value']);
  79. if ($param['type']) {
  80. $timeArr = getTimeByType($param['type']);
  81. $param['start_time'] = $timeArr[0];
  82. $param['end_time'] = $timeArr[1];
  83. } else {
  84. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  85. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  86. }
  87. $data = $customerModel->getStatistics($param);
  88. # 排序
  89. if (!empty($data['list'])) $data['list'] = $this->sortCommon($data['list'], $sortField, $sortValue);
  90. //导出使用
  91. if (!empty($param['excel_type'])) return $data;
  92. return resultArray(['data' => $data]);
  93. }
  94. /**
  95. * 员工客户总量分析
  96. * @param
  97. * @return
  98. * @author zhi
  99. */
  100. public function total()
  101. {
  102. $customerModel = new \app\crm\model\Customer();
  103. $userModel = new \app\admin\model\User();
  104. $adminModel = new \app\admin\model\Admin();
  105. $param = $this->param;
  106. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  107. $whereArr = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  108. $userIds = $whereArr['userIds'];
  109. # 处理无员工的情况
  110. if (empty($userIds)) return resultArray(['data' => []]);
  111. if (empty($param['type']) && empty($param['start_time'])) {
  112. $param['type'] = 'month';
  113. }
  114. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  115. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  116. $time = getTimeArray($param['start_time'], $param['end_time']);
  117. $where = [
  118. 'create_user_id' => implode(',', $userIds),
  119. 'deal_status' => '已成交'
  120. ];
  121. $sql = [];
  122. foreach ($time['list'] as $val) {
  123. $whereArr = $where;
  124. $whereArr['type'] = $val['type'];
  125. $whereArr['start_time'] = $val['start_time'];
  126. $whereArr['end_time'] = $val['end_time'];
  127. $sql[] = $customerModel->getAddDealSql($whereArr);
  128. }
  129. $sql = implode(' UNION ALL ', $sql);
  130. $list = queryCache($sql);
  131. return resultArray(['data' => $list]);
  132. }
  133. /**
  134. * 员工客户跟进次数分析
  135. *
  136. * @return \think\response\Json
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. * @throws \think\exception\DbException
  140. */
  141. public function recordTimes()
  142. {
  143. $userModel = new \app\admin\model\User();
  144. $adminModel = new \app\admin\model\Admin();
  145. $param = $this->param;
  146. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  147. $whereArr = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  148. $userIds = $whereArr['userIds'];
  149. if (empty($userIds)) return resultArray(['data' => []]);
  150. if (empty($param['type']) && empty($param['start_time'])) {
  151. $param['type'] = 'month';
  152. }
  153. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  154. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  155. $time = getTimeArray($param['start_time'], $param['end_time']);
  156. $recordWhere['type'] = 1;
  157. $recordWhere['activity_type'] = 2;
  158. $recordWhere['create_user_id'] = ['in', $userIds];
  159. $recordWhere['create_time'] = ['between', [$time['between'][0], $time['between'][1]]];
  160. $sql = db('crm_activity')->
  161. field([
  162. "FROM_UNIXTIME(`create_time`, '".$time['time_format']."') AS `type`",
  163. 'COUNT(DISTINCT(`activity_type_id`)) AS `customerCount`',
  164. 'COUNT(*) AS `dataCount`'
  165. ])
  166. ->where($recordWhere)
  167. ->group('type')
  168. ->select();
  169. $res = array_column((array)$sql, null, 'type');
  170. foreach ($time['list'] as &$val) {
  171. $val['customerCount'] = (int)$res[$val['type']]['customerCount'];
  172. $val['dataCount'] = (int)$res[$val['type']]['dataCount'];
  173. }
  174. return resultArray(['data' => $time['list']]);
  175. }
  176. /**
  177. * 员工客户跟进次数分析 具体员工列表
  178. *
  179. * @param string $param
  180. * @return array|\think\response\Json
  181. * @throws \think\db\exception\DataNotFoundException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. * @throws \think\exception\DbException
  184. */
  185. public function recordList($param='')
  186. {
  187. $userModel = new \app\admin\model\User();
  188. $adminModel = new \app\admin\model\Admin();
  189. if($param['excel_type']!=1){
  190. $param = $this->param;
  191. }
  192. # 排序参数
  193. $sortField = !empty($param['sort_field']) ? $param['sort_field'] : '';
  194. $sortValue = !empty($param['sort_value']) ? $param['sort_value'] : '';
  195. unset($param['sort_field']);
  196. unset($param['sort_value']);
  197. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  198. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  199. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  200. $whereArr = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  201. $userIds = $whereArr['userIds'];
  202. if (empty($userIds)) return resultArray(['data' => []]);
  203. # 员工列表
  204. $userData = [];
  205. $userList = db('admin_user')->field(['id', 'realname'])->whereIn('id', $userIds)->select();
  206. foreach ($userList AS $key => $value) {
  207. $userData[$value['id']] = $value['realname'];
  208. }
  209. # 跟进记录列表
  210. $recordData = [];
  211. $recordWhere['type'] = 1;
  212. $recordWhere['activity_type'] = 2;
  213. $recordWhere['create_user_id'] = ['in', $userIds];
  214. $recordWhere['create_time'] = ['between', [$whereArr['between_time'][0], $whereArr['between_time'][1]]];
  215. $recordList = db('crm_activity')->field(['count(*) as count', 'create_user_id', 'activity_type_id'])->where($recordWhere)
  216. ->group('create_user_id, activity_type_id')->select();
  217. # 跟进列表
  218. foreach ($recordList AS $key => $value) {
  219. if (empty($recordData[$value['create_user_id']]['realname'])) {
  220. $recordData[$value['create_user_id']]['realname'] = $userData[$value['create_user_id']];
  221. $recordData[$value['create_user_id']]['record_num'] = $value['count'];
  222. $recordData[$value['create_user_id']]['customer_num'] = 1;
  223. } else {
  224. $recordData[$value['create_user_id']]['record_num'] = $recordData[$value['create_user_id']]['record_num'] + $value['count'];
  225. $recordData[$value['create_user_id']]['customer_num'] = $recordData[$value['create_user_id']]['customer_num'] + 1;
  226. }
  227. if (!empty($userData[$value['create_user_id']])) unset($userData[(int)$value['create_user_id']]);
  228. }
  229. # 跟进客户总数
  230. $customerCount = db('crm_activity')->where($recordWhere)->group('activity_type_id')->count();
  231. # 没有跟进的员工设置默认值
  232. foreach ($userData AS $key => $value) {
  233. $recordData[$key]['realname'] = $value;
  234. $recordData[$key]['record_num'] = 0;
  235. $recordData[$key]['customer_num'] = 0;
  236. }
  237. $result = ['list' => array_values($recordData), 'total' => ['realname' => '总计', 'customer_num' => $customerCount]];
  238. # 排序
  239. if (!empty($result['list'])) $result['list'] = $this->sortCommon($result['list'], $sortField, $sortValue);
  240. //导出使用
  241. if (!empty($param['excel_type'])) return $recordData;
  242. return resultArray(['data' => $result]);
  243. }
  244. /**
  245. * 员工跟进方式分析
  246. *
  247. * @param string $param
  248. * @return mixed|\think\response\Json
  249. * @throws \think\db\exception\DataNotFoundException
  250. * @throws \think\db\exception\ModelNotFoundException
  251. * @throws \think\exception\DbException
  252. */
  253. public function recordMode($param='')
  254. {
  255. $biCustomerModel = new \app\bi\model\Customer();
  256. if($param['excel_type']!=1){
  257. $param = $this->param;
  258. }
  259. $userId = $this->userInfo['id'];
  260. # 处理无部门无员工的情况
  261. $groupType = !empty($param['group_type']) ? $param['group_type'] : 1; # 组类型:1部门;2员工
  262. if (empty($param['user_id']) && empty($param['structure_id'])) {
  263. if ($groupType == 1) $param['structure_id'] = Db::name('admin_user')->where('id', $userId)->value('structure_id');
  264. if ($groupType == 2) $param['user_id'] = $userId;
  265. }
  266. unset($param['group_type']);
  267. # 判断部门下是否有员工
  268. if (!empty($param['structure_id'])) {
  269. $userModel = new \app\admin\model\User();
  270. $userIds = $userModel->getSubUserByStr($param['structure_id'], 2);
  271. if (empty($userIds)) return resultArray(['data' => []]);
  272. }
  273. # 排序参数
  274. $sortField = !empty($param['sort_field']) ? $param['sort_field'] : '';
  275. $sortValue = !empty($param['sort_value']) ? $param['sort_value'] : '';
  276. unset($param['sort_field']);
  277. unset($param['sort_value']);
  278. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  279. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  280. $whereArr = $biCustomerModel->getParamByWhere($param, 'record');
  281. //跟进类型
  282. $record_type = db('crm_config')->where(['name' => 'record_type'])->find();
  283. if ($record_type) {
  284. $record_categorys = json_decode($record_type['value']);
  285. } else {
  286. $record_categorys = array('打电话', '发邮件', '发短信', '见面拜访', '活动');
  287. }
  288. $sql = db('crm_activity')
  289. ->field([
  290. 'category',
  291. 'COUNT(*)' => 'count'
  292. ])
  293. ->where([
  294. 'create_time' => $whereArr['create_time'],
  295. 'create_user_id' => $whereArr['create_user_id'],
  296. 'type' => 1,
  297. 'activity_type' => 2
  298. ])
  299. ->group('category')
  300. ->fetchSql()
  301. ->select();
  302. $list = queryCache($sql);
  303. $list = array_column($list, null, 'category');
  304. $sum = array_sum(array_column($list, 'count'));
  305. $res = [];
  306. $recordCount = 0; # 跟进类型总数
  307. foreach ($record_categorys as $val) {
  308. $item['category'] = $val;
  309. if ($sum) {
  310. $item['recordNum'] = (int)$list[$val]['count'];
  311. $item['proportion'] = round(($item['recordNum'] / $sum * 100), 4);
  312. } else {
  313. $item['recordNum'] = $item['proportion'] = 0;
  314. }
  315. $res[] = $item;
  316. $recordCount += $item['recordNum'];
  317. }
  318. $result = [
  319. 'list' => $res,
  320. 'total' => [
  321. 'category' => '合计',
  322. 'recordNum' => $recordCount,
  323. 'proportion' => !empty($recordCount) ? 100 : 0
  324. ]
  325. ];
  326. # 排序
  327. if (!empty($result['list'])) $result['list'] = $this->sortCommon($result['list'], $sortField, $sortValue);
  328. //导出使用
  329. if (!empty($param['excel_type'])) return $result['list'];
  330. return resultArray(['data' => $result]);
  331. }
  332. /**
  333. * 客户转化率分析
  334. *
  335. * @param
  336. * @return
  337. * @author zhi
  338. */
  339. public function conversion()
  340. {
  341. $customerModel = new \app\crm\model\Customer();
  342. $userModel = new \app\admin\model\User();
  343. $adminModel = new \app\admin\model\Admin();
  344. $param = $this->param;
  345. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  346. $whereArr = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  347. if (empty($whereArr['userIds'])) resultArray(['data' => []]);
  348. $userIds = $whereArr['userIds'];
  349. $user_ids = implode(',',$userIds);
  350. if (empty($param['type']) && empty($param['start_time'])) {
  351. $param['type'] = 'month';
  352. }
  353. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  354. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  355. $time = getTimeArray($param['start_time'], $param['end_time']);
  356. $sql = [];
  357. foreach ($time['list'] as $val) {
  358. $sql[] = $customerModel->getAddDealSql([
  359. 'create_user_id' => $user_ids ? $user_ids : '9999999999',
  360. 'type' => $val['type'],
  361. 'start_time' => $val['start_time'],
  362. 'end_time' => $val['end_time'],
  363. 'deal_status' => '已成交',
  364. ]);
  365. }
  366. $sql = implode(' UNION ALL ', $sql);
  367. $list = queryCache($sql);
  368. foreach ($list as &$val) {
  369. $val['proportion'] = $val['customer_num'] ? number_format($val['deal_customer_num'] / $val['customer_num'] * 100, 2) : 0;
  370. }
  371. return resultArray(['data' => $list]);
  372. }
  373. /**
  374. * 客户转化率分析具体数据
  375. *
  376. * @return \think\response\Json
  377. * @throws \think\Exception
  378. * @throws \think\db\exception\DataNotFoundException
  379. * @throws \think\db\exception\ModelNotFoundException
  380. * @throws \think\exception\DbException
  381. */
  382. public function conversionInfo()
  383. {
  384. $customerModel = new \app\bi\model\Customer();
  385. $userModel = new \app\admin\model\User();
  386. $param = $this->param;
  387. $userIds = [];
  388. $userId = $this->userInfo['id'];
  389. # 处理无部门无员工的情况
  390. $groupType = !empty($param['group_type']) ? $param['group_type'] : 1; # 组类型:1部门;2员工
  391. if (empty($param['user_id']) && empty($param['structure_id'])) {
  392. if ($groupType == 1) $param['structure_id'] = Db::name('admin_user')->where('id', $userId)->value('structure_id');
  393. if ($groupType == 2) $param['user_id'] = $userId;
  394. }
  395. unset($param['group_type']);
  396. # 如果存在员工参数,则不再使用部门参数
  397. if (!empty($param['user_id'])) {
  398. $userIds = [$param['user_id']];
  399. } elseif (!empty($param['structure_id'])) {
  400. $userIds = $userModel->getSubUserByStr($param['structure_id'], 2);
  401. }
  402. if (empty($userIds)) return resultArray(['data' => []]);
  403. # 排序参数
  404. $sortField = !empty($param['sort_field']) ? $param['sort_field'] : '';
  405. $sortValue = !empty($param['sort_value']) ? $param['sort_value'] : '';
  406. unset($param['sort_field']);
  407. unset($param['sort_value']);
  408. # 日期条件
  409. if (!empty($param['type'])) {
  410. $param['start_time'] = strtotime($param['type'] . '-01 00:00:00');
  411. $endMonth = strtotime(date('Y-m-d', $param['start_time']) . " +1 month -1 day");
  412. $param['end_time'] = strtotime(date('Y-m-d 23:59:59', $endMonth));
  413. unset($param['type']);
  414. }
  415. $whereArr = $customerModel->getParamByWhere($param);
  416. $whereArr['deal_status'] = '已成交';
  417. $list = $customerModel->getWhereByList($whereArr, $sortField, $sortValue);
  418. return resultArray(['data' => $list]);
  419. }
  420. /**
  421. * 公海客户分析
  422. *
  423. * @return \think\response\Json
  424. * @throws \think\db\exception\DataNotFoundException
  425. * @throws \think\db\exception\ModelNotFoundException
  426. * @throws \think\exception\DbException
  427. */
  428. public function pool()
  429. {
  430. $actionRecordModel = new \app\bi\model\ActionRecord();
  431. $userModel = new \app\admin\model\User();
  432. $adminModel = new \app\admin\model\Admin();
  433. $param = $this->param;
  434. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  435. $whereArr = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  436. $userIds = $whereArr['userIds'];
  437. if (empty($param['type']) && empty($param['start_time'])) {
  438. $param['type'] = 'month';
  439. }
  440. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  441. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  442. $time = getTimeArray($param['start_time'], $param['end_time']);
  443. $sql = $actionRecordModel
  444. ->field([
  445. "FROM_UNIXTIME(`create_time`, '{$time['time_format']}')" => 'type',
  446. 'SUM(CASE WHEN `content` = "将客户放入公海" THEN 1 ELSE 0 END)' => 'put_in',
  447. 'SUM(CASE WHEN `content` = "领取了客户" THEN 1 ELSE 0 END)' => 'receive'
  448. ])
  449. ->where([
  450. 'user_id' => ['IN', !empty($userIds) ? $userIds : '9999999999'],
  451. 'create_time' => ['BETWEEN', $time['between']],
  452. 'content' => ['IN', ['将客户放入公海', '领取了客户']]
  453. ])
  454. ->group('type')
  455. ->fetchSql()
  456. ->select();
  457. $res = queryCache($sql);
  458. $res = array_column($res, null, 'type');
  459. foreach ($time['list'] as &$val) {
  460. $val['put_in'] = (int)$res[$val['type']]['put_in'];
  461. $val['receive'] = (int)$res[$val['type']]['receive'];
  462. }
  463. return resultArray(['data' => $time['list']]);
  464. }
  465. /**
  466. * 公海客户分析 具体列表
  467. *
  468. * @param string $param
  469. * @return mixed|\think\response\Json
  470. * @throws \think\db\exception\DataNotFoundException
  471. * @throws \think\db\exception\ModelNotFoundException
  472. * @throws \think\exception\DbException
  473. */
  474. public function poolList($param='')
  475. {
  476. $userModel = new \app\admin\model\User();
  477. $actionRecordModel = new \app\bi\model\ActionRecord();
  478. $adminModel = new \app\admin\model\Admin();
  479. if($param['excel_type']!=1){
  480. $param = $this->param;
  481. }
  482. # 排序参数
  483. $sortField = !empty($param['sort_field']) ? $param['sort_field'] : '';
  484. $sortValue = !empty($param['sort_value']) ? $param['sort_value'] : '';
  485. unset($param['sort_field']);
  486. unset($param['sort_value']);
  487. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  488. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  489. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  490. $whereArr = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  491. $userIds = $whereArr['userIds'];
  492. $between_time = $whereArr['between_time'];
  493. $sql = CustomerModel::field([
  494. 'COUNT(*)' => 'customer_num',
  495. 'owner_user_id'
  496. ])
  497. ->where([
  498. 'create_time' => ['BETWEEN', $between_time],
  499. 'owner_user_id' => ['IN', !empty($userIds) ? $userIds : '9999999999']
  500. ])
  501. ->group('owner_user_id')
  502. ->fetchSql()
  503. ->select();
  504. $customer_num_list = queryCache($sql);
  505. $customer_num_list = array_column($customer_num_list, null, 'owner_user_id');
  506. $sql = $actionRecordModel
  507. ->field([
  508. 'user_id',
  509. 'SUM(CASE WHEN `content` = "将客户放入公海" THEN 1 ELSE 0 END)' => 'put_in',
  510. 'SUM(CASE WHEN `content` = "领取了客户" THEN 1 ELSE 0 END)' => 'receive'
  511. ])
  512. ->group('user_id')
  513. ->where([
  514. 'create_time' => ['BETWEEN', $between_time],
  515. 'user_id' => ['IN', $userIds],
  516. 'content' => ['IN', ['将客户放入公海', '领取了客户']],
  517. 'types' => 'crm_customer',
  518. ])
  519. ->fetchSql()
  520. ->select();
  521. $action_record_list = queryCache($sql);
  522. $action_record_list = array_column($action_record_list, null, 'user_id');
  523. $res = [];
  524. $receiveCount = 0; # 领取公海客户总数
  525. $putInCount = 0; # 进入公海客户总数
  526. foreach ($userIds as $val) {
  527. $item['put_in'] = !empty($action_record_list[$val]['put_in']) ? (int)$action_record_list[$val]['put_in'] : 0;
  528. $item['receive'] = !empty($action_record_list[$val]['receive']) ? (int)$action_record_list[$val]['receive'] : 0;
  529. $item['customer_num'] = !empty($customer_num_list[$val]['customer_num']) ? (int)$customer_num_list[$val]['customer_num'] : 0;
  530. $user_info = $userModel->getUserById($val);
  531. $item['realname'] = $user_info['realname'];
  532. $item['username'] = $user_info['structure_name'];
  533. $res[] = $item;
  534. $receiveCount += $item['receive'];
  535. $putInCount += $item['put_in'];
  536. }
  537. $result = ['list' => $res, 'total' => ['realname' => '总计', 'receive' => $receiveCount, 'put_in' => $putInCount]];
  538. # 排序
  539. if (!empty($result['list'])) $result['list'] = $this->sortCommon($result['list'], $sortField, $sortValue);
  540. //导出使用
  541. if (!empty($param['excel_type'])) return $result['list'];
  542. return resultArray(['data' => $result]);
  543. }
  544. /**
  545. * 员工客户成交周期
  546. *
  547. * @param string $param
  548. * @return \think\response\Json
  549. * @throws \think\db\exception\DataNotFoundException
  550. * @throws \think\db\exception\ModelNotFoundException
  551. * @throws \think\exception\DbException
  552. */
  553. public function userCycle($param='')
  554. {
  555. $userModel = new \app\admin\model\User();
  556. $adminModel = new \app\admin\model\Admin();
  557. if($param['excel_type']!=1){
  558. $param = $this->param;
  559. }
  560. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  561. $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  562. $userIds = $whereData['userIds'];
  563. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  564. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  565. $time = getTimeArray($param['start_time'], $param['end_time']);
  566. $prefix = config('database.prefix');
  567. $sql = CustomerModel::alias('a')
  568. ->field([
  569. "FROM_UNIXTIME(`a`.`create_time`, '{$time['time_format']}')" => 'type',
  570. 'COUNT(*)' => 'customer_num',
  571. 'SUM(
  572. CASE WHEN ISNULL(`b`.`order_date`) THEN 0 ELSE (
  573. UNIX_TIMESTAMP(`b`.`order_date`) - `a`.`create_time`
  574. ) / 86400 END
  575. )' => 'cycle_sum'
  576. ])
  577. ->join(
  578. "(
  579. SELECT
  580. `customer_id`, MIN(`order_date`) AS `order_date`
  581. FROM
  582. `{$prefix}crm_contract`
  583. WHERE
  584. `check_status` = 2
  585. GROUP BY
  586. `customer_id`
  587. ) b",
  588. '`a`.`customer_id` = `b`.`customer_id`',
  589. 'LEFT'
  590. )
  591. ->where([
  592. 'a.deal_status' => '已成交',
  593. 'a.create_time' => ['BETWEEN', $time['between']],
  594. 'a.owner_user_id' => ['IN', !empty($userIds) ? $userIds : '9999999999']
  595. ])
  596. ->group('type')
  597. ->fetchsql()
  598. ->select();
  599. $res = queryCache($sql);
  600. $res = array_column($res, null, 'type');
  601. foreach ($time['list'] as &$val) {
  602. $val['customer_num'] = (int)$res[$val['type']]['customer_num'];
  603. if ($res[$val['type']]['customer_num']) {
  604. $val['cycle'] = intval($res[$val['type']]['cycle_sum'] / $res[$val['type']]['customer_num']);
  605. } else {
  606. $val['cycle'] = 0;
  607. }
  608. }
  609. $datas = $time['list'];
  610. return resultArray(['data' => $datas]);
  611. }
  612. /**
  613. * 成交周期列表
  614. *
  615. * @return \think\response\Json
  616. * @throws \think\db\exception\DataNotFoundException
  617. * @throws \think\db\exception\ModelNotFoundException
  618. * @throws \think\exception\DbException
  619. */
  620. public function userCycleList()
  621. {
  622. $userModel = new \app\admin\model\User();
  623. $adminModel = new \app\admin\model\Admin();
  624. $param = $this->param;
  625. # 排序参数
  626. $sortField = !empty($param['sort_field']) ? $param['sort_field'] : '';
  627. $sortValue = !empty($param['sort_value']) ? $param['sort_value'] : '';
  628. unset($param['sort_field']);
  629. unset($param['sort_value']);
  630. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  631. $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  632. $userIds = $whereData['userIds'];
  633. if (empty($param['type']) && empty($param['start_time'])) {
  634. $param['type'] = 'month';
  635. }
  636. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  637. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  638. $time = getTimeArray($param['start_time'], $param['end_time']);
  639. $prefix = config('database.prefix');
  640. $sql = CustomerModel::alias('a')
  641. ->field([
  642. 'a.owner_user_id',
  643. 'COUNT(*)' => 'customer_num',
  644. 'SUM(
  645. CASE WHEN ISNULL(b.order_date) THEN 0 ELSE (
  646. UNIX_TIMESTAMP(b.order_date) - a.create_time
  647. ) / 86400 END
  648. )' => 'cycle_sum'
  649. ])
  650. ->join(
  651. "(
  652. SELECT
  653. `customer_id`,
  654. MIN(`order_date`) AS `order_date`
  655. FROM
  656. `{$prefix}crm_contract`
  657. WHERE
  658. `check_status` = 2
  659. GROUP BY
  660. `customer_id`
  661. ) b",
  662. 'a.customer_id = b.customer_id',
  663. 'LEFT'
  664. )
  665. ->where([
  666. 'a.deal_status' => '已成交',
  667. 'a.create_time' => ['BETWEEN', $time['between']],
  668. 'a.owner_user_id' => ['IN', !empty($userIds) ? $userIds : '9999999999']
  669. ])
  670. ->group('a.owner_user_id')
  671. ->fetchSql()
  672. ->select();
  673. $res = queryCache($sql);
  674. $res = array_column($res, null, 'owner_user_id');
  675. $user_data = [];
  676. $customerCount = 0; # 成交客户总数
  677. $cycleCount = 0; # 成交周期总数
  678. foreach ($userIds as $value) {
  679. $item['customer_num'] = !empty($res[$value]['customer_num']) ? $res[$value]['customer_num'] : 0;
  680. $item['cycle'] = $res[$value]['customer_num'] ? intval($res[$value]['cycle_sum'] / $res[$value]['customer_num']) : 0;
  681. $item['realname'] = $userModel->getUserById($value)['realname'];
  682. $user_data[] = $item;
  683. $customerCount += $item['customer_num'];
  684. $cycleCount += $item['cycle'];
  685. }
  686. $datas['list'] = $user_data;
  687. $datas['total'] = [
  688. 'realname' => '总计',
  689. 'customer_num' => $customerCount,
  690. 'cycle' => $cycleCount
  691. ];
  692. # 排序
  693. if (!empty($datas['users'])) $datas['users'] = $this->sortCommon($datas['users'], $sortField, $sortValue);
  694. //导出使用
  695. if (!empty($param['excel_type'])) return $datas['list'];
  696. return resultArray(['data' => $datas]);
  697. }
  698. /**
  699. * 产品成交周期
  700. * @param
  701. * @return
  702. * @author zhi
  703. */
  704. public function productCycle()
  705. {
  706. $biCustomerModel = new \app\bi\model\Customer();
  707. $productModel = new \app\bi\model\Product();
  708. $param = $this->param;
  709. $list = $productModel->getDealByProduct($param);
  710. $datas = array();
  711. $cycleCount = 0;
  712. $customerCount = 0;
  713. foreach ($list as $key => $value) {
  714. $item = array();
  715. //周期
  716. $customer_ids = $productModel->getCycleByProduct($param, $value['product_id']);
  717. $whereArr = array();
  718. $whereArr['customer_id'] = array('in', $customer_ids);
  719. $cycle = $biCustomerModel->getWhereByCycle($whereArr);
  720. $item['product_name'] = $value['product_name'];
  721. $item['customer_num'] = !empty($value['num']) ? (int)$value['num'] : 0;
  722. $item['cycle'] = !empty($cycle) ? (int)$cycle : 0;
  723. $datas['list'][] = $item;
  724. $cycleCount += $item['cycle'];
  725. $customerCount += $item['customer_num'];
  726. }
  727. $datas['total'] = ['product_name' => '总计', 'cycle' => $cycleCount, 'customer_num' => $customerCount];
  728. return resultArray(['data' => $datas]);
  729. }
  730. /**
  731. * 地区成交周期
  732. * @param
  733. * @return
  734. * @author zhi
  735. */
  736. public function addressCycle()
  737. {
  738. $userModel = new \app\admin\model\User();
  739. $customerModel = new \app\crm\model\Customer();
  740. $biCustomerModel = new \app\bi\model\Customer();
  741. $address_arr = \app\crm\model\Customer::$address;
  742. $param = $this->param;
  743. if (empty($param['type']) && empty($param['start_time'])) {
  744. $param['type'] = 'month';
  745. }
  746. $map_user_ids = [];
  747. if ($param['user_id']) {
  748. $map_user_ids = array($param['user_id']);
  749. } else {
  750. if ($param['structure_id']) {
  751. $map_user_ids = $userModel->getSubUserByStr($param['structure_id'], 2);
  752. }
  753. }
  754. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  755. $userIds = $map_user_ids ? array_intersect($map_user_ids, $perUserIds) : $perUserIds; //数组交集
  756. $time = getTimeArray();
  757. $prefix = config('database.prefix');
  758. $sql = CustomerModel::alias('a')
  759. ->field([
  760. 'SUBSTR(`a`.`address`, 1, 2)' => 'addr',
  761. 'COUNT(*)' => 'customer_num',
  762. 'SUM(
  763. CASE WHEN ISNULL(b.order_date) THEN 0 ELSE (
  764. UNIX_TIMESTAMP(b.order_date) - a.create_time
  765. ) / 86400 END
  766. )' => 'cycle_sum'
  767. ])
  768. ->join(
  769. "(
  770. SELECT
  771. `customer_id`,
  772. MIN(`order_date`) AS `order_date`
  773. FROM
  774. `{$prefix}crm_contract`
  775. WHERE
  776. `check_status` = 2
  777. GROUP BY
  778. `customer_id`
  779. ) b",
  780. 'a.customer_id = b.customer_id',
  781. 'LEFT'
  782. )
  783. ->where([
  784. 'a.deal_status' => '已成交',
  785. 'a.create_time' => ['BETWEEN', $time['between']],
  786. 'a.owner_user_id' => ['IN', $userIds]
  787. ])
  788. ->group('addr')
  789. ->fetchSql()
  790. ->select();
  791. $list = queryCache($sql);
  792. $list = array_column($list, null, 'addr');
  793. $list['黑龙江'] = $list['黑龙'];
  794. $list['内蒙古'] = $list['内蒙'];
  795. $res = [];
  796. $cycleCount = 0;
  797. $customerCount = 0;
  798. foreach ($address_arr as $val) {
  799. $item['address'] = $val;
  800. $item['customer_num'] = !empty($list[$val]['customer_num']) ? $list[$val]['customer_num'] : 0;
  801. $item['cycle'] = $list[$val]['customer_num'] ? intval($list[$val]['cycle_sum'] / $list[$val]['customer_num']) : 0;
  802. $res['list'][] = $item;
  803. $cycleCount += $item['cycle'];
  804. $customerCount += $item['customer_num'];
  805. }
  806. $res['total'] = ['address' => '总计', 'cycle' => $cycleCount, 'customer_num' => $customerCount];
  807. return resultArray(['data' => $res]);
  808. }
  809. /**
  810. * 客户所在城市分析
  811. *
  812. * @return \think\response\Json
  813. * @throws \think\db\exception\DataNotFoundException
  814. * @throws \think\db\exception\ModelNotFoundException
  815. * @throws \think\exception\DbException
  816. */
  817. public function addressAnalyse()
  818. {
  819. $param = $this->param;
  820. // $customerModel = new \app\crm\model\Customer();
  821. $userModel = new \app\admin\model\User();
  822. $address_arr = \app\crm\model\Customer::$address;
  823. $map_user_ids = [];
  824. # 如果存在员工参数,则不再使用部门参数
  825. if (!empty($param['user_id'])) {
  826. $map_user_ids = array($param['user_id']);
  827. } elseif (!empty($param['structure_id'])) {
  828. $map_user_ids = $userModel->getSubUserByStr($param['structure_id'], 2);
  829. }
  830. if (empty($map_user_ids)) return resultArray(['data' => []]);
  831. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  832. $userIds = $map_user_ids ? array_intersect($map_user_ids, $perUserIds) : $perUserIds; //数组交集
  833. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  834. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  835. $time = getTimeArray($param['start_time'], $param['end_time']);
  836. $sql = CustomerModel::alias('a')
  837. ->field([
  838. 'SUBSTR(`address`, 1, 2)' => 'addr',
  839. 'COUNT(*)' => 'allCustomer',
  840. 'SUM(
  841. CASE WHEN `deal_status` = "已成交" THEN 1 ELSE 0 END
  842. )' => 'dealCustomer',
  843. ])
  844. ->where([
  845. 'create_time' => ['BETWEEN', $time['between']],
  846. 'owner_user_id' => ['IN', $userIds]
  847. ])
  848. ->group('addr')
  849. ->fetchSql()
  850. ->select();
  851. $list = queryCache($sql);
  852. $list = array_column($list, null, 'addr');
  853. $list['黑龙江'] = $list['黑龙'];
  854. $list['内蒙古'] = $list['内蒙'];
  855. $data = [];
  856. foreach ($address_arr as $val) {
  857. $item['address'] = $val;
  858. $item['allCustomer'] = !empty($list[$val]['allCustomer']) ? (int)$list[$val]['allCustomer'] : 0;
  859. $item['dealCustomer'] = !empty($list[$val]['dealCustomer']) ? (int)$list[$val]['dealCustomer'] : 0;
  860. $data[] = $item;
  861. }
  862. return resultArray(['data' => $data]);
  863. }
  864. /**
  865. * 客户行业/级别/来源分析
  866. *
  867. * @return \think\response\Json
  868. * @throws \think\db\exception\DataNotFoundException
  869. * @throws \think\db\exception\ModelNotFoundException
  870. * @throws \think\exception\DbException
  871. */
  872. public function portrait()
  873. {
  874. $biCustomerModel = new \app\bi\model\Customer();
  875. $userModel = new \app\admin\model\User();
  876. $adminModel = new \app\admin\model\Admin();
  877. $param = $this->param;
  878. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  879. $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  880. $userIds = $whereData['userIds'];
  881. if (!in_array($param['type_analyse'], ['industry', 'source', 'level'])) {
  882. return resultArray(['error' => '参数错误']);
  883. }
  884. $poolWhere = $this->getWhereByPool();
  885. $poolId = db('crm_customer')->alias('customer')->where($poolWhere)->column('customer_id');
  886. $whereArr = array();
  887. $whereArr['types'] = 'crm_customer';
  888. $whereArr['field'] = $param['type_analyse'];
  889. $setting = $biCustomerModel->getOptionByField($whereArr);
  890. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  891. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  892. $time = getTimeArray($param['start_time'], $param['end_time']);
  893. $sql = CustomerModel::field([
  894. "(
  895. CASE WHEN
  896. `{$param['type_analyse']}` = ''
  897. THEN '(空)'
  898. ELSE {$param['type_analyse']} END
  899. )" => $param['type_analyse'],
  900. 'COUNT(*)' => 'allCustomer',
  901. 'SUM(
  902. CASE WHEN `deal_status` = "已成交" THEN 1 ELSE 0 END
  903. )' => 'dealCustomer',
  904. ])
  905. ->where([
  906. 'create_time' => ['BETWEEN', $time['between']],
  907. 'owner_user_id' => ['IN', !empty($userIds) ? $userIds : '9999999999']
  908. ])
  909. ->whereNotIn('customer_id', $poolId)
  910. ->group($param['type_analyse'])
  911. ->fetchSql()
  912. ->select();
  913. $list = queryCache($sql);
  914. $list = array_column($list, null, $param['type_analyse']);
  915. $other_keys = array_diff(array_keys($list), $setting);
  916. $setting = array_merge($setting, $other_keys);
  917. $data = [];
  918. foreach ($setting as $val) {
  919. $item = [];
  920. $item[$param['type_analyse']] = $val;
  921. $item['allCustomer'] = !empty($list[$val]['allCustomer']) ? (int)$list[$val]['allCustomer'] : 0;
  922. $item['dealCustomer'] = !empty($list[$val]['dealCustomer']) ? (int)$list[$val]['dealCustomer'] : 0;
  923. $data[] = $item;
  924. }
  925. return resultArray(['data' => $data]);
  926. }
  927. /**
  928. * [客户公海条件]
  929. * @author Michael_xu
  930. * @param
  931. * @return
  932. */
  933. public function getWhereByPool()
  934. {
  935. $configModel = new \app\crm\model\ConfigData();
  936. $configInfo = $configModel->getData();
  937. $config = $configInfo['config'] ? : 0;
  938. $follow_day = $configInfo['follow_day'] ? : 0;
  939. $deal_day = $configInfo['deal_day'] ? : 0;
  940. $whereData = [];
  941. //启用
  942. if ($config == 1) {
  943. //默认公海条件(没有负责人或已经到期)
  944. $data['follow_time'] = time()-$follow_day*86400;
  945. $data['deal_time'] = time()-$deal_day*86400;
  946. $data['deal_status'] = '未成交';
  947. if ($follow_day < $deal_day) {
  948. $whereData = function($query) use ($data){
  949. $query->where(['customer.owner_user_id'=>0])
  950. ->whereOr(function ($query) use ($data) {
  951. $query->where(function ($query) use ($data) {
  952. $query->where(['customer.update_time' => array('elt',$data['follow_time'])])
  953. ->whereOr(['customer.deal_time' => array('elt',$data['deal_time'])]);
  954. })
  955. ->where(['customer.is_lock' => 0])
  956. ->where(['customer.deal_status' => ['neq','已成交']]);
  957. });
  958. };
  959. } else {
  960. $whereData = function($query) use ($data){
  961. $query->where(['customer.owner_user_id'=>0])
  962. ->whereOr(function ($query) use ($data) {
  963. $query->where(function ($query) use ($data) {
  964. $query->where(['customer.deal_time' => array('elt',$data['deal_time'])]);
  965. })
  966. ->where(['customer.is_lock' => 0])
  967. ->where(['customer.deal_status' => ['neq','已成交']]);
  968. });
  969. };
  970. }
  971. } else {
  972. $whereData['customer.owner_user_id'] = 0;
  973. }
  974. return $whereData ? : '';
  975. }
  976. /**
  977. * 员工客户满意度分析
  978. *
  979. * @param BiCustomerLogic $biCustomerLogic
  980. * @param string $param
  981. * @return array|\think\response\Json
  982. * @throws \think\db\exception\DataNotFoundException
  983. * @throws \think\db\exception\ModelNotFoundException
  984. * @throws \think\exception\DbException
  985. */
  986. public function customerSatisfaction(BiCustomerLogic $biCustomerLogic, $param='')
  987. {
  988. $param = $this->param;
  989. $param['start_time'] = !empty($param['start_time']) ? strtotime($param['start_time']) : '';
  990. $param['end_time'] = !empty($param['end_time']) ? strtotime($param['end_time']) : '';
  991. if (!empty($param['type'])) {
  992. # 日期工具类
  993. $timeArr = getTimeByType($param['type']);
  994. # 设置日期参数
  995. $param['start_time'] = $timeArr[0];
  996. $param['end_time'] = $timeArr[1];
  997. }
  998. $data = $biCustomerLogic->getCustomerSatisfaction($param);
  999. //导出使用
  1000. if (!empty($param['excel_type'])) return $data;
  1001. return resultArray(['data' => $data]);
  1002. }
  1003. /**
  1004. * 产品满意度分析
  1005. *
  1006. * @param BiCustomerLogic $biCustomerLogic
  1007. * @param string $param
  1008. * @return array|\think\response\Json
  1009. * @throws \think\db\exception\DataNotFoundException
  1010. * @throws \think\db\exception\ModelNotFoundException
  1011. * @throws \think\exception\DbException
  1012. */
  1013. public function productSatisfaction(BiCustomerLogic $biCustomerLogic, $param='')
  1014. {
  1015. $param = $this->param;
  1016. $param['start_time'] = !empty($param['start_time']) ? strtotime($param['start_time']) : '';
  1017. $param['end_time'] = !empty($param['end_time']) ? strtotime($param['end_time']) : '';
  1018. if (!empty($param['type'])) {
  1019. # 日期工具类
  1020. $timeArr = getTimeByType($param['type']);
  1021. # 设置日期参数
  1022. $param['start_time'] = $timeArr[0];
  1023. $param['end_time'] = $timeArr[1];
  1024. }
  1025. $data = $biCustomerLogic->getProductSatisfaction($param);
  1026. //导出使用
  1027. if (!empty($param['excel_type'])) return $data;
  1028. return resultArray(['data' => $data]);
  1029. }
  1030. /**
  1031. * 导出
  1032. *
  1033. * @return mixed
  1034. * @throws \think\db\exception\DataNotFoundException
  1035. * @throws \think\db\exception\ModelNotFoundException
  1036. * @throws \think\exception\DbException
  1037. */
  1038. public function excelExport()
  1039. {
  1040. $param = $this->param;
  1041. $excel_type = $param['excel_type'];
  1042. $type=[];
  1043. $type['excel_types']=$param['excel_types'];
  1044. switch ($param['excel_types']) {
  1045. case 'statistics':
  1046. $list = $this->statistics($param);
  1047. $list=$list['list'];
  1048. $type['type'] = '客户总量分析列表';
  1049. break;
  1050. case'recordList':
  1051. $list = $this->recordList($param);
  1052. $list=$list['list'];
  1053. $type['type'] = '客户跟进次数分析';
  1054. break;
  1055. case 'recordMode':
  1056. $list = $this->recordMode($param);
  1057. $type['type'] = '客户跟进方式分析';
  1058. break;
  1059. case 'poolList':
  1060. $list = $this->poolList($param);
  1061. $type['type'] = '公海客户分析';
  1062. break;
  1063. case 'userCycle':
  1064. $list = $this->userCycle($param);
  1065. $list=$list['list'];
  1066. $type['type'] = '员工客户成交周期分析';
  1067. break;
  1068. case 'customerSatisfaction':
  1069. $list = $this->customerSatisfaction($param);
  1070. $list=$list['list'];
  1071. $type['type'] = '员工客户满意度分析';
  1072. break;
  1073. case 'productSatisfaction':
  1074. $list = $this->productSatisfaction($param);
  1075. $list=$list['list'];
  1076. $type['type'] = '产品满意度分析';
  1077. break;
  1078. }
  1079. if(empty($list)){
  1080. return resultArray(['data'=>'数据不存在']);
  1081. }
  1082. $excelLogic = new ExcelLogic();
  1083. $data = $excelLogic->biexcle($type, $list);
  1084. return $data;
  1085. }
  1086. }