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