Statistics2.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\empcrm\controller;
  4. use app\model\Company;
  5. use app\model\Customer;
  6. use app\model\CustomerDropPool;
  7. use app\model\CustomerExtension;
  8. use app\model\CustomerInvalidLog;
  9. use app\model\CustomerRecycle;
  10. use app\model\CustomerSource;
  11. use app\model\CustomerStateCheck;
  12. use app\model\CustomerVisitLog;
  13. use app\model\Employee;
  14. use app\model\FishCampaign;
  15. use app\model\FishData;
  16. use app\model\Org;
  17. use think\facade\Db;
  18. use think\facade\View;
  19. class Statistics2
  20. {
  21. private $employee;
  22. private $root_id;
  23. private $loginEmployeeOrg;
  24. private $searchOrg;
  25. private $saleOrg;
  26. private $designerOrg;
  27. private $designerList;
  28. private $request;
  29. private $param;
  30. private $searchEmployee;
  31. private $searchDesigner;
  32. public function __construct()
  33. {
  34. $this->request = request();
  35. $this->employee = $this->request->empcrm;
  36. $this->root_id = $this->request->empcrm->root_id;
  37. $orgId = $this->request->param('org_id');
  38. if (empty($orgId)) $orgId = $this->employee->org_id;
  39. $this->loginEmployeeOrg = Org::where([['id', '=', $orgId], ['path', 'like', $this->root_id . '-%']])->find();
  40. if (empty($this->loginEmployeeOrg)) abort(404, '页面异常');
  41. $this->searchOrg = Org::where([['path', 'like', $this->root_id . '-%']])->column('id');
  42. // 时间设置
  43. $start = '2020-01-01';
  44. $end = date('Y-m-d');
  45. $date = $this->request->param('date');
  46. empty($date) ?: list($start, $end) = explode(' - ', $date);
  47. $param['start'] = date('Y-m-d 00:00:00', strtotime($start));
  48. $param['end'] = date('Y-m-d 00:00:00', strtotime($end) + 86400);
  49. $this->param = $param;
  50. }
  51. /**
  52. * 渠道统计
  53. */
  54. public function source()
  55. {
  56. if (!$this->request->isAjax()) {
  57. $data = CustomerSource::where([
  58. ['root_id', '=', $this->root_id]
  59. ])->column('id as value,source as name');
  60. view::assign('source', $data);
  61. View::assign('org', $this->org());
  62. return View::fetch();
  63. }
  64. $sourceIdList = $this->request->param('source');
  65. $condition = [['root_id', '=', $this->root_id]];
  66. if (!empty($sourceIdList)) $condition[] = ['id', 'in', $sourceIdList];
  67. // 渠道查询
  68. $data = CustomerSource::where($condition)->column('id,source');
  69. $count = count($data);
  70. // 投放记录获取
  71. $this->searchOrg = Org::where([['path', 'like', $this->loginEmployeeOrg->path . '%']])->column('id');
  72. $putIn = CustomerExtension::with('source')->where([
  73. ['root_id', '>=', $this->root_id],
  74. ['org_id', 'in', $this->searchOrg],
  75. ['addtime', '>=', $this->param['start']],
  76. ['addtime', '<=', $this->param['end']],
  77. ])->group('source_id')->column('sum(money) as money', 'source_id');
  78. // 线索量获取
  79. $clue = $this->sourceClue();
  80. // 飞鱼线索获取
  81. $fishclue = $this->fish('source');
  82. // 有效线索
  83. $valid = $this->sourceValid();
  84. // 见面量
  85. $meet = $this->sourceMeet();
  86. // 定金量
  87. $deposit = $this->sourceDeposit();
  88. // 合同量/合同金额
  89. $sign = $this->customer('sign');
  90. // 数据合并
  91. $this->dealData($data, $putIn, 'money');
  92. $this->dealData($data, $clue, 'clue');
  93. $this->dealData($data, $valid, 'valid');
  94. $this->dealData($data, $meet, 'meet');
  95. $this->dealData($data, $deposit, 'deposit');
  96. $this->dealData($data, $sign, ['sign', 'sign_money']);
  97. foreach ($data as &$item) {
  98. if (!in_array($item['source'], ['飞鱼线索', '腾讯线索', '欧派线索'])) continue;
  99. foreach ($fishclue as $f => $n) {
  100. if (($f == 1 || $f == 4) && $item['source'] == '飞鱼线索') {
  101. $item['clue'] += $n;
  102. continue;
  103. } elseif ($f == 2 && $item['source'] == '腾讯线索') {
  104. $item['clue'] += $n;
  105. break;
  106. } elseif ($f == 3 && $item['source'] == '欧派线索') {
  107. $item['clue'] += $n;
  108. break;
  109. }
  110. }
  111. }
  112. $this->dealRowData($data, [
  113. 'clue_money' => ['division' => 'money/clue', '*' => 0, 'islv' => 0],
  114. 'valid_money' => ['division' => 'money/valid', '*' => 0, 'islv' => 0],
  115. 'valid_percen' => ['division' => 'valid/clue', '*' => 100, 'islv' => 1],
  116. 'meet_percen' => ['division' => 'meet/valid', '*' => 100, 'islv' => 1],
  117. 'grant' => ['division' => 'meet/clue', '*' => 100, 'islv' => 1],
  118. 'meet_money' => ['division' => 'money/meet', '*' => 0, 'islv' => 0],
  119. 'deposit_percen' => ['division' => 'deposit/meet', '*' => 100, 'islv' => 1],
  120. 'sign_agv_money' => ['division' => 'sign_money/sign', '*' => 0, 'islv' => 0],
  121. 'ROI' => ['division' => 'money/sign_money', '*' => 100, 'islv' => 0, 'lv' => 0],
  122. ], ['source' => '求和']);
  123. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  124. }
  125. /**
  126. * 销售/客服统计
  127. */
  128. public function sale()
  129. {
  130. View::assign('org', $this->org());
  131. if (!$this->request->isAjax()) return View::fetch();
  132. $this->saleOrg = Org::where([['path', 'like', $this->loginEmployeeOrg->path . '%'], ['org_type', '=', 1]])->column('id');
  133. // 获取员工列表和员工数
  134. $data = Employee::where([
  135. ['root_id', '=', $this->root_id],
  136. ['org_id', 'in', $this->saleOrg],
  137. ['state', '=', '在职'],
  138. ['verified', '=', 1],
  139. ['uid', '<>', 0]
  140. ])->column('id,name');
  141. $count = count($data);
  142. if ($count == 0) return json(['code' => 0, 'data' => $data, 'count' => $count]);
  143. // 线索量
  144. $clue = $this->employeeClue();
  145. // 飞鱼线索获取
  146. $fishclue = $this->fish('employee');
  147. // 有效量
  148. $valid = $this->employeeValid();
  149. // 待确认
  150. $toBeConfirmed = $this->employeeToBeConfirmed();
  151. // 见面量
  152. $meet = $this->employeeMeet();
  153. // 定金量
  154. $deposit = $this->employeeDeposit();
  155. // 合同量
  156. $sign = $this->customer('employee_sign');
  157. $this->dealData($data, $clue, 'clue');
  158. $this->dealData($data, $valid, 'valid');
  159. $this->dealData($data, $toBeConfirmed, 'to_be_confirmed');
  160. $this->dealData($data, $meet, 'meet');
  161. $this->dealData($data, $deposit, 'deposit');
  162. $this->dealData($data, $sign, ['sign', 'sign_money']);
  163. $validState = CustomerStateCheck::where([
  164. ['root_id', '=', $this->root_id],
  165. ['org_id', 'in', $this->saleOrg],
  166. ['check_state', '=', 1]
  167. ])->column('count(*) as num', 'employee_id');
  168. foreach ($data as &$item) {
  169. if (isset($fishclue[$item['id']])) $item['clue'] += $fishclue[$item['id']];
  170. $item['valid_check_num'] = $validState[$item['id']] ?? 0;
  171. }
  172. $this->dealRowData($data, [
  173. 'valid_percen' => ['division' => 'valid/clue', '*' => 100, 'islv' => 1],
  174. 'meet_percen' => ['division' => 'meet/valid', '*' => 100, 'islv' => 1],
  175. 'grant' => ['division' => 'meet/clue', '*' => 100, 'islv' => 1],
  176. 'deposit_percen' => ['division' => 'deposit/meet', '*' => 100, 'islv' => 1]
  177. ], ['name' => '求和']);
  178. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  179. }
  180. /**
  181. * 设计师统计
  182. */
  183. public function designer()
  184. {
  185. if (!$this->request->isAjax()) {
  186. View::assign('org', $this->org());
  187. return View::fetch();
  188. }
  189. $this->designerOrg = Org::where([['path', 'like', $this->loginEmployeeOrg->path . '%'], ['org_type', '=', 2]])->column('id');
  190. $data = Employee::where([
  191. ['root_id', '=', $this->root_id],
  192. ['org_id', 'in', $this->designerOrg],
  193. ['state', '=', '在职'],
  194. ['verified', '=', 1],
  195. ['uid', '<>', 0]
  196. ])->column('id,name');
  197. $this->designerList = array_column($data, 'id');
  198. // 获取员工列表和员工数
  199. $count = count($data);
  200. if ($count == 0) return json(['code' => 0, 'data' => $data, 'count' => $count]);
  201. // 见面量
  202. $meet = $this->designerMeet();
  203. // 定金量
  204. $deposit = $this->designerDeposit();
  205. // 合同量/业绩总额
  206. $sign = $this->customer('designer_sign');
  207. $this->dealData($data, $meet, 'meet');
  208. $this->dealData($data, $deposit, 'deposit');
  209. $this->dealData($data, $sign, ['sign', 'sign_money']);
  210. $this->dealRowData($data, [
  211. 'deposit_percen' => ['division' => 'deposit/meet', '*' => 100, 'islv' => 1]
  212. ], ['name' => '求和']);
  213. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  214. }
  215. /**
  216. * 设计师部门统计
  217. */
  218. public function designerDepartment()
  219. {
  220. if (!$this->request->isAjax()) return View::fetch();
  221. $this->designerOrg = Org::where([['path', 'like', $this->loginEmployeeOrg->path . '%'], ['org_type', '=', 2]])->column('id');
  222. $data = Employee::where([
  223. ['root_id', '=', $this->root_id],
  224. ['org_id', 'in', $this->designerOrg],
  225. ['state', '=', '在职'],
  226. ['verified', '=', 1],
  227. ['uid', '<>', 0]
  228. ])->column('id,org_id');
  229. $this->designerList = array_column($data, 'id');
  230. // 获取员工列表和员工数
  231. if (count($data) == 0) return json(['code' => 0, 'data' => $data, 'count' => 0]);
  232. // 见面量
  233. $meet = $this->designerMeet();
  234. // 定金量
  235. $deposit = $this->designerDeposit();
  236. // 合同量/业绩总额
  237. $sign = $this->customer('designer_sign');
  238. $this->dealData($data, $meet, 'meet');
  239. $this->dealData($data, $deposit, 'deposit');
  240. $this->dealData($data, $sign, ['sign', 'sign_money']);
  241. $departmentData = Org::where([['path', 'like', $this->loginEmployeeOrg->path . '%'], ['org_type', '=', 2]])->column('id,name');
  242. $dealData = [];
  243. foreach ($data as $designer) {
  244. if (!isset($dealData[$designer['org_id']])) $dealData[$designer['org_id']] = ['meet' => 0, 'deposit' => 0, 'sign' => 0, 'sign_money' => 0];
  245. $dealData[$designer['org_id']]['meet'] += $designer['meet'];
  246. $dealData[$designer['org_id']]['deposit'] += $designer['deposit'];
  247. $dealData[$designer['org_id']]['sign'] += $designer['sign'];
  248. $dealData[$designer['org_id']]['sign_money'] += $designer['sign_money'];
  249. }
  250. foreach ($departmentData as $k => $depart) {
  251. if (isset($dealData[$depart['id']])) {
  252. $departmentData[$k]['meet'] = $dealData[$depart['id']]['meet'];
  253. $departmentData[$k]['deposit'] = $dealData[$depart['id']]['deposit'];
  254. $departmentData[$k]['sign'] = $dealData[$depart['id']]['sign'];
  255. $departmentData[$k]['sign_money'] = $dealData[$depart['id']]['sign_money'];
  256. } else {
  257. $departmentData[$k] = array_merge($depart, ['meet' => 0, 'deposit' => 0, 'sign' => 0, 'sign_money' => 0]);
  258. }
  259. }
  260. $count = count($departmentData);
  261. $this->dealRowData($departmentData, [
  262. 'deposit_percen' => ['division' => 'deposit/meet', '*' => 100, 'islv' => 1]
  263. ], ['name' => '求和']);
  264. return json(['code' => 0, 'data' => $departmentData, 'count' => $count]);
  265. }
  266. /**
  267. * 飞鱼线索统计
  268. */
  269. private function fish($type)
  270. {
  271. $data = [];
  272. switch ($type) {
  273. case 'source':
  274. $data = FishData::where([
  275. ['org_id', 'in', $this->searchOrg],
  276. ['is_allocation', '=', 0],
  277. ['create_time_detail', '>=', $this->param['start']],
  278. ['create_time_detail', '<', $this->param['end']]
  279. ])->group('type')->column('count(id) as num', 'type');
  280. break;
  281. case 'employee':
  282. $data = FishData::where([
  283. ['org_id', 'in', $this->saleOrg],
  284. ['is_allocation', '=', 0],
  285. ['create_time_detail', '>=', $this->param['start']],
  286. ['create_time_detail', '<', $this->param['end']]
  287. ])->group('employee_id')->column('count(id) as num', 'employee_id');
  288. break;
  289. }
  290. return $data;
  291. }
  292. /**
  293. * 投放数据获取
  294. */
  295. private function putin($type, $adid)
  296. {
  297. $data = [];
  298. switch ($type) {
  299. case 'money': // 投放金额
  300. $data = FishCampaign::where([
  301. ['advertiser_id', 'in', $adid],
  302. ['data_time', '>=', $this->param['start']],
  303. ['data_time', '<', $this->param['end']]
  304. ])->group('advertiser_id')->column('sum(`cost`) as exten_money', 'advertiser_id');
  305. break;
  306. case 'clue': // 线索量
  307. $data = FishData::where([
  308. ['advertiser_id', 'in', $adid],
  309. ['create_time', '>=', $this->param['start']],
  310. ['create_time', '<', $this->param['end']]
  311. ])->column('count(*) as clue_num', 'advertiser_id');
  312. break;
  313. case 'valid': // 有效量
  314. $data = Customer::alias('customer')->join('fish_data fish', 'customer.id=fish.customer_id')
  315. ->where([
  316. ['fish.advertiser_id', 'in', $adid],
  317. ['fish.create_time', '>=', $this->param['start']],
  318. ['fish.create_time', '<', $this->param['end']],
  319. ['customer.source_id', 'NOTNULL', ''],
  320. ['customer.source_id', '<>', 0],
  321. ['customer.valid_time', 'NOTNULL', '']
  322. ])->column('count(*) as valid_num', 'fish.advertiser_id');
  323. break;
  324. case 'meet':
  325. $logQuery = CustomerVisitLog::alias('customer_visit_log')->join('fish_data fish', 'customer_visit_log.customer_id=fish.customer_id')
  326. ->where([
  327. ['fish.advertiser_id', 'in', $adid],
  328. ['fish.create_time', '>=', $this->param['start']],
  329. ['fish.create_time', '<', $this->param['end']],
  330. ['customer_visit_log.state', 'in', [2, 3, 7, 8, 9]],
  331. ])->group('customer_visit_log.customer_id')->fetchSql(true)->column('customer_visit_log.customer_id as id, fish.advertiser_id');
  332. $data = Db::table('(' . $logQuery . ') a')
  333. ->group('advertiser_id')->column('count(id) as meet_num', 'advertiser_id');
  334. break;
  335. case 'deposit':
  336. $logQuery = CustomerVisitLog::alias('customer_visit_log')->join('fish_data fish', 'customer_visit_log.customer_id=fish.customer_id')
  337. ->where([
  338. ['fish.advertiser_id', 'in', $adid],
  339. ['fish.create_time', '>=', $this->param['start']],
  340. ['fish.create_time', '<', $this->param['end']],
  341. ['customer_visit_log.state', 'in', [2, 3]],
  342. ])->group('customer_visit_log.customer_id')->fetchSql(true)->column('customer_visit_log.customer_id as id, fish.advertiser_id');
  343. $data = Db::table('(' . $logQuery . ') a')
  344. ->group('advertiser_id')->column('count(id) as meet_num', 'advertiser_id');
  345. break;
  346. case 'sign':
  347. $logQuery = CustomerVisitLog::alias('customer_visit_log')
  348. ->join('fish_data fish', 'customer_visit_log.customer_id=fish.customer_id')
  349. ->join('customer customer', 'customer.id=customer_visit_log.customer_id')
  350. ->where([
  351. ['fish.advertiser_id', 'in', $adid],
  352. ['fish.create_time', '>=', $this->param['start']],
  353. ['fish.create_time', '<', $this->param['end']],
  354. ['customer_visit_log.state', 'in', [3]],
  355. ])->group('customer_visit_log.customer_id')->fetchSql(true)->column('customer_visit_log.customer_id as id, fish.advertiser_id, customer.signed_money');
  356. $data = Db::table('(' . $logQuery . ') a')
  357. ->group('advertiser_id')->column('count(id) as meet_num, sum(signed_money) as signed_money', 'advertiser_id');
  358. break;
  359. }
  360. return $data;
  361. }
  362. /**
  363. * 线索量(按来源)
  364. */
  365. private function sourceClue()
  366. {
  367. $data = [];
  368. $this->merge($data, $this->customer('clue'));
  369. // 回收站表线索统计
  370. $this->merge($data, $this->customerRecycle('clue'));
  371. // 自动回收表线索统计
  372. $this->merge($data, $this->customerDropPool('clue'));
  373. return $data;
  374. }
  375. /**
  376. * 有效量(按来源)
  377. */
  378. private function sourceValid()
  379. {
  380. //客户有效统计
  381. $data = $this->customer('valid');
  382. // 回收站表线索统计
  383. $rdata = $this->customerRecycle('valid');
  384. $this->merge($data, $rdata);
  385. // 自动回收表线索统计
  386. $ddata = $this->customerDropPool('valid');
  387. $this->merge($data, $ddata);
  388. return $data;
  389. }
  390. /**
  391. * 见面量(按来源)
  392. */
  393. private function sourceMeet()
  394. {
  395. $data = [];
  396. $this->merge($data, $this->customer('meet'));
  397. $this->merge($data, $this->customerRecycle('meet'));
  398. $this->merge($data, $this->customerDropPool('meet'));
  399. return $data;
  400. }
  401. /**
  402. * 定金量(按来源)
  403. */
  404. private function sourceDeposit()
  405. {
  406. $data = [];
  407. $this->merge($data, $this->customer('deposit'));
  408. $this->merge($data, $this->customerRecycle('deposit'));
  409. $this->merge($data, $this->customerDropPool('deposit'));
  410. return $data;
  411. }
  412. /**
  413. * 线索量(按员工)
  414. */
  415. private function employeeClue()
  416. {
  417. $data = [];
  418. // 客户表线索统计
  419. $cdata = $this->customer('employee_clue');
  420. $this->merge($data, $cdata);
  421. // 客户无效表(获取无效时,客户所属员工)
  422. $iData = $this->customerRecycle('employee_clue');
  423. $this->merge($data, $iData);
  424. // 自动回收表线索统计
  425. $ddata = $this->customerDropPool('employee_clue');
  426. $this->merge($data, $ddata);
  427. return $data;
  428. }
  429. /**
  430. * 待确认
  431. */
  432. private function employeeToBeConfirmed()
  433. {
  434. $data = [];
  435. // 客户表待确认统计
  436. $this->merge($data, $this->customer("employee_to_be_confirmed"));
  437. // 客户回收表待确认统计
  438. $this->merge($data, $this->customerDropPool("employee_to_be_confirmed"));
  439. return $data;
  440. }
  441. /**
  442. * 有效量
  443. */
  444. private function employeeValid()
  445. {
  446. //客户有效统计
  447. $data = $this->customer('employee_valid');
  448. // 回收站表线索统计
  449. $rdata = $this->customerRecycle('employee_valid');
  450. $this->merge($data, $rdata);
  451. // 自动回收表线索统计
  452. $ddata = $this->customerDropPool('employee_valid');
  453. $this->merge($data, $ddata);
  454. return $data;
  455. }
  456. /**
  457. * 见面量
  458. */
  459. private function employeeMeet()
  460. {
  461. $data = [];
  462. $this->merge($data, $this->customer('employee_meet'));
  463. $this->merge($data, $this->customerRecycle('employee_meet'));
  464. $this->merge($data, $this->customerDropPool('employee_meet'));
  465. return $data;
  466. }
  467. /**
  468. * 定金量
  469. */
  470. private function employeeDeposit()
  471. {
  472. $data = [];
  473. $this->merge($data, $this->customer('employee_deposit'));
  474. $this->merge($data, $this->customerRecycle('employee_deposit'));
  475. $this->merge($data, $this->customerDropPool('employee_deposit'));
  476. return $data;
  477. }
  478. /**
  479. * 见面量(设计师)
  480. */
  481. private function designerMeet()
  482. {
  483. $data = [];
  484. $this->merge($data, $this->customer('designer_meet'));
  485. $this->merge($data, $this->customerDropPool('designer_meet'));
  486. return $data;
  487. }
  488. /**
  489. * 定金量(设计师)
  490. */
  491. private function designerDeposit()
  492. {
  493. $data = [];
  494. $this->merge($data, $this->customer('designer_deposit'));
  495. $this->merge($data, $this->customerDropPool('designer_deposit'));
  496. return $data;
  497. }
  498. /**
  499. * customer表查询
  500. */
  501. private function customer($type)
  502. {
  503. $data = [];
  504. switch ($type) {
  505. case 'clue':
  506. // 客户表线索统计
  507. $data = Customer::where([
  508. ['org_id', 'in', $this->searchOrg],
  509. ['sign_time', '>=', $this->param['start']],
  510. ['sign_time', '<', $this->param['end']],
  511. ['source_id', 'NOTNULL', ''],
  512. ['source_id', '<>', 0]
  513. ])->group('source_id')->column('count(id) as num', 'source_id');
  514. break;
  515. case 'employee_clue':
  516. $data = Customer::where([
  517. ['org_id', 'in', $this->saleOrg],
  518. ['employee_id', 'NOTNULL', ''],
  519. ['sign_time', '>=', $this->param['start']],
  520. ['sign_time', '<', $this->param['end']]
  521. ])->group('employee_id')->column('count(id) as num', 'employee_id');
  522. $start = $this->param['start'];
  523. $end = $this->param['end'];
  524. $saleOrg = $this->saleOrg;
  525. $data1 = CustomerInvalidLog::where([['id', 'in', function ($query) use ($start, $end, $saleOrg) {
  526. $query->name('customer_invalid_log')->where('customer_id', 'in', function ($query) use ($start, $end, $saleOrg) {
  527. $query->name('Customer')->where([
  528. ['org_id', 'in', $saleOrg],
  529. ['employee_id', 'NULL', ''],
  530. ['sign_time', '>=', $start],
  531. ['sign_time', '<', $end]
  532. ])->field('id');
  533. })->group('customer_id')->field('max(id)');
  534. }]])->group('employee_id')->column('count(id) as num', 'employee_id');
  535. foreach ($data1 as $k => $n) {
  536. if (isset($data[$k])) $data[$k] += $n;
  537. else $data[$k] = $n;
  538. }
  539. break;
  540. case 'employee_to_be_confirmed':
  541. $data = Customer::where([
  542. ['org_id', 'in', $this->saleOrg],
  543. ['employee_id', 'NOTNULL', ''],
  544. ['sign_time', '>=', $this->param['start']],
  545. ['sign_time', '<', $this->param['end']],
  546. ['state', '=', 0]
  547. ])->group('employee_id')->column('count(id) as num', 'employee_id');
  548. break;
  549. case 'valid':
  550. $data = Customer::where([
  551. ['org_id', 'in', $this->searchOrg],
  552. ['sign_time', '>=', $this->param['start']],
  553. ['sign_time', '<', $this->param['end']],
  554. ['source_id', 'NOTNULL', ''],
  555. ['source_id', '<>', 0],
  556. ['valid_time', 'NOTNULL', '']
  557. ])->group('source_id')->column('count(id) as num', 'source_id');
  558. break;
  559. case 'employee_valid':
  560. $data = Customer::where([
  561. ['org_id', 'in', $this->saleOrg],
  562. ['valid_time', '>=', $this->param['start']],
  563. ['valid_time', '<', $this->param['end']],
  564. ['employee_id', 'NOTNULL', '']
  565. ])->group('employee_id')->column('count(id) as num', 'employee_id');
  566. $customerIdList = Customer::where([
  567. ['org_id', 'in', $this->saleOrg],
  568. ['employee_id', 'NULL', ''],
  569. ['valid_time', '>=', $this->param['start']],
  570. ['valid_time', '<', $this->param['end']]
  571. ])->group('id')->column('id');
  572. $lastLogId = CustomerInvalidLog::where([['customer_id', 'in', $customerIdList]])->group('customer_id')->column('max(id)');
  573. $data1 = CustomerInvalidLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'employee_id');
  574. foreach ($data1 as $k => $n) {
  575. if (isset($data[$k])) $data[$k] += $n;
  576. else $data[$k] = $n;
  577. }
  578. break;
  579. case 'meet':
  580. $logQuery = CustomerVisitLog::withJoin('customer')
  581. ->where([
  582. ['customer.org_id', 'in', $this->searchOrg],
  583. ['customer_visit_log.state', 'in', [2, 3, 7, 8, 9]],
  584. ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.source_id as source_id, min(customer_visit_log.confirm_date) as first_meet_time');
  585. $data = Db::table('(' . $logQuery . ') a')
  586. ->where([
  587. ['first_meet_time', '>=', $this->param['start']],
  588. ['first_meet_time', '<', $this->param['end']]
  589. ])->group('source_id')->column('count(id) as num', 'source_id');
  590. break;
  591. case "employee_meet":
  592. $logQuery = CustomerVisitLog::withJoin('customer')
  593. ->where([
  594. ['customer.org_id', 'in', $this->saleOrg],
  595. ['customer_visit_log.state', 'in', [2, 3, 7, 8, 9]],
  596. ['customer.employee_id', 'NOTNULL', '']
  597. ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.employee_id as employee_id, min(customer_visit_log.confirm_date) as first_meet_time');
  598. $data = Db::table('(' . $logQuery . ') a')
  599. ->where([
  600. ['first_meet_time', '>=', $this->param['start']],
  601. ['first_meet_time', '<', $this->param['end']]
  602. ])->group('employee_id')->column('count(id) as num', 'employee_id');
  603. $logQuery1 = CustomerVisitLog::withJoin('customer')
  604. ->where([
  605. ['customer.org_id', 'in', $this->saleOrg],
  606. ['customer_visit_log.state', 'in', [2, 3, 7, 8, 9]],
  607. ['customer.employee_id', 'NULL', '']
  608. ])->group('customer.id')->fetchSql(true)->column('customer.id as id, min(customer_visit_log.confirm_date) as first_meet_time');
  609. $customerIdList = Db::table('(' . $logQuery1 . ') a')
  610. ->where([
  611. ['first_meet_time', '>=', $this->param['start']],
  612. ['first_meet_time', '<', $this->param['end']]
  613. ])->fetchSql(true)->column('id');
  614. $lastLogId = CustomerInvalidLog::where([['customer_id', 'in', $customerIdList]])->fetchSql(true)->group('customer_id')->column('max(id)');
  615. $data1 = CustomerInvalidLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'employee_id');
  616. foreach ($data1 as $k => $n) {
  617. if (isset($data[$k])) $data[$k] += $n;
  618. else $data[$k] = $n;
  619. }
  620. break;
  621. case "designer_meet":
  622. $logQuery = CustomerVisitLog::withJoin('customer')
  623. ->where([
  624. ['customer.org_id', 'in', $this->searchOrg],
  625. ['customer_visit_log.state', 'in', [2, 3, 7, 8, 9]],
  626. ['customer.designer_id', 'NOTNULL', ''],
  627. ['customer.designer_id', 'in', $this->designerList]
  628. ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.designer_id as employee_id, min(customer_visit_log.confirm_date) as first_meet_time');
  629. $data = Db::table('(' . $logQuery . ') a')
  630. ->where([
  631. ['first_meet_time', '>=', $this->param['start']],
  632. ['first_meet_time', '<', $this->param['end']]
  633. ])->group('employee_id')->column('count(id) as num', 'employee_id');
  634. $logQuery1 = CustomerVisitLog::withJoin('customer')
  635. ->where([
  636. ['customer.org_id', 'in', $this->searchOrg],
  637. ['customer_visit_log.state', 'in', [2, 3, 7, 8, 9]],
  638. ['customer.employee_id', 'NULL', '']
  639. ])->group('customer.id')->fetchSql(true)->column('customer.id as id, min(customer_visit_log.confirm_date) as first_meet_time');
  640. $customerIdList = Db::table('(' . $logQuery1 . ') a')
  641. ->where([
  642. ['first_meet_time', '>=', $this->param['start']],
  643. ['first_meet_time', '<', $this->param['end']]
  644. ])->fetchSql(true)->column('id');
  645. $lastLogId = CustomerInvalidLog::where([['customer_id', 'in', $customerIdList]])->group('customer_id')->column('max(id)');
  646. $data1 = CustomerInvalidLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'designer_id');
  647. foreach ($data1 as $k => $n) {
  648. if (isset($data[$k])) $data[$k] += $n;
  649. else $data[$k] = $n;
  650. }
  651. break;
  652. case 'deposit':
  653. $logQuery = CustomerVisitLog::withJoin('customer')
  654. ->where([
  655. ['customer.org_id', 'in', $this->searchOrg],
  656. ['customer_visit_log.state', 'in', [2, 3]],
  657. ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.source_id as source_id, min(customer_visit_log.confirm_date) as first_meet_time');
  658. $data = Db::table('(' . $logQuery . ') a')
  659. ->where([
  660. ['first_meet_time', '>=', $this->param['start']],
  661. ['first_meet_time', '<', $this->param['end']]
  662. ])->group('source_id')->column('count(id) as num', 'source_id');
  663. break;
  664. case 'employee_deposit':
  665. $logQuery = CustomerVisitLog::withJoin('customer')
  666. ->where([
  667. ['customer.org_id', 'in', $this->saleOrg],
  668. ['customer_visit_log.state', 'in', [2, 3]],
  669. ['customer.employee_id', 'NOTNULL', '']
  670. ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.employee_id as employee_id, min(customer_visit_log.confirm_date) as first_meet_time');
  671. $data = Db::table('(' . $logQuery . ') a')
  672. ->where([
  673. ['first_meet_time', '>=', $this->param['start']],
  674. ['first_meet_time', '<', $this->param['end']]
  675. ])->group('employee_id')->column('count(id) as num', 'employee_id');
  676. $logQuery1 = CustomerVisitLog::withJoin('customer')
  677. ->where([
  678. ['customer.org_id', 'in', $this->saleOrg],
  679. ['customer_visit_log.state', 'in', [2, 3]],
  680. ['customer.employee_id', 'NULL', '']
  681. ])->group('customer.id')->fetchSql(true)->column('customer.id as id, min(customer_visit_log.confirm_date) as first_meet_time');
  682. $customerIdList = Db::table('(' . $logQuery1 . ') a')
  683. ->where([
  684. ['first_meet_time', '>=', $this->param['start']],
  685. ['first_meet_time', '<', $this->param['end']]
  686. ])->fetchSql(true)->column('id');
  687. $lastLogId = CustomerInvalidLog::where([['customer_id', 'in', $customerIdList]])->fetchSql(true)->group('customer_id')->column('max(id)');
  688. $data1 = CustomerInvalidLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'employee_id');
  689. foreach ($data1 as $k => $n) {
  690. if (isset($data[$k])) $data[$k] += $n;
  691. else $data[$k] = $n;
  692. }
  693. break;
  694. case 'designer_deposit':
  695. $logQuery = CustomerVisitLog::withJoin('customer')
  696. ->where([
  697. ['customer.org_id', 'in', $this->searchOrg],
  698. ['customer_visit_log.state', 'in', [2, 3]],
  699. ['customer.designer_id', 'NOTNULL', ''],
  700. ['customer.designer_id', 'in', $this->designerList]
  701. ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.designer_id as employee_id, min(customer_visit_log.confirm_date) as first_meet_time');
  702. $data = Db::table('(' . $logQuery . ') a')
  703. ->where([
  704. ['first_meet_time', '>=', $this->param['start']],
  705. ['first_meet_time', '<', $this->param['end']]
  706. ])->group('employee_id')->column('count(id) as num', 'employee_id');
  707. $logQuery1 = CustomerVisitLog::withJoin('customer')
  708. ->where([
  709. ['customer.org_id', 'in', $this->searchOrg],
  710. ['customer_visit_log.state', 'in', [2, 3]],
  711. ['customer.employee_id', 'NULL', '']
  712. ])->group('customer.id')->fetchSql(true)->column('customer.id as id, min(customer_visit_log.confirm_date) as first_meet_time');
  713. $customerIdList = Db::table('(' . $logQuery1 . ') a')
  714. ->where([
  715. ['first_meet_time', '>=', $this->param['start']],
  716. ['first_meet_time', '<', $this->param['end']]
  717. ])->fetchSql(true)->column('id');
  718. $lastLogId = CustomerInvalidLog::where([['customer_id', 'in', $customerIdList]])->fetchSql(true)->group('customer_id')->column('max(id)');
  719. $data1 = CustomerInvalidLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'designer_id');
  720. foreach ($data1 as $k => $n) {
  721. if (isset($data[$k])) $data[$k] += $n;
  722. else $data[$k] = $n;
  723. }
  724. break;
  725. case 'sign':
  726. $logQuery = CustomerVisitLog::withJoin('customer')
  727. ->where([
  728. ['customer.org_id', 'in', $this->searchOrg],
  729. ['customer_visit_log.state', '=', 3],
  730. ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.source_id as source_id, customer.signed_money as signed_money, min(customer_visit_log.confirm_date) as first_meet_time');
  731. $data = Db::table('(' . $logQuery . ') a')
  732. ->where([
  733. ['first_meet_time', '>=', $this->param['start']],
  734. ['first_meet_time', '<', $this->param['end']]
  735. ])->group('source_id')->column('count(id) as sign, sum(signed_money) as sign_money', 'source_id');
  736. break;
  737. case 'employee_sign':
  738. $logQuery = CustomerVisitLog::withJoin('customer')
  739. ->where([
  740. ['customer.org_id', 'in', $this->saleOrg],
  741. ['customer_visit_log.state', '=', 3],
  742. ['customer.employee_id', 'NOTNULL', '']
  743. ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.employee_id as employee_id, customer.signed_money as signed_money, min(customer_visit_log.confirm_date) as first_meet_time');
  744. $data = Db::table('(' . $logQuery . ') a')
  745. ->where([
  746. ['first_meet_time', '>=', $this->param['start']],
  747. ['first_meet_time', '<', $this->param['end']]
  748. ])->group('employee_id')->column('count(id) as sign, sum(signed_money) as sign_money', 'employee_id');
  749. break;
  750. case 'designer_sign':
  751. $logQuery = CustomerVisitLog::withJoin('customer')
  752. ->where([
  753. ['customer.org_id', 'in', $this->searchOrg],
  754. ['customer_visit_log.state', '=', 3],
  755. ['customer.designer_id', 'NOTNULL', ''],
  756. ['customer.designer_id', 'in', $this->designerList]
  757. ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.designer_id as employee_id, customer.signed_money as signed_money, min(customer_visit_log.confirm_date) as first_meet_time');
  758. $data = Db::table('(' . $logQuery . ') a')
  759. ->where([
  760. ['first_meet_time', '>=', $this->param['start']],
  761. ['first_meet_time', '<', $this->param['end']]
  762. ])->group('employee_id')->column('count(id) as sign, sum(signed_money) as sign_money', 'employee_id');
  763. break;
  764. }
  765. return $data;
  766. }
  767. /**
  768. * customer表查询
  769. */
  770. private function customerRecycle($type)
  771. {
  772. $data = [];
  773. switch ($type) {
  774. case 'clue':
  775. // 客户表线索统计
  776. $data = CustomerRecycle::where([
  777. ['org_id', 'in', $this->searchOrg],
  778. ['sign_time', '>=', $this->param['start']],
  779. ['sign_time', '<', $this->param['end']],
  780. ['source_id', 'NOTNULL', ''],
  781. ['source_id', '<>', 0]
  782. ])->group("source_id")->column('count(id) as num', "source_id");
  783. break;
  784. case 'employee_clue':
  785. $customerIdList = Db::name('customer_visit_log')
  786. ->alias('log')
  787. ->join('customer_recycle customer', 'log.customer_id = customer.customer_id')
  788. ->where([
  789. ['customer.org_id', 'in', $this->saleOrg],
  790. ['log.customer_employee_id', '<>', 0],
  791. ['customer.sign_time', '>=', $this->param['start']],
  792. ['customer.sign_time', '<', $this->param['end']]
  793. ])
  794. ->group('log.customer_id')
  795. ->column('log.customer_id');
  796. if (empty($customerIdList)) {
  797. $data = [];
  798. break;
  799. }
  800. $lastLogId = CustomerVisitLog::where([['customer_id', 'in', $customerIdList], ['customer_employee_id', '<>', 0]])->group('customer_id')->column('max(id)');
  801. $data = CustomerVisitLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'customer_employee_id');
  802. break;
  803. case 'valid':
  804. $data = CustomerRecycle::where([
  805. ['org_id', 'in', $this->searchOrg],
  806. ['valid_time', '>=', $this->param['start']],
  807. ['valid_time', '<', $this->param['end']],
  808. ['source_id', 'NOTNULL', ''],
  809. ['source_id', '<>', 0]
  810. ])->group("source_id")->column('count(id) as num', "source_id");
  811. break;
  812. case 'employee_valid':
  813. $customerIdList = CustomerRecycle::where([
  814. ['org_id', 'in', $this->saleOrg],
  815. ['valid_time', '>=', $this->param['start']],
  816. ['valid_time', '<', $this->param['end']]
  817. ])->column('customer_id');
  818. if (empty($customerIdList)) {
  819. $data = [];
  820. break;
  821. }
  822. $lastLogId = CustomerVisitLog::where([['customer_id', 'in', $customerIdList], ['customer_employee_id', '<>', 0]])->group('customer_id')->column('max(id)');
  823. $data = CustomerVisitLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'customer_employee_id');
  824. break;
  825. case 'meet':
  826. $recycleQuery = Db::name('customer_visit_log')
  827. ->alias('log')
  828. ->join('customer_recycle customer', 'log.customer_id = customer.customer_id')
  829. ->where([
  830. ['customer.org_id', 'in', $this->searchOrg],
  831. ['log.state', 'in', [2, 3, 7, 8, 9]],
  832. ])
  833. ->group('log.customer_id')
  834. ->field('log.customer_id as id, customer.source_id as source_id, min(log.confirm_date) as first_meet_time')
  835. ->BuildSql();
  836. $data = Db::table($recycleQuery . ' a')
  837. ->where([
  838. ['first_meet_time', '>=', $this->param['start']],
  839. ['first_meet_time', '<', $this->param['end']]
  840. ])->group('source_id')->column('count(id) as num', 'source_id');
  841. break;
  842. case 'employee_meet':
  843. $recycleQuery = Db::name('customer_visit_log')
  844. ->alias('log')
  845. ->join('customer_recycle customer', 'log.customer_id = customer.customer_id')
  846. ->where([
  847. ['customer.org_id', 'in', $this->saleOrg],
  848. ['log.state', 'in', [2, 3, 7, 8, 9]],
  849. ['log.customer_employee_id', '<>', 0]
  850. ])
  851. ->group('log.customer_id')
  852. ->fetchSql(true)
  853. ->column('log.customer_id as id, min(log.confirm_date) as first_meet_time');
  854. $customerIdList = Db::table('(' . $recycleQuery . ') a')
  855. ->where([
  856. ['first_meet_time', '>=', $this->param['start']],
  857. ['first_meet_time', '<', $this->param['end']]
  858. ])->column('id');
  859. if (empty($customerIdList)) {
  860. $data = [];
  861. break;
  862. }
  863. $lastLogId = CustomerVisitLog::where([['customer_id', 'in', $customerIdList], ['customer_employee_id', '<>', 0]])->group('customer_id')->column('max(id)');
  864. $data = CustomerVisitLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'customer_employee_id');
  865. break;
  866. case 'deposit':
  867. $recycleQuery = Db::name('customer_visit_log')
  868. ->alias('log')
  869. ->join('customer_recycle customer', 'log.customer_id = customer.customer_id')
  870. ->where([
  871. ['customer.org_id', 'in', $this->searchOrg],
  872. ['log.state', 'in', [2, 3]],
  873. ])
  874. ->group('log.customer_id')
  875. ->field('log.customer_id as id, customer.source_id as source_id, min(log.confirm_date) as first_meet_time')
  876. ->BuildSql();
  877. $data = Db::table($recycleQuery . ' a')
  878. ->where([
  879. ['first_meet_time', '>=', $this->param['start']],
  880. ['first_meet_time', '<', $this->param['end']]
  881. ])->group('source_id')->column('count(id) as num', 'source_id');
  882. break;
  883. case 'employee_deposit':
  884. $recycleQuery = Db::name('customer_visit_log')
  885. ->alias('log')
  886. ->join('customer_recycle customer', 'log.customer_id = customer.customer_id')
  887. ->where([
  888. ['customer.org_id', 'in', $this->saleOrg],
  889. ['log.state', 'in', [2, 3]],
  890. ['log.customer_employee_id', '<>', 0]
  891. ])
  892. ->group('log.customer_id')
  893. ->fetchSql(true)
  894. ->column('log.customer_id as id, min(log.confirm_date) as first_meet_time');
  895. $customerIdList = Db::table('(' . $recycleQuery . ') a')
  896. ->where([
  897. ['first_meet_time', '>=', $this->param['start']],
  898. ['first_meet_time', '<', $this->param['end']]
  899. ])->column('id');
  900. if (empty($customerIdList)) {
  901. $data = [];
  902. break;
  903. }
  904. $lastLogId = CustomerVisitLog::where([['customer_id', 'in', $customerIdList], ['customer_employee_id', '<>', 0]])->group('customer_id')->column('max(id)');
  905. $data = CustomerVisitLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'customer_employee_id');
  906. break;
  907. }
  908. return $data;
  909. }
  910. /**
  911. * customer表查询
  912. */
  913. private function customerDropPool($type)
  914. {
  915. $data = [];
  916. switch ($type) {
  917. case 'clue':
  918. // 客户表线索统计
  919. $data = CustomerDropPool::where([
  920. ['org_id', 'in', $this->searchOrg],
  921. ['sign_time', '>=', $this->param['start']],
  922. ['sign_time', '<', $this->param['end']],
  923. ['source_id', 'NOTNULL', ''],
  924. ['source_id', '<>', 0]
  925. ])->group("source_id")->column('count(id) as num', "source_id");
  926. break;
  927. case 'employee_clue':
  928. $data = CustomerDropPool::where([
  929. ['org_id', 'in', $this->saleOrg],
  930. ['employee_id', 'NOTNULL', ''],
  931. ['sign_time', '>=', $this->param['start']],
  932. ['sign_time', '<', $this->param['end']]
  933. ])->group("employee_id")->column('count(id) as num', "employee_id");
  934. break;
  935. case 'employee_to_be_confirmed':
  936. $data = CustomerDropPool::where([
  937. ['org_id', 'in', $this->saleOrg],
  938. ['employee_id', 'NOTNULL', ''],
  939. ['sign_time', '>=', $this->param['start']],
  940. ['sign_time', '<', $this->param['end']],
  941. ['state', '=', 0]
  942. ])->group('employee_id')->column('count(id) as num', 'employee_id');
  943. case 'valid':
  944. $data = CustomerDropPool::where([
  945. ['org_id', 'in', $this->searchOrg],
  946. ['valid_time', '>=', $this->param['start']],
  947. ['valid_time', '<', $this->param['end']],
  948. ['source_id', 'NOTNULL', ''],
  949. ['source_id', '<>', 0]
  950. ])->group("source_id")->column('count(id) as num', "source_id");
  951. break;
  952. case 'employee_valid':
  953. $data = CustomerDropPool::where([
  954. ['org_id', 'in', $this->saleOrg],
  955. ['valid_time', '>=', $this->param['start']],
  956. ['valid_time', '<', $this->param['end']],
  957. ['employee_id', 'NOTNULL', '']
  958. ])->group("employee_id")->column('count(id) as num', "employee_id");
  959. break;
  960. case 'meet':
  961. $poolQuery = Db::name('customer_visit_log')
  962. ->alias('log')
  963. ->join('customer_drop_pool customer', 'log.customer_id = customer.customer_id')
  964. ->where([
  965. ['customer.org_id', 'in', $this->searchOrg],
  966. ['log.state', 'in', [2, 3, 7, 8, 9]],
  967. ])
  968. ->group('log.customer_id')
  969. ->field('log.customer_id as id, customer.source_id as source_id, min(log.confirm_date) as first_meet_time')
  970. ->BuildSql();
  971. $data = Db::table($poolQuery . ' a')
  972. ->where([
  973. ['first_meet_time', '>=', $this->param['start']],
  974. ['first_meet_time', '<', $this->param['end']]
  975. ])->group('source_id')->column('count(id) as num', 'source_id');
  976. break;
  977. case 'employee_meet':
  978. $poolQuery = Db::name('customer_visit_log')
  979. ->alias('log')
  980. ->join('customer_drop_pool customer', 'log.customer_id = customer.customer_id')
  981. ->where([
  982. ['customer.org_id', 'in', $this->saleOrg],
  983. ['log.state', 'in', [2, 3, 7, 8, 9]],
  984. ])
  985. ->group('log.customer_id')
  986. ->field('log.customer_id as id, customer.employee_id as employee_id, min(log.confirm_date) as first_meet_time')
  987. ->BuildSql();
  988. $data = Db::table($poolQuery . ' a')
  989. ->where([
  990. ['first_meet_time', '>=', $this->param['start']],
  991. ['first_meet_time', '<', $this->param['end']]
  992. ])->group('employee_id')->column('count(id) as num', 'employee_id');
  993. break;
  994. case 'designer_meet':
  995. $poolQuery = Db::name('customer_visit_log')
  996. ->alias('log')
  997. ->join('customer_drop_pool customer', 'log.customer_id = customer.customer_id')
  998. ->where([
  999. ['customer.org_id', 'in', $this->searchOrg],
  1000. ['log.state', 'in', [2, 3, 7, 8, 9]],
  1001. ['customer.designer_id', 'NOTNULL', ''],
  1002. ['customer.designer_id', 'in', $this->designerList]
  1003. ])
  1004. ->group('log.customer_id')
  1005. ->field('log.customer_id as id, customer.designer_id as employee_id, min(log.confirm_date) as first_meet_time')
  1006. ->BuildSql();
  1007. $data = Db::table($poolQuery . ' a')
  1008. ->where([
  1009. ['first_meet_time', '>=', $this->param['start']],
  1010. ['first_meet_time', '<', $this->param['end']]
  1011. ])->group('employee_id')->column('count(id) as num', 'employee_id');
  1012. break;
  1013. case 'deposit':
  1014. $poolQuery = Db::name('customer_visit_log')
  1015. ->alias('log')
  1016. ->join('customer_drop_pool customer', 'log.customer_id = customer.customer_id')
  1017. ->where([
  1018. ['customer.org_id', 'in', $this->searchOrg],
  1019. ['log.state', 'in', [2, 3]],
  1020. ])
  1021. ->group('log.customer_id')
  1022. ->field('log.customer_id as id, customer.source_id as source_id, min(log.confirm_date) as first_meet_time')
  1023. ->BuildSql();
  1024. $data = Db::table($poolQuery . ' a')
  1025. ->where([
  1026. ['first_meet_time', '>=', $this->param['start']],
  1027. ['first_meet_time', '<', $this->param['end']]
  1028. ])->group('source_id')->column('count(id) as num', 'source_id');
  1029. break;
  1030. case 'employee_deposit':
  1031. $poolQuery = Db::name('customer_visit_log')
  1032. ->alias('log')
  1033. ->join('customer_drop_pool customer', 'log.customer_id = customer.customer_id')
  1034. ->where([
  1035. ['customer.org_id', 'in', $this->saleOrg],
  1036. ['log.state', 'in', [2, 3]],
  1037. ])
  1038. ->group('log.customer_id')
  1039. ->field('log.customer_id as id, customer.employee_id as employee_id, min(log.confirm_date) as first_meet_time')
  1040. ->BuildSql();
  1041. $data = Db::table($poolQuery . ' a')
  1042. ->where([
  1043. ['first_meet_time', '>=', $this->param['start']],
  1044. ['first_meet_time', '<', $this->param['end']]
  1045. ])->group('employee_id')->column('count(id) as num', 'employee_id');
  1046. break;
  1047. case 'designer_deposit':
  1048. $poolQuery = Db::name('customer_visit_log')
  1049. ->alias('log')
  1050. ->join('customer_drop_pool customer', 'log.customer_id = customer.customer_id')
  1051. ->where([
  1052. ['customer.org_id', 'in', $this->searchOrg],
  1053. ['log.state', 'in', [2, 3]],
  1054. ['customer.designer_id', 'NOTNULL', ''],
  1055. ['customer.designer_id', 'in', $this->designerList]
  1056. ])
  1057. ->group('log.customer_id')
  1058. ->field('log.customer_id as id, customer.designer_id as employee_id, min(log.confirm_date) as first_meet_time')
  1059. ->BuildSql();
  1060. $data = Db::table($poolQuery . ' a')
  1061. ->where([
  1062. ['first_meet_time', '>=', $this->param['start']],
  1063. ['first_meet_time', '<', $this->param['end']]
  1064. ])->group('employee_id')->column('count(id) as num', 'employee_id');
  1065. break;
  1066. }
  1067. return $data;
  1068. }
  1069. /**
  1070. * 数据合并
  1071. */
  1072. private function merge(&$data, $mergeData)
  1073. {
  1074. foreach ($mergeData as $k => $num) {
  1075. if (isset($data[$k])) {
  1076. $data[$k] += $num;
  1077. } else {
  1078. $data[$k] = $num;
  1079. }
  1080. }
  1081. }
  1082. /**
  1083. * 数据处理
  1084. */
  1085. private function dealData(&$data, $mergeData, $kname, $searchK = 'id')
  1086. {
  1087. foreach ($data as &$d) {
  1088. if (is_array($kname)) {
  1089. foreach ($kname as $name) {
  1090. if (isset($mergeData[$d[$searchK]])) {
  1091. $d[$name] = floatval($mergeData[$d[$searchK]][$name]);
  1092. } else {
  1093. $d[$name] = 0;
  1094. }
  1095. }
  1096. } else {
  1097. if (isset($mergeData[$d[$searchK]])) {
  1098. $d[$kname] = floatval($mergeData[$d[$searchK]]);
  1099. } else {
  1100. $d[$kname] = 0;
  1101. }
  1102. }
  1103. }
  1104. }
  1105. /**
  1106. * 除法
  1107. */
  1108. private function division($divisor, $dividend, $lv = true)
  1109. {
  1110. if (is_string($divisor)) $divisor = floatval($divisor);
  1111. if (is_string($dividend)) $dividend = floatval($dividend);
  1112. if ($dividend == 0) {
  1113. if ($divisor == 0) return 0;
  1114. return $lv ? $divisor : 0;
  1115. }
  1116. $lv ? $w = 4 : $w = 2;
  1117. return floatval(round($divisor / $dividend, $w));
  1118. }
  1119. /**
  1120. * 数据计算
  1121. */
  1122. private function dealRowData(&$data, $suan, $totalRow)
  1123. {
  1124. $excludeKey = array_keys($totalRow);
  1125. foreach ($data as $row) {
  1126. foreach ($row as $k => $col) {
  1127. if (in_array($k, $excludeKey)) continue;
  1128. if ($k == 'id') continue;
  1129. if (!isset($totalRow[$k])) $totalRow[$k] = 0;
  1130. $totalRow[$k] += $col;
  1131. }
  1132. }
  1133. $data[] = $totalRow;
  1134. foreach ($data as $k => $item) {
  1135. foreach ($suan as $column => $s) {
  1136. list($divisor, $dividend) = explode('/', $s['division']);
  1137. isset($s['lv']) ? $lv = $s['lv'] : $lv = true;
  1138. $rs = $this->division($item[$divisor], $item[$dividend], $lv);
  1139. $s['*'] == 0 ?: $rs = $rs * $s['*'];
  1140. $s['islv'] != 1 ?: $rs = $rs . '%';
  1141. $data[$k][$column] = $rs;
  1142. }
  1143. }
  1144. }
  1145. /**
  1146. * 部门获取
  1147. */
  1148. private function org()
  1149. {
  1150. $data = Org::where([['id', 'in', $this->searchOrg], ['pid', '<>', 0]])->field('id,name,pid')->order('pid,id')->select();
  1151. return $data;
  1152. }
  1153. }