Customer.php 50KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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. $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. $userModel = new \app\admin\model\User();
  433. $adminModel = new \app\admin\model\Admin();
  434. $param = $this->param;
  435. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  436. $whereArr = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  437. $userIds = $whereArr['userIds'];
  438. if (empty($param['type']) && empty($param['start_time'])) {
  439. $param['type'] = 'month';
  440. }
  441. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  442. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  443. $time = getTimeArray($param['start_time'], $param['end_time']);
  444. // $sql = db('crm_customer_pool_record')
  445. // ->field([
  446. // "FROM_UNIXTIME(`create_time`, '{$time['time_format']}')" => 'time',
  447. // 'SUM(CASE WHEN `type` = 2 THEN 1 ELSE 0 END)' => 'put_in',
  448. // 'SUM(CASE WHEN `type` = 1 THEN 1 ELSE 0 END)' => 'receive'
  449. // ])
  450. // ->where([
  451. // 'user_id' => ['IN', !empty($userIds) ? $userIds : 0],
  452. // 'create_time' => ['BETWEEN', $time['between']],
  453. // ])
  454. // ->group('time')
  455. // ->fetchSql()
  456. // ->select();
  457. $prefix = config('database.prefix');
  458. $ids = !empty($userIds) ? implode(',', $userIds) : 0;
  459. $sql = "SELECT
  460. FROM_UNIXTIME( `create_time`, '%Y-%m' ) AS `time`,
  461. SUM( CASE WHEN `type` = 2 THEN 1 ELSE 0 END ) AS `put_in`,
  462. SUM( CASE WHEN `type` = 1 THEN 1 ELSE 0 END ) AS `receive`
  463. FROM
  464. (SELECT * FROM `".$prefix."crm_customer_pool_record` GROUP BY `customer_id`, `type`) AS `record`
  465. WHERE
  466. `user_id` IN ( ".$ids." ) AND `create_time` BETWEEN ".$time['between'][0]." AND ".$time['between'][1]."
  467. GROUP BY `time`";
  468. $res = queryCache($sql);
  469. $res = array_column($res, null, 'time');
  470. $result = [];
  471. foreach ($time['list'] AS $key => $value) {
  472. $result[] = [
  473. 'type' => $value['type'],
  474. 'start_time' => $value['start_time'],
  475. 'end_time' => $value['end_time'],
  476. 'put_in' => !empty($res[$value['type']]['put_in']) ? (int)$res[$value['type']]['put_in'] : 0,
  477. 'receive' => !empty($res[$value['type']]['receive']) ? (int)$res[$value['type']]['receive'] : 0
  478. ];
  479. }
  480. return resultArray(['data' => $result]);
  481. }
  482. /**
  483. * 公海客户分析 具体列表
  484. *
  485. * @param string $param
  486. * @return mixed|\think\response\Json
  487. * @throws \think\db\exception\DataNotFoundException
  488. * @throws \think\db\exception\ModelNotFoundException
  489. * @throws \think\exception\DbException
  490. */
  491. public function poolList($param='')
  492. {
  493. $userModel = new \app\admin\model\User();
  494. $actionRecordModel = new \app\bi\model\ActionRecord();
  495. $CustomerModel = new \app\crm\model\Customer();
  496. $adminModel = new \app\admin\model\Admin();
  497. if ($param['excel_type'] != 1) {
  498. $param = $this->param;
  499. }
  500. # 排序参数
  501. $sortField = !empty($param['sort_field']) ? $param['sort_field'] : '';
  502. $sortValue = !empty($param['sort_value']) ? $param['sort_value'] : '';
  503. unset($param['sort_field']);
  504. unset($param['sort_value']);
  505. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  506. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  507. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  508. $whereArr = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  509. $userIds = $whereArr['userIds'];
  510. $between_time = $whereArr['between_time'];
  511. // $sql = db('crm_customer_pool_record')
  512. // ->field([
  513. // 'user_id',
  514. // 'SUM(CASE WHEN `type` = 2 THEN 1 ELSE 0 END)' => 'put_in',
  515. // 'SUM(CASE WHEN `type` = 1 THEN 1 ELSE 0 END)' => 'receive'
  516. // ])
  517. // ->group('user_id')
  518. // ->where([
  519. // 'create_time' => ['BETWEEN', $between_time],
  520. // 'user_id' => ['IN', !empty($userIds) ? $userIds : 0]
  521. // ])
  522. // ->fetchSql()
  523. // ->select();
  524. $prefix = config('database.prefix');
  525. $ids = !empty($userIds) ? implode(',', $userIds) : 0;
  526. $sql = "SELECT
  527. `user_id`,
  528. SUM( CASE WHEN `type` = 2 THEN 1 ELSE 0 END ) AS `put_in`,
  529. SUM( CASE WHEN `type` = 1 THEN 1 ELSE 0 END ) AS `receive`
  530. FROM
  531. (SELECT * FROM `".$prefix."crm_customer_pool_record` GROUP BY `customer_id`, `type`) AS `record`
  532. WHERE
  533. `create_time` BETWEEN ".$between_time[0]." AND ".$between_time[1]." AND `user_id` IN ( ".$ids." )
  534. GROUP BY `user_id`";
  535. $poolRecord = queryCache($sql);
  536. $recordData = [];
  537. foreach ($poolRecord AS $key => $value) {
  538. $recordData[$value['user_id']] = [
  539. 'put_in' => $value['put_in'],
  540. 'receive' => $value['receive']
  541. ];
  542. }
  543. // $action_record_list = array_column($action_record_list, null, 'user_id');
  544. # 部门列表
  545. $structureData = [];
  546. $structureList = db('admin_structure')->select();
  547. foreach ($structureList AS $key => $value) {
  548. $structureData[$value['id']] = $value['name'];
  549. }
  550. # 员工列表
  551. $userList = db('admin_user')->field(['id', 'realname', 'structure_id'])->whereIn('id', $userIds)->select();
  552. $res = [];
  553. $receiveCount = 0; # 领取公海客户总数
  554. $putInCount = 0; # 进入公海客户总数
  555. foreach ($userList as $val) {
  556. $item['put'] = !empty($recordData[$val['id']]['put_in']) ? (int)$recordData[$val['id']]['put_in'] : 0;
  557. $item['put_in'] = !empty($recordData[$val['id']]['put_in']) ? (int)$recordData[$val['id']]['put_in'] : 0;
  558. $item['receive'] = !empty($recordData[$val['id']]['receive']) ? (int)$recordData[$val['id']]['receive'] : 0;
  559. $item['customer_num'] = 0;
  560. $item['realname'] = $val['realname'];
  561. $item['username'] = !empty($structureData[$val['structure_id']]) ? $structureData[$val['structure_id']] : '';
  562. $res[] = $item;
  563. $receiveCount += $item['receive'];
  564. $putInCount += $item['put_in'];
  565. }
  566. // foreach ($userIds as $val) {
  567. // $item['put'] = !empty($customer_list[$val]['put_in'])?(int)$customer_list[$val]['put_in']:0;
  568. // $item['put_in'] = !empty($customer_list[$val]['put_in']) ? $item['put'] : (int)$action_record_list[$val]['put_in'] + $item['put'];
  569. // $item['receive'] = !empty($action_record_list[$val]['receive']) ? (int)$action_record_list[$val]['receive'] : 0;
  570. // $item['customer_num'] = !empty($customer_num_list[$val]['customer_num']) ? (int)$customer_num_list[$val]['customer_num'] : 0;
  571. // $user_info = $userModel->getUserById($val);
  572. // $item['realname'] = $user_info['realname'];
  573. // $item['username'] = $user_info['structure_name'];
  574. // $res[] = $item;
  575. //
  576. // $receiveCount += $item['receive'];
  577. // $putInCount += $item['put_in'];
  578. // }
  579. $result = ['list' => $res, 'total' => ['realname' => '总计', 'receive' => $receiveCount, 'put_in' => $putInCount]];
  580. # 排序
  581. if (!empty($result['list'])) $result['list'] = $this->sortCommon($result['list'], $sortField, $sortValue);
  582. //导出使用
  583. if (!empty($param['excel_type'])) return $result['list'];
  584. return resultArray(['data' => $result]);
  585. }
  586. /**
  587. * 员工客户成交周期
  588. *
  589. * @param string $param
  590. * @return \think\response\Json
  591. * @throws \think\db\exception\DataNotFoundException
  592. * @throws \think\db\exception\ModelNotFoundException
  593. * @throws \think\exception\DbException
  594. */
  595. public function userCycle($param='')
  596. {
  597. $userModel = new \app\admin\model\User();
  598. $adminModel = new \app\admin\model\Admin();
  599. if($param['excel_type']!=1){
  600. $param = $this->param;
  601. }
  602. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  603. $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  604. $userIds = $whereData['userIds'];
  605. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  606. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  607. $time = getTimeArray($param['start_time'], $param['end_time']);
  608. $prefix = config('database.prefix');
  609. $sql = CustomerModel::alias('a')
  610. ->field([
  611. "FROM_UNIXTIME(`a`.`create_time`, '{$time['time_format']}')" => 'type',
  612. 'COUNT(*)' => 'customer_num',
  613. 'SUM(
  614. CASE WHEN ISNULL(`b`.`order_date`) THEN 0 ELSE (
  615. UNIX_TIMESTAMP(`b`.`order_date`) - `a`.`create_time`
  616. ) / 86400 END
  617. )' => 'cycle_sum'
  618. ])
  619. ->join(
  620. "(
  621. SELECT
  622. `customer_id`, MIN(`order_date`) AS `order_date`
  623. FROM
  624. `{$prefix}crm_contract`
  625. WHERE
  626. `check_status` = 2
  627. GROUP BY
  628. `customer_id`
  629. ) b",
  630. '`a`.`customer_id` = `b`.`customer_id`',
  631. 'LEFT'
  632. )
  633. ->where([
  634. 'a.deal_status' => '已成交',
  635. 'a.create_time' => ['BETWEEN', $time['between']],
  636. 'a.owner_user_id' => ['IN', !empty($userIds) ? $userIds : '9999999999']
  637. ])
  638. ->group('type')
  639. ->fetchsql()
  640. ->select();
  641. $res = queryCache($sql);
  642. $res = array_column($res, null, 'type');
  643. foreach ($time['list'] as &$val) {
  644. $val['customer_num'] = (int)$res[$val['type']]['customer_num'];
  645. if ($res[$val['type']]['customer_num']) {
  646. $val['cycle'] = intval($res[$val['type']]['cycle_sum'] / $res[$val['type']]['customer_num']);
  647. } else {
  648. $val['cycle'] = 0;
  649. }
  650. }
  651. $datas = $time['list'];
  652. return resultArray(['data' => $datas]);
  653. }
  654. /**
  655. * 成交周期列表
  656. *
  657. * @return \think\response\Json
  658. * @throws \think\db\exception\DataNotFoundException
  659. * @throws \think\db\exception\ModelNotFoundException
  660. * @throws \think\exception\DbException
  661. */
  662. public function userCycleList($param='')
  663. {
  664. $userModel = new \app\admin\model\User();
  665. $adminModel = new \app\admin\model\Admin();
  666. if($param['excel_type']!=1){
  667. $param = $this->param;
  668. }
  669. # 排序参数
  670. $sortField = !empty($param['sort_field']) ? $param['sort_field'] : '';
  671. $sortValue = !empty($param['sort_value']) ? $param['sort_value'] : '';
  672. unset($param['sort_field']);
  673. unset($param['sort_value']);
  674. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  675. $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  676. $userIds = $whereData['userIds'];
  677. if (empty($param['type']) && empty($param['start_time'])) {
  678. $param['type'] = 'month';
  679. }
  680. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  681. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  682. $time = getTimeArray($param['start_time'], $param['end_time']);
  683. $prefix = config('database.prefix');
  684. $sql = CustomerModel::alias('a')
  685. ->field([
  686. 'a.owner_user_id',
  687. 'COUNT(*)' => 'customer_num',
  688. 'SUM(
  689. CASE WHEN ISNULL(b.order_date) THEN 0 ELSE (
  690. UNIX_TIMESTAMP(b.order_date) - a.create_time
  691. ) / 86400 END
  692. )' => 'cycle_sum'
  693. ])
  694. ->join(
  695. "(
  696. SELECT
  697. `customer_id`,
  698. MIN(`order_date`) AS `order_date`
  699. FROM
  700. `{$prefix}crm_contract`
  701. WHERE
  702. `check_status` = 2
  703. GROUP BY
  704. `customer_id`
  705. ) b",
  706. 'a.customer_id = b.customer_id',
  707. 'LEFT'
  708. )
  709. ->where([
  710. 'a.deal_status' => '已成交',
  711. 'a.create_time' => ['BETWEEN', $time['between']],
  712. 'a.owner_user_id' => ['IN', !empty($userIds) ? $userIds : '9999999999']
  713. ])
  714. ->group('a.owner_user_id')
  715. ->fetchSql()
  716. ->select();
  717. $res = queryCache($sql);
  718. $res = array_column($res, null, 'owner_user_id');
  719. $user_data = [];
  720. $customerCount = 0; # 成交客户总数
  721. $cycleCount = 0; # 成交周期总数
  722. foreach ($userIds as $value) {
  723. $item['customer_num'] = !empty($res[$value]['customer_num']) ? $res[$value]['customer_num'] : 0;
  724. $item['cycle'] = $res[$value]['customer_num'] ? intval($res[$value]['cycle_sum'] / $res[$value]['customer_num']) : 0;
  725. $item['realname'] = $userModel->getUserById($value)['realname'];
  726. $user_data[] = $item;
  727. $customerCount += $item['customer_num'];
  728. $cycleCount += $item['cycle'];
  729. }
  730. $datas['list'] = $user_data;
  731. $datas['total'] = [
  732. 'realname' => '总计',
  733. 'customer_num' => $customerCount,
  734. 'cycle' => $cycleCount
  735. ];
  736. # 排序
  737. if (!empty($datas['users'])) $datas['users'] = $this->sortCommon($datas['users'], $sortField, $sortValue);
  738. //导出使用
  739. if (!empty($param['excel_type'])) return $datas;
  740. return resultArray(['data' => $datas]);
  741. }
  742. /**
  743. * 产品成交周期
  744. * @param
  745. * @return
  746. * @author zhi
  747. */
  748. public function productCycle()
  749. {
  750. $biCustomerModel = new \app\bi\model\Customer();
  751. $productModel = new \app\bi\model\Product();
  752. if($param['excel_type']!=1){
  753. $param = $this->param;
  754. }
  755. $list = $productModel->getDealByProduct($param);
  756. $datas = array();
  757. $cycleCount = 0;
  758. $customerCount = 0;
  759. foreach ($list as $key => $value) {
  760. $item = array();
  761. //周期
  762. $customer_ids = $productModel->getCycleByProduct($param, $value['product_id']);
  763. $whereArr = array();
  764. $whereArr['customer_id'] = array('in', $customer_ids);
  765. $cycle = $biCustomerModel->getWhereByCycle($whereArr);
  766. $item['product_name'] = $value['product_name'];
  767. $item['customer_num'] = !empty($value['num']) ? (int)$value['num'] : 0;
  768. $item['cycle'] = !empty($cycle) ? (int)$cycle : 0;
  769. $datas['list'][] = $item;
  770. $cycleCount += $item['cycle'];
  771. $customerCount += $item['customer_num'];
  772. }
  773. $datas['total'] = ['product_name' => '总计', 'cycle' => $cycleCount, 'customer_num' => $customerCount];
  774. //导出使用
  775. if (!empty($param['excel_type'])) return $datas;
  776. return resultArray(['data' => $datas]);
  777. }
  778. /**
  779. * 地区成交周期
  780. * @param
  781. * @return
  782. * @author zhi
  783. */
  784. public function addressCycle($param='')
  785. {
  786. $userModel = new \app\admin\model\User();
  787. $customerModel = new \app\crm\model\Customer();
  788. $biCustomerModel = new \app\bi\model\Customer();
  789. $address_arr = \app\crm\model\Customer::$address;
  790. if($param['excel_type']!=1){
  791. $param = $this->param;
  792. }
  793. if (empty($param['type']) && empty($param['start_time'])) {
  794. $param['type'] = 'month';
  795. }
  796. $map_user_ids = [];
  797. if ($param['user_id']) {
  798. $map_user_ids = array($param['user_id']);
  799. } else {
  800. if ($param['structure_id']) {
  801. $map_user_ids = $userModel->getSubUserByStr($param['structure_id'], 2);
  802. }
  803. }
  804. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  805. $userIds = $map_user_ids ? array_intersect($map_user_ids, $perUserIds) : $perUserIds; //数组交集
  806. $time = getTimeArray();
  807. $prefix = config('database.prefix');
  808. $sql = CustomerModel::alias('a')
  809. ->field([
  810. 'SUBSTR(`a`.`address`, 1, 2)' => 'addr',
  811. 'COUNT(*)' => 'customer_num',
  812. 'SUM(
  813. CASE WHEN ISNULL(b.order_date) THEN 0 ELSE (
  814. UNIX_TIMESTAMP(b.order_date) - a.create_time
  815. ) / 86400 END
  816. )' => 'cycle_sum'
  817. ])
  818. ->join(
  819. "(
  820. SELECT
  821. `customer_id`,
  822. MIN(`order_date`) AS `order_date`
  823. FROM
  824. `{$prefix}crm_contract`
  825. WHERE
  826. `check_status` = 2
  827. GROUP BY
  828. `customer_id`
  829. ) b",
  830. 'a.customer_id = b.customer_id',
  831. 'LEFT'
  832. )
  833. ->where([
  834. 'a.deal_status' => '已成交',
  835. 'a.create_time' => ['BETWEEN', $time['between']],
  836. 'a.owner_user_id' => ['IN', $userIds]
  837. ])
  838. ->group('addr')
  839. ->fetchSql()
  840. ->select();
  841. $list = queryCache($sql);
  842. $list = array_column($list, null, 'addr');
  843. $list['黑龙江'] = $list['黑龙'];
  844. $list['内蒙古'] = $list['内蒙'];
  845. $res = [];
  846. $cycleCount = 0;
  847. $customerCount = 0;
  848. foreach ($address_arr as $val) {
  849. $item['address'] = $val;
  850. $item['customer_num'] = !empty($list[$val]['customer_num']) ? $list[$val]['customer_num'] : 0;
  851. $item['cycle'] = $list[$val]['customer_num'] ? intval($list[$val]['cycle_sum'] / $list[$val]['customer_num']) : 0;
  852. $res['list'][] = $item;
  853. $cycleCount += $item['cycle'];
  854. $customerCount += $item['customer_num'];
  855. }
  856. $res['total'] = ['address' => '总计', 'cycle' => $cycleCount, 'customer_num' => $customerCount];
  857. //导出使用
  858. if (!empty($param['excel_type'])) return $res;
  859. return resultArray(['data' => $res]);
  860. }
  861. /**
  862. * 客户所在城市分析
  863. *
  864. * @return \think\response\Json
  865. * @throws \think\db\exception\DataNotFoundException
  866. * @throws \think\db\exception\ModelNotFoundException
  867. * @throws \think\exception\DbException
  868. */
  869. public function addressAnalyse($param='')
  870. {
  871. if($param['excel_type']!=1){
  872. $param = $this->param;
  873. }
  874. // $customerModel = new \app\crm\model\Customer();
  875. $userModel = new \app\admin\model\User();
  876. $address_arr = \app\crm\model\Customer::$address;
  877. $map_user_ids = [];
  878. # 如果存在员工参数,则不再使用部门参数
  879. if (!empty($param['user_id'])) {
  880. $map_user_ids = array($param['user_id']);
  881. } elseif (!empty($param['structure_id'])) {
  882. $map_user_ids = $userModel->getSubUserByStr($param['structure_id'], 2);
  883. }
  884. if (empty($map_user_ids)) return resultArray(['data' => []]);
  885. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  886. $userIds = $map_user_ids ? array_intersect($map_user_ids, $perUserIds) : $perUserIds; //数组交集
  887. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  888. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  889. $time = getTimeArray($param['start_time'], $param['end_time']);
  890. $sql = CustomerModel::alias('a')
  891. ->field([
  892. 'SUBSTR(`address`, 1, 2)' => 'addr',
  893. 'COUNT(*)' => 'allCustomer',
  894. 'SUM(
  895. CASE WHEN `deal_status` = "已成交" THEN 1 ELSE 0 END
  896. )' => 'dealCustomer',
  897. ])
  898. ->where([
  899. 'create_time' => ['BETWEEN', $time['between']],
  900. 'owner_user_id' => ['IN', $userIds]
  901. ])
  902. ->group('addr')
  903. ->fetchSql()
  904. ->select();
  905. $list = queryCache($sql);
  906. $list = array_column($list, null, 'addr');
  907. $list['黑龙江'] = $list['黑龙'];
  908. $list['内蒙古'] = $list['内蒙'];
  909. $data = [];
  910. foreach ($address_arr as $val) {
  911. $item['address'] = $val;
  912. $item['allCustomer'] = !empty($list[$val]['allCustomer']) ? (int)$list[$val]['allCustomer'] : 0;
  913. $item['dealCustomer'] = !empty($list[$val]['dealCustomer']) ? (int)$list[$val]['dealCustomer'] : 0;
  914. $data[] = $item;
  915. }
  916. //导出使用
  917. if (!empty($param['excel_type'])) return $res;
  918. return resultArray(['data' => $data]);
  919. }
  920. /**
  921. * 客户行业/级别/来源分析
  922. *
  923. * @return \think\response\Json
  924. * @throws \think\db\exception\DataNotFoundException
  925. * @throws \think\db\exception\ModelNotFoundException
  926. * @throws \think\exception\DbException
  927. */
  928. public function portrait()
  929. {
  930. $biCustomerModel = new \app\bi\model\Customer();
  931. $userModel = new \app\admin\model\User();
  932. $adminModel = new \app\admin\model\Admin();
  933. $param = $this->param;
  934. $perUserIds = $userModel->getUserByPer('bi', 'customer', 'read'); //权限范围内userIds
  935. $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  936. $userIds = $whereData['userIds'];
  937. if (!in_array($param['type_analyse'], ['industry', 'source', 'level'])) {
  938. return resultArray(['error' => '参数错误']);
  939. }
  940. $poolWhere = $this->getWhereByPool();
  941. $poolId = db('crm_customer')->alias('customer')->where($poolWhere)->column('customer_id');
  942. $whereArr = array();
  943. $whereArr['types'] = 'crm_customer';
  944. $whereArr['field'] = $param['type_analyse'];
  945. $setting = $biCustomerModel->getOptionByField($whereArr);
  946. if (!empty($param['start_time'])) $param['start_time'] = strtotime($param['start_time'] . ' 00:00:00');
  947. if (!empty($param['end_time'])) $param['end_time'] = strtotime($param['end_time'] . ' 23:59:59');
  948. $time = getTimeArray($param['start_time'], $param['end_time']);
  949. $sql = CustomerModel::field([
  950. "(
  951. CASE WHEN
  952. `{$param['type_analyse']}` = ''
  953. THEN '(空)'
  954. ELSE {$param['type_analyse']} END
  955. )" => $param['type_analyse'],
  956. 'COUNT(*)' => 'allCustomer',
  957. 'SUM(
  958. CASE WHEN `deal_status` = "已成交" THEN 1 ELSE 0 END
  959. )' => 'dealCustomer',
  960. ])
  961. ->where([
  962. 'create_time' => ['BETWEEN', $time['between']],
  963. 'owner_user_id' => ['IN', !empty($userIds) ? $userIds : '9999999999']
  964. ])
  965. ->whereNotIn('customer_id', $poolId)
  966. ->group($param['type_analyse'])
  967. ->fetchSql()
  968. ->select();
  969. $list = queryCache($sql);
  970. $list = array_column($list, null, $param['type_analyse']);
  971. $other_keys = array_diff(array_keys($list), $setting);
  972. $setting = array_merge($setting, $other_keys);
  973. $data = [];
  974. foreach ($setting as $val) {
  975. $item = [];
  976. $item[$param['type_analyse']] = $val;
  977. $item['allCustomer'] = !empty($list[$val]['allCustomer']) ? (int)$list[$val]['allCustomer'] : 0;
  978. $item['dealCustomer'] = !empty($list[$val]['dealCustomer']) ? (int)$list[$val]['dealCustomer'] : 0;
  979. $data[] = $item;
  980. }
  981. return resultArray(['data' => $data]);
  982. }
  983. /**
  984. * [客户公海条件]
  985. * @author Michael_xu
  986. * @param
  987. * @return
  988. */
  989. public function getWhereByPool()
  990. {
  991. $configModel = new \app\crm\model\ConfigData();
  992. $configInfo = $configModel->getData();
  993. $config = $configInfo['config'] ? : 0;
  994. $follow_day = $configInfo['follow_day'] ? : 0;
  995. $deal_day = $configInfo['deal_day'] ? : 0;
  996. $whereData = [];
  997. //启用
  998. if ($config == 1) {
  999. //默认公海条件(没有负责人或已经到期)
  1000. $data['follow_time'] = time()-$follow_day*86400;
  1001. $data['deal_time'] = time()-$deal_day*86400;
  1002. $data['deal_status'] = '未成交';
  1003. if ($follow_day < $deal_day) {
  1004. $whereData = function($query) use ($data){
  1005. $query->where(['customer.owner_user_id'=>0])
  1006. ->whereOr(function ($query) use ($data) {
  1007. $query->where(function ($query) use ($data) {
  1008. $query->where(['customer.update_time' => array('elt',$data['follow_time'])])
  1009. ->whereOr(['customer.deal_time' => array('elt',$data['deal_time'])]);
  1010. })
  1011. ->where(['customer.is_lock' => 0])
  1012. ->where(['customer.deal_status' => ['neq','已成交']]);
  1013. });
  1014. };
  1015. } else {
  1016. $whereData = function($query) use ($data){
  1017. $query->where(['customer.owner_user_id'=>0])
  1018. ->whereOr(function ($query) use ($data) {
  1019. $query->where(function ($query) use ($data) {
  1020. $query->where(['customer.deal_time' => array('elt',$data['deal_time'])]);
  1021. })
  1022. ->where(['customer.is_lock' => 0])
  1023. ->where(['customer.deal_status' => ['neq','已成交']]);
  1024. });
  1025. };
  1026. }
  1027. } else {
  1028. $whereData['customer.owner_user_id'] = 0;
  1029. }
  1030. return $whereData ? : '';
  1031. }
  1032. /**
  1033. * 员工客户满意度分析
  1034. *
  1035. * @param BiCustomerLogic $biCustomerLogic
  1036. * @param string $param
  1037. * @return array|\think\response\Json
  1038. * @throws \think\db\exception\DataNotFoundException
  1039. * @throws \think\db\exception\ModelNotFoundException
  1040. * @throws \think\exception\DbException
  1041. */
  1042. public function customerSatisfaction($param='')
  1043. {
  1044. $adminModel = new \app\admin\model\Admin();
  1045. $userModel = new \app\admin\model\User();
  1046. $param = $this->param;
  1047. $param['start_time'] = !empty($param['start_time']) ? strtotime($param['start_time']) : '';
  1048. $param['end_time'] = !empty($param['end_time']) ? strtotime($param['end_time']) : '';
  1049. if (!empty($param['type'])) {
  1050. # 日期工具类
  1051. $timeArr = getTimeByType($param['type']);
  1052. # 设置日期参数pool
  1053. $param['start_time'] = $timeArr[0];
  1054. $param['end_time'] = $timeArr[1];
  1055. }
  1056. $biCustomerLogic=new BiCustomerLogic();
  1057. $data = $biCustomerLogic->getCustomerSatisfaction($param);
  1058. //导出使用
  1059. if (!empty($param['excel_type'])) return $data;
  1060. return resultArray(['data' => $data]);
  1061. }
  1062. /**
  1063. * 产品满意度分析
  1064. *
  1065. * @param BiCustomerLogic $biCustomerLogic
  1066. * @param string $param
  1067. * @return array|\think\response\Json
  1068. * @throws \think\db\exception\DataNotFoundException
  1069. * @throws \think\db\exception\ModelNotFoundException
  1070. * @throws \think\exception\DbException
  1071. */
  1072. public function productSatisfaction($param='')
  1073. {
  1074. $param = $this->param;
  1075. $userInfo=$this->userInfo;
  1076. $param['start_time'] = !empty($param['start_time']) ? strtotime($param['start_time']) : '';
  1077. $param['end_time'] = !empty($param['end_time']) ? strtotime($param['end_time']) : '';
  1078. if (!empty($param['type'])) {
  1079. # 日期工具类
  1080. $timeArr = getTimeByType($param['type']);
  1081. # 设置日期参数
  1082. $param['start_time'] = $timeArr[0];
  1083. $param['end_time'] = $timeArr[1];
  1084. }
  1085. $biCustomerLogic= new BiCustomerLogic();
  1086. $data = $biCustomerLogic->getProductSatisfaction($param);
  1087. //导出使用
  1088. if (!empty($param['excel_type'])) return $data;
  1089. return resultArray(['data' => $data]);
  1090. }
  1091. /**
  1092. * 导出
  1093. *
  1094. * @return mixed
  1095. * @throws \think\db\exception\DataNotFoundException
  1096. * @throws \think\db\exception\ModelNotFoundException
  1097. * @throws \think\exception\DbException
  1098. */
  1099. public function excelExport()
  1100. {
  1101. $param = $this->param;
  1102. $excel_type = $param['excel_type'];
  1103. $type=[];
  1104. $type['excel_types']=$param['excel_types'];
  1105. switch ($param['excel_types']) {
  1106. case 'statistics':
  1107. $list = $this->statistics($param);
  1108. $list=$list['list'];
  1109. $type['type'] = '客户总量分析列表';
  1110. break;
  1111. case'recordList':
  1112. $list = $this->recordList($param);
  1113. $list=$list['list'];
  1114. $type['type'] = '客户跟进次数分析';
  1115. break;
  1116. case 'recordMode':
  1117. $list = $this->recordMode($param);
  1118. $list=$list['list'];
  1119. $type['type'] = '客户跟进方式分析';
  1120. break;
  1121. case 'poolList':
  1122. $list = $this->poolList($param);
  1123. $type['type'] = '公海客户分析';
  1124. break;
  1125. case 'userCycle':
  1126. $list = $this->userCycle($param);
  1127. $list=$list['list'];
  1128. $type['type'] = '员工客户成交周期分析';
  1129. break;
  1130. case 'customerSatisfaction':
  1131. $list = $this->customerSatisfaction($param);
  1132. $type['type'] = '员工客户满意度分析';
  1133. break;
  1134. case 'productSatisfaction':
  1135. $list = $this->productSatisfaction($param);
  1136. $type['type'] = '产品满意度分析';
  1137. break;
  1138. case 'userCycleList':
  1139. $list = $this->userCycleList($param);
  1140. $list=$list['list'];
  1141. $type['type'] = '成交周期';
  1142. break;
  1143. case 'productCycle':
  1144. $list = $this->addressCycle($param);
  1145. $list=$list['list'];
  1146. $type['type'] = '地区成交周期';
  1147. break;
  1148. case 'addressCycle':
  1149. $list = $this->productCycle($param);
  1150. $list=$list['list'];
  1151. $type['type'] = '地区成交周期';
  1152. break;
  1153. }
  1154. if(empty($list)){
  1155. return resultArray(['data'=>'数据不存在']);
  1156. }
  1157. $excelLogic = new ExcelLogic();
  1158. $data = $excelLogic->biexcle($type, $list);
  1159. return $data;
  1160. }
  1161. }