123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 消息模块
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\controller;
  8. use app\admin\controller\ApiCommon;
  9. use app\crm\logic\InvoiceLogic;
  10. use app\crm\logic\MessageLogic;
  11. use think\Cache;
  12. use think\cache\driver\Redis;
  13. use think\Db;
  14. use think\Hook;
  15. use think\Request;
  16. class Message extends ApiCommon
  17. {
  18. /**
  19. * 用于判断权限
  20. * @permission 无限制
  21. * @allow 登录用户可访问
  22. * @other 其他根据系统设置
  23. **/
  24. public function _initialize()
  25. {
  26. $action = [
  27. 'permission' => [],
  28. 'allow' => [
  29. 'num',
  30. 'todayleads',
  31. 'todaycustomer',
  32. 'todaybusiness',
  33. 'followleads',
  34. 'followcustomer',
  35. 'checkcontract',
  36. 'checkreceivables',
  37. 'remindreceivablesplan',
  38. 'endcontract',
  39. 'remindcustomer',
  40. 'checkinvoice',
  41. 'visitcontract',
  42. 'alldeal'
  43. ]
  44. ];
  45. Hook::listen('check_auth',$action);
  46. $request = Request::instance();
  47. $a = strtolower($request->action());
  48. if (!in_array($a, $action['permission'])) {
  49. parent::_initialize();
  50. }
  51. }
  52. /**
  53. * 系统通知
  54. *
  55. * @author Michael_xu
  56. * @return
  57. */
  58. public function index()
  59. {
  60. $messageModel = model('Message');
  61. $param = $this->param;
  62. $userInfo = $this->userInfo;
  63. $param['user_id'] = $userInfo['id'];
  64. $param['module_name'] = 'crm';
  65. $data = $messageModel->getDataList($param);
  66. return resultArray(['data' => $data]);
  67. }
  68. /**
  69. * 消息数
  70. *
  71. * @return \think\response\Json
  72. * @throws \think\exception\DbException
  73. */
  74. public function num()
  75. {
  76. $userInfo = $this->userInfo;
  77. $configDataModel = model('ConfigData');
  78. $configData = $configDataModel->getData();
  79. $data = [];
  80. # 今日需联系线索
  81. $todayLeadsTime = cache('todayLeadsTime'.$userInfo['id']);
  82. $todayLeadsCount = cache('todayLeadsCount'.$userInfo['id']);
  83. if (time() <= $todayLeadsTime) {
  84. $data['todayLeads'] = (int)$todayLeadsCount;
  85. } else {
  86. $todayLeads = $this->todayLeads(true);
  87. $data['todayLeads'] = $todayLeads['dataCount'] ? : 0;
  88. cache('todayLeadsCount'.$userInfo['id'], $data['todayLeads']);
  89. cache('todayLeadsTime'.$userInfo['id'], time() + 180);
  90. }
  91. # 今日需联系客户
  92. $todayCustomerTime = cache('todayCustomerTime'.$userInfo['id']);
  93. $todayCustomerCount = cache('todayCustomerCount'.$userInfo['id']);
  94. if (time() <= $todayCustomerTime) {
  95. $data['todayCustomer'] = (int)$todayCustomerCount;
  96. } else {
  97. $todayCustomer = $this->todayCustomer(true);
  98. $data['todayCustomer'] = $todayCustomer['dataCount'] ? : 0;
  99. cache('todayCustomerCount'.$userInfo['id'], $data['todayCustomer']);
  100. cache('todayCustomerTime'.$userInfo['id'], time() + 180);
  101. }
  102. # 今日需联系商机
  103. $todayBusinessTime = cache('todayBusinessTime'.$userInfo['id']);
  104. $todayBusinessCount = cache('todayBusinessCount'.$userInfo['id']);
  105. if (time() <= $todayBusinessTime) {
  106. $data['todayBusiness'] = (int)$todayBusinessCount;
  107. } else {
  108. $todayBusiness = $this->todayBusiness(true);
  109. $data['todayBusiness'] = $todayBusiness['dataCount'] ? : 0;
  110. cache('todayBusinessCount'.$userInfo['id'], $data['todayBusiness']);
  111. cache('todayBusinessTime'.$userInfo['id'], time() + 180);
  112. }
  113. # 分配给我的线索
  114. $followLeadsTime = cache('followLeadsTime'.$userInfo['id']);
  115. $followLeadsCount = cache('followLeadsCount'.$userInfo['id']);
  116. if (time() <= $followLeadsTime) {
  117. $data['followLeads'] = (int)$followLeadsCount;
  118. } else {
  119. $followLeads = $this->followLeads(true);
  120. $data['followLeads'] = $followLeads['dataCount'] ? : 0;
  121. cache('followLeadsCount'.$userInfo['id'], $data['followLeads']);
  122. cache('followLeadsTime'.$userInfo['id'], time() + 180);
  123. }
  124. # 分配给我的客户
  125. $followCustomerTime = cache('followCustomerTime'.$userInfo['id']);
  126. $followCustomerCount = cache('followCustomerCount'.$userInfo['id']);
  127. if (time() <= $followCustomerTime) {
  128. $data['followCustomer'] = (int)$followCustomerCount;
  129. } else {
  130. $followCustomer = $this->followCustomer(true);
  131. $data['followCustomer'] = $followCustomer['dataCount'] ? : 0;
  132. cache('followCustomerCount'.$userInfo['id'], $data['followCustomer']);
  133. cache('followCustomerTime'.$userInfo['id'], time() + 180);
  134. }
  135. # 待审核合同
  136. $checkContractTime = cache('checkContractTime'.$userInfo['id']);
  137. $checkContractCount = cache('checkContractCount'.$userInfo['id']);
  138. if (time() <= $checkContractTime) {
  139. $data['checkContract'] = (int)$checkContractCount;
  140. } else {
  141. $checkContract = $this->checkContract(true);
  142. $data['checkContract'] = $checkContract['dataCount'] ? : 0;
  143. cache('checkContractCount'.$userInfo['id'], $data['checkContract']);
  144. cache('checkContractTime'.$userInfo['id'], time() + 180);
  145. }
  146. # 待审核回款
  147. $checkReceivablesTime = cache('checkReceivablesTime'.$userInfo['id']);
  148. $checkReceivablesCount = cache('checkReceivablesCount'.$userInfo['id']);
  149. if (time() <= $checkReceivablesTime) {
  150. $data['checkReceivables'] = (int)$checkReceivablesCount;
  151. } else {
  152. $checkReceivables = $this->checkReceivables(true);
  153. $data['checkReceivables'] = $checkReceivables['dataCount'] ? : 0;
  154. cache('checkReceivablesCount'.$userInfo['id'], $data['checkReceivables']);
  155. cache('checkReceivablesTime'.$userInfo['id'], time() + 180);
  156. }
  157. # 待审核发票
  158. $checkInvoiceTime = cache('checkInvoiceTime'.$userInfo['id']);
  159. $checkInvoiceCount = cache('checkInvoiceCount'.$userInfo['id']);
  160. if (time() <= $checkInvoiceTime) {
  161. $data['checkInvoice'] = (int)$checkInvoiceCount;
  162. } else {
  163. $checkInvoice = $this->checkInvoice(true);
  164. $data['checkInvoice'] = $checkInvoice['dataCount'] ? : 0;
  165. cache('checkInvoiceCount'.$userInfo['id'], $data['checkInvoice']);
  166. cache('checkInvoiceTime'.$userInfo['id'], time() + 180);
  167. }
  168. # 待回款提醒
  169. $remindReceivablesPlanTime = cache('remindReceivablesPlanTime'.$userInfo['id']);
  170. $remindReceivablesPlanCount = cache('remindReceivablesPlanCount'.$userInfo['id']);
  171. if (time() <= $remindReceivablesPlanTime) {
  172. $data['remindReceivablesPlan'] = (int)$remindReceivablesPlanCount;
  173. } else {
  174. $remindReceivablesPlan = $this->remindReceivablesPlan(true);
  175. $data['remindReceivablesPlan'] = $remindReceivablesPlan['dataCount'] ? : 0;
  176. cache('remindReceivablesPlanCount'.$userInfo['id'], $data['remindReceivablesPlan']);
  177. cache('remindReceivablesPlanTime'.$userInfo['id'], time() + 180);
  178. }
  179. if ($configData['visit_config'] == 1) {
  180. # 待回访合同
  181. $visitContractTime = cache('visitContractTime'.$userInfo['id']);
  182. $visitContractCount = cache('visitContractCount'.$userInfo['id']);
  183. if (time() <= $visitContractTime) {
  184. $data['returnVisitRemind'] = (int)$visitContractCount;
  185. } else {
  186. $visitContract = $this->visitContract(true);
  187. $data['returnVisitRemind'] = $visitContract['dataCount'] ? : 0;
  188. cache('visitContractCount'.$userInfo['id'], $data['returnVisitRemind']);
  189. cache('visitContractTime'.$userInfo['id'], time() + 180);
  190. }
  191. }
  192. # 即将到期合同
  193. if ($configData['contract_config'] == 1) {
  194. $endContractTime = cache('endContractTime'.$userInfo['id']);
  195. $endContractCount = cache('endContractCount'.$userInfo['id']);
  196. if (time() <= $endContractTime) {
  197. $data['endContract'] = (int)$endContractCount;
  198. } else {
  199. $endContract = $this->endContract(true);
  200. $data['endContract'] = $endContract['dataCount'] ? : 0;
  201. cache('endContractCount'.$userInfo['id'], $data['endContract']);
  202. cache('endContractTime'.$userInfo['id'], time() + 180);
  203. }
  204. }
  205. # 待进入公海提醒
  206. $pool = db('crm_customer_pool')->where(['status' => 1, 'remind_conf' => 1])->count();
  207. if (!empty($pool)) {
  208. $remindCustomerTime = cache('remindCustomerTime'.$userInfo['id']);
  209. $remindCustomerCount = cache('remindCustomerCount'.$userInfo['id']);
  210. if (time() <= $remindCustomerTime) {
  211. $data['putInPoolRemind'] = (int)$remindCustomerCount;
  212. } else {
  213. $remindCustomer = $this->remindCustomer(true);
  214. $data['putInPoolRemind'] = !empty($remindCustomer['dataCount']) ? $remindCustomer['dataCount'] : 0;
  215. cache('remindCustomerCount'.$userInfo['id'], $data['putInPoolRemind']);
  216. cache('remindCustomerTime'.$userInfo['id'], time() + 180);
  217. }
  218. }
  219. return resultArray(['data' => $data]);
  220. }
  221. /**
  222. * 今日需联系线索
  223. *
  224. * @param false $getCount
  225. * @return array|\think\response\Json
  226. */
  227. public function todayLeads($getCount = false)
  228. {
  229. $param = $this->param;
  230. $userId = $this->userInfo['id'];
  231. $types = $param['types'];
  232. unset($param['types']);
  233. $param['user_id'] = $userId;
  234. if ($getCount == true) $param['getCount'] = 1;
  235. $messageLogic= new MessageLogic();
  236. $data = $messageLogic->todayLeads($param);
  237. if ($types == 'list') return resultArray(['data' => $data]);
  238. return $data;
  239. }
  240. /**
  241. * 今日需联系客户
  242. *
  243. * @param string $getCount
  244. * @return \think\response\Json
  245. */
  246. public function todayCustomer($getCount = false)
  247. {
  248. $param = $this->param;
  249. $userId = $this->userInfo['id'];
  250. $types = $param['types'];
  251. if ($getCount == true) {
  252. $param['getCount'] = 1;
  253. }
  254. unset($param['types']);
  255. $param['user_id'] = $userId;
  256. $messageLogic= new MessageLogic();
  257. $data = $messageLogic->remindCustomer($param);
  258. if ($types == 'list') {
  259. return resultArray(['data' => $data]);
  260. }
  261. return $data;
  262. }
  263. /**
  264. * 今日需联系商机
  265. *
  266. * @param false $getCount
  267. * @return array|\think\response\Json
  268. * @throws \think\db\exception\DataNotFoundException
  269. * @throws \think\db\exception\ModelNotFoundException
  270. * @throws \think\exception\DbException
  271. */
  272. public function todayBusiness($getCount = false)
  273. {
  274. $param = $this->param;
  275. $userId = $this->userInfo['id'];
  276. $types = $param['types'];
  277. unset($param['types']);
  278. if ($getCount == true) $param['getCount'] = 1;
  279. $messageLogic= new MessageLogic();
  280. $param['user_id'] = $userId;
  281. $data = $messageLogic->todayBusiness($param);
  282. if ($types == 'list') return resultArray(['data' => $data]);
  283. return $data;
  284. }
  285. /**
  286. * 分配给我的线索
  287. * @author Michael_xu
  288. * @return
  289. */
  290. public function followLeads($getCount = false)
  291. {
  292. $param = $this->param;
  293. $userInfo = $this->userInfo;
  294. $types = $param['types'];
  295. unset($param['types']);
  296. if ($getCount == true) $param['getCount'] = 1;
  297. $param['user_id'] = $userInfo['id'];
  298. $messageLogic=new MessageLogic();
  299. $data = $messageLogic->followLeads($param);
  300. if ($types == 'list') {
  301. return resultArray(['data' => $data]);
  302. }
  303. return $data;
  304. }
  305. /**
  306. * 分配给我的客户
  307. * @author Michael_xu
  308. * @return
  309. */
  310. public function followCustomer($getCount = false)
  311. {
  312. $param = $this->param;
  313. $userInfo = $this->userInfo;
  314. $types = $param['types'];
  315. if ($getCount == true) {
  316. $param['getCount'] = 1;
  317. }
  318. unset($param['types']);
  319. $messageLogic=new MessageLogic();
  320. $param['user_id'] = $userInfo['id'];
  321. $data = $messageLogic->followCustomer($param);
  322. if ($types == 'list') {
  323. return resultArray(['data' => $data]);
  324. }
  325. return $data;
  326. }
  327. /**
  328. * 待审核合同
  329. *
  330. * @param false $getCount
  331. * @return \think\response\Json
  332. */
  333. public function checkContract($getCount = false)
  334. {
  335. $param = $this->param;
  336. $userInfo = $this->userInfo;
  337. $types = $param['types'];
  338. unset($param['types']);
  339. if ($getCount == true) {
  340. $param['getCount'] = 1;
  341. }
  342. $messageLogic=new MessageLogic();
  343. $param['user_id'] = $userInfo['id'];
  344. $data = $messageLogic->checkContract($param);
  345. if ($types == 'list') {
  346. return resultArray(['data' => $data]);
  347. }
  348. return $data;
  349. }
  350. /**
  351. * 待审核回款
  352. * @author Michael_xu
  353. * @return
  354. */
  355. public function checkReceivables($getCount = false)
  356. {
  357. $param = $this->param;
  358. $userInfo = $this->userInfo;
  359. $types = $param['types'];
  360. unset($param['types']);
  361. if ($getCount == true) $param['getCount'] = 1;
  362. $messageLogic=new MessageLogic();
  363. $param['user_id'] = $userInfo['id'];
  364. $data = $messageLogic->checkReceivables($param);
  365. if ($types == 'list') {
  366. return resultArray(['data' => $data]);
  367. }
  368. return $data;
  369. }
  370. /**
  371. * 待审核发票
  372. *
  373. * @param InvoiceLogic $invoiceLogic
  374. * @return array|\think\response\Json
  375. * @throws \think\exception\DbException
  376. */
  377. public function checkInvoice($getCount = false)
  378. {
  379. $param = $this->param;
  380. $userId = $this->userInfo['id'];
  381. $types = $param['types'];
  382. if ($getCount == true) $param['getCount'] = 1;
  383. # 清除与模型无关的数据
  384. unset($param['types']);
  385. $param['user_id'] = $userId;
  386. $messageLogic=new MessageLogic();
  387. $data = $messageLogic->checkInvoice($param);
  388. if ($types == 'list') return resultArray(['data' => $data]);
  389. return $data;
  390. }
  391. /**
  392. * 待回款提醒
  393. * @author Michael_xu
  394. * @return
  395. */
  396. public function remindReceivablesPlan($getCount = false)
  397. {
  398. $param = $this->param;
  399. $userInfo = $this->userInfo;
  400. $types = $param['types'];
  401. $type = $param['type'] ? : 1;
  402. $isSub = $param['isSub'] ? : '';
  403. unset($param['types']);
  404. unset($param['type']);
  405. unset($param['isSub']);
  406. $receivablesPlanModel = model('ReceivablesPlan');
  407. if ($getCount == true) $param['getCount'] = 1;
  408. $param['owner_user_id'] = $userInfo['id'];
  409. if ($isSub) {
  410. $param['owner_user_id'] = ['in', getSubUserId(false)];
  411. }
  412. switch ($type) {
  413. case '1' :
  414. $param['receivables_id'] = 0;
  415. $param['check_status'] = ['lt', 2];
  416. $param['remind_date'] = ['elt', date('Y-m-d', time())];
  417. $param['return_date'] = ['egt', date('Y-m-d', time())];
  418. $param['types'] = 1;
  419. $param['is_dealt'] = 0;
  420. break;
  421. case '2' :
  422. $param['receivables_id'] = ['gt', 0];
  423. $param['check_status'] = 2;
  424. $param['dealt'] = 1;
  425. break;
  426. case '3' :
  427. $param['receivables_id'] = 0;
  428. $param['return_date'] = ['lt', date('Y-m-d', time())];
  429. break;
  430. }
  431. $data = $receivablesPlanModel->getDataList($param);
  432. if ($types == 'list') {
  433. return resultArray(['data' => $data]);
  434. }
  435. return $data;
  436. }
  437. /**
  438. * 即将到期合同
  439. * @author Michael_xu
  440. * @return
  441. */
  442. public function endContract($getCount = false)
  443. {
  444. $param = $this->param;
  445. $userInfo = $this->userInfo;
  446. $types = $param['types'];
  447. $type = $param['type'] ? : 1;
  448. $isSub = $param['isSub'] ? : '';
  449. if ($getCount == true) $param['getCount'] = 1;
  450. unset($param['types']);
  451. unset($param['type']);
  452. unset($param['isSub']);
  453. $contractModel = model('Contract');
  454. $configModel = new \app\crm\model\ConfigData();
  455. $configInfo = $configModel->getData();
  456. $expireDay = $configInfo['contract_day'] ? : '7';
  457. // 合同到期不提醒
  458. if (empty($configInfo['contract_config'])) return resultArray(['data' => []]);
  459. $param['owner_user_id'] = $userInfo['id'];
  460. if ($isSub) {
  461. $param['owner_user_id'] = array('in',getSubUserId(false));
  462. }
  463. switch ($type) {
  464. case '1' :
  465. $param['end_time'] = array('between',array(date('Y-m-d',time()),date('Y-m-d',time()+86400*$expireDay)));
  466. $param['expire_remind'] = 0;
  467. break;
  468. case '2' : $param['end_time'] = array('lt',date('Y-m-d',time())); break;
  469. }
  470. $data = $contractModel->getDataList($param);
  471. // p($contractModel->getLastSql());
  472. if ($types == 'list') {
  473. return resultArray(['data' => $data]);
  474. }
  475. return $data;
  476. }
  477. /**
  478. * 待进入客户池
  479. * @author Michael_xu
  480. * @return
  481. */
  482. public function remindCustomer($getCount = false)
  483. {
  484. $customerModel = model('Customer');
  485. $param = $this->param;
  486. $userInfo = $this->userInfo;
  487. $types = $param['types'];
  488. $isSub = $param['isSub'] ? : '';
  489. if ($getCount == true) $param['getCount'] = 1;
  490. unset($param['types']);
  491. unset($param['type']);
  492. unset($param['isSub']);
  493. unset($param['deal_status']);
  494. unset($param['owner_user_id']);
  495. # 负责人
  496. $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userInfo['id'])] : $userInfo['id'];
  497. # 是否提醒
  498. $data = [];
  499. $remind = db('crm_customer_pool')->where(['status' => 1, 'remind_conf' => 1])->count();
  500. if (!empty($remind)) {
  501. $whereData = $param ? : [];
  502. $whereData['is_remind'] = 1;
  503. $whereData['user_id'] = $userInfo['id'];
  504. $whereData['pool_remain'] = 0;
  505. $whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer','bydata' => 'me'])->value('scene_id');
  506. if ($isSub) {
  507. $whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer','bydata' => 'sub'])->value('scene_id');
  508. }
  509. $data = $customerModel->getDataList($whereData);
  510. }
  511. if ($types == 'list') {
  512. return resultArray(['data' => $data]);
  513. }
  514. return $data;
  515. }
  516. /**
  517. * 待回访合同
  518. *
  519. * @param false $getCount
  520. * @return array|\think\response\Json
  521. * @throws \think\db\exception\DataNotFoundException
  522. * @throws \think\db\exception\ModelNotFoundException
  523. * @throws \think\exception\DbException
  524. */
  525. public function visitContract($getCount = false)
  526. {
  527. $param = $this->param;
  528. $userId = $this->userInfo['id'];
  529. $isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
  530. $types = !empty($param['types']) ? $param['types'] : '';
  531. if ($getCount == true) $param['getCount'] = 1;
  532. unset($param['isSub']);
  533. unset($param['types']);
  534. $param['is_visit'] = 0; # 未回访
  535. $param['check_status'] = 2; # 审核通过
  536. $contractModel = new \app\crm\model\Contract();
  537. # 负责人
  538. $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId;
  539. $param['user_id'] = $userId;
  540. $data = $contractModel->getDataList($param);
  541. if ($types == 'list') return resultArray(['data' => $data]);
  542. return $data;
  543. }
  544. /**
  545. * 全部标记已处理
  546. *
  547. * @return \think\response\Json
  548. * @throws \think\Exception
  549. * @throws \think\exception\PDOException
  550. */
  551. public function allDeal()
  552. {
  553. $type = $this->param['type'];
  554. $typeId = !empty($this->param['type_id']) ? $this->param['type_id'] : '';
  555. $isSub = !empty($this->param['isSub']) ? $this->param['isSub'] : 0;
  556. $userId = $this->userInfo['id'];
  557. if (empty($type)) return resultArray(['error' => '缺少模块类型参数']);
  558. # 获得今日开始和结束时间戳
  559. $todayTime = getTimeByType('today');
  560. # 处理今日需联系线索、客户、商机
  561. if (in_array($type, ['todayLeads', 'todayCustomer', 'todayBusiness'])) {
  562. # 负责人
  563. $where['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId;
  564. # 下次联系时间
  565. $where['next_time'] = ['between', [$todayTime[0], $todayTime[1]]];
  566. # 是否已处理(联系)
  567. $where['is_dealt'] = 0;
  568. # 线索
  569. if ($type == 'todayLeads') {
  570. $leadsId = !empty($typeId) ? $typeId : Db::name('crm_leads')->where($where)->column('leads_id');
  571. Db::name('crm_leads')->whereIn('leads_id', $leadsId)->update([
  572. 'last_time' => time(),
  573. 'is_dealt' => 1,
  574. 'follow' => '已跟进'
  575. ]);
  576. }
  577. # 客户
  578. if ($type == 'todayCustomer') {
  579. $customerId = !empty($typeId) ? $typeId : Db::name('crm_customer')->where($where)->column('customer_id');
  580. Db::name('crm_customer')->whereIn('customer_id', $customerId)->update([
  581. 'last_time' => time(),
  582. 'is_dealt' => 1,
  583. 'follow' => '已跟进'
  584. ]);
  585. }
  586. # 商机
  587. if ($type == 'todayBusiness') {
  588. $businessId = !empty($typeId) ? $typeId : Db::name('crm_business')->where($where)->column('business_id');
  589. Db::name('crm_business')->whereIn('business_id', $businessId)->update([
  590. 'last_time' => time(),
  591. 'is_dealt' => 1
  592. ]);
  593. }
  594. }
  595. # 处理分配给我的线索、客户
  596. if (in_array($type, ['followLeads', 'followCustomer'])) {
  597. $where['owner_user_id'] = $userId;
  598. $where['follow'] = [['neq','已跟进'], null, 'or'];
  599. $where['is_allocation'] = 1;
  600. # 线索
  601. if ($type == 'followLeads') {
  602. $leadsId = !empty($typeId) ? $typeId : Db::name('crm_leads')->where($where)->column('leads_id');
  603. Db::name('crm_leads')->whereIn('leads_id', $leadsId)->update(['follow' => '已跟进']);
  604. }
  605. # 客户
  606. if ($type == 'followCustomer') {
  607. $customerId = !empty($typeId) ? $typeId : Db::name('crm_customer')->where($where)->column('customer_id');
  608. Db::name('crm_customer')->whereIn('customer_id', $customerId)->update(['follow' => '已跟进']);
  609. }
  610. }
  611. # 处理待审核合同、回款、发票
  612. if (in_array($type, ['checkContract', 'checkReceivables', 'checkInvoice'])) {
  613. $where['check_status'] = ['lt','2'];
  614. $where['check_user_id'] = ['like',',%' . $userId . '%,'];
  615. # 合同
  616. if ($type == 'checkContract') {
  617. $contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
  618. db('crm_dealt_relation')->where('user_id', $userId)->where('types', 'crm_contract')->whereIn('types_id', $contractId)->delete();
  619. }
  620. # 回款
  621. if ($type == 'checkReceivables') {
  622. $receivablesId = !empty($typeId) ? $typeId : Db::name('crm_receivables')->where($where)->column('receivables_id');
  623. db('crm_dealt_relation')->where('user_id', $userId)->where('types', 'crm_receivables')->whereIn('types_id', $receivablesId)->delete();
  624. }
  625. # 发票
  626. if ($type == 'checkInvoice') {
  627. $invoiceId = !empty($typeId) ? $typeId : Db::name('crm_invoice')->where($where)->column('invoice_id');
  628. db('crm_dealt_relation')->where('user_id', $userId)->where('types', 'crm_invoice')->whereIn('types_id', $invoiceId)->delete();
  629. }
  630. }
  631. # 处理到期合同
  632. if ($type == 'endContract') {
  633. $configModel = new \app\crm\model\ConfigData();
  634. $configInfo = $configModel->getData();
  635. $expireDay = $configInfo['contract_day'] ? : '7';
  636. $where['owner_user_id'] = $userId;
  637. $where['end_time'] = ['between', [date('Y-m-d',time()), date('Y-m-d',time()+86400*$expireDay)]];
  638. $where['expire_remind'] = 1;
  639. $contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
  640. Db::name('crm_contract')->whereIn('contract_id', $contractId)->update(['expire_remind' => 0]);
  641. }
  642. # 处理待回访合同
  643. if ($type == 'returnVisitRemind') {
  644. $where['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId; # 负责人
  645. $where['is_visit'] = 0; # 未回访
  646. $where['check_status'] = 2; # 审核通过
  647. $contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
  648. Db::name('crm_contract')->whereIn('contract_id', $contractId)->update(['is_visit' => 1]);
  649. }
  650. # 处理待进入公海
  651. if ($type == 'putInPoolRemind') {
  652. if (!empty($typeId)) {
  653. Db::name('crm_customer')->whereIn('customer_id', $typeId)->update(['pool_remain' => 1]);
  654. } else {
  655. $poolConfig = db('crm_customer_pool')->where(['status' => 1, 'remind_conf' => 1])->count();
  656. if (!empty($poolConfig)) {
  657. $whereData['page'] = 1;
  658. $whereData['limit'] = 100;
  659. $whereData['is_remind'] = 1;
  660. $whereData['user_id'] = $userId;
  661. $whereData['pool_remain'] = 0;
  662. $whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer','bydata' => empty($isSub) ? 'me' : 'sub'])->value('scene_id');
  663. $whereData['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userId)] : $userId;
  664. $poolCustomers = (new \app\crm\model\Customer())->getDataList($whereData);
  665. $ids = [];
  666. foreach ($poolCustomers['list'] AS $key => $value) {
  667. if (!empty($value['customer_id'])) $ids[] = $value['customer_id'];
  668. }
  669. if (!empty($ids)) Db::name('crm_customer')->whereIn('customer_id', $ids)->update(['pool_remain' => 1]);
  670. }
  671. }
  672. }
  673. # 带回款提醒
  674. if ($type == 'remindReceivablesPlan') {
  675. $planId = [];
  676. if (!empty($typeId)) {
  677. $planId = $typeId;
  678. } else {
  679. $param['owner_user_id'] = $isSub ? ['in',getSubUserId(false)] : $userId;
  680. $param['receivables_id'] = 0;
  681. $param['check_status'] = ['lt', 2];
  682. $param['remind_date'] = ['elt', date('Y-m-d',time())];
  683. $param['return_date'] = ['egt', date('Y-m-d',time())];
  684. $param['types'] = 1;
  685. $param['page'] = 1;
  686. $param['limit'] = 1000;
  687. $receivablesPlanModel = model('ReceivablesPlan');
  688. $data = $receivablesPlanModel->getDataList($param);
  689. foreach ($data['list'] AS $key => $value) {
  690. $planId[] = $value['plan_id'];
  691. }
  692. }
  693. if (!empty($planId)) db('crm_receivables_plan')->whereIn('plan_id', $planId)->update(['is_dealt' => 1]);
  694. }
  695. cache::rm('todayLeadsCount'.$userId);
  696. cache::rm('todayCustomerCount'.$userId);
  697. cache::rm('todayBusinessCount'.$userId);
  698. cache::rm('followLeadsCount'.$userId);
  699. cache::rm('followCustomerCount'.$userId);
  700. cache::rm('checkContractCount'.$userId);
  701. cache::rm('checkReceivablesCount'.$userId);
  702. cache::rm('checkInvoiceCount'.$userId);
  703. cache::rm('remindReceivablesPlanCount'.$userId);
  704. cache::rm('visitContractCount'.$userId);
  705. cache::rm('endContractCount'.$userId);
  706. cache::rm('remindCustomerCount'.$userId);
  707. cache::rm('todayLeadsTime'.$userId);
  708. cache::rm('todayCustomerTime'.$userId);
  709. cache::rm('todayBusinessTime'.$userId);
  710. cache::rm('followLeadsTime'.$userId);
  711. cache::rm('followCustomerTime'.$userId);
  712. cache::rm('checkContractTime'.$userId);
  713. cache::rm('checkReceivablesTime'.$userId);
  714. cache::rm('checkInvoiceTime'.$userId);
  715. cache::rm('remindReceivablesPlanTime'.$userId);
  716. cache::rm('visitContractTime'.$userId);
  717. cache::rm('endContractTime'.$userId);
  718. cache::rm('remindCustomerTime'.$userId);
  719. return resultArray(['data' => '操作成功!']);
  720. }
  721. }