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