1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 客户
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\controller;
  8. use app\admin\controller\ApiCommon;
  9. use app\crm\logic\CustomerLogic;
  10. use app\crm\traits\SearchConditionTrait;
  11. use app\crm\traits\StarTrait;
  12. use think\Hook;
  13. use think\Request;
  14. use think\Db;
  15. class Customer extends ApiCommon
  16. {
  17. use StarTrait, SearchConditionTrait;
  18. /**
  19. * 用于判断权限
  20. * @permission 无限制
  21. * @allow 登录用户可访问
  22. * @other 其他根据系统设置
  23. **/
  24. public function _initialize()
  25. {
  26. $action = [
  27. 'permission' => ['exceldownload', 'setfollow', 'delete'],
  28. 'allow' => ['read', 'system', 'count', 'poolauthority', 'level']
  29. ];
  30. Hook::listen('check_auth', $action);
  31. $request = Request::instance();
  32. $a = strtolower($request->action());
  33. if (!in_array($a, $action['permission'])) {
  34. parent::_initialize();
  35. } else {
  36. $param = Request::instance()->param();
  37. $this->param = $param;
  38. }
  39. }
  40. /**
  41. * 客户列表
  42. * @return
  43. * @author Michael_xu
  44. */
  45. public function index()
  46. {
  47. $customerModel = model('Customer');
  48. $param = $this->param;
  49. $userInfo = $this->userInfo;
  50. $param['user_id'] = $userInfo['id'];
  51. $data = $customerModel->getDataList($param);
  52. return resultArray(['data' => $data]);
  53. }
  54. /**
  55. * 客户公海(没有负责人或已经到期)
  56. * @return
  57. * @author Michael_xu
  58. */
  59. public function pool()
  60. {
  61. $param = $this->param;
  62. $param['action'] = 'pool';
  63. unset($param['poolId']); # todo uniApp传来的参数,临时删除掉 fanqi。
  64. $data = model('Customer')->getDataList($param);
  65. return resultArray(['data' => $data]);
  66. }
  67. /**
  68. * 添加客户
  69. * @param
  70. * @return
  71. * @author Michael_xu
  72. */
  73. public function save()
  74. {
  75. $customerModel = model('Customer');
  76. $param = $this->param;
  77. $userInfo = $this->userInfo;
  78. $param['create_user_id'] = $userInfo['id'];
  79. $param['owner_user_id'] = $userInfo['id'];
  80. if ($res = $customerModel->createData($param)) {
  81. return resultArray(['data' => $res]);
  82. } else {
  83. return resultArray(['error' => $customerModel->getError()]);
  84. }
  85. }
  86. /**
  87. * 客户详情
  88. * @param
  89. * @return
  90. * @author Michael_xu
  91. */
  92. public function read()
  93. {
  94. $customerModel = model('Customer');
  95. $param = $this->param;
  96. $userInfo = $this->userInfo;
  97. $data = $customerModel->getDataById($param['id'], $userInfo['id']);
  98. if (!$data) {
  99. return resultArray(['error' => $customerModel->getError()]);
  100. }
  101. //数据权限判断
  102. $userModel = new \app\admin\model\User();
  103. $auth_user_ids = $userModel->getUserByPer('crm', 'customer', 'read');
  104. //读权限
  105. $roPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'read');
  106. $rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
  107. //判断是否客户池数据
  108. $wherePool = $customerModel->getWhereByPool();
  109. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $param['id']])->where($wherePool)->find();
  110. if (!$resPool && !in_array($data['owner_user_id'], $auth_user_ids) && !$roPre && !$rwPre) {
  111. $authData['dataAuth'] = (int)0;
  112. return resultArray(['data' => $authData]);
  113. }
  114. return resultArray(['data' => $data]);
  115. }
  116. /**
  117. * 编辑客户
  118. * @param
  119. * @return
  120. * @author Michael_xu
  121. */
  122. public function update()
  123. {
  124. $customerModel = model('Customer');
  125. $param = $this->param;
  126. $userInfo = $this->userInfo;
  127. //数据详情
  128. $data = $customerModel->getDataById($param['id']);
  129. if (!$data) {
  130. return resultArray(['error' => $customerModel->getError()]);
  131. }
  132. $param['user_id'] = $userInfo['id'];
  133. if ($customerModel->updateDataById($param, $param['id'])) {
  134. return resultArray(['data' => '编辑成功']);
  135. } else {
  136. return resultArray(['error' => $customerModel->getError()]);
  137. }
  138. }
  139. /**
  140. * 删除客户
  141. * @param
  142. * @return
  143. * @author Michael_xu
  144. */
  145. public function delete()
  146. {
  147. $param = $this->param;
  148. $user=new ApiCommon();
  149. $userInfo = $user->userInfo;
  150. // 是否客户池
  151. if ($param['isSeas'] == 1) {
  152. $permission = checkPerByAction('crm', 'customer', 'poolDelete');
  153. } else {
  154. $permission = checkPerByAction('crm', 'customer', 'delete');
  155. }
  156. if ($permission == false) {
  157. return resultArray(['error' => '无权操作']);
  158. }
  159. $customerModel = model('Customer');
  160. $userModel = new \app\admin\model\User();
  161. $recordModel = new \app\admin\model\Record();
  162. $fileModel = new \app\admin\model\File();
  163. $actionRecordModel = new \app\admin\model\ActionRecord();
  164. if (!is_array($param['id'])) {
  165. $customer_id[] = $param['id'];
  166. } else {
  167. $customer_id = $param['id'];
  168. }
  169. $delIds = [];
  170. $errorMessage = [];
  171. //数据权限判断
  172. $auth_user_ids = $userModel->getUserByPer('crm', 'customer', 'delete');
  173. //判断是否客户池数据(客户池数据只有管理员可以删)
  174. $adminId = $userModel->getAdminId();
  175. $wherePool = $customerModel->getWhereByPool();
  176. foreach ($customer_id as $k => $v) {
  177. $isDel = true;
  178. //数据详情
  179. $data = db('crm_customer')->where(['customer_id' => $v])->find();
  180. if (!$data) {
  181. $isDel = false;
  182. $errorMessage[] = 'id为' . $v . '的客户删除失败,错误原因:' . $customerModel->getError();
  183. }
  184. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $v])->where($wherePool)->find();
  185. if (!$resPool && !in_array($data['owner_user_id'], $auth_user_ids) && $isDel) {
  186. $isDel = false;
  187. $errorMessage[] = '名称为' . $data['name'] . '的客户删除失败,错误原因:无权操作';
  188. }
  189. // 公海 (原逻辑,公海仅允许管理员删除,修改为授权,不再限制)
  190. // if ($resPool && !in_array($data['owner_user_id'],$adminId)) {
  191. // $isDel = false;
  192. // $errorMessage[] = '名称为'.$data['name'].'的客户删除失败,错误原因:无权操作';
  193. // }
  194. //有商机、合同、联系人则不能删除
  195. if ($isDel) {
  196. $resBusiness = db('crm_business')->where(['customer_id' => $v])->find();
  197. if ($resBusiness) {
  198. $isDel = false;
  199. $errorMessage[] = '名称为' . $data['name'] . '的客户删除失败,错误原因:客户下存在商机,不能删除';
  200. }
  201. }
  202. if ($isDel) {
  203. $resContacts = db('crm_contacts')->where(['customer_id' => $v])->find();
  204. if ($resContacts) {
  205. $isDel = false;
  206. $errorMessage[] = '名称为' . $data['name'] . '的客户删除失败,错误原因:客户下存在联系人,不能删除';
  207. }
  208. }
  209. if ($isDel) {
  210. $resContract = db('crm_contract')->where(['customer_id' => $v])->find();
  211. if ($resContract) {
  212. $isDel = false;
  213. $errorMessage[] = '名称为' . $data['name'] . '的客户删除失败,错误原因:客户下存在合同,不能删除';
  214. }
  215. }
  216. if ($isDel) {
  217. $delIds[] = $v;
  218. }
  219. }
  220. $dataInfo = $customerModel->where('customer_id',['in',$delIds])->select();
  221. if ($delIds) {
  222. $delRes = $customerModel->delDatas($delIds);
  223. if (!$delRes) {
  224. return resultArray(['error' => $customerModel->getError()]);
  225. }
  226. //删除跟进记录
  227. $recordModel->delDataByTypes(2, $delIds);
  228. //删除关联附件
  229. $fileModel->delRFileByModule('crm_customer', $delIds);
  230. //删除关联操作记录
  231. $actionRecordModel->delDataById(['types' => 'crm_customer', 'action_id' => $delIds]);
  232. foreach ($dataInfo as $k => $v) {
  233. RecordActionLog($userInfo['id'], 'crm_customer', 'delete', $v['name'], '', '', '删除了客户:' . $v['name']);
  234. }
  235. }
  236. if ($errorMessage) {
  237. return resultArray(['error' => $errorMessage]);
  238. } else {
  239. return resultArray(['data' => '删除成功']);
  240. }
  241. }
  242. /**
  243. * 客户转移
  244. * @param owner_user_id 变更负责人
  245. * @param is_remove 1移出,2转为团队成员
  246. * @param types business,contract 相关模块
  247. * @param type 权限 1只读2读写
  248. * @return
  249. * @author Michael_xu
  250. */
  251. public function transfer()
  252. {
  253. $param = $this->param;
  254. $userInfo = $this->userInfo;
  255. $customerModel = model('Customer');
  256. $businessModel = model('Business');
  257. $contractModel = model('Contract');
  258. $contactsModel = model('Contacts');
  259. $settingModel = model('Setting');
  260. $customerConfigModel = model('CustomerConfig');
  261. $userModel = new \app\admin\model\User();
  262. if (!$param['owner_user_id']) {
  263. return resultArray(['error' => '变更负责人不能为空']);
  264. }
  265. if (!$param['customer_id'] || !is_array($param['customer_id'])) {
  266. return resultArray(['error' => '请选择需要转移的客户']);
  267. }
  268. $is_remove = ($param['is_remove'] == 2) ? 2 : 1;
  269. $type = $param['type'] == 2 ?: 1;
  270. $types = $param['types'] ?: [];
  271. $data = [];
  272. $data['owner_user_id'] = $param['owner_user_id'];
  273. $data['update_time'] = time();
  274. $data['follow'] = '待跟进';
  275. # 获取客户的时间
  276. $data['obtain_time'] = time();
  277. $ownerUserName = $userModel->getUserNameById($param['owner_user_id']);
  278. $errorMessage = [];
  279. foreach ($param['customer_id'] as $customer_id) {
  280. $customerInfo = db('crm_customer')->where(['customer_id' => $customer_id])->find();
  281. if (!$customerInfo) {
  282. $errorMessage[] = '名称:为《' . $customerInfo['name'] . '》的客户转移失败,错误原因:数据不存在;';
  283. continue;
  284. }
  285. $resCustomer = true;
  286. //权限判断
  287. if (!$customerModel->checkData($customer_id)) {
  288. $errorMessage[] = $customerInfo['name'] . '转移失败,错误原因:无权限;';
  289. continue;
  290. }
  291. //拥有客户数上限检测
  292. if (!$customerConfigModel->checkData($param['owner_user_id'], 1)) {
  293. $errorMessage[] = $customerInfo['name'] . '转移失败,错误原因:' . $customerConfigModel->getError();
  294. continue;
  295. }
  296. //团队成员
  297. $teamData = [];
  298. $teamData['type'] = $type; //权限 1只读2读写
  299. $teamData['user_id'] = [$customerInfo['owner_user_id']]; //协作人
  300. $teamData['types'] = 'crm_customer'; //类型
  301. $teamData['types_id'] = $customer_id; //类型ID
  302. $teamData['is_del'] = ($is_remove == 1) ? 1 : '';
  303. $res = $settingModel->createTeamData($teamData);
  304. # 处理分配标识,待办事项专用
  305. $data['is_allocation'] = 1;
  306. $resCustomer = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
  307. if (!$resCustomer) {
  308. $errorMessage[] = $customerInfo['name'] . '转移失败,错误原因:数据出错;';
  309. continue;
  310. } else {
  311. # 处理转移时,负责人出现在只读和读写成员列表中
  312. $customerArray = [];
  313. $teamCustomer = db('crm_customer')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('customer_id', $customer_id)->find();
  314. if (!empty($teamCustomer['ro_user_id'])) {
  315. $customerRo = arrayToString(array_diff(stringToArray($teamCustomer['ro_user_id']), [$teamCustomer['owner_user_id']]));
  316. $customerArray['ro_user_id'] = $customerRo;
  317. }
  318. if (!empty($teamCustomer['rw_user_id'])) {
  319. $customerRo = arrayToString(array_diff(stringToArray($teamCustomer['rw_user_id']), [$teamCustomer['owner_user_id']]));
  320. $customerArray['rw_user_id'] = $customerRo;
  321. }
  322. db('crm_customer')->where('customer_id', $customer_id)->update($customerArray);
  323. }
  324. if (in_array('crm_contacts', $types)) {
  325. $contactsIds = [];
  326. $contactsIds = db('crm_contacts')->where(['customer_id' => $customer_id])->column('contacts_id');
  327. if ($contactsIds) {
  328. $resContacts = $contactsModel->transferDataById($contactsIds, $param['owner_user_id'], $type, $is_remove);
  329. if ($resContacts !== true) {
  330. $errorMessage[] = $resContacts;
  331. continue;
  332. }
  333. }
  334. }
  335. //商机、合同转移
  336. if (in_array('crm_business', $types)) {
  337. $businessIds = [];
  338. $businessIds = db('crm_business')->where(['customer_id' => $customer_id])->column('business_id');
  339. if ($businessIds) {
  340. $resBusiness = $businessModel->transferDataById($businessIds, $param['owner_user_id'], $type, $is_remove);
  341. if ($resBusiness !== true) {
  342. $errorMessage = $errorMessage ? array_merge($errorMessage, $resBusiness) : $resBusiness;
  343. continue;
  344. }
  345. }
  346. }
  347. if (in_array('crm_contract', $types)) {
  348. $contractIds = [];
  349. $contractIds = db('crm_contract')->where(['customer_id' => $customer_id])->column('contract_id');
  350. if ($contractIds) {
  351. $resContract = $contractModel->transferDataById($contractIds, $param['owner_user_id'], $type, $is_remove);
  352. if ($resContract !== true) {
  353. $errorMessage = $errorMessage ? array_merge($errorMessage, $resContract) : $resContract;
  354. continue;
  355. }
  356. }
  357. }
  358. //修改记录
  359. updateActionLog($userInfo['id'], 'crm_customer', $customer_id, '', '', '将客户转移给:' . $ownerUserName);
  360. RecordActionLog($userInfo['id'], 'crm_customer', 'transfer',$customerInfo['name'], '','','将客户:'.$customerInfo['name'].'转移给:' . $ownerUserName);
  361. }
  362. if (!$errorMessage) {
  363. return resultArray(['data' => '转移成功']);
  364. } else {
  365. return resultArray(['error' => $errorMessage]);
  366. }
  367. }
  368. /**
  369. * 客户放入公海(负责人置为0)
  370. * @param
  371. * @return
  372. * @author Michael_xu
  373. */
  374. public function putInPool()
  375. {
  376. if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择要放入公海的客户!']);
  377. if (!is_array($this->param['customer_id'])) return resultArray(['error' => '客户ID格式不正确!']);
  378. if (empty($this->param['pool_id'])) return resultArray(['error' => '请选择公海!']);
  379. $userInfo = $this->userInfo;
  380. $userId = $userInfo['id'];
  381. $customerIds = $this->param['customer_id'];
  382. $poolId = $this->param['pool_id'];
  383. # 消息数据
  384. $message = [];
  385. # 获取客户数据
  386. $customerData = [];
  387. $customerList = db('crm_customer')->field(['customer_id', 'owner_user_id', 'name'])->whereIn('customer_id', $customerIds)->select();
  388. foreach ($customerList AS $key => $value) {
  389. $customerData[$value['customer_id']] = $value;
  390. }
  391. # 整理数据
  392. $ip = request()->ip();
  393. $poolRelationData = [];
  394. $poolRecordData = [];
  395. $fieldRecordData = [];
  396. $operationLogData = [];
  397. foreach ($customerIds AS $key => $value)
  398. {
  399. if (empty($customerData[$value])) {
  400. $message[] = '将客户放入公海失败,错误原因:数据不存在!';
  401. unset($customerIds[(int)$key]);
  402. continue;
  403. }
  404. if (isset($customerData[$value]['owner_user_id']) && empty($customerData[$value]['owner_user_id'])) {
  405. $message[] = '将客户《' . $customerData[$value]['name'] . '》放入公海失败,错误原因:已经处于公海!';
  406. unset($customerIds[(int)$key]);
  407. continue;
  408. }
  409. # 公海关联数据
  410. $poolRelationData[] = [
  411. 'pool_id' => $poolId,
  412. 'customer_id' => $value
  413. ];
  414. # 公海操作记录数据
  415. $poolRecordData[] = [
  416. 'customer_id' => $value,
  417. 'user_id' => $userId,
  418. 'pool_id' => $poolId,
  419. 'type' => 2,
  420. 'create_time' => time()
  421. ];
  422. # 字段操作记录数据
  423. $fieldRecordData[] = [
  424. 'user_id' => $userId,
  425. 'types' => 'crm_customer',
  426. 'action_id' => $value,
  427. 'content' => '将客户放入公海',
  428. 'create_time' => time()
  429. ];
  430. # 数据操作日志数据
  431. $operationLogData[] = [
  432. 'user_id' => $userId,
  433. 'client_ip' => $ip,
  434. 'module' => 'crm_customer',
  435. 'action_id' => $value,
  436. 'content' => '将客户放入公海',
  437. 'create_time' => time(),
  438. 'action_name' => 'update',
  439. 'target_name' => !empty($customerData[$value]['name']) ? $customerData[$value]['name'] : ''
  440. ];
  441. }
  442. if (empty($customerIds)) return resultArray(['error' => $message]);
  443. Db::startTrans();
  444. try {
  445. # 修改客户数据
  446. Db::name('crm_customer')->whereIn('customer_id', $customerIds)->exp('before_owner_user_id', 'owner_user_id')->update([
  447. 'ro_user_id' => '',
  448. 'rw_user_id' => '',
  449. 'owner_user_id' => 0,
  450. 'into_pool_time' => time()
  451. ]);
  452. # 删除联系人的负责人
  453. Db::name('crm_contacts')->whereIn('customer_id', $customerIds)->update(['owner_user_id' => 0]);
  454. # 将客户放入公海
  455. Db::name('crm_customer_pool_relation')->insertAll($poolRelationData);
  456. # 公海操作记录
  457. Db::name('crm_customer_pool_record')->insertAll($poolRecordData);
  458. # 字段操作记录
  459. Db::name('admin_action_record')->insertAll($fieldRecordData);
  460. # 数据操作日志
  461. Db::name('admin_operation_log')->insertAll($operationLogData);
  462. Db::commit();
  463. } catch (\Exception $e) {
  464. Db::rollback();
  465. $message = ['操作失败!'];
  466. }
  467. return resultArray(!empty($message) ? ['error' => $message] : ['data' => '操作成功!']);
  468. // $param = $this->param;
  469. // $userInfo = $this->userInfo;
  470. // $customerModel = model('Customer');
  471. // $settingModel = new \app\crm\model\Setting();
  472. // if (!$param['customer_id'] || !is_array($param['customer_id'])) {
  473. // return resultArray(['error' => '请选择需要放入公海的客户']);
  474. // }
  475. // $data = [];
  476. // $data['owner_user_id'] = 0;
  477. // $data['is_lock'] = 0;
  478. // $data['update_time'] = time();
  479. // $errorMessage = [];
  480. // foreach ($param['customer_id'] as $customer_id) {
  481. // $customerInfo = [];
  482. // $customerInfo = db('crm_customer')->where(['customer_id' => $customer_id])->find();
  483. // if (!$customerInfo) {
  484. // $errorMessage[] = '名称:为《' . $customerInfo['name'] . '》的客户放入公海失败,错误原因:数据不存在;';
  485. // continue;
  486. // }
  487. // //权限判断
  488. // if (!$customerModel->checkData($customer_id)) {
  489. // $errorMessage[] = '"' . $customerInfo['name'] . '"放入公海失败,错误原因:无权限';
  490. // continue;
  491. // }
  492. // //将团队成员全部清除
  493. // $data['ro_user_id'] = '';
  494. // $data['rw_user_id'] = '';
  495. // $resCustomer = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
  496. // if (!$resCustomer) {
  497. // $errorMessage[] = '"' . $customerInfo['name'] . '"放入公海失败,错误原因:数据出错;';
  498. // continue;
  499. // }
  500. // //联系人负责人清除
  501. // db('crm_contacts')->where(['customer_id' => $customer_id])->update(['owner_user_id' => 0]);
  502. // //修改记录
  503. // updateActionLog($userInfo['id'], 'crm_customer', $customer_id, '', '', '将客户放入公海');
  504. // RecordActionLog($userInfo['id'],'crm_pool','pool',$customerInfo['name'],'','','将客户'.$customerInfo['name'].'放入公海');
  505. // }
  506. // if (!$errorMessage) {
  507. // return resultArray(['data' => '操作成功']);
  508. // } else {
  509. // return resultArray(['error' => $errorMessage]);
  510. // }
  511. }
  512. /**
  513. * 客户锁定,解锁
  514. * @param is_lock 1锁定,2解锁
  515. * @return
  516. * @author Michael_xu
  517. */
  518. public function lock()
  519. {
  520. $param = $this->param;
  521. $userInfo = $this->userInfo;
  522. $customerModel = model('Customer');
  523. $customerConfigModel = model('CustomerConfig');
  524. $is_lock = ((int)$param['is_lock'] == 2) ? (int)$param['is_lock'] : 1;
  525. $lock_name = ($is_lock == 2) ? '解锁' : '锁定';
  526. if (!$param['customer_id'] || !is_array($param['customer_id'])) {
  527. return resultArray(['error' => '请选择需要' . $lock_name . '的客户']);
  528. }
  529. $data = [];
  530. $data['is_lock'] = ($is_lock == 1) ? $is_lock : 0;
  531. $data['update_time'] = time();
  532. $errorMessage = [];
  533. foreach ($param['customer_id'] as $customer_id) {
  534. $customerInfo = [];
  535. $customerInfo = $customerModel->getDataById($customer_id);
  536. if (!$customerInfo) {
  537. $errorMessage[] = '名称:为《' . $customerInfo['name'] . '》的客户' . $lock_name . '失败,错误原因:数据不存在;';
  538. continue;
  539. }
  540. //权限判断
  541. if (!$customerModel->checkData($customer_id)) {
  542. $errorMessage[] = $customerInfo['name'] . $lock_name . '失败,错误原因:无权限';
  543. continue;
  544. }
  545. //锁定上限检测
  546. if ($is_lock == 1 && !$customerConfigModel->checkData($customerInfo['owner_user_id'], 2)) {
  547. $errorMessage[] = $customerInfo['name'] . $lock_name . '失败,错误原因:' . $customerConfigModel->getError();
  548. continue;
  549. }
  550. //已成交客户,锁定,提示无需锁定
  551. // if ($customerInfo['deal_status'] == '已成交' && $is_lock == 1) {
  552. // $errorMessage[] = $customerInfo['name'].$lock_name.'失败,错误原因:已成交状态,无需锁定';
  553. // continue;
  554. // }
  555. $resCustomer = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
  556. if (!$resCustomer) {
  557. $errorMessage[] = $customerInfo['name'] . $lock_name . '失败,错误原因:数据出错;';
  558. }
  559. //修改记录
  560. updateActionLog($userInfo['id'], 'crm_customer', $customer_id, '', '', '将客户' . $lock_name);
  561. if($is_lock == 2){
  562. RecordActionLog($userInfo['id'], 'crm_customer', 'islock',$customerInfo['name'], '','','将客户'.$customerInfo['name'].$lock_name );
  563. }else{
  564. RecordActionLog($userInfo['id'], 'crm_customer', 'lock',$customerInfo['name'], '','','将客户'.$customerInfo['name'].$lock_name );
  565. }
  566. }
  567. if (!$errorMessage) {
  568. return resultArray(['data' => '操作成功']);
  569. } else {
  570. return resultArray(['error' => $errorMessage]);
  571. }
  572. }
  573. /**
  574. * 客户领取
  575. * @param
  576. * @return
  577. * @author Michael_xu
  578. */
  579. public function receive()
  580. {
  581. $param = $this->param;
  582. $userInfo = $this->userInfo;
  583. $customerModel = model('Customer');
  584. $customerConfigModel = model('CustomerConfig');
  585. $customer_ids = $param['customer_id'] ?: $userInfo['id'];
  586. if (!$customer_ids || !is_array($customer_ids)) {
  587. return resultArray(['error' => '请选择需要领取的客户']);
  588. }
  589. $errorMessage = [];
  590. $wherePool = $customerModel->getWhereByPool();
  591. foreach ($customer_ids as $k => $v) {
  592. $dataName = db('crm_customer')->where(['customer_id' => $v])->value('name');
  593. //判断是否是客户池数据
  594. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $v])->where($wherePool)->find();
  595. if (!$resPool) {
  596. $errorMessage[] = '客户《' . $dataName . '》领取失败,错误原因:非公海数据无权操作;';
  597. continue;
  598. }
  599. //拥有客户数上限检测
  600. if (!$customerConfigModel->checkData($userInfo['id'], 1)) {
  601. $errorMessage[] = '客户《' . $dataName . '》领取失败,错误原因:' . $customerConfigModel->getError();
  602. continue;
  603. }
  604. $data = [];
  605. $data['owner_user_id'] = $userInfo['id'];
  606. $data['update_time'] = time();
  607. $data['deal_time'] = time();
  608. $data['follow'] = '待跟进';
  609. //将团队成员全部清除
  610. $data['ro_user_id'] = '';
  611. $data['rw_user_id'] = '';
  612. # 获取客户的时间
  613. $data['obtain_time'] = time();
  614. $resCustomer = db('crm_customer')->where(['customer_id' => $v])->update($data);
  615. if (!$resCustomer) {
  616. $errorMessage[] = '客户《' . $dataName . '》领取失败,错误原因:数据出错;';
  617. continue;
  618. }
  619. //联系人领取
  620. db('crm_contacts')->where(['customer_id' => $v])->update(['owner_user_id' => $userInfo['id']]);
  621. //修改记录
  622. updateActionLog($userInfo['id'], 'crm_customer', $v, '', '', '领取了客户');
  623. RecordActionLog($userInfo['id'], 'crm_customer', 'update',$dataName, '','','领取了客户:'.$dataName);
  624. }
  625. if (!$errorMessage) {
  626. return resultArray(['data' => '领取成功']);
  627. } else {
  628. return resultArray(['error' => $errorMessage]);
  629. }
  630. }
  631. /**
  632. * 客户分配
  633. * @param
  634. * @return
  635. * @author Michael_xu
  636. */
  637. public function distribute()
  638. {
  639. $param = $this->param;
  640. $userInfo = $this->userInfo;
  641. $customerModel = model('Customer');
  642. $userModel = new \app\admin\model\User();
  643. $customerConfigModel = model('CustomerConfig');
  644. $customer_ids = $param['customer_id'];
  645. $owner_user_id = $param['owner_user_id'];
  646. if (!$customer_ids || !is_array($customer_ids)) {
  647. return resultArray(['error' => '请选择需要分配的客户']);
  648. }
  649. if (!$owner_user_id) {
  650. return resultArray(['error' => '请选择分配人']);
  651. }
  652. $ownerUserName = $userModel->getUserNameById($owner_user_id);
  653. $errorMessage = [];
  654. $wherePool = $customerModel->getWhereByPool();
  655. foreach ($customer_ids as $k => $v) {
  656. $dataName = db('crm_customer')->where(['customer_id' => $v])->value('name');
  657. //判断是否是客户池数据
  658. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $v])->where($wherePool)->find();
  659. if (!$resPool) {
  660. $errorMessage[] = '客户《' . $dataName . '》分配失败,错误原因:非公海数据无权操作;';
  661. continue;
  662. }
  663. //拥有客户数上限检测
  664. if (!$customerConfigModel->checkData($owner_user_id, 1)) {
  665. $errorMessage[] = '客户《' . $dataName . '》分配失败,错误原因:' . $customerConfigModel->getError();
  666. continue;
  667. }
  668. $data = [];
  669. $data['owner_user_id'] = $owner_user_id;
  670. $data['update_time'] = time();
  671. $data['deal_time'] = time();
  672. $data['follow'] = '待跟进';
  673. //将团队成员全部清除
  674. $data['ro_user_id'] = '';
  675. $data['rw_user_id'] = '';
  676. # 处理分配标识,待办事项专用
  677. $data['is_allocation'] = 1;
  678. # 获取客户的时间
  679. $data['obtain_time'] = time();
  680. $resCustomer = db('crm_customer')->where(['customer_id' => $v])->update($data);
  681. if (!$resCustomer) {
  682. $errorMessage[] = '客户《' . $dataName . '》分配失败,错误原因:数据出错;';
  683. }
  684. db('crm_contacts')->where(['customer_id' => $v])->update(['owner_user_id' => $owner_user_id]);
  685. //修改记录
  686. updateActionLog($userInfo['id'], 'crm_customer', $v, '', '', '将客户分配给:' . $ownerUserName);
  687. RecordActionLog($userInfo['id'], 'crm_customer', 'distribute',$dataName, '','','将客户'.$dataName.'分配给:' . $ownerUserName);
  688. //站内信
  689. $send_user_id[] = $owner_user_id;
  690. $sendContent = $userInfo['realname'] . '将客户《' . $dataName . '》,分配给您';
  691. if ($send_user_id) {
  692. sendMessage($send_user_id, $sendContent, $v, 1);
  693. }
  694. }
  695. if (!$errorMessage) {
  696. return resultArray(['data' => '分配成功']);
  697. } else {
  698. return resultArray(['error' => $errorMessage]);
  699. }
  700. }
  701. /**
  702. * 客户导出
  703. * @param
  704. * @return
  705. * @author Michael_xu
  706. */
  707. public function excelExport()
  708. {
  709. $param = $this->param;
  710. $userInfo = $this->userInfo;
  711. $param['user_id'] = $userInfo['id'];
  712. $action_name='导出全部';
  713. if ($param['customer_id']) {
  714. $param['customer_id'] = ['condition' => 'in', 'value' => $param['customer_id'], 'form_type' => 'text', 'name' => ''];
  715. $action_name='导出选中';
  716. }
  717. $param['is_excel'] = 1;
  718. $excelModel = new \app\admin\model\Excel();
  719. // 导出的字段列表
  720. $fieldModel = new \app\admin\model\Field();
  721. $field_list = $fieldModel->getIndexFieldConfig('crm_customer', $userInfo['id']);
  722. // 文件名
  723. $file_name = '5kcrm_customer_' . date('Ymd');
  724. $model = model('Customer');
  725. $temp_file = $param['temp_file'];
  726. unset($param['temp_file']);
  727. $page = $param['page'] ?: 1;
  728. unset($param['page']);
  729. unset($param['export_queue_index']);
  730. RecordActionLog($userInfo['id'],'crm_customer','excelexport',$action_name,'','','导出客户');
  731. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list) {
  732. $param['page'] = $page;
  733. $param['limit'] = $limit;
  734. $data = $model->getDataList($param);
  735. $data['list'] = $model->exportHandle($data['list'], $field_list, 'customer');
  736. return $data;
  737. });
  738. }
  739. /**
  740. * 客户导入模板下载
  741. * @param string $save_path 本地保存路径 用于错误数据导出,在 Admin\Model\Excel::batchImportData()调用
  742. * @return
  743. * @author Michael_xu
  744. */
  745. public function excelDownload($save_path = '')
  746. {
  747. $param = $this->param;
  748. $userInfo = $this->userInfo;
  749. $excelModel = new \app\admin\model\Excel();
  750. // 导入的字段列表
  751. $fieldModel = new \app\admin\model\Field();
  752. $fieldParam['types'] = 'crm_customer';
  753. $fieldParam['action'] = 'excel';
  754. $field_list = $fieldModel->field($fieldParam);
  755. $excelModel->excelImportDownload($field_list, 'crm_customer', $save_path);
  756. # 下次升级
  757. // $param = $this->param;
  758. // $userInfo = $this->userInfo;
  759. // $excelModel = new \app\admin\model\Excel();
  760. //
  761. // // 导入的字段列表
  762. // $fieldModel = new \app\admin\model\Field();
  763. // $fieldParam['types'] = 'crm_customer';
  764. // $fieldParam['action'] = 'excel';
  765. // $field_list = $fieldModel->field($fieldParam);
  766. // $field=[1=>[
  767. // 'field'=>'owner_user_id',
  768. // 'types'=>'crm_customer',
  769. // 'name'=>'负责人',
  770. // 'form_type'=>'user',
  771. // 'default_value'=>'',
  772. // 'is_unique' => 1,
  773. // 'is_null' => 1,
  774. // 'input_tips' =>'',
  775. // 'setting' => Array(),
  776. // 'is_hidden'=>0,
  777. // 'writeStatus' => 1,
  778. // 'value' => '']
  779. // ];
  780. // $first_array = array_splice($field_list, 2,0, $field);
  781. // $array = array_merge($first_array, $field, $field_list);
  782. // $excelModel->excelImportDownload($field_list, 'crm_customer', $save_path);
  783. }
  784. /**
  785. * 客户数据导入
  786. * @param
  787. * @return
  788. * @author Michael_xu
  789. */
  790. public function excelImport()
  791. {
  792. $param = $this->param;
  793. $userInfo = $this->userInfo;
  794. $excelModel = new \app\admin\model\Excel();
  795. $param['create_user_id'] = $userInfo['id'];
  796. $param['owner_user_id'] = $param['owner_user_id'] ?: 0;
  797. $param['deal_time'] = time();
  798. $param['deal_status'] = '未成交';
  799. $param['types'] = 'crm_customer';
  800. $file = request()->file('file');
  801. // $res = $excelModel->importExcel($file, $param, $this);
  802. $res = $excelModel->batchImportData($file, $param, $this);
  803. RecordActionLog($userInfo['id'],'crm_customer','excel','导入客户','','','导入客户');
  804. return resultArray(['data' => $excelModel->getError()]);
  805. }
  806. /**
  807. * 客户标记为已跟进
  808. * @param
  809. * @return
  810. * @author Michael_xu
  811. */
  812. public function setFollow()
  813. {
  814. $param = $this->param;
  815. $customerIds = $param['id'] ?: [];
  816. if (!$customerIds || !is_array($customerIds)) {
  817. return resultArray(['error' => '参数错误']);
  818. }
  819. $data['follow'] = '已跟进';
  820. $data['update_time'] = time();
  821. $res = db('crm_customer')->where(['customer_id' => ['in', $customerIds]])->update($data);
  822. if (!$res) {
  823. return resultArray(['error' => '操作失败,请重试']);
  824. }
  825. return resultArray(['data' => '跟进成功']);
  826. }
  827. /**
  828. * 置顶 / 取消置顶
  829. * @return [type] [description]
  830. */
  831. public function top()
  832. {
  833. $param = $this->param;
  834. $userInfo = $this->userInfo;
  835. $param['create_role_id'] = $userInfo['id'];
  836. $param['top_time'] = time();
  837. $top_id = Db::name('crm_top')->where(['module' => ['eq', $param['module']], 'create_role_id' => ['eq', $userInfo['id']], 'module_id' => ['eq', $param['module_id']]])->column('top_id');
  838. if ($top_id) {
  839. if ($res = Db::name('crm_top')->where('top_id', $top_id[0])->update($param)) {
  840. return resultArray(['data' => $res]);
  841. } else {
  842. return resultArray(['error' => Db::name('crm_top')->getError()]);
  843. }
  844. } else {
  845. if ($res = Db::name('crm_top')->data($param)->insert()) {
  846. return resultArray(['data' => $res]);
  847. } else {
  848. return resultArray(['error' => $customerModel->getError()]);
  849. }
  850. }
  851. }
  852. /**
  853. * 客户公海导出
  854. * @param
  855. * @return
  856. * @author Michael_xu
  857. */
  858. public function poolExcelExport()
  859. {
  860. $param = $this->param;
  861. $userInfo = $this->userInfo;
  862. $param['user_id'] = $userInfo['id'];
  863. if ($param['customer_id']) {
  864. $param['customer_id'] = ['condition' => 'in', 'value' => $param['customer_id'], 'form_type' => 'text', 'name' => ''];
  865. $action_name='导出选中';
  866. }
  867. $param['is_excel'] = 1;
  868. $excelModel = new \app\admin\model\Excel();
  869. // 导出的字段列表
  870. $fieldModel = new \app\admin\model\Field();
  871. $field_list = $fieldModel->getIndexFieldConfig('crm_customer', $userInfo['id']);
  872. $field_list = array_filter($field_list, function ($val) {
  873. return $val['field'] != 'owner_user_id';
  874. });
  875. // 文件名
  876. $file_name = '5kcrm_customer_pool_' . date('Ymd');
  877. $param['action'] = 'pool';
  878. $model = model('Customer');
  879. $temp_file = $param['temp_file'];
  880. unset($param['temp_file']);
  881. $page = $param['page'] ?: 1;
  882. unset($param['page']);
  883. unset($param['export_queue_index']);
  884. RecordActionLog($userInfo['id'],'crm_customer','excelexport',$action_name,'','','导出客户');
  885. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param) {
  886. $param['page'] = $page;
  887. $param['limit'] = $limit;
  888. $data = $model->getDataList($param);
  889. $data['list'] = $model->exportHandle($data['list'], $field_list,'crm_customer');
  890. return $data;
  891. });
  892. }
  893. /**
  894. * 客户成交状态
  895. * @param status 1已成交,2未成交
  896. * @return
  897. * @author Michael_xu
  898. */
  899. public function deal_status()
  900. {
  901. $param = $this->param;
  902. $userInfo = $this->userInfo;
  903. $statusArr = ['1' => '已成交', '2' => '未成交'];
  904. $statusList = ['1', '2'];
  905. if (!$param['customer_id'] || !in_array($param['status'], $statusList)) {
  906. return resultArray(['error' => '参数错误']);
  907. }
  908. $customerModel = model('Customer');
  909. $customerConfigModel = model('CustomerConfig');
  910. $userModel = new \app\admin\model\User();
  911. $customer_ids = $param['customer_id'];
  912. if (!is_array($customer_ids) || !$customer_ids) {
  913. $customer_ids[] = $customer_ids;
  914. }
  915. $data = [];
  916. $data['update_time'] = time();
  917. $data['deal_time'] = time();
  918. $data['deal_status'] = $statusArr[$param['status']];
  919. $errorMessage = [];
  920. foreach ($customer_ids as $customer_id) {
  921. $dataInfo = [];
  922. $dataInfo = db('crm_customer')->where(['customer_id' => $customer_id])->field('owner_user_id,deal_status,name')->find();
  923. //权限判断
  924. if (!$customerModel->checkData($customer_id, 1)) {
  925. $errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:' . $customerModel->getError();
  926. continue;
  927. }
  928. $owner_user_id = $dataInfo['owner_user_id'];;
  929. if (!$owner_user_id) {
  930. $errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:公海数据无权操作';
  931. continue;
  932. }
  933. //拥有客户数上限检测
  934. if ($statusArr[$param['status']] == '未成交' && $dataInfo['deal_status'] == '已成交') {
  935. if (!$customerConfigModel->checkData($owner_user_id, 1, 1)) {
  936. $errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:' . $customerConfigModel->getError();
  937. continue;
  938. }
  939. }
  940. if ($statusArr[$param['status']] == '已成交') {
  941. $data['is_lock'] = 0;
  942. }
  943. $res = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
  944. if (!$res) {
  945. $errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:操作失败,请重试!';
  946. continue;
  947. }
  948. //修改记录
  949. updateActionLog($userInfo['id'], 'crm_customer', $customer_id, ['deal_status' => $dataInfo['deal_status']], ['deal_status' => $data['deal_status']]);
  950. if($param['status']==1){
  951. RecordActionLog($userInfo['id'], 'crm_customer', 'status',$dataInfo['name'], '','','修改客户:'.$dataInfo['name'].'成交状态:'.$statusArr[$param['status']]);
  952. }else{
  953. RecordActionLog($userInfo['id'], 'crm_customer', 'status',$dataInfo['name'], '','','修改客户:'.$dataInfo['name'].'成交状态:'.$statusArr[$param['status']]);
  954. }
  955. }
  956. if (!$errorMessage) {
  957. return resultArray(['data' => '操作成功']);
  958. } else {
  959. return resultArray(['error' => $errorMessage]);
  960. }
  961. }
  962. /**
  963. * 设置关注
  964. *
  965. * @return \think\response\Json
  966. * @throws \think\Exception
  967. * @throws \think\exception\PDOException
  968. */
  969. public function star()
  970. {
  971. $userId = $this->userInfo['id'];
  972. $targetId = $this->param['target_id'];
  973. $type = $this->param['type'];
  974. if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']);
  975. if (!$this->setStar($type, $userId, $targetId)) {
  976. return resultArray(['error' => '设置关注失败!']);
  977. }
  978. return resultArray(['data' => '设置关注成功!']);
  979. }
  980. /**
  981. * 附近客户
  982. *
  983. * @return \think\response\Json
  984. */
  985. public function nearby()
  986. {
  987. if (empty($this->param['lng'])) return resultArray(['error' => '缺少经度参数!']);
  988. if (empty($this->param['lat'])) return resultArray(['error' => '缺少纬度参数!']);
  989. if (empty($this->param['distance'])) return resultArray(['error' => '请选择距离!']);
  990. $customerModel = model('Customer');
  991. $data = $customerModel->getNearbyList($this->param);
  992. return resultArray(['data' => $data]);
  993. }
  994. /**
  995. * 系统信息
  996. *
  997. * @return \think\response\Json
  998. * @throws \think\db\exception\DataNotFoundException
  999. * @throws \think\db\exception\ModelNotFoundException
  1000. * @throws \think\exception\DbException
  1001. */
  1002. public function system()
  1003. {
  1004. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  1005. $customerModel = new \app\crm\model\Customer();
  1006. $data = $customerModel->getSystemInfo($this->param['id']);
  1007. return resultArray(['data' => $data]);
  1008. }
  1009. /**
  1010. * table标签栏数量
  1011. *
  1012. * @return \think\response\Json
  1013. * @throws \think\db\exception\DataNotFoundException
  1014. * @throws \think\db\exception\ModelNotFoundException
  1015. * @throws \think\exception\DbException
  1016. */
  1017. public function count()
  1018. {
  1019. if (empty($this->param['customer_id'])) return resultArray(['error' => '参数错误!']);
  1020. $userInfo = $this->userInfo;
  1021. $customerId = $this->param['customer_id'];
  1022. # 联系人
  1023. $contactsAuth = $this->getContactsSearchWhere($userInfo['id']);
  1024. $contactsCount = Db::name('crm_contacts')->where('customer_id', $customerId)->where($contactsAuth)->count();
  1025. # 团队成员
  1026. $customer = Db::name('crm_customer')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('customer_id', $customerId)->find();
  1027. $customer['ro_user_id'] = explode(',', trim($customer['ro_user_id'], ','));
  1028. $customer['rw_user_id'] = explode(',', trim($customer['rw_user_id'], ','));
  1029. $customer['owner_user_id'] = [$customer['owner_user_id']];
  1030. $teamCount = array_filter(array_unique(array_merge($customer['ro_user_id'], $customer['rw_user_id'], $customer['owner_user_id'])));
  1031. # 商机
  1032. $businessAuth = $this->getBusinessSearchWhere($userInfo['id']);
  1033. $businessCount = Db::name('crm_business')->where('customer_id', $customerId)->where($businessAuth)->count();
  1034. # 合同
  1035. $contractAuth = $this->getContractSearchWhere($userInfo['id']);
  1036. $contractCount = Db::name('crm_contract')->where('customer_id', $customerId)->where($contractAuth)->count();
  1037. # 回款
  1038. $receivablesAuth = $this->getReceivablesSearchWhere();
  1039. $receivablesCount = Db::name('crm_receivables')->where('customer_id', $customerId)->whereIn('owner_user_id', $receivablesAuth)->count();
  1040. # 回访
  1041. $visitAuth = $this->getVisitSearchWhere($userInfo['id']);
  1042. $visitCount = Db::name('crm_visit')->where(['customer_id' => $customerId, 'deleted_state' => 0])->where($visitAuth)->count();
  1043. # 发票
  1044. $invoiceAuth = $this->getInvoiceSearchWhere();
  1045. $invoiceCount = Db::name('crm_invoice')->where('customer_id', $customerId)->whereIn('owner_user_id', $invoiceAuth)->count();
  1046. # 附件
  1047. $fileCount = Db::name('crm_customer_file')->alias('customer')->join('__ADMIN_FILE__ file', 'file.file_id = customer.file_id')->where('customer_id', $customerId)->count();
  1048. $data = [
  1049. 'businessCount' => $businessCount,
  1050. 'contactCount' => $contactsCount,
  1051. 'contractCount' => $contractCount,
  1052. 'fileCount' => $fileCount,
  1053. 'invoiceCount' => $invoiceCount,
  1054. 'memberCount' => count($teamCount),
  1055. 'receivablesCount' => $receivablesCount,
  1056. 'returnVisitCount' => $visitCount
  1057. ];
  1058. return resultArray(['data' => $data]);
  1059. }
  1060. /**
  1061. * 公海权限
  1062. *
  1063. * @param CustomerLogic $customerLogic
  1064. * @return \think\response\Json
  1065. * @throws \think\db\exception\DataNotFoundException
  1066. * @throws \think\db\exception\ModelNotFoundException
  1067. * @throws \think\exception\DbException
  1068. */
  1069. public function poolAuthority(CustomerLogic $customerLogic)
  1070. {
  1071. $authority = [
  1072. 'delete' => false, # 删除
  1073. 'distribute' => false, # 分配
  1074. 'excelexport' => false, # 导出
  1075. 'index' => false, # 列表
  1076. 'receive' => false, # 领取
  1077. ];
  1078. $userId = $this->userInfo['id'];
  1079. if (empty($userId)) return resultArray(['data' => $authority]);
  1080. # 员工角色数据
  1081. $groupIds = $customerLogic->getEmployeeGroups($userId);
  1082. # 员工角色下的规则数据
  1083. $ruleIds = $customerLogic->getEmployeeRules($groupIds);
  1084. # 公海规则数据
  1085. $poolRules = $customerLogic->getPoolRules();
  1086. # 整理员工规则数据
  1087. $rules = [];
  1088. $ruleIds = implode(',', $ruleIds);
  1089. $rules = array_filter(array_unique(explode(',', $ruleIds)));
  1090. # 整理公海规则数据
  1091. $deleteId = $distributeId = $exportId = $indexId = $receiveId = 0;
  1092. foreach ($poolRules as $key => $value) {
  1093. if ($value['name'] == 'pool') $indexId = $value['id'];
  1094. if ($value['name'] == 'distribute') $distributeId = $value['id'];
  1095. if ($value['name'] == 'receive') $receiveId = $value['id'];
  1096. if ($value['name'] == 'poolExcelExport') $exportId = $value['id'];
  1097. if ($value['name'] == 'poolDelete') $deleteId = $value['id'];
  1098. }
  1099. # 权限判断
  1100. $authority['delete'] = $userId == 1 || in_array(1, $groupIds) || in_array($deleteId, $rules) ? true : false;
  1101. $authority['distribute'] = $userId == 1 || in_array(1, $groupIds) || in_array($distributeId, $rules) ? true : false;
  1102. $authority['excelexport'] = $userId == 1 || in_array(1, $groupIds) || in_array($exportId, $rules) ? true : false;
  1103. $authority['index'] = $userId == 1 || in_array(1, $groupIds) || in_array($indexId, $rules) ? true : false;
  1104. $authority['receive'] = $userId == 1 || in_array(1, $groupIds) || in_array($receiveId, $rules) ? true : false;
  1105. return resultArray(['data' => $authority]);
  1106. }
  1107. /**
  1108. * 客户级别列表
  1109. *
  1110. * @author fanqi
  1111. * @since 2021-03-29
  1112. * @return \think\response\Json
  1113. */
  1114. public function level()
  1115. {
  1116. $data = db('admin_field')->where(['types' => 'crm_customer', 'field' => 'level'])->value('setting');
  1117. $data = explode(chr(10), $data);
  1118. return resultArray(['data' => $data]);
  1119. }
  1120. }