123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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\traits\SearchConditionTrait;
  10. use app\crm\traits\StarTrait;
  11. use think\Hook;
  12. use think\Request;
  13. use think\Db;
  14. class Business extends ApiCommon
  15. {
  16. use StarTrait, SearchConditionTrait;
  17. /**
  18. * 用于判断权限
  19. * @permission 无限制
  20. * @allow 登录用户可访问
  21. * @other 其他根据系统设置
  22. **/
  23. public function _initialize()
  24. {
  25. $action = [
  26. 'permission' => [''],
  27. 'allow' => ['statuslist', 'advance', 'product', 'system', 'count', 'setprimary']
  28. ];
  29. Hook::listen('check_auth', $action);
  30. $request = Request::instance();
  31. $a = strtolower($request->action());
  32. if (!in_array($a, $action['permission'])) {
  33. parent::_initialize();
  34. }
  35. }
  36. /**
  37. * 商机列表
  38. * @return
  39. * @author Michael_xu
  40. */
  41. public function index()
  42. {
  43. $businessModel = model('Business');
  44. $param = $this->param;
  45. $userInfo = $this->userInfo;
  46. $param['user_id'] = $userInfo['id'];
  47. $data = $businessModel->getDataList($param);
  48. return resultArray(['data' => $data]);
  49. }
  50. /**
  51. * 添加商机
  52. * @param
  53. * @return
  54. * @author Michael_xu
  55. */
  56. public function save()
  57. {
  58. $businessModel = model('Business');
  59. $param = $this->param;
  60. $userInfo = $this->userInfo;
  61. $param['create_user_id'] = $userInfo['id'];
  62. $param['owner_user_id'] = $userInfo['id'];
  63. if ($businessModel->createData($param)) {
  64. return resultArray(['data' => '添加成功']);
  65. } else {
  66. return resultArray(['error' => $businessModel->getError()]);
  67. }
  68. }
  69. /**
  70. * 商机详情
  71. * @param
  72. * @return
  73. * @author Michael_xu
  74. */
  75. public function read()
  76. {
  77. $businessModel = model('Business');
  78. $businessStatusModel = model('BusinessStatus');
  79. $userModel = new \app\admin\model\User();
  80. $param = $this->param;
  81. $userInfo = $this->userInfo;
  82. $data = $businessModel->getDataById($param['id'], $userInfo['id']);
  83. //判断权限
  84. $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'read');
  85. //读权限
  86. $roPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'read');
  87. $rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
  88. if (!in_array($data['owner_user_id'], $auth_user_ids) && !$rwPre && !$roPre) {
  89. $authData['dataAuth'] = (int)0;
  90. return resultArray(['data' => $authData]);
  91. }
  92. //商机状态组
  93. $data['status_list'] = $businessStatusModel->getDataById($data['type_id']);
  94. $data['lose_reason'] = Db::name('CrmBusinessLog')
  95. ->where(['business_id' => $data['business_id']])
  96. ->order(['id' => 'DESC'])
  97. ->value('remark');
  98. if (!$data) {
  99. return resultArray(['error' => $businessModel->getError()]);
  100. }
  101. return resultArray(['data' => $data]);
  102. }
  103. /**
  104. * 编辑商机
  105. * @param
  106. * @return
  107. * @author Michael_xu
  108. */
  109. public function update()
  110. {
  111. $businessModel = model('Business');
  112. $userModel = new \app\admin\model\User();
  113. $param = $this->param;
  114. $userInfo = $this->userInfo;
  115. $param['user_id'] = $userInfo['id'];
  116. //判断权限
  117. $data = $businessModel->getDataById($param['id']);
  118. $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'update');
  119. //读写权限
  120. $rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
  121. if (!in_array($data['owner_user_id'], $auth_user_ids) && !$rwPre) {
  122. header('Content-Type:application/json; charset=utf-8');
  123. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  124. }
  125. if ($businessModel->updateDataById($param, $param['id'])) {
  126. return resultArray(['data' => '编辑成功']);
  127. } else {
  128. return resultArray(['error' => $businessModel->getError()]);
  129. }
  130. }
  131. /**
  132. * 删除商机(逻辑删)
  133. * @param
  134. * @return
  135. * @author Michael_xu
  136. */
  137. public function delete()
  138. {
  139. $param = $this->param;
  140. $userInfo = $this->userInfo;
  141. $businessModel = model('Business');
  142. $recordModel = new \app\admin\model\Record();
  143. $fileModel = new \app\admin\model\File();
  144. $actionRecordModel = new \app\admin\model\ActionRecord();
  145. if (!is_array($param['id'])) {
  146. $business_id[] = $param['id'];
  147. } else {
  148. $business_id = $param['id'];
  149. }
  150. $delIds = [];
  151. $errorMessage = [];
  152. //数据权限判断
  153. $userModel = new \app\admin\model\User();
  154. $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'delete');
  155. foreach ($business_id as $k => $v) {
  156. $isDel = true;
  157. //数据详情
  158. $data = $businessModel->getDataById($v);
  159. if (!$data) {
  160. $isDel = false;
  161. $errorMessage[] = 'id为' . $v . '的商机删除失败,错误原因:' . $businessModel->getError();
  162. }
  163. if (!in_array($data['owner_user_id'], $auth_user_ids)) {
  164. $isDel = false;
  165. $errorMessage[] = '名称为' . $data['name'] . '的商机删除失败,错误原因:无权操作';
  166. }
  167. if ($isDel) {
  168. if (db('crm_contract')->where(['business_id' => $v, 'check_status' => ['in', '0,1,2']])->value('contract_id')) {
  169. $isDel = false;
  170. $errorMessage[] = '名称为' . $data['name'] . '的商机删除失败,错误原因:商机下关联的有合同,无法删除!';
  171. }
  172. }
  173. if ($isDel) {
  174. $delIds[] = $v;
  175. }
  176. }
  177. if ($delIds) {
  178. $data = $businessModel->delDatas($delIds);
  179. if (!$data) {
  180. return resultArray(['error' => $businessModel->getError()]);
  181. }
  182. // 删除客户扩展数据
  183. db('crm_business_data')->whereIn('business_id', $delIds)->delete();
  184. //删除跟进记录
  185. $recordModel->delDataByTypes(5, $delIds);
  186. //删除关联附件
  187. $fileModel->delRFileByModule('crm_business', $delIds);
  188. //删除关联操作记录
  189. $actionRecordModel->delDataById(['types' => 'crm_business', 'action_id' => $delIds]);
  190. $dataInfo = $businessModel->where('business_id',['in',$delIds])->select();
  191. foreach ($dataInfo as $k => $v) {
  192. RecordActionLog($userInfo['id'], 'crm_business', 'delete', $v['name'], '', '', '删除了商机:' . $v['name']);
  193. }
  194. }
  195. if ($errorMessage) {
  196. return resultArray(['error' => $errorMessage]);
  197. } else {
  198. return resultArray(['data' => '删除成功']);
  199. }
  200. }
  201. /**
  202. * 符合条件的商机状态组
  203. * @param
  204. * @return
  205. * @author Michael_xu
  206. */
  207. public function statusList()
  208. {
  209. $businessStatusModel = model('BusinessStatus');
  210. $key = 'BI_queryCache_StatusList_Data';
  211. $list = cache($key);
  212. if (!$list) {
  213. $userInfo = $this->userInfo;
  214. $authMap = function($query) use ($userInfo){
  215. $query->where(['structure_id' => ['like', '%,' . $userInfo['structure_id'] . ',%'],'is_display'=> 1,'status'=> 1])
  216. ->whereOr(function ($query) use ($userInfo) {
  217. $query->where(['structure_id' => ''])->where(['is_display'=> 1,'status'=> 1]);
  218. });
  219. };
  220. $list = db('crm_business_type')
  221. ->field(['name', 'status', 'structure_id', 'type_id'])
  222. ->where($authMap)
  223. ->select();
  224. foreach ($list as $k => $v) {
  225. $list[$k]['statusList'] = $businessStatusModel->getDataList($v['type_id']);
  226. }
  227. cache($key, $list, config('business_status_cache_time'));
  228. }else{
  229. cache($key, NULL);
  230. }
  231. return resultArray(['data' => $list]);
  232. }
  233. /**
  234. * 商机转移
  235. * @param owner_user_id 变更负责人
  236. * @param is_remove 1移出,2转为团队成员
  237. * @param type 权限 1只读2读写
  238. * @return
  239. * @author Michael_xu
  240. */
  241. public function transfer()
  242. {
  243. $param = $this->param;
  244. $userInfo = $this->userInfo;
  245. $businessModel = model('Business');
  246. $settingModel = model('Setting');
  247. $userModel = new \app\admin\model\User();
  248. $authIds = $userModel->getUserByPer(); //权限范围的user_id
  249. if (!$param['owner_user_id']) {
  250. return resultArray(['error' => '变更负责人不能为空']);
  251. }
  252. if (!$param['business_id'] || !is_array($param['business_id'])) {
  253. return resultArray(['error' => '请选择需要转移的商机']);
  254. }
  255. $is_remove = $param['is_remove'] == 2 ? 2 : 1;
  256. $type = $param['type'] == 2 ? 2 : 1;
  257. $data = [];
  258. $data['owner_user_id'] = $param['owner_user_id'];
  259. $data['update_time'] = time();
  260. $ownerUserName = $userModel->getUserNameById($param['owner_user_id']);
  261. $errorMessage = [];
  262. foreach ($param['business_id'] as $business_id) {
  263. $businessInfo = $businessModel->getDataById($business_id);
  264. if (!$businessInfo) {
  265. $errorMessage[] = '名称:为《' . $businessInfo['name'] . '》的商机转移失败,错误原因:数据不存在;';
  266. continue;
  267. }
  268. //权限判断
  269. if (!in_array($businessInfo['owner_user_id'], $authIds)) {
  270. $errorMessage[] = $businessInfo['name'] . '"转移失败,错误原因:无权限;';
  271. continue;
  272. }
  273. //团队成员
  274. teamUserId(
  275. $param,
  276. 'crm_business',
  277. $business_id,
  278. $type,
  279. [$businessInfo['owner_user_id']],
  280. $is_remove,
  281. 0
  282. );
  283. $resBusiness = db('crm_business')->where(['business_id' => $business_id])->update($data);
  284. if (!$resBusiness) {
  285. $errorMessage[] = $businessInfo['name'] . '"转移失败,错误原因:数据出错;';
  286. continue;
  287. } else {
  288. $businessArray = [];
  289. $teamBusiness = db('crm_business')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('business_id', $business_id)->find();
  290. if (!empty($teamBusiness['ro_user_id'])) {
  291. $businessRo = arrayToString(array_diff(stringToArray($teamBusiness['ro_user_id']), [$teamBusiness['owner_user_id']]));
  292. $businessArray['ro_user_id'] = $businessRo;
  293. }
  294. if (!empty($teamBusiness['rw_user_id'])) {
  295. $businessRo = arrayToString(array_diff(stringToArray($teamBusiness['rw_user_id']), [$teamBusiness['owner_user_id']]));
  296. $businessArray['rw_user_id'] = $businessRo;
  297. }
  298. db('crm_business')->where('business_id', $business_id)->update($businessArray);
  299. }
  300. //修改记录
  301. updateActionLog($userInfo['id'], 'crm_business', $business_id, '', '', '将商机转移给:' . $ownerUserName);
  302. RecordActionLog($userInfo['id'], 'crm_business', 'transfer', $businessInfo['name'], '', '', '将商机:' . $businessInfo['name'] . '转移给:' . $ownerUserName);
  303. }
  304. if (!$errorMessage) {
  305. return resultArray(['data' => '转移成功']);
  306. } else {
  307. return resultArray(['error' => $errorMessage]);
  308. }
  309. }
  310. /**
  311. * 相关产品
  312. * @param
  313. * @return
  314. * @author Michael_xu
  315. */
  316. public function product()
  317. {
  318. $productModel = model('Product');
  319. $userModel = new \app\admin\model\User();
  320. $param = $this->param;
  321. $userInfo = $this->userInfo;
  322. if (!$param['business_id']) {
  323. return resultArray(['error' => '参数错误']);
  324. }
  325. $businessInfo = db('crm_business')->where(['business_id' => $param['business_id']])->find();
  326. //判断权限
  327. $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'read');
  328. //读写权限
  329. $roPre = $userModel->rwPre($userInfo['id'], $businessInfo['ro_user_id'], $businessInfo['rw_user_id'], 'read');
  330. $rwPre = $userModel->rwPre($userInfo['id'], $businessInfo['ro_user_id'], $businessInfo['rw_user_id'], 'update');
  331. if (!in_array($businessInfo['owner_user_id'], $auth_user_ids) && !$roPre && !$rwPre) {
  332. header('Content-Type:application/json; charset=utf-8');
  333. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  334. }
  335. $dataList = db('crm_business_product')->where(['business_id' => $param['business_id']])->select();
  336. foreach ($dataList as $k => $v) {
  337. $where = [];
  338. $where['product_id'] = $v['product_id'];
  339. $productInfo = db('crm_product')->where($where)->field('name,category_id')->find();
  340. $category_name = db('crm_product_category')->where(['category_id' => $productInfo['category_id']])->value('name');
  341. $dataList[$k]['name'] = $productInfo['name'] ?: '';
  342. $dataList[$k]['category_id_info'] = $category_name ?: '';
  343. }
  344. $list['list'] = $dataList ?: [];
  345. $list['total_price'] = $businessInfo['total_price'] ?: '0.00';
  346. $list['discount_rate'] = $businessInfo['discount_rate'] ?: '0.00';
  347. return resultArray(['data' => $list]);
  348. }
  349. /**
  350. * 商机状态推进
  351. * @param business_id 商机ID
  352. * @param status_id 推进商机状态ID
  353. * @return
  354. * @author Michael_xu
  355. */
  356. public function advance()
  357. {
  358. $param = $this->param;
  359. $userInfo = $this->userInfo;
  360. $userModel = new \app\admin\model\User();
  361. $is_end = $param['is_end'] ?: 0; //1赢单2输单3无效
  362. if (!$param['business_id']) {
  363. return resultArray(['error' => '参数错误']);
  364. }
  365. $businessInfo = db('crm_business')->where(['business_id' => $param['business_id']])->find();
  366. if ($businessInfo['is_end']) {
  367. return resultArray(['error' => '已结束,不能推进']);
  368. }
  369. //判断权限
  370. $auth_user_ids = $userModel->getUserByPer('crm', 'business', 'update');
  371. //读写权限
  372. $rwPre = $userModel->rwPre($userInfo['id'], $businessInfo['ro_user_id'], $businessInfo['rw_user_id'], 'update');
  373. if (!in_array($businessInfo['owner_user_id'], $auth_user_ids) && !$rwPre) {
  374. header('Content-Type:application/json; charset=utf-8');
  375. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  376. }
  377. $status_id = $param['status_id'] ?: $businessInfo['status_id'];
  378. $statusInfo = db('crm_business_status')->where(['type_id' => $businessInfo['type_id'], 'status_id' => $status_id])->find();
  379. if (!$statusInfo && !$is_end) {
  380. return resultArray(['error' => '参数错误']);
  381. }
  382. $data = [];
  383. $data['update_time'] = time();
  384. $data['is_end'] = $is_end;
  385. // if ($is_end) {
  386. // $status_id = $is_end;
  387. // }
  388. $data['status_id'] = $status_id;
  389. $data['status_time'] = time();
  390. $res = db('crm_business')->where(['business_id' => $param['business_id']])->update($data);
  391. if (!$res) {
  392. return resultArray(['error' => '推进失败,请重试']);
  393. } else {
  394. # 商机变更后的名称
  395. $businessStatusName = Db::name('crm_business_status')->where('status_id', $param['status_id'])->value('name');
  396. if (empty($businessStatusName) && $is_end == 1) $businessStatusName = '赢单';
  397. if (empty($businessStatusName) && $is_end == 2) $businessStatusName = '输单';
  398. if (empty($businessStatusName) && $is_end == 3) $businessStatusName = '无效';
  399. # 添加活动记录
  400. Db::name('crm_activity')->insert([
  401. 'type' => 3,
  402. 'activity_type' => 5,
  403. 'activity_type_id' => $businessInfo['business_id'],
  404. 'content' => '阶段变更为 ' . $businessStatusName,
  405. 'create_user_id' => $businessInfo['owner_user_id'],
  406. 'update_time' => time(),
  407. 'create_time' => time(),
  408. 'customer_ids' => ',' . $businessInfo['customer_id'] . ','
  409. ]);
  410. //推进记录添加
  411. $temp['status_id'] = $status_id ?: 0;
  412. $temp['is_end'] = $is_end ?: 0;
  413. $temp['business_id'] = $param['business_id'];
  414. $temp['create_time'] = time();
  415. $temp['owner_user_id'] = $userInfo['id'];
  416. $temp['remark'] = $param['statusRemark'] ?: '';
  417. Db::name('CrmBusinessLog')->insert($temp);
  418. # 返回商机阶段数据
  419. $typeId = db('crm_business')->where('business_id', $param['business_id'])->value('type_id');
  420. $businessStatus = db('crm_business_status')->where('type_id', $typeId)->select();
  421. $result = [
  422. 'business_id' => $param['business_id'],
  423. 'type_id' => $typeId,
  424. 'status_id' => $param['status_id'],
  425. 'status_list' => $businessStatus
  426. ];
  427. return resultArray(['data' => $result]);
  428. }
  429. }
  430. /**
  431. * 商机导出
  432. * @param
  433. * @return
  434. * @author Michael_xu
  435. */
  436. public function excelExport()
  437. {
  438. $param = $this->param;
  439. $userInfo = $this->userInfo;
  440. $param['user_id'] = $userInfo['id'];
  441. $action_name = '导出全部';
  442. if ($param['business_id']) {
  443. $param['business_id'] = ['condition' => 'in', 'value' => $param['business_id'], 'form_type' => 'text', 'name' => ''];
  444. $param['is_excel'] = 1;
  445. $action_name = '导出选中';
  446. }
  447. $excelModel = new \app\admin\model\Excel();
  448. // 导出的字段列表
  449. $fieldModel = new \app\admin\model\Field();
  450. $field_list = $fieldModel->getIndexFieldConfig('crm_business', $userInfo['id']);
  451. // 文件名
  452. $file_name = '5kcrm_business_' . date('Ymd');
  453. $model = model('Business');
  454. $temp_file = $param['temp_file'];
  455. unset($param['temp_file']);
  456. $page = $param['page'] ?: 1;
  457. unset($param['page']);
  458. unset($param['export_queue_index']);
  459. RecordActionLog($userInfo['id'], 'crm_customer', 'excelexport', $action_name, '', '', '导出商机');
  460. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list) {
  461. $param['page'] = $page;
  462. $param['limit'] = $limit;
  463. $data = $model->getDataList($param);
  464. $data['list'] = $model->exportHandle($data['list'], $field_list, 'business');
  465. return $data;
  466. });
  467. }
  468. /**
  469. * 设置关注
  470. *
  471. * @return \think\response\Json
  472. * @throws \think\Exception
  473. * @throws \think\exception\PDOException
  474. */
  475. public function star()
  476. {
  477. $userId = $this->userInfo['id'];
  478. $targetId = $this->param['target_id'];
  479. $type = $this->param['type'];
  480. if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']);
  481. if (!$this->setStar($type, $userId, $targetId)) {
  482. return resultArray(['error' => '设置关注失败!']);
  483. }
  484. return resultArray(['data' => '设置关注成功!']);
  485. }
  486. /**
  487. * 系统信息
  488. *
  489. * @return \think\response\Json
  490. * @throws \think\db\exception\DataNotFoundException
  491. * @throws \think\db\exception\ModelNotFoundException
  492. * @throws \think\exception\DbException
  493. */
  494. public function system()
  495. {
  496. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  497. $businessModel = new \app\crm\model\Business();
  498. $data = $businessModel->getSystemInfo($this->param['id']);
  499. return resultArray(['data' => $data]);
  500. }
  501. /**
  502. * table栏数量统计
  503. *
  504. * @return \think\response\Json
  505. * @throws \think\db\exception\DataNotFoundException
  506. * @throws \think\db\exception\ModelNotFoundException
  507. * @throws \think\exception\DbException
  508. */
  509. public function count()
  510. {
  511. if (empty($this->param['business_id'])) return resultArray(['error' => '参数错误!']);
  512. $businessId = $this->param['business_id'];
  513. $userInfo = $this->userInfo;
  514. # 查询联系人和商机关联数据
  515. $contactsIds = Db::name('crm_contacts_business')->where('business_id', $businessId)->column('contacts_id');
  516. # 联系人
  517. $contactsAuth = $this->getContactsSearchWhere($userInfo['id']);
  518. $contactsCount = Db::name('crm_contacts')->whereIn('contacts_id', $contactsIds)->where($contactsAuth)->count();
  519. # 合同
  520. $contractAuth = $this->getContractSearchWhere($userInfo['id']);
  521. $contractCount = Db::name('crm_contract')->where('business_id', $businessId)->where($contractAuth)->count();
  522. # 查询商机和产品的关联表
  523. $productIds = Db::name('crm_business_product')->where('business_id', $businessId)->column('product_id');
  524. # 产品
  525. $productAuth = $this->getProductSearchWhere();
  526. $productCount = Db::name('crm_product')->whereIn('product_id', $productIds)->whereIn('owner_user_id', $productAuth)->count();
  527. # 附件
  528. $fileCount = Db::name('crm_business_file')->alias('business')->join('__ADMIN_FILE__ file', 'file.file_id = business.file_id', 'LEFT')->where('business_id', $businessId)->count();
  529. # 团队
  530. $business = Db::name('crm_business')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('business_id', $businessId)->find();
  531. $business['ro_user_id'] = explode(',', trim($business['ro_user_id'], ','));
  532. $business['rw_user_id'] = explode(',', trim($business['rw_user_id'], ','));
  533. $business['owner_user_id'] = [$business['owner_user_id']];
  534. $teamCount = array_filter(array_unique(array_merge($business['ro_user_id'], $business['rw_user_id'], $business['owner_user_id'])));
  535. $data = [
  536. 'contactCount' => $contactsCount,
  537. 'contractCount' => $contractCount,
  538. 'fileCount' => $fileCount,
  539. 'memberCount' => count($teamCount),
  540. 'productCount' => $productCount
  541. ];
  542. return resultArray(['data' => $data]);
  543. }
  544. /**
  545. * 设置首要联系人
  546. *
  547. * @return \think\response\Json
  548. * @throws \think\Exception
  549. * @throws \think\exception\PDOException
  550. */
  551. public function setPrimary()
  552. {
  553. $businessId = $this->param['business_id'];
  554. $contactsId = $this->param['contacts_id'];
  555. if (empty($businessId) || empty($contactsId)) return resultArray(['error' => '参数错误!']);
  556. if (!Db::name('crm_business')->where('business_id', $businessId)->update(['contacts_id' => $contactsId])) {
  557. return resultArray(['error' => '操作失败!']);
  558. }
  559. return resultArray(['data' => '操作成功!']);
  560. }
  561. }