IndexLogic.php 45KB

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