IndexLogic.php 49KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. <?php
  2. namespace app\crm\logic;
  3. use app\admin\controller\ApiCommon;
  4. use app\admin\model\Common;
  5. use think\Db;
  6. class IndexLogic extends Common
  7. {
  8. protected $monthName = [
  9. '01' => 'january',
  10. '02' => 'february',
  11. '03' => 'march',
  12. '04' => 'april',
  13. '05' => 'may',
  14. '06' => 'june',
  15. '07' => 'july',
  16. '08' => 'august',
  17. '09' => 'september',
  18. '10' => 'october',
  19. '11' => 'november',
  20. '12' => 'december',
  21. ];
  22. /**
  23. * @param $param
  24. * @return array
  25. */
  26. public function index($param)
  27. {
  28. $adminModel = new \app\admin\model\Admin();
  29. $userModel = new \app\admin\model\User();
  30. $customerModel = new \app\crm\model\Customer();
  31. $contactsModel = new \app\crm\model\Contacts();
  32. $businessModel = new \app\crm\model\Business();
  33. $contractModel = new \app\crm\model\Contract();
  34. $receivablesModel = new \app\crm\model\Receivables();
  35. $activityModel = new \app\crm\model\Activity();
  36. $last_between_time = ByDateTime($param['type']);
  37. if($param['start_time'] && $param['end_time']){
  38. $param['start_time']=$param['start_time'].' 00:00:00';
  39. $param['end_time']=$param['end_time'].' 23:59:59';
  40. }
  41. $whereArr = $adminModel->getWhere($param, 1, ''); //统计条件
  42. // $lastArr = $adminModel->getWhere($param, 1, '', true); //统计条件
  43. $userIds = $whereArr['userIds'];
  44. $between_time = $whereArr['between_time'];
  45. $customerNum = 0; //新增客户
  46. $customerLastNum = 0; //上期对比
  47. $contactsNum = 0; //新增联系人
  48. $contactsLastNum = 0; ////上期对比
  49. $businessNum = 0; //新增商机
  50. $businessLastNum = 0; //上期对比d
  51. $contractNum = 0; //新增合同
  52. $contractLastNum = 0; //上期对比
  53. $recordNum = 0; //新增跟进记录
  54. $recordLastNum = 0; //上期对比
  55. $businessMoneyNum = 0; //新增商机金额
  56. $businessLastMoneyNum = 0; //上期对比
  57. $contractMoneyNum = 0; //新增合同金额
  58. $contractLastMoneyNum = 0; //上期对比
  59. $receivablesMoneyNum = 0; //新增回款金额
  60. $receivablesLastMoneyNum = 0; //上期对比
  61. $where = [];
  62. $where['owner_user_id']['value'] = $userIds;
  63. $where['create_time']['start'] = $between_time[0];
  64. $where['create_time']['end'] = $between_time[1];
  65. $where['getCount'] = 1;
  66. $customer_auth_user_ids = $userModel->getUserByPer('crm', 'customer', 'index');
  67. $contacts_auth_user_ids = $userModel->getUserByPer('crm', 'contacts', 'index');
  68. $business_auth_user_ids = $userModel->getUserByPer('crm', 'business', 'index');
  69. $contract_auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'index');
  70. $receivables_auth_user_ids = $userModel->getUserByPer('crm', 'receivables', 'index');
  71. $record_auth_user_ids = $userModel->getUserByPer('crm', 'activity', 'index');
  72. $resCount = queryCache(
  73. $this->getCountSql([
  74. 'start_time' => $between_time[0],
  75. 'end_time' => $between_time[1],
  76. 'customer_auth_user_ids' => array_intersect($userIds, $customer_auth_user_ids) ? :[-1],
  77. 'contacts_auth_user_ids' => array_intersect($userIds, $contacts_auth_user_ids) ? :[-1],
  78. 'business_auth_user_ids' => array_intersect($userIds, $business_auth_user_ids) ? :[-1],
  79. 'contract_auth_user_ids' => array_intersect($userIds, $contract_auth_user_ids) ? :[-1],
  80. 'receivables_auth_user_ids' => array_intersect($userIds, $receivables_auth_user_ids) ? :[-1],
  81. 'record_auth_user_ids' => array_intersect($userIds, $record_auth_user_ids) ? :[-1],
  82. ])
  83. );
  84. $resLastCount = queryCache(
  85. $this->getCountSql([
  86. 'start_time' => $last_between_time['last_time'][0],
  87. 'end_time' => $last_between_time['last_time'][1],
  88. 'customer_auth_user_ids' => array_intersect($userIds, $customer_auth_user_ids) ? : [-1],
  89. 'contacts_auth_user_ids' => array_intersect($userIds, $contacts_auth_user_ids) ? : [-1],
  90. 'business_auth_user_ids' => array_intersect($userIds, $business_auth_user_ids) ? : [-1],
  91. 'contract_auth_user_ids' => array_intersect($userIds, $contract_auth_user_ids) ? : [-1],
  92. 'receivables_auth_user_ids' => array_intersect($userIds, $receivables_auth_user_ids) ? : [-1],
  93. 'record_auth_user_ids' => array_intersect($userIds, $record_auth_user_ids) ? : [-1],
  94. ])
  95. );
  96. $customerNum = (int)$resCount[0]['count1'] ?: 0;
  97. $contactsNum = (int)$resCount[1]['count1'] ?: 0;
  98. $businessNum = (int)$resCount[2]['count1'] ?: 0;
  99. $contractNum = (int)$resCount[3]['count1'] ?: 0;
  100. $businessMoneyNum = $resCount[2]['count2'] ?: 0;
  101. $contractMoneyNum = $resCount[3]['count2'] ?: 0;
  102. $receivablesMoneyNum = $resCount[4]['count1'] ?: 0;
  103. $recordNum = (int)$resCount[5]['count1'] ?: 0;
  104. $customerLastNum = (int)$resLastCount[0]['count1'] ?: 0;
  105. $contactsLastNum = (int)$resLastCount[1]['count1'] ?: 0;
  106. $businessLastNum = (int)$resLastCount[2]['count1'] ?: 0;
  107. $contractLastNum = (int)$resLastCount[3]['count1'] ?: 0;
  108. $businessLastMoneyNum = $resLastCount[2]['count2'] ?: 0;
  109. $contractLastMoneyNum = $resLastCount[3]['count2'] ?: 0;
  110. $receivablesLastMoneyNum = $resLastCount[4]['count'] ?: 0;
  111. $recordLastNum = (int)$resLastCount[5]['count'] ?: 0;
  112. $data = [];
  113. $data['data']['customerNum'] = $customerNum;
  114. $data['prev']['customerNum'] = $this->getProportion($customerNum, $customerLastNum);
  115. $data['data']['contactsNum'] = $contactsNum;
  116. $data['prev']['contactsNum'] = $this->getProportion($contactsNum, $contactsLastNum);
  117. $data['data']['businessNum'] = $businessNum;
  118. $data['prev']['businessNum'] = $this->getProportion($businessNum, $businessLastNum);
  119. $data['data']['contractNum'] = $contractNum;
  120. $data['prev']['contractNum'] = $this->getProportion($contractNum, $contractLastNum);
  121. $data['data']['recordNum'] = $recordNum;
  122. $data['prev']['recordNum'] = $this->getProportion($recordNum, $recordLastNum);
  123. $data['data']['businessMoneyNum'] = $businessMoneyNum;
  124. $data['prev']['businessMoneyNum'] = $this->getProportion($businessMoneyNum, $businessLastMoneyNum);
  125. $data['data']['contractMoneyNum'] = $contractMoneyNum;
  126. $data['prev']['contractMoneyNum'] = $this->getProportion($contractMoneyNum, $contractLastMoneyNum);
  127. $data['data']['receivablesMoneyNum'] = $receivablesMoneyNum;
  128. $data['prev']['receivablesMoneyNum'] = $this->getProportion($receivablesMoneyNum, $receivablesLastMoneyNum);
  129. return $data;
  130. }
  131. public function getCountSql($param)
  132. {
  133. $countSql = "SELECT
  134. count(1) count1,
  135. 0 count2
  136. FROM 5kcrm_crm_customer
  137. WHERE (create_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . " OR obtain_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . ")
  138. and owner_user_id IN (" . implode(',', $param['customer_auth_user_ids']) . ")
  139. UNION ALL
  140. SELECT
  141. count(1) AS count1,
  142. 0 count2
  143. FROM 5kcrm_crm_contacts contacts
  144. LEFT JOIN 5kcrm_crm_customer customer ON contacts.customer_id=customer.customer_id
  145. WHERE contacts.create_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . "
  146. and contacts.owner_user_id IN (" . implode(',', $param['contacts_auth_user_ids']) . ")
  147. UNION ALL
  148. SELECT
  149. count(1) AS count1,
  150. SUM( CASE WHEN is_end IN (1, 0) THEN money ELSE 0 END) AS count2
  151. FROM 5kcrm_crm_business
  152. WHERE create_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . "
  153. and owner_user_id IN (" . implode(',', $param['business_auth_user_ids']) . ")
  154. UNION ALL
  155. SELECT
  156. count( CASE WHEN check_status = 2 THEN 1 ELSE null END) AS count1,
  157. SUM( CASE WHEN check_status = 2 THEN money ELSE 0 END) AS count2
  158. FROM 5kcrm_crm_contract
  159. WHERE create_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . "
  160. and owner_user_id IN (" . implode(',', $param['contract_auth_user_ids']) . ")
  161. UNION ALL
  162. SELECT
  163. sum(money) AS count,
  164. 0 count2
  165. FROM 5kcrm_crm_receivables
  166. WHERE create_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . "
  167. AND check_status = 2
  168. and owner_user_id IN (" . implode(',', $param['receivables_auth_user_ids']) . ")
  169. UNION ALL
  170. SELECT
  171. count(1) AS count,
  172. 0 count2
  173. FROM 5kcrm_crm_activity
  174. WHERE create_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . "
  175. AND type = 1
  176. AND status = 1
  177. AND activity_type IN (1, 2, 3, 5, 6)
  178. and create_user_id IN (" . implode(',', $param['record_auth_user_ids']) . ")";
  179. return $countSql;
  180. }
  181. ///计算涨幅
  182. public function getProportion($now, $last)
  183. {
  184. if ($last && $last != 0.00) {
  185. if($now-$last>0){
  186. $res = round(($now-$last) / $last,2);
  187. }else{
  188. $res =round(($now-$last) / $last,2);
  189. }
  190. } else {
  191. $res = 0;
  192. }
  193. return $res;
  194. }
  195. /**
  196. * 遗忘数据统计
  197. * @return mixed
  198. */
  199. public function getDataList($param)
  200. {
  201. $userModel = new \app\admin\model\User();
  202. $adminModel = new \app\admin\model\Admin();
  203. $customerModel = new \app\crm\model\Customer();
  204. $dateTime = date('Y-m-d H:i:s');
  205. if($param['start_time'] && $param['end_time']){
  206. $param['start_time']=$param['start_time'].'00:00:00';
  207. $param['end_time']=$param['end_time'].'23:59:59';
  208. }
  209. $whereArr = $adminModel->getWhere($param, 1, ''); //统计条件
  210. $userIds = $whereArr['userIds'];
  211. //权限控制
  212. $auth_customer_user_ids = $userModel->getUserByPer('crm', 'customer', 'index');
  213. $auth_customer_user_ids = $auth_customer_user_ids ? array_intersect($userIds, $auth_customer_user_ids) : []; //取交集
  214. $owner_user_ids = array('in', $auth_customer_user_ids);
  215. $customerParam = [];
  216. $customerParam['limit'] = $param['limit'];
  217. $customerParam['page'] = $param['page'];
  218. $customerParam['search'] = $param['search'];
  219. $customerParam['getCount'] = 1;
  220. $customerParam['owner_user_id'] = $owner_user_ids;
  221. $sevenDaysParam['otherMap'] = " ( IFNULL(last_time,create_time) < " . strtotime('-1 week') . ") ";
  222. $fifteenDaysParam['otherMap'] = " ( IFNULL(last_time,create_time) < " . strtotime('-15 day') . ") ";
  223. $oneMonthParam['otherMap'] = " ( IFNULL(last_time,create_time) < " . strtotime('-30 day') . ") ";
  224. $threeMonthParam['otherMap'] = " ( IFNULL(last_time,create_time) < " . strtotime('-3 month') . ") ";
  225. $sixMonthParam['otherMap'] = " ( IFNULL(last_time,create_time) < " . strtotime('-6 month') . ") ";
  226. $unContactParam['otherMap'] = " ( next_time < " . strtotime(date('Y-m-d 00:00:00')) . " AND (next_time) >0) ";
  227. $data['sevenDays'] = $customerModel->getDataList(array_merge($customerParam, $sevenDaysParam))['dataCount'] ?: 0;
  228. $data['fifteenDays'] = $customerModel->getDataList(array_merge($customerParam, $fifteenDaysParam))['dataCount'] ?: 0;
  229. $data['oneMonth'] = $customerModel->getDataList(array_merge($customerParam, $oneMonthParam))['dataCount'] ?: 0;
  230. $data['threeMonth'] = $customerModel->getDataList(array_merge($customerParam, $threeMonthParam))['dataCount'] ?: 0;
  231. $data['sixMonth'] = $customerModel->getDataList(array_merge($customerParam, $sixMonthParam))['dataCount'] ?: 0;
  232. $data['unContactCustomerCount'] = $customerModel->getDataList(array_merge($customerParam, $unContactParam))['dataCount'] ?: 0;
  233. return $data;
  234. }
  235. /**
  236. * 遗忘数据列表
  237. * @return mixed
  238. * @throws \think\db\exception\DataNotFoundException
  239. * @throws \think\db\exception\ModelNotFoundException
  240. * @throws \think\exception\DbException
  241. */
  242. public function forgottenCustomerPageList($param)
  243. {
  244. $userModel = new \app\admin\model\User();
  245. $adminModel = new \app\admin\model\Admin();
  246. $customerModel = new \app\crm\model\Customer();
  247. if($param['start_time'] && $param['end_time']){
  248. $param['start_time']=$param['start_time'].'00:00:00';
  249. $param['end_time']=$param['end_time'].'23:59:59';
  250. }
  251. $whereArr = $adminModel->getWhere($param, 1, ''); //统计条件
  252. $userIds = $whereArr['userIds'];
  253. //权限控制
  254. $auth_customer_user_ids = $userModel->getUserByPer('crm', 'customer', 'index');
  255. $auth_customer_user_ids = $auth_customer_user_ids ? array_intersect($userIds, $auth_customer_user_ids) : []; //取交集
  256. $owner_user_ids = array('in', $auth_customer_user_ids);
  257. $sql_unContactCustomerList = db('crm_customer')
  258. ->where(['owner_user_id' => $owner_user_ids])
  259. ->where('last_time < next_time AND next_time < now()')
  260. ->fetchSql()
  261. ->select();
  262. $label = $param['label'];
  263. $day = $param['day'] ?: '';
  264. $customerParam = [];
  265. $customerParam['limit'] = $param['limit'];
  266. $customerParam['page'] = $param['page'];
  267. $customerParam['search'] = $param['search'];
  268. $customerParam['owner_user_id'] = $owner_user_ids;
  269. switch ($label) {
  270. case 1 :
  271. // ((( next_time < " . strtotime(date('Y-m-d 00:00:00')).") AND (last_time IS NOT NULL)) AND (IFNULL(last_time,next_time) >0))
  272. $customerParam['otherMap'] = " (next_time < " . strtotime(date('Y-m-d 00:00:00')) . " AND (next_time) >0) ";
  273. break;
  274. case 2 :
  275. $customerParam['otherMap'] = " ( IFNULL(last_time,create_time) < " . strtotime('-1 week') . ") ";
  276. break;
  277. case 3 :
  278. $customerParam['otherMap'] = " ( IFNULL(last_time,create_time) < " . strtotime('-15 day') . ") ";
  279. break;
  280. case 4 :
  281. $customerParam['otherMap'] = " ( IFNULL(last_time,create_time) < " . strtotime('-30 day') . ") ";
  282. break;
  283. case 5 :
  284. $customerParam['otherMap'] = " ( IFNULL(last_time,create_time) < " . strtotime('-3 month') . ") ";
  285. break;
  286. case 6 :
  287. $customerParam['otherMap'] = " ( IFNULL(last_time,create_time) < " . strtotime('-6 month') . ") ";
  288. break;
  289. }
  290. return $customerModel->getDataList($customerParam);
  291. }
  292. /**
  293. * 排行榜
  294. * @param $param
  295. * @throws \think\db\exception\DataNotFoundException
  296. * @throws \think\db\exception\ModelNotFoundException
  297. * @throws \think\exception\DbException
  298. */
  299. public function ranking($param)
  300. {
  301. $userModel = new \app\admin\model\User();
  302. $adminModel = new \app\admin\model\Admin();
  303. $user_id = $param['user_id'] ?: [-1];
  304. $status = $param['label'] ?: 1; //1合同目标 2回款目标 3合同数 4新增客户数 5新增联系人数 6新增跟进记录数
  305. if($param['start_time'] && $param['end_time']){
  306. $param['start_time']=$param['start_time'].'00:00:00';
  307. $param['end_time']=$param['end_time'].'23:59:59';
  308. }
  309. $whereArr = $adminModel->getWhere($param, 1, ''); //统计条件
  310. $userIds = $whereArr['userIds'];
  311. $between_time = $whereArr['between_time'];
  312. $auth_user_ids = $userModel->getUserByPer('bi', 'ranking', 'read');
  313. $auth_user_ids = $auth_user_ids ? array_intersect($userIds, $auth_user_ids) : []; //取交集
  314. switch ($param['label']) {
  315. //合同金额
  316. case '1':
  317. //合同金额
  318. $sql = db('crm_contract')
  319. ->alias('contract')
  320. ->join('__ADMIN_USER__ user', 'contract.owner_user_id=user.id')
  321. ->join('__ADMIN_STRUCTURE__ structure', 'user.structure_id=structure.id')
  322. ->field([
  323. 'SUM(CASE WHEN check_status = 2 THEN money ELSE 0 END) as money',
  324. 'contract.owner_user_id as owner_user_id',
  325. ])
  326. ->where([
  327. 'contract.owner_user_id' => ['in', $auth_user_ids],
  328. 'contract.create_time' => ['between', $between_time],
  329. ])
  330. ->group('contract.owner_user_id')
  331. ->order('money desc,owner_user_id asc')
  332. ->fetchSql()
  333. ->select();
  334. $list = queryCache($sql, 200);
  335. break;
  336. //回款金额
  337. case '2':
  338. //回款金额
  339. $sql1 = db('crm_receivables')
  340. ->alias('receivables')
  341. ->join('__ADMIN_USER__ user', 'receivables.owner_user_id=user.id')
  342. ->join('__ADMIN_STRUCTURE__ structure', 'user.structure_id=structure.id')
  343. ->field([
  344. 'SUM(CASE WHEN receivables.check_status = 2 THEN receivables.money ELSE 0 END) as money',
  345. 'receivables.owner_user_id as owner_user_id',
  346. ])
  347. ->where([
  348. 'receivables.owner_user_id' => ['in', $auth_user_ids],
  349. 'receivables.create_time' => ['between', $between_time],
  350. 'receivables.check_status' => 2
  351. ])
  352. ->group('receivables.owner_user_id')
  353. ->order('money desc,owner_user_id asc')
  354. ->fetchSql()
  355. ->select();
  356. $list = queryCache($sql1, 200);
  357. break;
  358. //合同数
  359. case '3':
  360. //合同
  361. $where_contract['contract.check_status'] = 2; //审核通过
  362. $list = db('crm_contract')
  363. ->alias('contract')
  364. ->join('__ADMIN_USER__ user', 'contract.owner_user_id=user.id')
  365. ->join('__ADMIN_STRUCTURE__ structure', 'user.structure_id=structure.id')
  366. ->field([
  367. 'count(contract.contract_id) as count',
  368. 'contract.owner_user_id as owner_user_id'])
  369. ->where([
  370. 'contract.owner_user_id' => ['in', $auth_user_ids],
  371. 'contract.create_time' => ['between', $between_time],
  372. ])
  373. ->group('contract.owner_user_id')
  374. ->order('count desc,owner_user_id asc')
  375. ->select();
  376. break;
  377. //新增客户数
  378. case '4':
  379. //新增客户
  380. $logMap=function ($query) use ($between_time) {
  381. $query->where('customer.create_time', array('between', $between_time))
  382. ->whereOr('customer.obtain_time', array('between', $between_time));
  383. };
  384. $list = db('crm_customer')
  385. ->alias('customer')
  386. ->join('__ADMIN_USER__ user', 'customer.create_user_id=user.id')
  387. ->join('__ADMIN_STRUCTURE__ structure', 'user.structure_id=structure.id')
  388. ->field([
  389. 'count(customer.customer_id) as count',
  390. 'customer.owner_user_id as owner_user_id'])
  391. ->where(
  392. 'customer.owner_user_id' ,['in', $auth_user_ids]
  393. )
  394. ->where($logMap)
  395. ->group('customer.owner_user_id')
  396. ->order('count desc,owner_user_id asc')
  397. ->select();
  398. break;
  399. //新增联系人
  400. case '5':
  401. //新增联系人
  402. $list = db('crm_contacts')
  403. ->alias('contacts')
  404. ->join('__ADMIN_USER__ user', 'contacts.owner_user_id=user.id')
  405. ->join('__ADMIN_STRUCTURE__ structure', 'user.structure_id=structure.id')
  406. ->field([
  407. 'count(contacts.contacts_id) as count',
  408. 'contacts.contacts_id',
  409. 'contacts.owner_user_id as owner_user_id'])
  410. ->where([
  411. 'contacts.owner_user_id' => ['in', $auth_user_ids],
  412. 'contacts.create_time' => ['between', $between_time],
  413. ])
  414. ->group('contacts.owner_user_id')
  415. ->order('count desc,owner_user_id asc')
  416. ->select();
  417. break;
  418. //新增跟进记录
  419. case '8':
  420. //新增跟进
  421. $list = db('crm_activity')
  422. ->alias('activity')
  423. ->join('__ADMIN_USER__ user', 'activity.create_user_id=user.id')
  424. ->field([
  425. 'count(activity.activity_id) as count',
  426. 'activity.create_user_id as create_user_id'])
  427. ->where([
  428. 'activity.create_user_id' => ['in', $auth_user_ids],
  429. 'activity.create_time' => ['between', $between_time],
  430. 'activity.activity_type' => ['in', [1, 2, 3, 5, 6]],
  431. 'activity.type' => 1,
  432. 'activity.status' => 1
  433. ])
  434. ->group('activity.create_user_id')
  435. ->order('count desc,create_user_id asc')
  436. ->select();
  437. break;
  438. }
  439. foreach ($list as $k=>$v){
  440. $userInfo=$userModel->getUserById($v['owner_user_id']);
  441. if(!empty($v['create_user_id'])){
  442. $userInfo=$userModel->getUserById($v['create_user_id']);
  443. }
  444. $list[$k]['realname']= $userInfo['realname'];
  445. $list[$k]['id']= $userInfo['id'];
  446. $list[$k]['thumb_img']=$userInfo['thumb_img'] ? getFullPath($v['thumb_img']) : '';
  447. }
  448. //业绩目标
  449. $between_time = getTimeByType($param['type']);
  450. $start_time = $between_time[0];
  451. $end_time = $between_time[1];
  452. $where_achievement = [];
  453. $where_achievement['status'] = $param['label'];
  454. //获取时间段包含年份
  455. $year = getYearByTime($start_time, $end_time);
  456. $where_achievement['year'] = array('in', $year);
  457. $achievement = '';
  458. //获取需要查询的月份
  459. $month = getmonthByTime($start_time, $end_time);
  460. //月份
  461. foreach ($month as $key => $val) {
  462. foreach ($val as $key1 => $val1) {
  463. $achievement = $this->monthName[$val1];
  464. }
  465. }
  466. if ($param['dataType'] == 1 || $param['dataType'] == 2) {
  467. $where_achievement['type'] = 3;
  468. }
  469. if ($param['dataType'] == 3 || $param['dataType'] == 4) {
  470. $where_achievement['type'] = 2;
  471. }
  472. $userInfo = new ApiCommon();
  473. $user_id = $userInfo->userInfo;
  474. $userName = db('admin_user')
  475. ->alias('user')
  476. ->join('__ADMIN_STRUCTURE__ structure', 'user.structure_id=structure.id')
  477. ->where('user.id', $user_id['id'])
  478. ->field('user.id,user.thumb_img,user.realname,structure.name')->find();
  479. $ranking = [];
  480. foreach ($list as $k => $v) {
  481. $where_achievement['obj_id'] = $v['owner_user_id'];
  482. $achievementMoney = db('crm_achievement')->where($where_achievement)->find();
  483. $v['thumb_img'] = $v['thumb_img'] ? getFullPath($v['thumb_img']) : '';
  484. if ($achievementMoney[$achievement] == 0.00) {
  485. $v['rate'] = 0;
  486. } else {
  487. $v['rate'] = round($v['money'] / $achievementMoney[$achievement]*100,2);
  488. }
  489. if ($userName['realname'] == $v['realname']) {
  490. $list['self']['sort'] = $k + 1;
  491. $list['self']['thumb_img'] = $userName['thumb_img'] ? getFullPath($userName['thumb_img']) : '';
  492. $list['self']['realname'] = $v['realname'];
  493. if(in_array($param['label'],['1','2'])){
  494. $list['self']['rate'] = $v['rate'];$list['self']['money'] = $v['money'];
  495. }
  496. if(!in_array($param['label'],['1','2'])) $list['self']['count'] = $v['count'];
  497. $list['self']['owner_user_id'] = $v['owner_user_id'];
  498. $list['self']['structureName'] = $userName['name'];
  499. $list['self']['user_id'] = $userName['id'];
  500. } else {
  501. $money['self'] = [];
  502. }
  503. $ranking['ranking'][] = $v;
  504. $ranking['self'] = $list['self'];
  505. }
  506. $data = [];
  507. $data = $ranking ?: [];
  508. return $data;
  509. }
  510. /**
  511. * 数据汇总
  512. * @param $param
  513. */
  514. public function queryDataInfo($param)
  515. {
  516. $userModel = new \app\admin\model\User();
  517. $adminModel = new \app\admin\model\Admin();
  518. if($param['start_time'] && $param['end_time']){
  519. $param['start_time']=$param['start_time'].'00:00:00';
  520. $param['end_time']=$param['end_time'].'23:59:59';
  521. }
  522. $whereArr = $adminModel->getWhere($param, 1, ''); //统计条件
  523. $user_id = $param['user_id'] ?: [-1];
  524. $userIds = $whereArr['userIds'];
  525. $between_time = $whereArr['between_time'];
  526. $customer_auth_user_ids = $userModel->getUserByPer('crm', 'customer', 'index');
  527. $contacts_auth_user_ids = $userModel->getUserByPer('crm', 'contacts', 'index');
  528. $business_auth_user_ids = $userModel->getUserByPer('crm', 'business', 'index');
  529. $contract_auth_user_ids = $userModel->getUserByPer('crm', 'contract', 'index');
  530. $receivables_auth_user_ids = $userModel->getUserByPer('crm', 'receivables', 'index');
  531. $receivablesPlan_auth_user_ids = $userModel->getUserByPer('crm', 'receivables_plan', 'index');
  532. $record_auth_user_ids = $userModel->getUserByPer('crm', 'activity', 'index');
  533. $resDataArr = [];
  534. for ($i = 1; $i <= 6; $i++) {
  535. $resData = queryCache(
  536. $this->getQueryDataSql([
  537. 'type' => $i,
  538. 'start_time' => $between_time[0],
  539. 'end_time' => $between_time[1],
  540. 'customer_auth_user_ids' => array_intersect($userIds, $customer_auth_user_ids) ? : [-1],
  541. 'contacts_auth_user_ids' => array_intersect($userIds, $contacts_auth_user_ids) ? : [-1],
  542. 'business_auth_user_ids' => array_intersect($userIds, $business_auth_user_ids) ? : [-1],
  543. 'contract_auth_user_ids' => array_intersect($userIds, $contract_auth_user_ids) ? : [-1],
  544. 'receivables_auth_user_ids' => array_intersect($userIds, $receivables_auth_user_ids) ? : [-1],
  545. 'record_auth_user_ids' => array_intersect($userIds, $record_auth_user_ids) ? : [-1],
  546. '$receivablesPlan_auth_user_ids' => array_intersect($userIds, $receivablesPlan_auth_user_ids) ? : [-1],
  547. ])
  548. );
  549. $resDataArr = array_merge($resDataArr, $resData[0]);
  550. }
  551. return $resDataArr;
  552. }
  553. /**
  554. * [数据汇总sql]
  555. * @return
  556. * @author Michael_xu
  557. */
  558. public function getQueryDataSql($param)
  559. {
  560. $configModel = new \app\crm\model\ConfigData();
  561. $configInfo = $configModel->getData();
  562. $follow_day = $configInfo['follow_day'] ? : 0;
  563. $deal_day = $configInfo['deal_day'] ? : 0;
  564. //默认公海条件(没有负责人或已经到期)
  565. $data['follow_time'] = time()-$follow_day*86400;
  566. $data['deal_time'] = time()-$deal_day*86400;
  567. $data['deal_status'] = '未成交';
  568. switch ($param['type']) {
  569. case 1 :
  570. $countSql = "SELECT
  571. count(1) allCustomer,
  572. COUNT(CASE WHEN deal_status = '已成交' THEN 1 ELSE NULL END) AS dealCustomer
  573. FROM 5kcrm_crm_customer
  574. WHERE (create_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . " OR obtain_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . ")
  575. AND owner_user_id IN (" . implode(',', $param['customer_auth_user_ids']) . ")";
  576. break;
  577. case 2 :
  578. $countSql = "SELECT
  579. COUNT(distinct CASE WHEN b.activity_type_id in (SELECT customer_id FROM 5kcrm_crm_customer
  580. WHERE (create_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . " OR obtain_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . ")
  581. AND owner_user_id IN (" . implode(',', $param['customer_auth_user_ids']) . ") AND next_time is not null
  582. ) THEN b.activity_type_id ELSE NULL END) as activityNum
  583. FROM 5kcrm_crm_activity AS b
  584. WHERE b.create_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . "
  585. AND b.type = '1'
  586. AND b.activity_type = '2'
  587. AND b.status = '1'
  588. AND b.create_user_id IN (" . implode(',', $param['record_auth_user_ids']) . ")";
  589. break;
  590. case 3 :
  591. $countSql = "SELECT
  592. count(1) allBusiness,
  593. COUNT(CASE WHEN is_end = 1 THEN 1 ELSE NULL END) AS endBusiness,
  594. SUM( CASE WHEN is_end IN (1, 0) THEN money ELSE 0 END) AS businessMoney
  595. FROM 5kcrm_crm_business
  596. WHERE create_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . "
  597. AND owner_user_id IN (" . implode(',', $param['business_auth_user_ids']) . ")";
  598. break;
  599. case 4 :
  600. $countSql = "SELECT
  601. count(1) allContract,
  602. SUM( CASE WHEN check_status = 2 THEN money ELSE 0 END) AS contractMoney
  603. FROM 5kcrm_crm_contract
  604. WHERE create_time BETWEEN " . $param['start_time'] . " AND " . $param['end_time'] . "
  605. AND owner_user_id IN (" . implode(',', $param['contract_auth_user_ids']) . ")";
  606. break;
  607. case 5 :
  608. $start_time=date('Y-m-d',$param['start_time']);
  609. $end_time=date('Y-m-d',$param['end_time']);
  610. $countSql = "SELECT
  611. SUM( CASE WHEN r.check_status = 2 THEN r.money ELSE 0 END) AS receivablesMoney
  612. FROM 5kcrm_crm_receivables as r
  613. WHERE r.create_time BETWEEN " . "'".$start_time."'" . " AND " ."'".$end_time."'" . "
  614. AND r.owner_user_id IN (" . implode(',', $param['receivables_auth_user_ids']) . ")";
  615. break;
  616. case 6 :
  617. $start_time=date('Y-m-d',$param['start_time']);
  618. $end_time=date('Y-m-d',$param['end_time']);
  619. $countSql = "SELECT
  620. SUM(r.money) AS planMoney
  621. FROM 5kcrm_crm_receivables_plan as r
  622. WHERE r.return_date BETWEEN " . "'".$start_time."'" . " AND " ."'". $end_time."'" . " AND r.receivables_id = 0
  623. AND r.owner_user_id IN (" . implode(',', $param['receivables_auth_user_ids']) . ")";
  624. break;
  625. }
  626. return $countSql;
  627. }
  628. /**
  629. * 销售漏斗查看
  630. */
  631. public function businessList($param)
  632. {
  633. $adminModel = new \app\admin\model\Admin();
  634. if($param['start_time'] && $param['end_time']){
  635. $between_time[0]=$param['start_time'].'00:00:00';
  636. $between_time[1]=$param['end_time'].'23:59:59';
  637. }
  638. $whereArr = $adminModel->getWhere($param, 1, ''); //统计条件
  639. $userIds = $whereArr['userIds'];
  640. $between_time = $whereArr['between_time'];
  641. $start_time = $between_time[0];
  642. $end_time = $between_time[1];
  643. //场景默认全部
  644. $scene_id = db('admin_scene')->where(['types' => 'crm_business', 'bydata' => 'all'])->value('scene_id');
  645. $param['scene_id'] = $scene_id ?: '';
  646. $param['create_time']['start'] = $start_time;
  647. $param['create_time']['end'] = $end_time;
  648. $param['owner_user_id']['value'] = $userIds;
  649. unset($param['type']);
  650. unset($param['dataType']);
  651. unset($param['label']);
  652. unset($param['log_type']);
  653. unset($param['types']);
  654. $businessModel = new \app\crm\model\Business();
  655. $data=$businessModel->getDataList($param);
  656. return $data;
  657. }
  658. /**
  659. * 仪表盘布局列表
  660. */
  661. public function dashboard($param)
  662. {
  663. $data = [];
  664. $list = db('crm_dashboard')->where('user_id', $param['user_id'])->find();
  665. if ($list) {
  666. $data = unserialize($list['dashboard']);
  667. return $data ?: [];
  668. } else {
  669. $data['left'][0]['modelId'] = 1;
  670. $data['left'][0]['list'] = 1;
  671. $data['left'][0]['isHidden'] = 0;
  672. $data['left'][]['modelId'] = 5;
  673. $data['left'][1]['list'] = 1;
  674. $data['left'][1]['isHidden'] = 0;
  675. $data['left'][2]['modelId'] = 7;
  676. $data['left'][2]['list'] = 1;
  677. $data['left'][2]['isHidden'] = 0;
  678. $data['right'][0]['modelId'] = 2;
  679. $data['right'][0]['list'] = 2;
  680. $data['right'][0]['isHidden'] = 0;
  681. $data['right'][1]['modelId'] = 4;
  682. $data['right'][1]['list'] = 2;
  683. $data['right'][1]['isHidden'] = 0;
  684. $data['right'][2]['modelId'] = 6;
  685. $data['right'][2]['list'] = 2;
  686. $data['right'][2]['isHidden'] = 0;
  687. return $data;
  688. }
  689. }
  690. /**
  691. * 修改自定义仪表盘
  692. * @param $param
  693. */
  694. public function updateDashboard($param)
  695. {
  696. $data = db('crm_dashboard')->where('user_id', $param['user_id'])->find();
  697. if ($data) {
  698. $list = db('crm_dashboard')->where('user_id', $param['user_id'])->update(['dashboard' => serialize($param['dashboard'])]);
  699. return $list;
  700. } else {
  701. $list = db('crm_dashboard')->insert(['user_id' => $param['user_id'], 'dashboard' => serialize($param['dashboard'])]);
  702. return $list;
  703. }
  704. }
  705. /**
  706. * 跟进记录列表
  707. * @param $param
  708. * @return bool|\PDOStatement|string|\think\Collection
  709. * @throws \think\db\exception\DataNotFoundException
  710. * @throws \think\db\exception\ModelNotFoundException
  711. * @throws \think\exception\DbException
  712. */
  713. public function activityList($param)
  714. {
  715. $userModel = new \app\admin\model\User();
  716. $adminModel = new \app\admin\model\Admin();
  717. $whereArr = $adminModel->getWhere($param, 1, ''); //统计条件
  718. $userIds = $whereArr['userIds'];
  719. //权限控制
  720. if (!empty($param['type'])) {
  721. $last_where_contract = getTimeByType($param['type']);
  722. $between_time = [$last_where_contract[0], $last_where_contract[1]];
  723. $where_activity['t.create_time'] = array('between', $between_time);
  724. } else {
  725. //自定义时间
  726. $start_time = $param['start_time'] ?strtotime($param['start_time'].'00:00:00'): strtotime(date('Y-01-01', time()));
  727. $end_time = $param['end_time'] ? strtotime($param['end_time'].'23:59:59') : strtotime(date('Y-m-01', time()) . ' +1 month -1 day');
  728. $where_activity['t.create_time'] = ['between', [$start_time, $end_time]];
  729. }
  730. $auth_customer_user_ids = $userModel->getUserByPer('crm', 'activity', 'index');
  731. $auth_customer_user_ids = $auth_customer_user_ids ? array_intersect($userIds, $auth_customer_user_ids) : []; //取交集
  732. $where_activity['t.create_user_id'] = array('in', $auth_customer_user_ids);
  733. # 跟进记录类型
  734. $where_activity['t.activity_type'] = $param['activity_type']?:['elt',7];
  735. $where_activity['t.type'] = 1;
  736. $where_activity['t.status'] = 1;
  737. if ($param['label'] != 1) {
  738. if ($param['search']) {
  739. $type['t.content'] = array('like', '%' . $param['search'] . '%');
  740. }
  741. //跟进记录类型
  742. if ($param['crmType'] == 0) {
  743. $type['t.activity_type'] = ['in', [1, 2, 3, 5, 6]];
  744. } else {
  745. $type['t.activity_type'] = $param['crmType'];
  746. }
  747. if ($param['type']) {
  748. $timeAry = getTimeByType($param['type']);
  749. $between_time = [$timeAry[0], $timeAry[1]];
  750. $type['t.create_time'] = array('between', $between_time);
  751. } else {
  752. //自定义时间
  753. $start_time = $param['start_time'] ? strtotime($param['start_time'] . ' 00:00:00') : strtotime(date('Y-m-01', time()));
  754. $end_time = $param['end_time'] ? strtotime($param['end_time'] . ' 23:59:59') : strtotime(date('Y-m-01', time()) . ' +1 month -1 day');
  755. $type['t.create_time'] = ['between', [$start_time, $end_time]];
  756. }
  757. if ($param['queryType'] == 0) {
  758. $type['t.type'] = ['in', [1, 4]];
  759. } else {
  760. $type['t.type'] = $param['queryType'];
  761. }
  762. if ($param['user_id'] == '') {
  763. if ($param['subUser'] === "0" || $param['subUser']===0) {
  764. $type['t.create_user_id'] = $param['id'];
  765. //下属创建
  766. } elseif ($param['subUser'] == '1') {
  767. $subList = getSubUserId(false, 0, $param['id']);
  768. $subStr = $subList ? implode(',', $subList) : '-1';
  769. $type['t.create_user_id'] = array('in', $subStr);
  770. } else {
  771. $userIds = getSubUserId(true, 0, $param['id']);
  772. $subStr = $userIds ? implode(',', $userIds) : '-1';
  773. $type['t.create_user_id'] = array('in', $subStr);
  774. }
  775. } else {
  776. $type['t.create_user_id'] = $param['user_id'];
  777. }
  778. $type['t.status'] = 1;
  779. $list = db('crm_activity')
  780. ->alias('t')
  781. ->join('__ADMIN_USER__ user', 'user.id = t.create_user_id', 'LEFT')
  782. ->field('t.content,t.next_time,t.category,t.activity_type,t.type,t.activity_id,t.activity_type_id,t.update_time,t.create_time,user.realname as create_user_name,user.thumb_img')
  783. ->where($type)
  784. ->page($param['page'], $param['limit'])
  785. ->order('t.create_time desc')
  786. ->select();
  787. if($param['is_excel']){
  788. $list = db('crm_activity')
  789. ->alias('t')
  790. ->join('__ADMIN_USER__ user', 'user.id = t.create_user_id', 'LEFT')
  791. ->field('t.content,t.next_time,t.category,t.activity_type,t.type,t.activity_id,t.activity_type_id,t.update_time,t.create_time,user.realname as create_user_name,user.thumb_img')
  792. ->where($type)
  793. ->order('t.create_time desc')
  794. ->select();
  795. }
  796. $dataCount = db('crm_activity')
  797. ->alias('t')
  798. ->join('__ADMIN_USER__ user', 'user.id = t.create_user_id', 'LEFT')
  799. ->where($type)
  800. ->count();
  801. foreach ($list as $k => $v) {
  802. // 业务名称(客户、线索、合同...)
  803. if ($v['activity_type'] == 1) {
  804. $activity_name = Db::name('crm_leads')->where('leads_id', $v['activity_type_id'])->find();
  805. $list[$k]['activity_type_name'] = $activity_name['name'];
  806. }
  807. if ($v['activity_type'] == 2) {
  808. $activity_name = Db::name('crm_customer')->where('customer_id', $v['activity_type_id'])->find();
  809. $list[$k]['activity_type_name'] = $activity_name['name'];
  810. $activity_business = Db::name('crm_business')->where('business_id', $v['activity_type_id'])->select();
  811. $activity_contacts = Db::name('crm_contacts')->where('contacts_id', $v['activity_type_id'])->select();
  812. $list[$k]['business_list'] = $activity_business ?: [];
  813. $list[$k]['contacts_list'] = $activity_contacts ?: [];
  814. }
  815. if ($v['activity_type'] == 3) {
  816. $activity_name = Db::name('crm_contacts')->where('contacts_id', $v['activity_type_id'])->find();
  817. $list[$k]['activity_type_name'] = $activity_name['name'];
  818. }
  819. if ($v['activity_type'] == 5) {
  820. $activity_name = Db::name('crm_business')->where('business_id', $v['activity_type_id'])->find();
  821. $list[$k]['activity_type_name'] = $activity_name['name'];
  822. }
  823. if ($v['activity_type'] == 6) {
  824. $activity_name = Db::name('crm_contract')->where('contract_id', $v['activity_type_id'])->find();
  825. $list[$k]['activity_type_name'] = $activity_name['name'];
  826. }
  827. $list[$k]['create_time'] = !empty($v['create_time']) ? date('Y-m-d H:i:s', $v['create_time']) : null;
  828. $list[$k]['update_time'] = !empty($v['update_time']) ? date('Y-m-d H:i:s', $v['update_time']) : null;
  829. $list[$k]['next_time'] = !empty($v['next_time']) ? date('Y-m-d H:i:s', $v['next_time']) : null;
  830. $fileModel = new \app\admin\model\File();
  831. $recordModel = new \app\admin\model\Record();
  832. $f_where = [];
  833. $f_where['module_id'] = $v['activity_id'];
  834. $relation_list = [];
  835. $f_where['module'] = 'crm_activity';
  836. $relation_list = $recordModel->getListByRelationId('activity', $v['activity_id']);
  837. $dataInfo = [];
  838. $newFileList = [];
  839. $imgList = [];
  840. $fileList = [];
  841. $newFileList = $fileModel->getDataList($f_where, 'all');
  842. if ($newFileList['list']) {
  843. foreach ($newFileList['list'] as $val) {
  844. if ($val['types'] == 'file') {
  845. $fileList[] = $val;
  846. } else {
  847. $imgList[] = $val;
  848. }
  849. }
  850. };
  851. $list[$k]['fileList'] = $fileList ?: [];
  852. $list[$k]['imgList'] = $imgList ?: [];
  853. $dataInfo['customerList'] = $relation_list['customerList'] ?: [];
  854. $dataInfo['contactsList'] = $relation_list['contactsList'] ?: [];
  855. $dataInfo['businessList'] = $relation_list['businessList'] ?: [];
  856. $dataInfo['contractList'] = $relation_list['contractList'] ?: [];
  857. $list[$k]['business_ids'] = implode(',',array_column($relation_list['businessList'],'name'));
  858. $list[$k]['contacts_ids'] = implode(',',array_column($relation_list['contactsList'],'name'));
  859. $list[$k]['thumb_img'] = $v['thumb_img'] ? getFullPath($v['thumb_img']) : '';
  860. $list[$k]['dataInfo'] = $dataInfo ?: [];
  861. }
  862. } else {
  863. $list = db('crm_activity')
  864. ->alias('t')
  865. ->join('__ADMIN_USER__ user', 'user.id = t.create_user_id', 'LEFT')
  866. ->where($where_activity)
  867. ->field('t.content,t.next_time,t.update_time,t.create_time,t.category,t.activity_type,t.type,t.activity_id,t.activity_type_id,user.realname as create_user_name,user.thumb_img')
  868. ->page($param['page'], $param['limit'])
  869. ->order('t.create_time desc')
  870. ->select();
  871. $dataCount = db('crm_activity')->alias('t')
  872. ->join('__ADMIN_USER__ user', 'user.id = t.create_user_id', 'LEFT')->where($where_activity)->count();
  873. foreach ($list as $k => $v) {
  874. // 业务名称(客户、线索、合同...)
  875. if ($param['activity_type'] == 1) {
  876. $activity_name = Db::name('crm_leads')->where('leads_id', $v['activity_type_id'])->find();
  877. $list[$k]['activity_type_name'] = $activity_name['name'];
  878. }
  879. if ($param['activity_type'] == 2) {
  880. $activity_name = Db::name('crm_customer')->where('customer_id', $v['activity_type_id'])->find();
  881. $list[$k]['activity_type_name'] = $activity_name['name'];
  882. $activity_business = Db::name('crm_business')->where('business_id', $v['activity_type_id'])->select();
  883. $activity_contacts = Db::name('crm_contacts')->where('contacts_id', $v['activity_type_id'])->select();
  884. $list[$k]['business_list'] = $activity_business ?: [];
  885. $list[$k]['contacts_list'] = $activity_contacts ?: [];
  886. }
  887. if ($param['activity_type'] == 3) {
  888. $activity_name = Db::name('crm_contacts')->where('contacts_id', $v['activity_type_id'])->find();
  889. $list[$k]['activity_type_name'] = $activity_name['name'];
  890. }
  891. if ($param['activity_type'] == 5) {
  892. $activity_name = Db::name('crm_business')->where('business_id', $v['activity_type_id'])->find();
  893. $list[$k]['activity_type_name'] = $activity_name['name'];
  894. }
  895. if ($param['activity_type'] == 6) {
  896. $activity_name = Db::name('crm_contract')->where('contract_id', $v['activity_type_id'])->find();
  897. $list[$k]['activity_type_name'] = $activity_name['name'];
  898. }
  899. $list[$k]['create_time'] = !empty($v['create_time']) ? date('Y-m-d H:i:s', $v['create_time']) : null;
  900. $list[$k]['update_time'] = !empty($v['update_time']) ? date('Y-m-d H:i:s', $v['update_time']) : null;
  901. $list[$k]['next_time'] = !empty($v['next_time']) ? date('Y-m-d H:i:s', $v['next_time']) : null;
  902. $fileModel = new \app\admin\model\File();
  903. $recordModel = new \app\admin\model\Record();
  904. $f_where = [];
  905. $f_where['module_id'] = $v['activity_id'];
  906. $relation_list = [];
  907. $f_where['module'] = 'crm_activity';
  908. $relation_list = $recordModel->getListByRelationId('activity', $v['activity_id']);
  909. $dataInfo = [];
  910. $newFileList = [];
  911. $imgList = [];
  912. $fileList = [];
  913. $newFileList = $fileModel->getDataList($f_where, 'all');
  914. if ($newFileList['list']) {
  915. foreach ($newFileList['list'] as $val) {
  916. if ($val['types'] == 'file') {
  917. $fileList[] = $val;
  918. } else {
  919. $imgList[] = $val;
  920. }
  921. }
  922. }
  923. $list[$k]['fileList'] = $fileList ?: [];
  924. $list[$k]['imgList'] = $imgList ?: [];
  925. $dataInfo['customerList'] = $relation_list['customerList'] ?: [];
  926. $dataInfo['contactsList'] = $relation_list['contactsList'] ?: [];
  927. $dataInfo['businessList'] = $relation_list['businessList'] ?: [];
  928. $dataInfo['contractList'] = $relation_list['contractList'] ?: [];
  929. $list[$k]['thumb_img'] = $v['thumb_img'] ? getFullPath($v['thumb_img']) : '';
  930. $list[$k]['dataInfo'] = $dataInfo ?: [];
  931. }
  932. }
  933. $data = [];
  934. $data['list'] = $list;
  935. $data['dataCount'] = $dataCount ?: 0;
  936. if ($param['page'] != 1 && ($param['page'] * $param['limit']) >= $dataCount) {
  937. $data['firstPage'] = false;
  938. $data['lastPage'] = true;
  939. } else if ($param['page'] != 1 && (int)($param['page'] * $param['limit']) < $dataCount) {
  940. $data['firstPage'] = false;
  941. $data['lastPage'] = false;
  942. } else if ($param['page'] == 1 && (int)($param['page'] * $param['limit']) < $dataCount) {
  943. $data['firstPage'] = true;
  944. $data['lastPage'] = false;
  945. }
  946. if(!empty($param['action'])) return $list;
  947. return $data ?: [];
  948. }
  949. }