Customer.php 49KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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'] = $param['start_time'] . ' 00:00:00';
  85. if (!empty($param['end_time'])) $param['end_time'] =$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'])) {
  242. return $result;
  243. }
  244. return resultArray(['data' => $result]);
  245. }
  246. /**
  247. * 员工跟进方式分析
  248. *
  249. * @param string $param
  250. * @return mixed|\think\response\Json
  251. * @throws \think\db\exception\DataNotFoundException
  252. * @throws \think\db\exception\ModelNotFoundException
  253. * @throws \think\exception\DbException
  254. */
  255. public function recordMode($param='')
  256. {
  257. $biCustomerModel = new \app\bi\model\Customer();
  258. if($param['excel_type']!=1){
  259. $param = $this->param;
  260. }
  261. $userId = $this->userInfo['id'];
  262. # 处理无部门无员工的情况
  263. $groupType = !empty($param['group_type']) ? $param['group_type'] : 1; # 组类型:1部门;2员工
  264. if (empty($param['user_id']) && empty($param['structure_id'])) {
  265. if ($groupType == 1) $param['structure_id'] = Db::name('admin_user')->where('id', $userId)->value('structure_id');
  266. if ($groupType == 2) $param['user_id'] = $userId;
  267. }
  268. unset($param['group_type']);
  269. # 判断部门下是否有员工
  270. if (!empty($param['structure_id'])) {
  271. $userModel = new \app\admin\model\User();
  272. $userIds = $userModel->getSubUserByStr($param['structure_id'], 2);
  273. if (empty($userIds)) return resultArray(['data' => []]);
  274. }
  275. # 排序参数
  276. $sortField = !empty($param['sort_field']) ? $param['sort_field'] : '';
  277. $sortValue = !empty($param['sort_value']) ? $param['sort_value'] : '';
  278. unset($param['sort_field']);
  279. unset($param['sort_value']);
  280. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  281. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  282. $whereArr = $biCustomerModel->getParamByWhere($param, 'record');
  283. //跟进类型
  284. $record_type = db('crm_config')->where(['name' => 'record_type'])->find();
  285. if ($record_type) {
  286. $record_categorys = json_decode($record_type['value']);
  287. } else {
  288. $record_categorys = array('打电话', '发邮件', '发短信', '见面拜访', '活动');
  289. }
  290. $sql = db('crm_activity')
  291. ->field([
  292. 'category',
  293. 'COUNT(*)' => 'count'
  294. ])
  295. ->where([
  296. 'create_time' => $whereArr['create_time'],
  297. 'create_user_id' => $whereArr['create_user_id'],
  298. 'type' => 1,
  299. 'activity_type' => 2
  300. ])
  301. ->group('category')
  302. ->fetchSql()
  303. ->select();
  304. $list = queryCache($sql);
  305. $list = array_column($list, null, 'category');
  306. $sum = array_sum(array_column($list, 'count'));
  307. $res = [];
  308. $recordCount = 0; # 跟进类型总数
  309. foreach ($record_categorys as $val) {
  310. $item['category'] = $val;
  311. if ($sum) {
  312. $item['recordNum'] = (int)$list[$val]['count'];
  313. $item['proportion'] = round(($item['recordNum'] / $sum * 100), 4);
  314. } else {
  315. $item['recordNum'] = $item['proportion'] = 0;
  316. }
  317. $res[] = $item;
  318. $recordCount += $item['recordNum'];
  319. }
  320. $result = [
  321. 'list' => $res,
  322. 'total' => [
  323. 'category' => '合计',
  324. 'recordNum' => $recordCount,
  325. 'proportion' => !empty($recordCount) ? 100 : 0
  326. ]
  327. ];
  328. # 排序
  329. if (!empty($result['list'])) $result['list'] = $this->sortCommon($result['list'], $sortField, $sortValue);
  330. //导出使用
  331. if (!empty($param['excel_type'])) return $result;
  332. return resultArray(['data' => $result]);
  333. }
  334. /**
  335. * 客户转化率分析
  336. *
  337. * @param
  338. * @return
  339. * @author zhi
  340. */
  341. public function conversion()
  342. {
  343. $customerModel = new \app\crm\model\Customer();
  344. $userModel = new \app\admin\model\User();
  345. $adminModel = new \app\admin\model\Admin();
  346. $param = $this->param;
  347. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  348. $whereArr = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  349. if (empty($whereArr['userIds'])) resultArray(['data' => []]);
  350. $userIds = $whereArr['userIds'];
  351. $user_ids = implode(',',$userIds);
  352. if (empty($param['type']) && empty($param['start_time'])) {
  353. $param['type'] = 'month';
  354. }
  355. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  356. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  357. $time = getTimeArray($param['start_time'], $param['end_time']);
  358. $sql = [];
  359. foreach ($time['list'] as $val) {
  360. $sql[] = $customerModel->getAddDealSql([
  361. 'create_user_id' => $user_ids ? $user_ids : '9999999999',
  362. 'type' => $val['type'],
  363. 'start_time' => $val['start_time'],
  364. 'end_time' => $val['end_time'],
  365. 'deal_status' => '已成交',
  366. ]);
  367. }
  368. $sql = implode(' UNION ALL ', $sql);
  369. $list = queryCache($sql);
  370. foreach ($list as &$val) {
  371. $val['proportion'] = $val['customer_num'] ? number_format($val['deal_customer_num'] / $val['customer_num'] * 100, 2) : 0;
  372. }
  373. return resultArray(['data' => $list]);
  374. }
  375. /**
  376. * 客户转化率分析具体数据
  377. *
  378. * @return \think\response\Json
  379. * @throws \think\Exception
  380. * @throws \think\db\exception\DataNotFoundException
  381. * @throws \think\db\exception\ModelNotFoundException
  382. * @throws \think\exception\DbException
  383. */
  384. public function conversionInfo()
  385. {
  386. $customerModel = new \app\bi\model\Customer();
  387. $userModel = new \app\admin\model\User();
  388. $param = $this->param;
  389. $userIds = [];
  390. $userId = $this->userInfo['id'];
  391. # 处理无部门无员工的情况
  392. $groupType = !empty($param['group_type']) ? $param['group_type'] : 1; # 组类型:1部门;2员工
  393. if (empty($param['user_id']) && empty($param['structure_id'])) {
  394. if ($groupType == 1) $param['structure_id'] = Db::name('admin_user')->where('id', $userId)->value('structure_id');
  395. if ($groupType == 2) $param['user_id'] = $userId;
  396. }
  397. unset($param['group_type']);
  398. # 如果存在员工参数,则不再使用部门参数
  399. if (!empty($param['user_id'])) {
  400. $userIds = [$param['user_id']];
  401. } elseif (!empty($param['structure_id'])) {
  402. $userIds = $userModel->getSubUserByStr($param['structure_id'], 2);
  403. }
  404. if (empty($userIds)) return resultArray(['data' => []]);
  405. # 排序参数
  406. $sortField = !empty($param['sort_field']) ? $param['sort_field'] : '';
  407. $sortValue = !empty($param['sort_value']) ? $param['sort_value'] : '';
  408. unset($param['sort_field']);
  409. unset($param['sort_value']);
  410. # 日期条件
  411. if (!empty($param['type'])) {
  412. $param['start_time'] = strtotime($param['type'] . '-01 00:00:00');
  413. $endMonth = strtotime(date('Y-m-d', $param['start_time']) . " +1 month -1 day");
  414. $param['end_time'] = strtotime(date('Y-m-d 23:59:59', $endMonth));
  415. unset($param['type']);
  416. }
  417. $whereArr = $customerModel->getParamByWhere($param);
  418. $whereArr['deal_status'] = '已成交';
  419. $list = $customerModel->getWhereByList($whereArr, $sortField, $sortValue);
  420. return resultArray(['data' => $list]);
  421. }
  422. /**
  423. * 公海客户分析
  424. *
  425. * @return \think\response\Json
  426. * @throws \think\db\exception\DataNotFoundException
  427. * @throws \think\db\exception\ModelNotFoundException
  428. * @throws \think\exception\DbException
  429. */
  430. public function pool()
  431. {
  432. $actionRecordModel = new \app\bi\model\ActionRecord();
  433. $userModel = new \app\admin\model\User();
  434. $adminModel = new \app\admin\model\Admin();
  435. $param = $this->param;
  436. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  437. $whereArr = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  438. $userIds = $whereArr['userIds'];
  439. if (empty($param['type']) && empty($param['start_time'])) {
  440. $param['type'] = 'month';
  441. }
  442. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  443. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  444. $time = getTimeArray($param['start_time'], $param['end_time']);
  445. $sql = $actionRecordModel
  446. ->field([
  447. "FROM_UNIXTIME(`create_time`, '{$time['time_format']}')" => 'type',
  448. 'SUM(CASE WHEN `content` = "将客户放入公海" THEN 1 ELSE 0 END)' => 'put_in',
  449. 'SUM(CASE WHEN `content` = "领取了客户" THEN 1 ELSE 0 END)' => 'receive'
  450. ])
  451. ->where([
  452. 'user_id' => ['IN', !empty($userIds) ? $userIds : '9999999999'],
  453. 'create_time' => ['BETWEEN', $time['between']],
  454. 'content' => ['IN', ['将客户放入公海', '领取了客户']]
  455. ])
  456. ->group('type')
  457. ->fetchSql()
  458. ->select();
  459. $res = queryCache($sql);
  460. $res = array_column($res, null, 'type');
  461. foreach ($time['list'] as &$val) {
  462. $val['put_in'] = (int)$res[$val['type']]['put_in'];
  463. $val['receive'] = (int)$res[$val['type']]['receive'];
  464. }
  465. return resultArray(['data' => $time['list']]);
  466. }
  467. /**
  468. * 公海客户分析 具体列表
  469. *
  470. * @param string $param
  471. * @return mixed|\think\response\Json
  472. * @throws \think\db\exception\DataNotFoundException
  473. * @throws \think\db\exception\ModelNotFoundException
  474. * @throws \think\exception\DbException
  475. */
  476. public function poolList($param='')
  477. {
  478. $userModel = new \app\admin\model\User();
  479. $actionRecordModel = new \app\bi\model\ActionRecord();
  480. $CustomerModel = new \app\crm\model\Customer();
  481. $adminModel = new \app\admin\model\Admin();
  482. if ($param['excel_type'] != 1) {
  483. $param = $this->param;
  484. }
  485. # 排序参数
  486. $sortField = !empty($param['sort_field']) ? $param['sort_field'] : '';
  487. $sortValue = !empty($param['sort_value']) ? $param['sort_value'] : '';
  488. unset($param['sort_field']);
  489. unset($param['sort_value']);
  490. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  491. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  492. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  493. $whereArr = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  494. $userIds = $whereArr['userIds'];
  495. $between_time = $whereArr['between_time'];
  496. $sql = CustomerModel::field([
  497. 'COUNT(*)' => 'customer_num',
  498. 'owner_user_id'
  499. ])
  500. ->where([
  501. 'create_time' => ['BETWEEN', $between_time],
  502. 'owner_user_id' => ['IN', !empty($userIds) ? $userIds : '9999999999']
  503. ])
  504. ->group('owner_user_id')
  505. ->fetchSql()
  506. ->select();
  507. $customer_num_list = queryCache($sql);
  508. $customer_num_list = array_column($customer_num_list, null, 'owner_user_id');
  509. $configModel = new \app\crm\model\ConfigData();
  510. $configInfo = $configModel->getData();
  511. $paramPool = [];
  512. $paramPool['config'] = $configInfo['config'] ? : 0;
  513. $paramPool['follow_day'] = $configInfo['follow_day'] ? : 0;
  514. $paramPool['deal_day'] = $configInfo['deal_day'] ? : 0;
  515. $paramPool['remind_config'] = $configInfo['remind_config'] ? : 0;
  516. $customer_list='';
  517. $action_record_list='';
  518. if($paramPool['config']==1){
  519. $sql = $CustomerModel
  520. ->alias('customer')
  521. ->field([
  522. 'customer.create_user_id as user_id' ,
  523. 'COUNT(customer.customer_id) as put_in'
  524. ])
  525. ->group('user_id')
  526. ->where($CustomerModel->getWhereByPool())
  527. ->where([
  528. 'create_time' => ['BETWEEN', $between_time],
  529. 'create_user_id' => ['IN', $userIds],
  530. ])
  531. ->fetchSql()
  532. ->select();
  533. $customer_list = queryCache($sql);
  534. $customer_list = array_column($customer_list, null, 'user_id');
  535. }
  536. $sql = $actionRecordModel
  537. ->field([
  538. 'user_id',
  539. 'SUM(CASE WHEN `content` = "将客户放入公海" THEN 1 ELSE 0 END)' => 'put_in',
  540. 'SUM(CASE WHEN `content` = "领取了客户" THEN 1 ELSE 0 END)' => 'receive'
  541. ])
  542. ->group('user_id')
  543. ->where([
  544. 'create_time' => ['BETWEEN', $between_time],
  545. 'user_id' => ['IN', $userIds],
  546. 'content' => ['IN', ['将客户放入公海', '领取了客户']],
  547. 'types' => 'crm_customer',
  548. ])
  549. ->fetchSql()
  550. ->select();
  551. $action_record_list = queryCache($sql);
  552. $action_record_list = array_column($action_record_list, null, 'user_id');
  553. $res = [];
  554. $receiveCount = 0; # 领取公海客户总数
  555. $putInCount = 0; # 进入公海客户总数
  556. foreach ($userIds as $val) {
  557. $item['put'] = !empty($customer_list[$val]['put_in'])?(int)$customer_list[$val]['put_in']:0;
  558. $item['put_in'] = !empty($customer_list[$val]['put_in']) ? $item['put'] : (int)$action_record_list[$val]['put_in'] + $item['put'];
  559. $item['receive'] = !empty($action_record_list[$val]['receive']) ? (int)$action_record_list[$val]['receive'] : 0;
  560. $item['customer_num'] = !empty($customer_num_list[$val]['customer_num']) ? (int)$customer_num_list[$val]['customer_num'] : 0;
  561. $user_info = $userModel->getUserById($val);
  562. $item['realname'] = $user_info['realname'];
  563. $item['username'] = $user_info['structure_name'];
  564. $res[] = $item;
  565. $receiveCount += $item['receive'];
  566. $putInCount += $item['put_in'];
  567. }
  568. $result = ['list' => $res, 'total' => ['realname' => '总计', 'receive' => $receiveCount, 'put_in' => $putInCount]];
  569. # 排序
  570. if (!empty($result['list'])) $result['list'] = $this->sortCommon($result['list'], $sortField, $sortValue);
  571. //导出使用
  572. if (!empty($param['excel_type'])) return $result['list'];
  573. return resultArray(['data' => $result]);
  574. }
  575. /**
  576. * 员工客户成交周期
  577. *
  578. * @param string $param
  579. * @return \think\response\Json
  580. * @throws \think\db\exception\DataNotFoundException
  581. * @throws \think\db\exception\ModelNotFoundException
  582. * @throws \think\exception\DbException
  583. */
  584. public function userCycle($param='')
  585. {
  586. $userModel = new \app\admin\model\User();
  587. $adminModel = new \app\admin\model\Admin();
  588. if($param['excel_type']!=1){
  589. $param = $this->param;
  590. }
  591. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  592. $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  593. $userIds = $whereData['userIds'];
  594. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  595. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  596. $time = getTimeArray($param['start_time'], $param['end_time']);
  597. $prefix = config('database.prefix');
  598. $sql = CustomerModel::alias('a')
  599. ->field([
  600. "FROM_UNIXTIME(`a`.`create_time`, '{$time['time_format']}')" => 'type',
  601. 'COUNT(*)' => 'customer_num',
  602. 'SUM(
  603. CASE WHEN ISNULL(`b`.`order_date`) THEN 0 ELSE (
  604. UNIX_TIMESTAMP(`b`.`order_date`) - `a`.`create_time`
  605. ) / 86400 END
  606. )' => 'cycle_sum'
  607. ])
  608. ->join(
  609. "(
  610. SELECT
  611. `customer_id`, MIN(`order_date`) AS `order_date`
  612. FROM
  613. `{$prefix}crm_contract`
  614. WHERE
  615. `check_status` = 2
  616. GROUP BY
  617. `customer_id`
  618. ) b",
  619. '`a`.`customer_id` = `b`.`customer_id`',
  620. 'LEFT'
  621. )
  622. ->where([
  623. 'a.deal_status' => '已成交',
  624. 'a.create_time' => ['BETWEEN', $time['between']],
  625. 'a.owner_user_id' => ['IN', !empty($userIds) ? $userIds : '9999999999']
  626. ])
  627. ->group('type')
  628. ->fetchsql()
  629. ->select();
  630. $res = queryCache($sql);
  631. $res = array_column($res, null, 'type');
  632. foreach ($time['list'] as &$val) {
  633. $val['customer_num'] = (int)$res[$val['type']]['customer_num'];
  634. if ($res[$val['type']]['customer_num']) {
  635. $val['cycle'] = intval($res[$val['type']]['cycle_sum'] / $res[$val['type']]['customer_num']);
  636. } else {
  637. $val['cycle'] = 0;
  638. }
  639. }
  640. $datas = $time['list'];
  641. return resultArray(['data' => $datas]);
  642. }
  643. /**
  644. * 成交周期列表
  645. *
  646. * @return \think\response\Json
  647. * @throws \think\db\exception\DataNotFoundException
  648. * @throws \think\db\exception\ModelNotFoundException
  649. * @throws \think\exception\DbException
  650. */
  651. public function userCycleList($param='')
  652. {
  653. $userModel = new \app\admin\model\User();
  654. $adminModel = new \app\admin\model\Admin();
  655. if($param['excel_type']!=1){
  656. $param = $this->param;
  657. }
  658. # 排序参数
  659. $sortField = !empty($param['sort_field']) ? $param['sort_field'] : '';
  660. $sortValue = !empty($param['sort_value']) ? $param['sort_value'] : '';
  661. unset($param['sort_field']);
  662. unset($param['sort_value']);
  663. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  664. $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  665. $userIds = $whereData['userIds'];
  666. if (empty($param['type']) && empty($param['start_time'])) {
  667. $param['type'] = 'month';
  668. }
  669. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  670. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  671. $time = getTimeArray($param['start_time'], $param['end_time']);
  672. $prefix = config('database.prefix');
  673. $sql = CustomerModel::alias('a')
  674. ->field([
  675. 'a.owner_user_id',
  676. 'COUNT(*)' => 'customer_num',
  677. 'SUM(
  678. CASE WHEN ISNULL(b.order_date) THEN 0 ELSE (
  679. UNIX_TIMESTAMP(b.order_date) - a.create_time
  680. ) / 86400 END
  681. )' => 'cycle_sum'
  682. ])
  683. ->join(
  684. "(
  685. SELECT
  686. `customer_id`,
  687. MIN(`order_date`) AS `order_date`
  688. FROM
  689. `{$prefix}crm_contract`
  690. WHERE
  691. `check_status` = 2
  692. GROUP BY
  693. `customer_id`
  694. ) b",
  695. 'a.customer_id = b.customer_id',
  696. 'LEFT'
  697. )
  698. ->where([
  699. 'a.deal_status' => '已成交',
  700. 'a.create_time' => ['BETWEEN', $time['between']],
  701. 'a.owner_user_id' => ['IN', !empty($userIds) ? $userIds : '9999999999']
  702. ])
  703. ->group('a.owner_user_id')
  704. ->fetchSql()
  705. ->select();
  706. $res = queryCache($sql);
  707. $res = array_column($res, null, 'owner_user_id');
  708. $user_data = [];
  709. $customerCount = 0; # 成交客户总数
  710. $cycleCount = 0; # 成交周期总数
  711. foreach ($userIds as $value) {
  712. $item['customer_num'] = !empty($res[$value]['customer_num']) ? $res[$value]['customer_num'] : 0;
  713. $item['cycle'] = $res[$value]['customer_num'] ? intval($res[$value]['cycle_sum'] / $res[$value]['customer_num']) : 0;
  714. $item['realname'] = $userModel->getUserById($value)['realname'];
  715. $user_data[] = $item;
  716. $customerCount += $item['customer_num'];
  717. $cycleCount += $item['cycle'];
  718. }
  719. $datas['list'] = $user_data;
  720. $datas['total'] = [
  721. 'realname' => '总计',
  722. 'customer_num' => $customerCount,
  723. 'cycle' => $cycleCount
  724. ];
  725. # 排序
  726. if (!empty($datas['users'])) $datas['users'] = $this->sortCommon($datas['users'], $sortField, $sortValue);
  727. //导出使用
  728. if (!empty($param['excel_type'])) return $datas;
  729. return resultArray(['data' => $datas]);
  730. }
  731. /**
  732. * 产品成交周期
  733. * @param
  734. * @return
  735. * @author zhi
  736. */
  737. public function productCycle()
  738. {
  739. $biCustomerModel = new \app\bi\model\Customer();
  740. $productModel = new \app\bi\model\Product();
  741. if($param['excel_type']!=1){
  742. $param = $this->param;
  743. }
  744. $list = $productModel->getDealByProduct($param);
  745. $datas = array();
  746. $cycleCount = 0;
  747. $customerCount = 0;
  748. foreach ($list as $key => $value) {
  749. $item = array();
  750. //周期
  751. $customer_ids = $productModel->getCycleByProduct($param, $value['product_id']);
  752. $whereArr = array();
  753. $whereArr['customer_id'] = array('in', $customer_ids);
  754. $cycle = $biCustomerModel->getWhereByCycle($whereArr);
  755. $item['product_name'] = $value['product_name'];
  756. $item['customer_num'] = !empty($value['num']) ? (int)$value['num'] : 0;
  757. $item['cycle'] = !empty($cycle) ? (int)$cycle : 0;
  758. $datas['list'][] = $item;
  759. $cycleCount += $item['cycle'];
  760. $customerCount += $item['customer_num'];
  761. }
  762. $datas['total'] = ['product_name' => '总计', 'cycle' => $cycleCount, 'customer_num' => $customerCount];
  763. //导出使用
  764. if (!empty($param['excel_type'])) return $datas;
  765. return resultArray(['data' => $datas]);
  766. }
  767. /**
  768. * 地区成交周期
  769. * @param
  770. * @return
  771. * @author zhi
  772. */
  773. public function addressCycle($param='')
  774. {
  775. $userModel = new \app\admin\model\User();
  776. $customerModel = new \app\crm\model\Customer();
  777. $biCustomerModel = new \app\bi\model\Customer();
  778. $address_arr = \app\crm\model\Customer::$address;
  779. if($param['excel_type']!=1){
  780. $param = $this->param;
  781. }
  782. if (empty($param['type']) && empty($param['start_time'])) {
  783. $param['type'] = 'month';
  784. }
  785. $map_user_ids = [];
  786. if ($param['user_id']) {
  787. $map_user_ids = array($param['user_id']);
  788. } else {
  789. if ($param['structure_id']) {
  790. $map_user_ids = $userModel->getSubUserByStr($param['structure_id'], 2);
  791. }
  792. }
  793. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  794. $userIds = $map_user_ids ? array_intersect($map_user_ids, $perUserIds) : $perUserIds; //数组交集
  795. $time = getTimeArray();
  796. $prefix = config('database.prefix');
  797. $sql = CustomerModel::alias('a')
  798. ->field([
  799. 'SUBSTR(`a`.`address`, 1, 2)' => 'addr',
  800. 'COUNT(*)' => 'customer_num',
  801. 'SUM(
  802. CASE WHEN ISNULL(b.order_date) THEN 0 ELSE (
  803. UNIX_TIMESTAMP(b.order_date) - a.create_time
  804. ) / 86400 END
  805. )' => 'cycle_sum'
  806. ])
  807. ->join(
  808. "(
  809. SELECT
  810. `customer_id`,
  811. MIN(`order_date`) AS `order_date`
  812. FROM
  813. `{$prefix}crm_contract`
  814. WHERE
  815. `check_status` = 2
  816. GROUP BY
  817. `customer_id`
  818. ) b",
  819. 'a.customer_id = b.customer_id',
  820. 'LEFT'
  821. )
  822. ->where([
  823. 'a.deal_status' => '已成交',
  824. 'a.create_time' => ['BETWEEN', $time['between']],
  825. 'a.owner_user_id' => ['IN', $userIds]
  826. ])
  827. ->group('addr')
  828. ->fetchSql()
  829. ->select();
  830. $list = queryCache($sql);
  831. $list = array_column($list, null, 'addr');
  832. $list['黑龙江'] = $list['黑龙'];
  833. $list['内蒙古'] = $list['内蒙'];
  834. $res = [];
  835. $cycleCount = 0;
  836. $customerCount = 0;
  837. foreach ($address_arr as $val) {
  838. $item['address'] = $val;
  839. $item['customer_num'] = !empty($list[$val]['customer_num']) ? $list[$val]['customer_num'] : 0;
  840. $item['cycle'] = $list[$val]['customer_num'] ? intval($list[$val]['cycle_sum'] / $list[$val]['customer_num']) : 0;
  841. $res['list'][] = $item;
  842. $cycleCount += $item['cycle'];
  843. $customerCount += $item['customer_num'];
  844. }
  845. $res['total'] = ['address' => '总计', 'cycle' => $cycleCount, 'customer_num' => $customerCount];
  846. //导出使用
  847. if (!empty($param['excel_type'])) return $res;
  848. return resultArray(['data' => $res]);
  849. }
  850. /**
  851. * 客户所在城市分析
  852. *
  853. * @return \think\response\Json
  854. * @throws \think\db\exception\DataNotFoundException
  855. * @throws \think\db\exception\ModelNotFoundException
  856. * @throws \think\exception\DbException
  857. */
  858. public function addressAnalyse($param='')
  859. {
  860. if($param['excel_type']!=1){
  861. $param = $this->param;
  862. }
  863. // $customerModel = new \app\crm\model\Customer();
  864. $userModel = new \app\admin\model\User();
  865. $address_arr = \app\crm\model\Customer::$address;
  866. $map_user_ids = [];
  867. # 如果存在员工参数,则不再使用部门参数
  868. if (!empty($param['user_id'])) {
  869. $map_user_ids = array($param['user_id']);
  870. } elseif (!empty($param['structure_id'])) {
  871. $map_user_ids = $userModel->getSubUserByStr($param['structure_id'], 2);
  872. }
  873. if (empty($map_user_ids)) return resultArray(['data' => []]);
  874. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  875. $userIds = $map_user_ids ? array_intersect($map_user_ids, $perUserIds) : $perUserIds; //数组交集
  876. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  877. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  878. $time = getTimeArray($param['start_time'], $param['end_time']);
  879. $sql = CustomerModel::alias('a')
  880. ->field([
  881. 'SUBSTR(`address`, 1, 2)' => 'addr',
  882. 'COUNT(*)' => 'allCustomer',
  883. 'SUM(
  884. CASE WHEN `deal_status` = "已成交" THEN 1 ELSE 0 END
  885. )' => 'dealCustomer',
  886. ])
  887. ->where([
  888. 'create_time' => ['BETWEEN', $time['between']],
  889. 'owner_user_id' => ['IN', $userIds]
  890. ])
  891. ->group('addr')
  892. ->fetchSql()
  893. ->select();
  894. $list = queryCache($sql);
  895. $list = array_column($list, null, 'addr');
  896. $list['黑龙江'] = $list['黑龙'];
  897. $list['内蒙古'] = $list['内蒙'];
  898. $data = [];
  899. foreach ($address_arr as $val) {
  900. $item['address'] = $val;
  901. $item['allCustomer'] = !empty($list[$val]['allCustomer']) ? (int)$list[$val]['allCustomer'] : 0;
  902. $item['dealCustomer'] = !empty($list[$val]['dealCustomer']) ? (int)$list[$val]['dealCustomer'] : 0;
  903. $data[] = $item;
  904. }
  905. //导出使用
  906. if (!empty($param['excel_type'])) return $res;
  907. return resultArray(['data' => $data]);
  908. }
  909. /**
  910. * 客户行业/级别/来源分析
  911. *
  912. * @return \think\response\Json
  913. * @throws \think\db\exception\DataNotFoundException
  914. * @throws \think\db\exception\ModelNotFoundException
  915. * @throws \think\exception\DbException
  916. */
  917. public function portrait()
  918. {
  919. $biCustomerModel = new \app\bi\model\Customer();
  920. $userModel = new \app\admin\model\User();
  921. $adminModel = new \app\admin\model\Admin();
  922. $param = $this->param;
  923. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  924. $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  925. $userIds = $whereData['userIds'];
  926. if (!in_array($param['type_analyse'], ['industry', 'source', 'level'])) {
  927. return resultArray(['error' => '参数错误']);
  928. }
  929. $poolWhere = $this->getWhereByPool();
  930. $poolId = db('crm_customer')->alias('customer')->where($poolWhere)->column('customer_id');
  931. $whereArr = array();
  932. $whereArr['types'] = 'crm_customer';
  933. $whereArr['field'] = $param['type_analyse'];
  934. $setting = $biCustomerModel->getOptionByField($whereArr);
  935. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  936. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  937. $time = getTimeArray($param['start_time'], $param['end_time']);
  938. $sql = CustomerModel::field([
  939. "(
  940. CASE WHEN
  941. `{$param['type_analyse']}` = ''
  942. THEN '(空)'
  943. ELSE {$param['type_analyse']} END
  944. )" => $param['type_analyse'],
  945. 'COUNT(*)' => 'allCustomer',
  946. 'SUM(
  947. CASE WHEN `deal_status` = "已成交" THEN 1 ELSE 0 END
  948. )' => 'dealCustomer',
  949. ])
  950. ->where([
  951. 'create_time' => ['BETWEEN', $time['between']],
  952. 'owner_user_id' => ['IN', !empty($userIds) ? $userIds : '9999999999']
  953. ])
  954. ->whereNotIn('customer_id', $poolId)
  955. ->group($param['type_analyse'])
  956. ->fetchSql()
  957. ->select();
  958. $list = queryCache($sql);
  959. $list = array_column($list, null, $param['type_analyse']);
  960. $other_keys = array_diff(array_keys($list), $setting);
  961. $setting = array_merge($setting, $other_keys);
  962. $data = [];
  963. foreach ($setting as $val) {
  964. $item = [];
  965. $item[$param['type_analyse']] = $val;
  966. $item['allCustomer'] = !empty($list[$val]['allCustomer']) ? (int)$list[$val]['allCustomer'] : 0;
  967. $item['dealCustomer'] = !empty($list[$val]['dealCustomer']) ? (int)$list[$val]['dealCustomer'] : 0;
  968. $data[] = $item;
  969. }
  970. return resultArray(['data' => $data]);
  971. }
  972. /**
  973. * [客户公海条件]
  974. * @author Michael_xu
  975. * @param
  976. * @return
  977. */
  978. public function getWhereByPool()
  979. {
  980. $configModel = new \app\crm\model\ConfigData();
  981. $configInfo = $configModel->getData();
  982. $config = $configInfo['config'] ? : 0;
  983. $follow_day = $configInfo['follow_day'] ? : 0;
  984. $deal_day = $configInfo['deal_day'] ? : 0;
  985. $whereData = [];
  986. //启用
  987. if ($config == 1) {
  988. //默认公海条件(没有负责人或已经到期)
  989. $data['follow_time'] = time()-$follow_day*86400;
  990. $data['deal_time'] = time()-$deal_day*86400;
  991. $data['deal_status'] = '未成交';
  992. if ($follow_day < $deal_day) {
  993. $whereData = function($query) use ($data){
  994. $query->where(['customer.owner_user_id'=>0])
  995. ->whereOr(function ($query) use ($data) {
  996. $query->where(function ($query) use ($data) {
  997. $query->where(['customer.update_time' => array('elt',$data['follow_time'])])
  998. ->whereOr(['customer.deal_time' => array('elt',$data['deal_time'])]);
  999. })
  1000. ->where(['customer.is_lock' => 0])
  1001. ->where(['customer.deal_status' => ['neq','已成交']]);
  1002. });
  1003. };
  1004. } else {
  1005. $whereData = function($query) use ($data){
  1006. $query->where(['customer.owner_user_id'=>0])
  1007. ->whereOr(function ($query) use ($data) {
  1008. $query->where(function ($query) use ($data) {
  1009. $query->where(['customer.deal_time' => array('elt',$data['deal_time'])]);
  1010. })
  1011. ->where(['customer.is_lock' => 0])
  1012. ->where(['customer.deal_status' => ['neq','已成交']]);
  1013. });
  1014. };
  1015. }
  1016. } else {
  1017. $whereData['customer.owner_user_id'] = 0;
  1018. }
  1019. return $whereData ? : '';
  1020. }
  1021. /**
  1022. * 员工客户满意度分析
  1023. *
  1024. * @param BiCustomerLogic $biCustomerLogic
  1025. * @param string $param
  1026. * @return array|\think\response\Json
  1027. * @throws \think\db\exception\DataNotFoundException
  1028. * @throws \think\db\exception\ModelNotFoundException
  1029. * @throws \think\exception\DbException
  1030. */
  1031. public function customerSatisfaction($param='')
  1032. {
  1033. $adminModel = new \app\admin\model\Admin();
  1034. $userModel = new \app\admin\model\User();
  1035. $param = $this->param;
  1036. $param['start_time'] = !empty($param['start_time']) ? strtotime($param['start_time']) : '';
  1037. $param['end_time'] = !empty($param['end_time']) ? strtotime($param['end_time']) : '';
  1038. if (!empty($param['type'])) {
  1039. # 日期工具类
  1040. $timeArr = getTimeByType($param['type']);
  1041. # 设置日期参数pool
  1042. $param['start_time'] = $timeArr[0];
  1043. $param['end_time'] = $timeArr[1];
  1044. }
  1045. $biCustomerLogic=new BiCustomerLogic();
  1046. $data = $biCustomerLogic->getCustomerSatisfaction($param);
  1047. //导出使用
  1048. if (!empty($param['excel_type'])) return $data;
  1049. return resultArray(['data' => $data]);
  1050. }
  1051. /**
  1052. * 产品满意度分析
  1053. *
  1054. * @param BiCustomerLogic $biCustomerLogic
  1055. * @param string $param
  1056. * @return array|\think\response\Json
  1057. * @throws \think\db\exception\DataNotFoundException
  1058. * @throws \think\db\exception\ModelNotFoundException
  1059. * @throws \think\exception\DbException
  1060. */
  1061. public function productSatisfaction($param='')
  1062. {
  1063. $param = $this->param;
  1064. $userInfo=$this->userInfo;
  1065. $param['start_time'] = !empty($param['start_time']) ? strtotime($param['start_time']) : '';
  1066. $param['end_time'] = !empty($param['end_time']) ? strtotime($param['end_time']) : '';
  1067. if (!empty($param['type'])) {
  1068. # 日期工具类
  1069. $timeArr = getTimeByType($param['type']);
  1070. # 设置日期参数
  1071. $param['start_time'] = $timeArr[0];
  1072. $param['end_time'] = $timeArr[1];
  1073. }
  1074. $biCustomerLogic= new BiCustomerLogic();
  1075. $data = $biCustomerLogic->getProductSatisfaction($param);
  1076. //导出使用
  1077. if (!empty($param['excel_type'])) return $data;
  1078. return resultArray(['data' => $data]);
  1079. }
  1080. /**
  1081. * 导出
  1082. *
  1083. * @return mixed
  1084. * @throws \think\db\exception\DataNotFoundException
  1085. * @throws \think\db\exception\ModelNotFoundException
  1086. * @throws \think\exception\DbException
  1087. */
  1088. public function excelExport()
  1089. {
  1090. $param = $this->param;
  1091. $excel_type = $param['excel_type'];
  1092. $type=[];
  1093. $type['excel_types']=$param['excel_types'];
  1094. switch ($param['excel_types']) {
  1095. case 'statistics':
  1096. $list = $this->statistics($param);
  1097. $list=$list['list'];
  1098. $type['type'] = '客户总量分析列表';
  1099. break;
  1100. case'recordList':
  1101. $list = $this->recordList($param);
  1102. $list=$list['list'];
  1103. $type['type'] = '客户跟进次数分析';
  1104. break;
  1105. case 'recordMode':
  1106. $list = $this->recordMode($param);
  1107. $list=$list['list'];
  1108. $type['type'] = '客户跟进方式分析';
  1109. break;
  1110. case 'poolList':
  1111. $list = $this->poolList($param);
  1112. $type['type'] = '公海客户分析';
  1113. break;
  1114. case 'userCycle':
  1115. $list = $this->userCycle($param);
  1116. $list=$list['list'];
  1117. $type['type'] = '员工客户成交周期分析';
  1118. break;
  1119. case 'customerSatisfaction':
  1120. $list = $this->customerSatisfaction($param);
  1121. $type['type'] = '员工客户满意度分析';
  1122. break;
  1123. case 'productSatisfaction':
  1124. $list = $this->productSatisfaction($param);
  1125. $type['type'] = '产品满意度分析';
  1126. break;
  1127. case 'userCycleList':
  1128. $list = $this->userCycleList($param);
  1129. $list=$list['list'];
  1130. $type['type'] = '成交周期';
  1131. break;
  1132. case 'productCycle':
  1133. $list = $this->addressCycle($param);
  1134. $list=$list['list'];
  1135. $type['type'] = '地区成交周期';
  1136. break;
  1137. case 'addressCycle':
  1138. $list = $this->productCycle($param);
  1139. $list=$list['list'];
  1140. $type['type'] = '地区成交周期';
  1141. break;
  1142. }
  1143. if(empty($list)){
  1144. return resultArray(['data'=>'数据不存在']);
  1145. }
  1146. $excelLogic = new ExcelLogic();
  1147. $data = $excelLogic->biexcle($type, $list);
  1148. return $data;
  1149. }
  1150. }