Customer.php 50KB

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