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