Platform.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\index\controller;
  4. use app\model\Company;
  5. use app\model\CountryCity;
  6. use app\model\Customer;
  7. use app\model\CustomerClue;
  8. use app\model\CustomerPortraitField;
  9. use app\model\CustomerPortraitFieldSelect;
  10. use app\model\CustomerVisitLog;
  11. use app\model\Employee;
  12. use app\model\Org;
  13. use think\facade\Db;
  14. use think\facade\Request;
  15. class Platform
  16. {
  17. private $company;
  18. private $orgList = [];
  19. private $start_date;
  20. private $end_date;
  21. private $dayOrMonth;
  22. public function __construct()
  23. {
  24. if (!session('?platform')) {
  25. json(['code' => 1, 'msg' => '请登录'])->send();
  26. exit;
  27. }
  28. $employee = session('platform');
  29. // $empid = Request::param('emp_id');
  30. // $employee = Employee::find($empid);
  31. $this->company = Company::where([['root_id', '=', $employee->root_id]])->find();
  32. $start_date = Request::param('start_date');
  33. $end_date = Request::param('end_date');
  34. $this->start_date = empty($start_date) ? date('Y-m-d 00:00:00', strtotime($this->company->addtime)) : date('Y-m-d 00:00:00', strtotime($start_date));
  35. $this->end_date = empty($end_date) ? date('Y-m-d 00:00:00', time()) : date('Y-m-d 00:00:00', strtotime($end_date) + 86400);
  36. $dateDiff = get_date_diff($this->start_date, $this->end_date);
  37. $this->dayOrMonth = $dateDiff->days > 45 ? 'month' : 'day';
  38. $this->orgList = orgSubIds($employee->org_id);
  39. }
  40. /**
  41. * 按月获取线索量
  42. */
  43. public function clue()
  44. {
  45. $sql = CustomerClue::where([
  46. ['org_id', 'in', $this->orgList],
  47. ['addtime', '>=', $this->start_date],
  48. ['addtime', '<', $this->end_date]
  49. ]);
  50. if ($this->dayOrMonth == 'month')
  51. $data = $sql->field('YEAR(addtime) as year, MONTH(addtime) as month, count(*) as clue_num')->group('YEAR(addtime),MONTH(addtime)')->order("year desc,month desc")->select();
  52. else
  53. $data = $sql->field('YEAR(addtime) as year, MONTH(addtime) as month, DAY(addtime) as day,count(*) as clue_num')->group('YEAR(addtime),MONTH(addtime),DAY(addtime)')->order("year desc,month desc, day desc")->select();
  54. $data = $this->setMonthOrDay($data, ['clue_num' => 0]);
  55. $count = array_sum(array_column($data, 'clue_num'));
  56. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  57. }
  58. /**
  59. * 线索详情
  60. */
  61. public function clueDetail(int $page = 1)
  62. {
  63. $condition = [
  64. ['org_id', 'in', $this->orgList],
  65. ['addtime', '>=', $this->start_date],
  66. ['addtime', '<', $this->end_date]
  67. ];
  68. $data = CustomerClue::with(['org', 'employee', 'user'])->where($condition)->order('addtime desc')->page($page, 10)->select();
  69. $data = $data->visible(['nickname', 'employee_name', 'org_name', 'addtime'])->toArray();
  70. $count = CustomerClue::where($condition)->count();
  71. foreach ($data as &$item) {
  72. $orglist = explode('/', $item['org_name']);
  73. $item['org_name'] = array_pop($orglist);
  74. }
  75. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  76. }
  77. /**
  78. * 按月获取加微客户量
  79. */
  80. public function wx()
  81. {
  82. $sql = Customer::where([
  83. ['org_id', 'in', $this->orgList],
  84. ['add_wechat_time', 'NOTNULL', ''],
  85. ['add_wechat_time', '>=', $this->start_date],
  86. ['add_wechat_time', '<', $this->end_date]
  87. ]);
  88. if ($this->dayOrMonth == 'month')
  89. $data = $sql->field('YEAR(add_wechat_time) as year, MONTH(add_wechat_time) as month, count(*) as wx_num')
  90. ->group('YEAR(add_wechat_time),MONTH(add_wechat_time)')
  91. ->order('year desc, month desc')
  92. ->select();
  93. else
  94. $data = $sql->field('YEAR(add_wechat_time) as year, MONTH(add_wechat_time) as month, DAY(add_wechat_time) as day, count(*) as wx_num')
  95. ->group('YEAR(add_wechat_time),MONTH(add_wechat_time),DAY(add_wechat_time)')
  96. ->order('year desc, month desc, day desc')
  97. ->select();
  98. $data = $this->setMonthOrDay($data, ['wx_num' => 0]);
  99. $count = array_sum(array_column($data, 'wx_num'));
  100. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  101. }
  102. public function wxDetail(int $page = 1)
  103. {
  104. $condition = [
  105. ['org_id', 'in', $this->orgList],
  106. ['add_wechat_time', 'NOTNULL', ''],
  107. ['add_wechat_time', '>=', $this->start_date],
  108. ['add_wechat_time', '<', $this->end_date]
  109. ];
  110. $data = Customer::with(['employee', 'org', 'source'])->where($condition)
  111. ->order('add_wechat_time desc')->page($page, 10)->select();
  112. $data = $data->visible(['name', 'add_wechat_time', 'employee.name', ' org.name', 'community_name', 'square', 'source.source'])->toArray();
  113. $count = Customer::where($condition)->count();
  114. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  115. }
  116. /**
  117. * 获取意向客户量以及总量
  118. */
  119. public function intention()
  120. {
  121. $intentionId = CustomerPortraitField::where(['root_id' => $this->company['root_id'], 'keyname' => 'intention'])->value('id');
  122. $intention = CustomerPortraitFieldSelect::where(['pid' => $intentionId])->field('id,name')->select();
  123. $data = [];
  124. foreach ($intention as $i) {
  125. $sql = Customer::where([
  126. ['org_id', 'in', $this->orgList],
  127. ['ext', 'REGEXP', '"value": "' . $i['id'] . '", "keyname": "intention"'],
  128. ['addtime', '>=', $this->start_date],
  129. ['addtime', '<', $this->end_date]
  130. ]);
  131. if ($this->dayOrMonth == 'month')
  132. $intentionMonthData = $sql->field('YEAR(addtime) as year, MONTH(addtime) as month, count(*) as num')->group('YEAR(addtime),MONTH(addtime)')->order('year desc,month desc')->select();
  133. else
  134. $intentionMonthData = $sql->field('YEAR(addtime) as year, MONTH(addtime) as month, DAY(addtime) as day, count(*) as num')->group('YEAR(addtime),MONTH(addtime),DAY(addtime)')->order('year desc,month desc,day desc')->select();
  135. $intentionMonthData = $this->setMonthOrDay($intentionMonthData, ['num' => 0]);
  136. $intentionCount = array_sum(array_column($intentionMonthData, 'num'));
  137. $data[] = ['intention' => $i['name'], 'data' => $intentionMonthData, 'count' => $intentionCount];
  138. }
  139. $total = array_sum(array_column($data, 'count'));
  140. return json(['code' => 0, 'data' => $data, 'count' => $total]);
  141. }
  142. public function intentionDetail(int $page = 1, string $intention = '')
  143. {
  144. $intentionId = CustomerPortraitField::where(['root_id' => $this->company['root_id'], 'keyname' => 'intention'])->value('id');
  145. $condition = [
  146. ['org_id', 'in', $this->orgList],
  147. ['addtime', '>=', $this->start_date],
  148. ['addtime', '<', $this->end_date]
  149. ];
  150. if (empty($intention)) {
  151. $condition[] = ['ext', 'REGEXP', '"value": "[0-9]+", "keyname": "intention"'];
  152. } else {
  153. $intentionId = CustomerPortraitField::where(['root_id' => $this->company['root_id'], 'keyname' => 'intention'])->value('id');
  154. $intention = CustomerPortraitFieldSelect::where(['pid' => $intentionId, 'name' => $intention])->value('id');
  155. $condition[] = ['ext', 'REGEXP', '"value": "' . $intention . '", "keyname": "intention"'];
  156. }
  157. $intentionMonthData = Customer::with(['employee', 'org', 'source'])->where($condition)
  158. ->order('addtime desc')->page($page, 10)->select();
  159. $intentionMonthData = $intentionMonthData->visible(['name', 'employee.name', 'org_name', 'community_name', 'square', 'source.source'])->toArray();
  160. $count = Customer::where($condition)->count();
  161. return json(['code' => 0, 'data' => $intentionMonthData, 'count' => $count]);
  162. }
  163. /**
  164. * 按月获取量房,到店客户量
  165. */
  166. public function tan()
  167. {
  168. $orgList = $this->orgList;
  169. $sql = CustomerVisitLog::withJoin(['customer'])->where([
  170. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')],
  171. ['customer.org_id', 'in', $orgList],
  172. ['customer_visit_log.addtime', '>=', $this->start_date],
  173. ['customer_visit_log.addtime', '<', $this->end_date]
  174. ]);
  175. if ($this->dayOrMonth == 'month')
  176. $daodian = $sql->order('year desc, month desc')
  177. ->group('customer_visit_log.customer_id')->column('YEAR(min(customer_visit_log.addtime)) as year, MONTH(min(customer_visit_log.addtime)) as month, customer_visit_log.customer_id');
  178. else
  179. $daodian = $sql->order('year desc, month desc, day desc')
  180. ->group('customer_visit_log.customer_id')->column('YEAR(min(customer_visit_log.addtime)) as year, MONTH(min(customer_visit_log.addtime)) as month, DAY(min(customer_visit_log.addtime)) as day, customer_visit_log.customer_id');
  181. $daodiancount = $this->yearMonthNum($daodian);
  182. $daodiancount = $this->setMonthOrDay($daodiancount, ['num' => 0]);
  183. $daodiantotal = array_sum(array_column($daodiancount, 'num'));
  184. $sql1 = CustomerVisitLog::withJoin(['customer'])->where([
  185. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')],
  186. ['customer.org_id', 'in', $orgList],
  187. ['customer_visit_log.addtime', '>=', $this->start_date],
  188. ['customer_visit_log.addtime', '<', $this->end_date]
  189. ]);
  190. if ($this->dayOrMonth == 'month')
  191. $liangfang = $sql1->order('year desc, month desc')
  192. ->group('customer_visit_log.customer_id')->column('YEAR(min(customer_visit_log.addtime)) as year, MONTH(min(customer_visit_log.addtime)) as month, customer_visit_log.customer_id');
  193. else
  194. $liangfang = $sql1->order('year desc, month desc, day desc')
  195. ->group('customer_visit_log.customer_id')->column('YEAR(min(customer_visit_log.addtime)) as year, MONTH(min(customer_visit_log.addtime)) as month, DAY(min(customer_visit_log.addtime)) as day, customer_visit_log.customer_id');
  196. $liangfangcount = $this->yearMonthNum($liangfang);
  197. $liangfangcount = $this->setMonthOrDay($liangfangcount, ['num' => 0]);
  198. $liangfangtotal = array_sum(array_column($liangfangcount, 'num'));
  199. return json(['code' => 0, 'data' => [
  200. 'liangfang' => ['data' => $liangfangcount, 'count' => $liangfangtotal],
  201. 'daodian' => ['data' => $daodiancount, 'count' => $daodiantotal]
  202. ]]);
  203. }
  204. /**
  205. * 按区域获取客户量
  206. */
  207. public function region()
  208. {
  209. $city = $this->company['city'];
  210. $cityNum = CountryCity::where(['name' => $city])->value('num');
  211. $provinceNum = substr((string)$cityNum, 0, 2);
  212. $province = CountryCity::where(['num' => $provinceNum])->value('name');
  213. $qu = CountryCity::where([['num', 'like', $cityNum . '%'], ['num', '<>', $cityNum]])->column('name');
  214. $provinceCity = $province . '/' . $city . '/';
  215. $data = [];
  216. foreach ($qu as $q) {
  217. $pcq = $provinceCity . $q;
  218. $q_num = Customer::where([
  219. ['org_id', 'in', $this->orgList],
  220. ['ext', 'like', '%"value": "' . $pcq . '", "keyname": "house_location"%'],
  221. ['addtime', '>=', $this->start_date],
  222. ['addtime', '<', $this->end_date]
  223. ])->count();
  224. $data[] = ['county' => $q, 'count' => $q_num];
  225. }
  226. return json(['code' => 0, 'data' => $data, 'province_city' => $provinceCity]);
  227. }
  228. /**
  229. * 按区域获取客户量
  230. */
  231. public function regionDetail($qu, int $page = 1)
  232. {
  233. $city = $this->company['city'];
  234. $cityNum = CountryCity::where(['name' => $city])->value('num');
  235. $provinceNum = substr((string)$cityNum, 0, 2);
  236. $province = CountryCity::where(['num' => $provinceNum])->value('name');
  237. $pcq = $province . '/' . $city . '/' . $qu;
  238. $data = Customer::with(['employee', 'org', 'source'])
  239. ->where([
  240. ['org_id', 'in', $this->orgList],
  241. ['ext', 'like', '%"value": "' . $pcq . '", "keyname": "house_location"%'],
  242. ['addtime', '>=', $this->start_date],
  243. ['addtime', '<', $this->end_date]
  244. ])->page($page, 10)->select();
  245. $data = $data->visible(['name', 'employee.name', 'org_name', 'community_name', 'square', 'source.source'])->toArray();
  246. $count = Customer::where([
  247. ['org_id', 'in', $this->orgList],
  248. ['ext', 'like', '%"value": "' . $pcq . '", "keyname": "house_location"%'],
  249. ['addtime', '>=', $this->start_date],
  250. ['addtime', '<', $this->end_date]
  251. ])->count();
  252. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  253. }
  254. /**
  255. * 按月获取交定数量和交定金额
  256. */
  257. public function ding()
  258. {
  259. $orgList = $this->orgList;
  260. $sql = CustomerVisitLog::withJoin(['customer'])->where([
  261. ['customer.org_id', 'in', $orgList],
  262. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')],
  263. ['customer_visit_log.addtime', '>=', $this->start_date],
  264. ['customer_visit_log.addtime', '<', $this->end_date]
  265. ]);
  266. if ($this->dayOrMonth == 'month')
  267. $ding = $sql->order('year desc, month desc')
  268. ->group('customer_visit_log.customer_id')
  269. ->column('YEAR(max(customer_visit_log.addtime)) as year, MONTH(max(customer_visit_log.addtime)) as month, customer_visit_log.customer_id, customer.deposit_money as money');
  270. else
  271. $ding = $sql->order('year desc, month desc, day desc')
  272. ->group('customer_visit_log.customer_id')
  273. ->column('YEAR(max(customer_visit_log.addtime)) as year, MONTH(max(customer_visit_log.addtime)) as month, DAY(max(customer_visit_log.addtime)) as day, customer_visit_log.customer_id, customer.deposit_money as money');
  274. $dingcount = $this->yearMonthNum($ding);
  275. $dingcount = $this->setMonthOrDay($dingcount, ['num' => 0, 'money' => 0]);
  276. $total = array_sum(array_column($dingcount, 'num'));
  277. $money = array_sum(array_column($dingcount, 'money'));
  278. return json(['code' => 0, 'data' => $dingcount, 'count' => $total, 'money' => $money]);
  279. }
  280. public function detail(int $page = 1, string $type)
  281. {
  282. $orgList = $this->orgList;
  283. $logQuery = CustomerVisitLog::withJoin('customer')
  284. ->where([
  285. ['customer.org_id', 'in', $orgList],
  286. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已' . $type, 'chaos')],
  287. ])->group('customer.id')->fetchSql(true)->column('customer.id as customer_id, min(customer_visit_log.confirm_date) as first_meet_time');
  288. $qian = Db::table('(' . $logQuery . ') a')
  289. ->where([
  290. ['first_meet_time', '>=', $this->start_date],
  291. ['first_meet_time', '<', $this->end_date]
  292. ])->page($page, 10)->column('customer_id, first_meet_time');
  293. $count = Db::table('(' . $logQuery . ') a')
  294. ->where([
  295. ['first_meet_time', '>=', $this->start_date],
  296. ['first_meet_time', '<', $this->end_date]
  297. ])->count();
  298. $customerIds = array_column($qian, 'customer_id');
  299. $customer = Customer::with(['employee', 'org', 'source'])->where([['id', 'in', $customerIds]])->select();
  300. $customer = $customer->toArray();
  301. $customer = array_combine(array_column($customer, 'id'), $customer);
  302. $data = [];
  303. foreach ($qian as $item) {
  304. $data[] = [
  305. 'name' => $customer[$item['customer_id']]['name'],
  306. 'employee_name' => empty($customer[$item['customer_id']]['employee']) ? '' : $customer[$item['customer_id']]['employee']['name'],
  307. 'org_name' => $customer[$item['customer_id']]['org_name'],
  308. 'community_name' => $customer[$item['customer_id']]['community_name'],
  309. 'square' => $customer[$item['customer_id']]['square'],
  310. 'source' => !isset($customer[$item['customer_id']]['source']) || empty($customer[$item['customer_id']]['source']) ? '' : $customer[$item['customer_id']]['source']['source'],
  311. 'addtime' => $item['first_meet_time']
  312. ];
  313. }
  314. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  315. }
  316. /**
  317. * 按月获取签单数量和签单金额,总数量,总金额
  318. */
  319. public function sign()
  320. {
  321. $orgList = $this->orgList;
  322. $sql = CustomerVisitLog::withJoin(['customer'])->where([
  323. ['customer.org_id', 'in', $orgList],
  324. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')],
  325. ['customer_visit_log.confirm_date', '>=', $this->start_date],
  326. ['customer_visit_log.confirm_date', '<', $this->end_date]
  327. ]);
  328. if ($this->dayOrMonth == 'month')
  329. $sign = $sql->order('year desc, month desc')
  330. ->group('customer_visit_log.customer_id')
  331. ->column('YEAR(max(customer_visit_log.confirm_date)) as year, MONTH(max(customer_visit_log.confirm_date)) as month, customer_visit_log.customer_id, if(customer.signed_money,customer.signed_money,0) as money');
  332. else
  333. $sign = $sql->order('year desc, month desc, day desc')
  334. ->group('customer_visit_log.customer_id')
  335. ->column('YEAR(max(customer_visit_log.confirm_date)) as year, MONTH(max(customer_visit_log.confirm_date)) as month, DAY(max(customer_visit_log.confirm_date)) as day, customer_visit_log.customer_id, if(customer.signed_money,customer.signed_money,0) as money');
  336. $signcount = $this->yearMonthNum($sign);
  337. $signcount = $this->setMonthOrDay($signcount, ['num' => 0, 'money' => 0]);
  338. $signTotal = ['count' => 0, 'money' => 0];
  339. foreach ($signcount as $item) {
  340. $signTotal['count'] += $item['num'];
  341. $signTotal['money'] += $item['money'];
  342. }
  343. return json(['code' => 0, 'data' => ['month' => $signcount, 'total' => $signTotal]]);
  344. }
  345. /**
  346. * 按时间查询总业绩
  347. */
  348. public function yeji()
  349. {
  350. $orgList = $this->orgList;
  351. $data = CustomerVisitLog::withJoin(['customer'])->where([
  352. ['customer.org_id', 'in', $orgList],
  353. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')],
  354. ['customer_visit_log.addtime', '>=', $this->start_date],
  355. ['customer_visit_log.addtime', '<', $this->end_date],
  356. ])->group('customer.id')->column('customer.id, customer.signed_money as money');
  357. $total = array_sum(array_column($data, 'money'));
  358. return json(['code' => 0, 'data' => $total]);
  359. }
  360. /**
  361. * 按时间查询员工排行
  362. */
  363. public function employee()
  364. {
  365. $orgList = $this->orgList;
  366. $data = CustomerVisitLog::withJoin(['customer'])->where([
  367. ['customer.org_id', 'in', $orgList],
  368. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')],
  369. ['customer_visit_log.addtime', '>=', $this->start_date],
  370. ['customer_visit_log.addtime', '<', $this->end_date],
  371. ])->group('customer.employee_id,customer.id')->column('customer.employee_id,customer.signed_money as money');
  372. $d = [];
  373. foreach ($data as $item) {
  374. if (!isset($d[$item['employee_id']])) $d[$item['employee_id']] = 0;
  375. $d[$item['employee_id']] += $item['money'];
  376. }
  377. $employees = Employee::where([['id', 'in', array_keys($d)]])->column('name', 'id');
  378. $r = [];
  379. foreach ($d as $empid => $money) {
  380. $r[] = [
  381. 'employee_name' => $employees[$empid],
  382. 'money' => $money
  383. ];
  384. }
  385. array_multisort(array_column($r, 'money'), SORT_DESC, $r);
  386. return json(['code' => 0, 'data' => $r]);
  387. }
  388. /**
  389. * 按时间查询团队排行
  390. */
  391. public function team()
  392. {
  393. $orgList = $this->orgList;
  394. $data = CustomerVisitLog::withJoin(['customer'])->where([
  395. ['customer.org_id', 'in', $orgList],
  396. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')],
  397. ['customer_visit_log.addtime', '>=', $this->start_date],
  398. ['customer_visit_log.addtime', '<', $this->end_date],
  399. ])->group('customer.id')
  400. ->column('customer.org_id,customer.signed_money as money');
  401. $d = [];
  402. foreach ($data as $item) {
  403. if (!isset($d[$item['org_id']])) $d[$item['org_id']] = 0;
  404. $d[$item['org_id']] += $item['money'];
  405. }
  406. $orgs = Org::where([['id', 'in', array_keys($d)]])->column('name', 'id');
  407. $r = [];
  408. foreach ($d as $orgid => $money) {
  409. $r[] = [
  410. 'org_name' => $orgs[$orgid],
  411. 'money' => $money
  412. ];
  413. }
  414. array_multisort(array_column($r, 'money'), SORT_DESC, $r);
  415. return json(['code' => 0, 'data' => $r]);
  416. }
  417. /**
  418. * 按时间查询业绩排行
  419. */
  420. public function performance()
  421. {
  422. $orgList = $this->orgList;
  423. // 客户总量
  424. $total = Customer::where([['org_id', 'in', $orgList], ['addtime', '>=', $this->start_date], ['addtime', '<', $this->end_date]])->count();
  425. // 加微客户统计
  426. $jiawei = Customer::where([['org_id', 'in', $orgList], ['add_wechat_time', 'NOTNULL', ''], ['add_wechat_time', '>=', $this->start_date], ['add_wechat_time', '<', $this->end_date]])->count();
  427. // 意向客户统计
  428. $yixiang = Customer::where([['org_id', 'in', $this->orgList], ['ext', 'REGEXP', '"value": "[0-9]+", "keyname": "intention"'], ['addtime', '>=', $this->start_date], ['addtime', '<', $this->end_date]])->count();
  429. // 量房客户统计
  430. $liangfang = CustomerVisitLog::withJoin(['customer'])->where([
  431. ['customer.org_id', 'in', $orgList],
  432. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')],
  433. ['customer_visit_log.addtime', '>=', $this->start_date],
  434. ['customer_visit_log.addtime', '<', $this->end_date]
  435. ])->group('customer_visit_log.customer_id')->field('customer_visit_log.customer_id')->count();
  436. // 到店客户统计
  437. $daodian = CustomerVisitLog::withJoin(['customer'])->where([
  438. ['customer.org_id', 'in', $orgList],
  439. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')],
  440. ['customer_visit_log.addtime', '>=', $this->start_date],
  441. ['customer_visit_log.addtime', '<', $this->end_date]
  442. ])->group('customer_visit_log.customer_id')->field('customer_visit_log.customer_id')->count();
  443. // 交定客户统计
  444. $jiaoding = CustomerVisitLog::withJoin(['customer'])->where([
  445. ['customer.org_id', 'in', $orgList],
  446. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')],
  447. ['customer_visit_log.addtime', '>=', $this->start_date],
  448. ['customer_visit_log.addtime', '<', $this->end_date]
  449. ])->group('customer_visit_log.customer_id')->field('customer_visit_log.customer_id')->count();
  450. // 签单客户统计
  451. $sign = CustomerVisitLog::withJoin(['customer'])->where([
  452. ['customer.org_id', 'in', $orgList],
  453. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')],
  454. ['customer_visit_log.addtime', '>=', $this->start_date],
  455. ['customer_visit_log.addtime', '<', $this->end_date]
  456. ])->group('customer_visit_log.customer_id')->field('customer_visit_log.customer_id')->count();
  457. // 二访数据统计
  458. $daodian2 = CustomerVisitLog::withJoin(['customer'])->where([
  459. ['customer.org_id', 'in', $orgList],
  460. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')],
  461. ['customer_visit_log.addtime', '>=', $this->start_date],
  462. ['customer_visit_log.addtime', '<', $this->end_date]
  463. ])->group('customer_visit_log.customer_id')->field('customer_visit_log.customer_id')->having('count(customer_visit_log.id)=2')->count();
  464. // 比例计算
  465. $jiaodingType = '签单';
  466. $qiandanType = '转单';
  467. $data = [
  468. ['lv' => $this->lv($jiawei, $total), 'type' => '加微客户', 'count' => $jiawei],
  469. ['lv' => $this->lv($yixiang, $total), 'type' => '意向客户', 'count' => $yixiang],
  470. ['lv' => $this->lv($liangfang, $total), 'type' => '量房客户', 'count' => $liangfang],
  471. ['lv' => $this->lv($daodian, $total), 'type' => '到店客户', 'count' => $daodian],
  472. ['lv' => $this->lv($daodian2, $total), 'type' => '二访客户', 'count' => $daodian2],
  473. ['lv' => $this->lv($jiaoding, $total), 'type' => $jiaodingType . '客户', 'count' => $jiaoding],
  474. ['lv' => $this->lv($sign, $total), 'type' => $qiandanType . '客户', 'count' => $sign]
  475. ];
  476. // 数据排序
  477. // array_multisort($data, array_column($data, 'count'));
  478. return json(['code' => 0, 'data' => $data]);
  479. }
  480. private function lv($chued, $chu)
  481. {
  482. if (empty($chued)) return '0%';
  483. if (empty($chu)) return '100%';
  484. return round($chued / $chu * 100, 2) . '%';
  485. }
  486. private function yearMonthNum($data)
  487. {
  488. $count = [];
  489. foreach ($data as $row) {
  490. $year = $row['year'];
  491. $month = $row['month'];
  492. // 构建年月的键名
  493. $key = $year . '-' . $month;
  494. if ($this->dayOrMonth == 'day') {
  495. $key .= '-' . $row['day'];
  496. }
  497. if (!isset($count[$key])) $count[$key]['num'] = 0;
  498. $count[$key]['num']++;
  499. if (isset($row['money'])) {
  500. if (!isset($count[$key]['money'])) $count[$key]['money'] = 0;
  501. $count[$key]['money'] += $row['money'];
  502. }
  503. }
  504. $data = [];
  505. foreach ($count as $ym => $num) {
  506. if ($this->dayOrMonth == 'day') {
  507. list($year, $month, $day) = explode('-', $ym);
  508. $r = [
  509. 'year' => $year,
  510. 'month' => $month,
  511. 'day' => $day,
  512. ];
  513. } else {
  514. list($year, $month) = explode('-', $ym);
  515. $r = [
  516. 'year' => $year,
  517. 'month' => $month
  518. ];
  519. }
  520. foreach ($num as $k => $v) {
  521. $r[$k] = $v;
  522. }
  523. $data[] = $r;
  524. }
  525. return $data;
  526. }
  527. /**
  528. * 处理中间没有月份的数据
  529. */
  530. private function setMonthOrDay($existingData, $initData)
  531. {
  532. $startTimestamp = strtotime($this->end_date);
  533. $endTimestamp = strtotime($this->start_date);
  534. $result = array();
  535. while ($startTimestamp >= $endTimestamp) {
  536. $year = date('Y', $startTimestamp);
  537. $month = date('n', $startTimestamp);
  538. $day = date('j', $startTimestamp);
  539. $row = ['year' => $year, 'month' => $month];
  540. $this->dayOrMonth !== 'day' ?: $row['day'] = $day;
  541. $row = array_merge($row, $initData);
  542. foreach ($existingData as $existing) {
  543. if ($existing['year'] == $year && $existing['month'] == $month) {
  544. if ($this->dayOrMonth == 'day' && $existing['day'] == $day) {
  545. $row = $existing;
  546. break;
  547. } elseif ($this->dayOrMonth == 'month') {
  548. $row = $existing;
  549. break;
  550. }
  551. }
  552. }
  553. $result[] = $row;
  554. $startTimestamp = strtotime('-1 ' . $this->dayOrMonth, $startTimestamp);
  555. }
  556. return $result;
  557. }
  558. public function logout()
  559. {
  560. session('platform', null);
  561. return json(['code' => 0, 'msg' => '退出登录成功']);
  562. }
  563. }