Business.php 46KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 商机
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\model;
  8. use app\admin\traits\FieldVerificationTrait;
  9. use think\Db;
  10. use app\admin\model\Common;
  11. use app\crm\model\Business as CrmBusinessModel;
  12. class Business extends Common
  13. {
  14. use FieldVerificationTrait;
  15. /**
  16. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  17. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  18. */
  19. protected $name = 'crm_business';
  20. protected $createTime = 'create_time';
  21. protected $updateTime = 'update_time';
  22. protected $autoWriteTimestamp = true;
  23. /**
  24. * [getDataList 商机list]
  25. *
  26. * @param $request
  27. * @return array
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. * @throws \think\exception\DbException
  31. */
  32. public function getDataList($request)
  33. {
  34. $userModel = new \app\admin\model\User();
  35. $structureModel = new \app\admin\model\Structure();
  36. $fieldModel = new \app\admin\model\Field();
  37. $search = $request['search'];
  38. $user_id = $request['user_id'];
  39. $scene_id = (int)$request['scene_id'];
  40. $contacts_id = $request['contacts_id'];
  41. $order_field = $request['order_field'];
  42. $order_type = $request['order_type'];
  43. $is_excel = $request['is_excel']; //导出
  44. $getCount = $request['getCount'];
  45. $businessTypeId = $request['typesId']; // 针对mobile
  46. $businessStatusId = $request['statusId']; // 针对mobile
  47. $overdue = $request['overdue']; // 待办事项下需联系商机(逾期)
  48. unset($request['scene_id']);
  49. unset($request['search']);
  50. unset($request['user_id']);
  51. unset($request['contacts_id']);
  52. unset($request['order_field']);
  53. unset($request['order_type']);
  54. unset($request['is_excel']);
  55. unset($request['getCount']);
  56. unset($request['typesId']);
  57. unset($request['statusId']);
  58. unset($request['overdue']);
  59. $request = $this->fmtRequest($request);
  60. $requestMap = $request['map'] ?: [];
  61. $sceneModel = new \app\admin\model\Scene();
  62. # getCount是代办事项传来的参数,代办事项不需要使用场景
  63. $sceneMap = [];
  64. if (empty($getCount)) {
  65. if ($scene_id) {
  66. //自定义场景
  67. $sceneMap = $sceneModel->getDataById($scene_id, $user_id, 'business') ?: [];
  68. } else {
  69. //默认场景
  70. $sceneMap = $sceneModel->getDefaultData('crm_business', $user_id) ?: [];
  71. }
  72. }
  73. if ($search || $search == '0') {
  74. //普通筛选
  75. $sceneMap['name'] = ['condition' => 'contains', 'value' => $search, 'form_type' => 'text', 'name' => '商机名称'];
  76. }
  77. if (isset($requestMap['type_id'])) {
  78. $requestMap['type_id']['value'] = $requestMap['type_id']['type_id'];
  79. if(in_array($requestMap['type_id']['status_id'],[1,2,3])){
  80. $requestMap['is_end']=$requestMap['type_id']['status_id'];
  81. }else{
  82. if ($requestMap['type_id']['status_id']) $requestMap['status_id']['value'] = $requestMap['type_id']['status_id'];
  83. $requestMap['is_end']=0;
  84. }
  85. }
  86. if ($sceneMap['type_id']) {
  87. $requestMap['type_id']['value'] = $sceneMap['type_id']['type_id'];
  88. if(in_array($sceneMap['type_id']['status_id'],[1,2,3])){
  89. $sceneMap['is_end']=$sceneMap['type_id']['status_id'];
  90. }else{
  91. if ($sceneMap['type_id']['status_id']) $requestMap['status_id']['value'] = $sceneMap['type_id']['status_id'];
  92. $sceneMap['is_end']=0;
  93. }
  94. unset($sceneMap['type_id']);
  95. }
  96. $partMap = [];
  97. //优先级:普通筛选>高级筛选>场景
  98. if ($requestMap['team_id']) {
  99. //相关团队查询
  100. $map = $requestMap;
  101. $partMap= advancedQueryFormatForTeam($requestMap,'crm_business','business_id');
  102. unset($map['team_id']);
  103. } else {
  104. $map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
  105. }
  106. //高级筛选
  107. $map = advancedQuery($map, 'crm', 'business', 'index');
  108. $authMap = [];
  109. if (!$partMap) {
  110. $a = 'index';
  111. if ($is_excel) $a = 'excelExport';
  112. $auth_user_ids = $userModel->getUserByPer('crm', 'business', $a);
  113. if (isset($map['business.owner_user_id']) && $map['business.owner_user_id'][0] != 'like') {
  114. if (!is_array($map['business.owner_user_id'][1])) {
  115. $map['business.owner_user_id'][1] = [$map['business.owner_user_id'][1]];
  116. }
  117. if (in_array($map['business.owner_user_id'][0], ['neq', 'notin'])) {
  118. $auth_user_ids = array_diff($auth_user_ids, $map['business.owner_user_id'][1]) ?: []; //取差集
  119. } else {
  120. $auth_user_ids = array_intersect($map['business.owner_user_id'][1], $auth_user_ids) ?: []; //取交集
  121. }
  122. unset($map['business.owner_user_id']);
  123. $auth_user_ids = array_merge(array_unique(array_filter($auth_user_ids))) ?: ['-1'];
  124. $authMap['business.owner_user_id'] = array('in', $auth_user_ids);
  125. } else {
  126. $authMapData = [];
  127. $authMapData['auth_user_ids'] = $auth_user_ids;
  128. $authMapData['user_id'] = $user_id;
  129. $authMap = function ($query) use ($authMapData) {
  130. $query->where('business.owner_user_id', array('in', $authMapData['auth_user_ids']))
  131. ->whereOr('business.ro_user_id', array('like', '%,' . $authMapData['user_id'] . ',%'))
  132. ->whereOr('business.rw_user_id', array('like', '%,' . $authMapData['user_id'] . ',%'));
  133. };
  134. }
  135. }
  136. //联系人商机
  137. if ($contacts_id) {
  138. $business_id = Db::name('crm_contacts_business')->where(['contacts_id' => $contacts_id])->column('business_id');
  139. if ($business_id) {
  140. $map['business.business_id'] = array('in', $business_id);
  141. } else {
  142. $map['business.business_id'] = array('eq', -1);
  143. }
  144. }
  145. //列表展示字段
  146. $indexField = $fieldModel->getIndexField('crm_business', $user_id, 1) ?: array('name');
  147. if (!empty($indexField)) {
  148. foreach ($indexField as $key => $value) {
  149. if ($value == 'business.customer_name') unset($indexField[(int)$key]);
  150. }
  151. }
  152. $userField = $fieldModel->getFieldByFormType('crm_business', 'user'); //人员类型
  153. $structureField = $fieldModel->getFieldByFormType('crm_business', 'structure'); //部门类型
  154. $datetimeField = $fieldModel->getFieldByFormType('crm_business', 'datetime'); //日期时间类型
  155. $booleanField = $fieldModel->getFieldByFormType('crm_business', 'boolean_value'); //布尔值
  156. $dateIntervalField = $fieldModel->getFieldByFormType('crm_business', 'date_interval'); // 日期区间类型字段
  157. $positionField = $fieldModel->getFieldByFormType('crm_business', 'position'); // 地址类型字段
  158. $handwritingField = $fieldModel->getFieldByFormType('crm_business', 'handwriting_sign'); // 手写签名类型字段
  159. $locationField = $fieldModel->getFieldByFormType('crm_business', 'location'); // 定位类型字段
  160. $boxField = $fieldModel->getFieldByFormType('crm_business', 'checkbox'); // 多选类型字段
  161. $floatField = $fieldModel->getFieldByFormType('crm_business', 'floatnumber'); // 货币类型字段
  162. // $fieldGrant = db('admin_field_mask')->where('types', 'business')->select();
  163. # 处理人员和部门类型的排序报错问题(前端传来的是包含_name的别名字段)
  164. $temporaryField = str_replace('_name', '', $order_field);
  165. if (in_array($temporaryField, $userField) || in_array($temporaryField, $structureField)) {
  166. $order_field = $temporaryField;
  167. }
  168. //排序
  169. if ($order_type && $order_field) {
  170. $order = $fieldModel->getOrderByFormtype('crm_business', 'business', $order_field, $order_type);
  171. } else {
  172. $order = 'business.update_time desc';
  173. }
  174. # 商机组和商机状态搜索
  175. if (!empty($businessTypeId)) $map['business.type_id'] = ['eq', $businessTypeId];
  176. if (!empty($businessStatusId)) {
  177. if(preg_match("/^[1-9][0-9]*$/" ,$businessStatusId)){
  178. $map['is_end']=0;
  179. $map['business.status_id'] = ['eq', $businessStatusId];
  180. }else{
  181. $map['is_end']=abs($businessStatusId);
  182. }
  183. }
  184. // 待办事项下需联系商机(逾期)
  185. $overdueWhere = '';
  186. if (!empty($overdue)) {
  187. $overdueWhere = "(FROM_UNIXTIME(`business`.`last_time`,'%Y-%m-%d') < FROM_UNIXTIME(`business`.`next_time`,'%Y-%m-%d') OR (ISNULL(`business`.`last_time`) AND `business`.`next_time` < ".time()."))";
  188. }
  189. $readAuthIds = $userModel->getUserByPer('crm', 'business', 'read');
  190. $updateAuthIds = $userModel->getUserByPer('crm', 'business', 'update');
  191. $deleteAuthIds = $userModel->getUserByPer('crm', 'business', 'delete');
  192. $dataCount = db('crm_business')
  193. ->alias('business')
  194. ->join('__CRM_CUSTOMER__ customer', 'business.customer_id = customer.customer_id', 'LEFT')
  195. ->where($map)->where($partMap)->where($authMap)->where($overdueWhere)->count('business_id');
  196. if (!empty($getCount) && $getCount == 1) {
  197. $data['dataCount'] = !empty($dataCount) ? $dataCount : 0;
  198. # 商机总金额
  199. $sumMoney = Db::name('crm_business')->alias('business')
  200. ->whereIn('is_end', [0, 1])->where($map)->where($partMap)->where($authMap)->sum('money');
  201. $data['extraData']['money'] = ['businessSumMoney' => !empty($sumMoney) ? sprintf("%.2f", $sumMoney) : 0.00];
  202. return $data;
  203. }
  204. $list = db('crm_business')
  205. ->alias('business')
  206. ->join('__CRM_CUSTOMER__ customer', 'business.customer_id = customer.customer_id', 'LEFT')
  207. ->where($map)
  208. ->where($partMap)
  209. ->where($authMap)
  210. ->where($overdueWhere)
  211. ->limit($request['offset'], $request['length'])
  212. ->field('business.*,customer.name as customer_name')
  213. ->orderRaw($order)
  214. ->select();
  215. $endStatus = ['1' => '赢单', '2' => '输单', '3' => '无效'];
  216. # 扩展数据
  217. $extraData = [];
  218. $business_id_list = !empty($list) ? array_column($list, 'business_id') : [];
  219. $extraList = db('crm_business_data')->whereIn('business_id', $business_id_list)->select();
  220. foreach ($extraList AS $key => $value) {
  221. $extraData[$value['business_id']][$value['field']] = $value['content'];
  222. }
  223. $grantData = getFieldGrantData($user_id);
  224. foreach ($grantData['crm_business'] as $key => $value) {
  225. foreach ($value as $ke => $va) {
  226. if($va['maskType']!=0){
  227. $fieldGrant[$ke]['maskType'] = $va['maskType'];
  228. $fieldGrant[$ke]['form_type'] = $va['form_type'];
  229. $fieldGrant[$ke]['field'] = $va['field'];
  230. }
  231. }
  232. }
  233. foreach ($list as $k => $v) {
  234. $list[$k]['customer_id_info']['customer_id'] = $v['customer_id'];
  235. $list[$k]['customer_id_info']['name'] = $v['customer_name'];
  236. $list[$k]['create_user_id_info'] = isset($v['create_user_id']) ? $userModel->getUserById($v['create_user_id']) : [];
  237. $list[$k]['owner_user_id_info'] = isset($v['owner_user_id']) ? $userModel->getUserById($v['owner_user_id']) : [];
  238. $list[$k]['create_user_name'] = !empty($list[$k]['create_user_id_info']['realname']) ? $list[$k]['create_user_id_info']['realname'] : '';
  239. $list[$k]['owner_user_name'] = !empty($list[$k]['owner_user_id_info']['realname']) ? $list[$k]['owner_user_id_info']['realname'] : '';
  240. foreach ($userField as $key => $val) {
  241. $usernameField = !empty($v[$val]) ? db('admin_user')->whereIn('id', stringToArray($v[$val]))->column('realname') : [];
  242. $list[$k][$val] = implode($usernameField, ',');
  243. }
  244. foreach ($structureField as $key => $val) {
  245. $structureNameField = !empty($v[$val]) ? db('admin_structure')->whereIn('id', stringToArray($v[$val]))->column('name') : [];
  246. $list[$k][$val] = implode($structureNameField, ',');
  247. }
  248. foreach ($datetimeField as $key => $val) {
  249. $list[$k][$val] = !empty($v[$val]) ? date('Y-m-d H:i:s', $v[$val]) : null;
  250. }
  251. foreach ($booleanField as $key => $val) {
  252. $list[$k][$val] = !empty($v[$val]) ? (string)$v[$val] : '0';
  253. }
  254. // 处理日期区间类型字段的格式
  255. foreach ($dateIntervalField AS $key => $val) {
  256. $list[$k][$val] = !empty($extraData[$v['business_id']][$val]) ? json_decode($extraData[$v['business_id']][$val], true) : null;
  257. }
  258. // 处理地址类型字段的格式
  259. foreach ($positionField AS $key => $val) {
  260. $list[$k][$val] = !empty($extraData[$v['business_id']][$val]) ? json_decode($extraData[$v['business_id']][$val], true) : null;
  261. }
  262. // 手写签名类型字段
  263. foreach ($handwritingField AS $key => $val) {
  264. $handwritingData = !empty($v[$val]) ? db('admin_file')->where('file_id', $v[$val])->value('file_path') : null;
  265. $list[$k][$val] = ['url' => !empty($handwritingData) ? getFullPath($handwritingData) : null];
  266. }
  267. // 定位类型字段
  268. foreach ($locationField AS $key => $val) {
  269. $list[$k][$val] = !empty($extraData[$v['business_id']][$val]) ? json_decode($extraData[$v['business_id']][$val], true) : null;
  270. }
  271. // 多选框类型字段
  272. foreach ($boxField AS $key => $val) {
  273. $list[$k][$val] = !empty($v[$val]) ? trim($v[$val], ',') : null;
  274. }
  275. // 货币类型字段
  276. foreach ($floatField AS $key => $val) {
  277. $list[$k][$val] = $v[$val]!='0.00' ? (string)$v[$val] : null;
  278. }
  279. //掩码相关类型字段
  280. foreach ($fieldGrant AS $key => $val){
  281. //掩码相关类型字段
  282. if ($val['maskType']!=0 && $val['form_type'] == 'mobile') {
  283. $pattern = "/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i";
  284. $rs = preg_replace($pattern, "$1****$2", $v[$val['field']]);
  285. $list[$k][$val['field']] = !empty($v[$val['field']]) ? (string)$rs : null;
  286. } elseif ($val['maskType']!=0 && $val['form_type'] == 'email') {
  287. $email_array = explode("@", $v[$val['field']]);
  288. $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($v[$val['field']], 0, 2); //邮箱前缀
  289. $str = preg_replace('/([\d\w+_-]{0,100})@/', "***@", $v[$val['field']], -1, $count);
  290. $rs = $prevfix . $str;
  291. $list[$k][$val['field']] = !empty($v[$val['field']]) ?$rs: null;
  292. } elseif ($val['maskType']!=0 && in_array($val['form_type'],['position','floatnumber'])) {
  293. $list[$k][$val['field']] = !empty($v[$val['field']]) ? (string)substr_replace($v[$val['field']], '*****',0,strlen($v[$val['field']])) : null;
  294. }
  295. }
  296. $statusInfo = [];
  297. $status_count = 0;
  298. if (!$v['is_end']) {
  299. $statusInfo = db('crm_business_status')->where('status_id', $v['status_id'])->find();
  300. if ($statusInfo['order_id'] < 99) {
  301. $status_count = db('crm_business_status')->where('type_id', ['eq', $v['type_id']])->count();
  302. }
  303. //进度
  304. $list[$k]['status_progress'] = [$statusInfo['order_id'], $status_count + 1];
  305. } else {
  306. $statusInfo['name'] = $endStatus[$v['is_end']];
  307. }
  308. $list[$k]['status_id_info'] = $statusInfo['name'];//销售阶段
  309. $list[$k]['type_id_info'] = db('crm_business_type')->where('type_id', $v['type_id'])->value('name');//商机状态组
  310. //权限
  311. $roPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'read');
  312. $rwPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'update');
  313. $permission = [];
  314. $is_read = 0;
  315. $is_update = 0;
  316. $is_delete = 0;
  317. if (in_array($v['owner_user_id'], $readAuthIds) || $roPre || $rwPre) $is_read = 1;
  318. if (in_array($v['owner_user_id'], $updateAuthIds) || $rwPre) $is_update = 1;
  319. if (in_array($v['owner_user_id'], $deleteAuthIds)) $is_delete = 1;
  320. $permission['is_read'] = $is_read;
  321. $permission['is_update'] = $is_update;
  322. $permission['is_delete'] = $is_delete;
  323. $list[$k]['permission'] = $permission;
  324. # 下次联系时间
  325. $list[$k]['next_time'] = !empty($v['next_time']) ? date('Y-m-d H:i:s', $v['next_time']) : null;
  326. # 关注
  327. $starWhere = ['user_id' => $user_id, 'target_id' => $v['business_id'], 'type' => 'crm_business'];
  328. $star = Db::name('crm_star')->where($starWhere)->value('star_id');
  329. $list[$k]['star'] = !empty($star) ? 1 : 0;
  330. # 日期
  331. $list[$k]['create_time'] = !empty($v['create_time']) ? date('Y-m-d H:i:s', $v['create_time']) : null;
  332. $list[$k]['update_time'] = !empty($v['update_time']) ? date('Y-m-d H:i:s', $v['update_time']) : null;
  333. $list[$k]['last_time'] = !empty($v['last_time']) ? date('Y-m-d H:i:s', $v['last_time']) : null;
  334. # 系统字段 负责人部门 zjf 20210726
  335. $list[$k]['owner_user_structure_name'] = $list[$k]['owner_user_id_info']['structure_name'];
  336. }
  337. $data = [];
  338. $data['list'] = $list ?: [];
  339. $data['dataCount'] = $dataCount ?: 0;
  340. # 商机总金额
  341. $sumMoney = Db::name('crm_business')->alias('business')
  342. ->join('__CRM_CUSTOMER__ customer', 'business.customer_id = customer.customer_id', 'LEFT')
  343. ->whereIn('is_end', [0, 1])->where($map)->where($partMap)->where($authMap)->sum('money');
  344. $data['extraData']['money'] = ['businessSumMoney' => !empty($sumMoney) ? sprintf("%.2f", $sumMoney) : 0.00];
  345. return $data;
  346. }
  347. /**
  348. * 创建商机主表信息
  349. * @param
  350. * @return
  351. * @author Michael_xu
  352. */
  353. public function createData($param)
  354. {
  355. // 商机扩展表数据
  356. $businessData = [];
  357. $fieldModel = new \app\admin\model\Field();
  358. $productModel = new \app\crm\model\Product();
  359. // 数据验证
  360. $validateResult = $this->fieldDataValidate($param, $this->name, $param['create_user_id']);
  361. if (!empty($validateResult)) {
  362. $this->error = $validateResult;
  363. return false;
  364. }
  365. if (!$param['customer_id']) {
  366. $this->error = '请选择相关客户';
  367. return false;
  368. }
  369. // 处理部门、员工、附件、多选类型字段
  370. $arrFieldAtt = $fieldModel->getArrayField('crm_business');
  371. foreach ($arrFieldAtt as $k => $v) {
  372. $param[$v] = arrayToString($param[$v]);
  373. }
  374. // 处理日期(date)类型
  375. $dateField = $fieldModel->getFieldByFormType('crm_business', 'date');
  376. if (!empty($dateField)) {
  377. foreach ($param AS $key => $value) {
  378. if (in_array($key, $dateField) && empty($value)) $param[$key] = null;
  379. }
  380. }
  381. // 处理手写签名类型
  382. $handwritingField = $fieldModel->getFieldByFormType('crm_business', 'handwriting_sign');
  383. if (!empty($handwritingField)) {
  384. foreach ($param AS $key => $value) {
  385. if (in_array($key, $handwritingField)) {
  386. $param[$key] = !empty($value['file_id']) ? $value['file_id'] : '';
  387. }
  388. }
  389. }
  390. // 处理地址、定位、日期区间、明细表格类型字段
  391. $positionField = $fieldModel->getFieldByFormType($this->name, 'position');
  392. $locationField = $fieldModel->getFieldByFormType($this->name, 'location');
  393. $dateIntervalField = $fieldModel->getFieldByFormType($this->name, 'date_interval');
  394. $detailTableField = $fieldModel->getFieldByFormType($this->name, 'detail_table');
  395. foreach ($param AS $key => $value) {
  396. // 处理地址类型字段数据
  397. if (in_array($key, $positionField)) {
  398. if (!empty($value)) {
  399. $businessData[] = [
  400. 'field' => $key,
  401. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  402. 'create_time' => time()
  403. ];
  404. $positionNames = array_column($value, 'name');
  405. $param[$key] = implode(',', $positionNames);
  406. } else {
  407. $param[$key] = '';
  408. }
  409. }
  410. // 处理定位类型字段数据
  411. if (in_array($key, $locationField)) {
  412. if (!empty($value)) {
  413. $businessData[] = [
  414. 'field' => $key,
  415. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  416. 'create_time' => time()
  417. ];
  418. $param[$key] = $value['address'];
  419. } else {
  420. $param[$key] = '';
  421. }
  422. }
  423. // 处理日期区间类型字段数据
  424. if (in_array($key, $dateIntervalField)) {
  425. if (!empty($value)) {
  426. $businessData[] = [
  427. 'field' => $key,
  428. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  429. 'create_time' => time()
  430. ];
  431. $param[$key] = implode('_', $value);
  432. } else {
  433. $param[$key] = '';
  434. }
  435. }
  436. // 处理明细表格类型字段数据
  437. if (in_array($key, $detailTableField)) {
  438. if (!empty($value)) {
  439. $businessData[] = [
  440. 'field' => $key,
  441. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  442. 'create_time' => time()
  443. ];
  444. $param[$key] = $key;
  445. } else {
  446. $param[$key] = '';
  447. }
  448. }
  449. }
  450. # 设置今日需联系商机
  451. if (!empty($param['next_time']) && $param['next_time'] >= strtotime(date('Y-m-d 00:00:00'))) $param['is_dealt'] = 0;
  452. $param['money'] = $param['money'] ?: '0.00';
  453. $param['discount_rate'] = $param['discount_rate'] ?: '0.00';
  454. if ($this->data($param)->allowField(true)->save()) {
  455. updateActionLog($param['create_user_id'], 'crm_business', $this->business_id, '', '', '创建了商机');
  456. RecordActionLog($param['create_user_id'],'crm_business','save',$param['name'],'','','新增了商机'.$param['name']);
  457. $business_id = $this->business_id;
  458. $data['business_id'] = $business_id;
  459. if ($param['product']) {
  460. //产品数据处理
  461. $resProduct = $productModel->createObject('crm_business', $param, $business_id);
  462. if ($resProduct == false) {
  463. $this->error = '产品添加失败';
  464. return false;
  465. }
  466. }
  467. //添加商机日志
  468. $data_log['business_id'] = $business_id;
  469. $data_log['is_end'] = 0;
  470. $data_log['status_id'] = $param['status_id'];
  471. $data_log['create_time'] = time();
  472. $data_log['owner_user_id'] = $param['owner_user_id'];
  473. $data_log['remark'] = '新建商机';
  474. Db::name('CrmBusinessLog')->insert($data_log);
  475. # 添加活动记录
  476. Db::name('crm_activity')->insert([
  477. 'type' => 2,
  478. 'activity_type' => 5,
  479. 'activity_type_id' => $data['business_id'],
  480. 'content' => $param['name'],
  481. 'create_user_id' => $param['create_user_id'],
  482. 'update_time' => time(),
  483. 'create_time' => time(),
  484. 'customer_ids' => ',' . $param['customer_id'] . ','
  485. ]);
  486. // 添加商机扩展数据
  487. array_walk($businessData, function (&$val) use ($data) {
  488. $val['business_id'] = $data['business_id'];
  489. });
  490. db('crm_business_data')->insertAll($businessData);
  491. return $data;
  492. } else {
  493. $this->error = '添加失败';
  494. return false;
  495. }
  496. }
  497. /**
  498. * 编辑商机主表信息
  499. * @param
  500. * @return
  501. * @author Michael_xu
  502. */
  503. public function updateDataById($param, $business_id = '')
  504. {
  505. // 商机扩展表数据
  506. $businessData = [];
  507. $productModel = new \app\crm\model\Product();
  508. $dataInfo = $this->getDataById($business_id);
  509. if (!$dataInfo) {
  510. $this->error = '数据不存在或已删除';
  511. return false;
  512. }
  513. $param['business_id'] = $business_id;
  514. //过滤不能修改的字段
  515. $unUpdateField = ['create_user_id', 'is_deleted', 'delete_time'];
  516. foreach ($unUpdateField as $v) {
  517. unset($param[$v]);
  518. }
  519. $fieldModel = new \app\admin\model\Field();
  520. // 数据验证
  521. $validateResult = $this->fieldDataValidate($param, $this->name, $param['user_id'], $param['business_id']);
  522. if (!empty($validateResult)) {
  523. $this->error = $validateResult;
  524. return false;
  525. }
  526. # 商机金额小数处理
  527. if (!empty($param['money']) && is_numeric($param['money']) && strpos($param['money'], ".") === false) {
  528. $param['money'] .= '.00';
  529. }
  530. // 处理部门、员工、附件、多选类型字段
  531. $arrFieldAtt = $fieldModel->getArrayField('crm_business');
  532. foreach ($arrFieldAtt as $k => $v) {
  533. if (isset($param[$v])) $param[$v] = arrayToString($param[$v]);
  534. }
  535. // 处理日期(date)类型
  536. $dateField = $fieldModel->getFieldByFormType('crm_business', 'date');
  537. if (!empty($dateField)) {
  538. foreach ($param AS $key => $value) {
  539. if (in_array($key, $dateField) && empty($value)) $param[$key] = null;
  540. }
  541. }
  542. // 处理手写签名类型
  543. $handwritingField = $fieldModel->getFieldByFormType('crm_business', 'handwriting_sign');
  544. if (!empty($handwritingField)) {
  545. foreach ($param AS $key => $value) {
  546. if (in_array($key, $handwritingField)) {
  547. $param[$key] = !empty($value['file_id']) ? $value['file_id'] : '';
  548. }
  549. }
  550. }
  551. // 处理地址、定位、日期区间、明细表格类型字段
  552. $positionField = $fieldModel->getFieldByFormType($this->name, 'position');
  553. $locationField = $fieldModel->getFieldByFormType($this->name, 'location');
  554. $dateIntervalField = $fieldModel->getFieldByFormType($this->name, 'date_interval');
  555. $detailTableField = $fieldModel->getFieldByFormType($this->name, 'detail_table');
  556. foreach ($param AS $key => $value) {
  557. // 处理地址类型字段数据
  558. if (in_array($key, $positionField)) {
  559. if (!empty($value)) {
  560. $businessData[] = [
  561. 'field' => $key,
  562. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  563. 'create_time' => time()
  564. ];
  565. $positionNames = array_column($value, 'name');
  566. $param[$key] = implode(',', $positionNames);
  567. } else {
  568. $param[$key] = '';
  569. }
  570. }
  571. // 处理定位类型字段数据
  572. if (in_array($key, $locationField)) {
  573. if (!empty($value)) {
  574. $businessData[] = [
  575. 'field' => $key,
  576. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  577. 'create_time' => time()
  578. ];
  579. $param[$key] = $value['address'];
  580. } else {
  581. $param[$key] = '';
  582. }
  583. }
  584. // 处理日期区间类型字段数据
  585. if (in_array($key, $dateIntervalField)) {
  586. if (!empty($value)) {
  587. $businessData[] = [
  588. 'field' => $key,
  589. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  590. 'create_time' => time()
  591. ];
  592. $param[$key] = implode('_', $value);
  593. } else {
  594. $param[$key] = '';
  595. }
  596. }
  597. // 处理明细表格类型字段数据
  598. if (in_array($key, $detailTableField)) {
  599. if (!empty($value)) {
  600. $businessData[] = [
  601. 'field' => $key,
  602. 'content' => json_encode($value, JSON_NUMERIC_CHECK),
  603. 'create_time' => time()
  604. ];
  605. $param[$key] = $key;
  606. } else {
  607. $param[$key] = '';
  608. }
  609. }
  610. }
  611. # 设置今日需联系商机
  612. if (!empty($param['next_time']) && $param['next_time'] >= strtotime(date('Y-m-d 00:00:00'))) $param['is_dealt'] = 0;
  613. $param['money'] = $param['money'] ?: '0.00';
  614. $param['discount_rate'] = $param['discount_rate'] ?: '0.00';
  615. //商机状态改变
  616. $statusInfo = db('crm_business_status')->where(['status_id' => $param['status_id']])->find();
  617. if ($statusInfo['type_id']) {
  618. $param['is_end'] = 0;
  619. } else {
  620. $param['is_end'] = $param['status_id'];
  621. }
  622. if ($this->update($param, ['business_id' => $business_id], true)) {
  623. $data['business_id'] = $business_id;
  624. //产品数据处理
  625. $resProduct = $productModel->createObject('crm_business', $param, $business_id);
  626. //修改记录
  627. updateActionLog($param['user_id'], 'crm_business', $business_id, $dataInfo, $param);
  628. RecordActionLog($param['user_id'], 'crm_business', 'update',$dataInfo['name'], $dataInfo, $param);
  629. // 添加商机扩展数据
  630. db('crm_business_data')->where('business_id', $business_id)->delete();
  631. array_walk($businessData, function (&$val) use ($business_id) {
  632. $val['business_id'] = $business_id;
  633. });
  634. db('crm_business_data')->insertAll($businessData);
  635. return $data;
  636. } else {
  637. $this->rollback();
  638. $this->error = '编辑失败';
  639. return false;
  640. }
  641. }
  642. /**
  643. * 商机数据
  644. *
  645. * @param string $id
  646. * @param int $userId
  647. * @return Common|array|bool|\PDOStatement|string|\think\Model|null
  648. * @throws \think\db\exception\DataNotFoundException
  649. * @throws \think\db\exception\ModelNotFoundException
  650. * @throws \think\exception\DbException
  651. */
  652. public function getDataById($id = '', $userId = 0,$model='')
  653. {
  654. $dataInfo = db('crm_business')->where('business_id', $id)->find();
  655. if (!$dataInfo) {
  656. $this->error = '暂无此数据';
  657. return false;
  658. }
  659. if(empty($model) && $model!='update'){
  660. $grantData = getFieldGrantData($userId);
  661. foreach ($grantData['crm_business'] as $key => $value) {
  662. foreach ($value as $ke => $va) {
  663. if($va['maskType']!=0){
  664. $fieldGrant[$ke]['maskType'] = $va['maskType'];
  665. $fieldGrant[$ke]['form_type'] = $va['form_type'];
  666. $fieldGrant[$ke]['field'] = $va['field'];
  667. }
  668. }
  669. }
  670. foreach ($fieldGrant AS $key => $val){
  671. //掩码相关类型字段
  672. if ($val['maskType']!=0 && $val['form_type'] == 'mobile') {
  673. $pattern = "/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i";
  674. $rs = preg_replace($pattern, "$1****$2", $dataInfo[$val['field']]);
  675. $dataInfo[$val['field']] = !empty($dataInfo[$val['field']]) ? (string)$rs : null;
  676. } elseif ($val['maskType']!=0 && $val['form_type'] == 'email') {
  677. $email_array = explode("@", $dataInfo[$val['field']]);
  678. $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($dataInfo[$val['field']], 0, 2); //邮箱前缀
  679. $str = preg_replace('/([\d\w+_-]{0,100})@/', "***@", $dataInfo[$val['field']], -1, $count);
  680. $rs = $prevfix . $str;
  681. $dataInfo[$val['field']] = !empty($dataInfo[$val['field']]) ?$rs: null;
  682. } elseif ($val['maskType']!=0 && in_array($val['form_type'],['position','floatnumber'])) {
  683. $dataInfo[$val['field']] = !empty($dataInfo[$val['field']]) ? (string)substr_replace($dataInfo[$val['field']], '*****',0,strlen($dataInfo[$val['field']])) : null;
  684. }
  685. }
  686. }
  687. $userModel = new \app\admin\model\User();
  688. $dataInfo['create_user_id_info'] = isset($dataInfo['create_user_id']) ? $userModel->getUserById($dataInfo['create_user_id']) : [];
  689. $dataInfo['owner_user_id_info'] = isset($dataInfo['owner_user_id']) ? $userModel->getUserById($dataInfo['owner_user_id']) : [];
  690. $dataInfo['create_user_name'] = !empty($dataInfo['create_user_id_info']['realname']) ? $dataInfo['create_user_id_info']['realname'] : '';
  691. $dataInfo['owner_user_name'] = !empty($dataInfo['owner_user_id_info']['realname']) ? $dataInfo['owner_user_id_info']['realname'] : '';
  692. $dataInfo['type_id_info'] = db('crm_business_type')->where(['type_id' => $dataInfo['type_id']])->value('name');
  693. $dataInfo['status_id_info'] = db('crm_business_status')->where(['status_id' => $dataInfo['status_id']])->value('name');
  694. $dataInfo['customer_id_info'] = db('crm_customer')->where(['customer_id' => $dataInfo['customer_id']])->field('customer_id,name')->find();
  695. $dataInfo['customer_name'] = !empty($dataInfo['customer_id_info']['name']) ? $dataInfo['customer_id_info']['name'] : '';
  696. # 关注
  697. $starId = empty($userId) ? 0 : Db::name('crm_star')->where(['user_id' => $userId, 'target_id' => $id, 'type' => 'crm_business'])->value('star_id');
  698. $dataInfo['star'] = !empty($starId) ? 1 : 0;
  699. # 首要联系人
  700. $primaryId = Db::name('crm_contacts')->where(['contacts_id' => $dataInfo['contacts_id']])->value('contacts_id');
  701. $dataInfo['contacts_id'] = !empty($primaryId) && $this->getContactsAuth($primaryId) ? $primaryId : 0;
  702. # 处理日期格式
  703. $fieldModel = new \app\admin\model\Field();
  704. $datetimeField = $fieldModel->getFieldByFormType('crm_business', 'datetime'); //日期时间类型
  705. foreach ($datetimeField as $key => $val) {
  706. $dataInfo[$val] = !empty($dataInfo[$val]) ? date('Y-m-d H:i:s', $dataInfo[$val]) : null;
  707. }
  708. if($dataInfo['is_end']!=1){
  709. $dataInfo['statusRemark']=db('crm_business_log')->where(['business_id'=>$id,'is_end'=>$dataInfo['is_end']])->value('remark');
  710. }
  711. $dataInfo['next_time'] = !empty($dataInfo['next_time']) ? date('Y-m-d H:i:s', $dataInfo['next_time']) : null;
  712. $dataInfo['create_time'] = !empty($dataInfo['create_time']) ? date('Y-m-d H:i:s', $dataInfo['create_time']) : null;
  713. $dataInfo['update_time'] = !empty($dataInfo['update_time']) ? date('Y-m-d H:i:s', $dataInfo['update_time']) : null;
  714. $dataInfo['last_time'] = !empty($dataInfo['last_time']) ? date('Y-m-d H:i:s', $dataInfo['last_time']) : null;
  715. // 字段授权
  716. if (!empty($userId)) {
  717. $grantData = getFieldGrantData($userId);
  718. $userLevel = isSuperAdministrators($userId);
  719. foreach ($dataInfo AS $key => $value) {
  720. if (!$userLevel && !empty($grantData['crm_business'])) {
  721. $status = getFieldGrantStatus($key, $grantData['crm_business']);
  722. # 查看权限
  723. if ($status['read'] == 0) unset($dataInfo[$key]);
  724. }
  725. }
  726. }
  727. return $dataInfo;
  728. }
  729. //根据IDs获取数组
  730. public function getDataByStr($idstr)
  731. {
  732. $idArr = stringToArray($idstr);
  733. if (!$idArr) {
  734. return [];
  735. }
  736. $list = Db::name('CrmBusiness')->where(['business_id' => ['in', $idArr]])->select();
  737. return $list;
  738. }
  739. /**
  740. * [商机漏斗]
  741. * @param
  742. * @return
  743. * @author Michael_xu
  744. */
  745. public function getFunnel($request)
  746. {
  747. $merge = $request['merge'] ?: 0;
  748. $perUserIds = $request['perUserIds'] ?: [];
  749. $adminModel = new \app\admin\model\Admin();
  750. $whereArr = $adminModel->getWhere($request, $merge, $perUserIds); //统计查询
  751. $userIds = $whereArr['userIds'];
  752. $between_time = $whereArr['between_time'];
  753. $where['owner_user_id'] = array('in', $userIds);
  754. $where['create_time'] = array('between', $between_time);
  755. //商机状态组
  756. $default_type_id = db('crm_business_type')->order('type_id asc')->value('type_id');
  757. $type_id = $request['type_id'] ? $request['type_id'] : $default_type_id;
  758. $statusList = db('crm_business_status')->where(['type_id' => $type_id])->select();
  759. $map = [];
  760. $map['create_time'] = $where['create_time'];
  761. $map['owner_user_id'] = ['in', $userIds];
  762. $map['type_id'] = $type_id;
  763. $sql_a = CrmBusinessModel::field([
  764. 'SUM(CASE WHEN is_end = 1 THEN money ELSE 0 END) AS sum_ying',
  765. 'SUM(CASE WHEN is_end = 2 THEN money ELSE 0 END) AS sum_shu',
  766. 'type_id'
  767. ])
  768. ->where($map)
  769. ->fetchSql()
  770. ->find();
  771. $res_a = queryCache($sql_a, 200);
  772. $sql = CrmBusinessModel::field([
  773. "status_id",
  774. 'COUNT(*)' => 'count',
  775. 'SUM(`money`)' => 'sum',
  776. 'type_id'
  777. ])
  778. ->where($where)
  779. ->whereNotIn('is_end', '1,2,3')
  780. ->group('status_id')
  781. ->fetchSql()
  782. ->select();
  783. $res = queryCache($sql, 200);
  784. $res = array_column($res, null, 'status_id');
  785. $sum_money = 0;
  786. $count = 0; # 商机数总和
  787. $moneyCount = 0; # 金额总和
  788. foreach ($statusList as $k => $v) {
  789. $v['count'] = $res[$v['status_id']]['count'] ?: 0;
  790. $v['money'] = $res[$v['status_id']]['sum'] ?: 0;
  791. $v['status_name'] = $v['name'];
  792. $statusList[$k] = $v;
  793. $sum_money += $v['money'];
  794. $moneyCount += $v['money'];
  795. $count += $v['count'];
  796. }
  797. $data['list'] = $statusList;
  798. $data['sum_ying'] = $res_a[0]['sum_ying'] ?: 0;
  799. $data['sum_shu'] = $res_a[0]['sum_shu'] ?: 0;
  800. $data['sum_money'] = $sum_money ?: 0;
  801. $data['total'] = ['name' => '合计', 'money_count' => $moneyCount, 'count' => $count];
  802. return $data ?: [];
  803. }
  804. /**
  805. * [商机转移]
  806. * @param ids 商机ID数组
  807. * @param owner_user_id 变更负责人
  808. * @param is_remove 1移出,2转为团队成员
  809. * @return
  810. * @author Michael_xu
  811. */
  812. public function transferDataById($ids, $owner_user_id, $type = 1, $is_remove)
  813. {
  814. $settingModel = new \app\crm\model\Setting();
  815. $errorMessage = [];
  816. foreach ($ids as $id) {
  817. $businessInfo = db('crm_business')->where(['business_id' => $id])->find();
  818. //团队成员
  819. $teamData = [];
  820. $teamData['type'] = $type; //权限 1只读2读写
  821. $teamData['user_id'] = [$businessInfo['owner_user_id']]; //协作人
  822. $teamData['types'] = 'crm_business'; //类型
  823. $teamData['types_id'] = $id; //类型ID
  824. $teamData['is_del'] = ($is_remove == 1) ? 1 : '';
  825. $res = $settingModel->createTeamData($teamData);
  826. $data = [];
  827. $data['owner_user_id'] = $owner_user_id;
  828. $data['update_time'] = time();
  829. if (!db('crm_business')->where(['business_id' => $id])->update($data)) {
  830. $errorMessage[] = '商机:' . $businessInfo['name'] . '"转移失败,错误原因:数据出错;';
  831. continue;
  832. } else {
  833. $businessArray = [];
  834. $teamBusiness = db('crm_business')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('business_id', $id)->find();
  835. if (!empty($teamBusiness['ro_user_id'])) {
  836. $businessRo = arrayToString(array_diff(stringToArray($teamBusiness['ro_user_id']), [$teamBusiness['owner_user_id']]));
  837. $businessArray['ro_user_id'] = $businessRo;
  838. }
  839. if (!empty($teamBusiness['rw_user_id'])) {
  840. $businessRo = arrayToString(array_diff(stringToArray($teamBusiness['rw_user_id']), [$teamBusiness['owner_user_id']]));
  841. $businessArray['rw_user_id'] = $businessRo;
  842. }
  843. db('crm_business')->where('business_id', $id)->update($businessArray);
  844. }
  845. }
  846. if ($errorMessage) {
  847. return $errorMessage;
  848. } else {
  849. return true;
  850. }
  851. }
  852. /**
  853. * [商机统计]
  854. * @param
  855. * @return
  856. */
  857. public function getTrendql($map)
  858. {
  859. $prefix = config('database.prefix');
  860. $sql = "SELECT
  861. '{$map['type']}' AS type,
  862. '{$map['start_time']}' AS start_time,
  863. '{$map['end_time']}' AS end_time,
  864. IFNULL(
  865. (
  866. SELECT
  867. sum(money)
  868. FROM
  869. {$prefix}crm_business
  870. WHERE
  871. create_time BETWEEN {$map['start_time']} AND {$map['end_time']}
  872. AND owner_user_id IN ({$map['owner_user_id']})
  873. ),
  874. 0
  875. ) AS business_money,
  876. IFNULL(
  877. count(business_id),
  878. 0
  879. ) AS business_num
  880. FROM
  881. {$prefix}crm_business
  882. WHERE
  883. create_time BETWEEN {$map['start_time']} AND {$map['end_time']}
  884. AND owner_user_id IN ({$map['owner_user_id']})";
  885. return $sql;
  886. }
  887. /**
  888. * [赢单机会转化率趋势分析]
  889. * @param
  890. * @return
  891. */
  892. public function getWinSql($map)
  893. {
  894. $prefix = config('database.prefix');
  895. $sql = "SELECT
  896. '{$map['type']}' AS type,
  897. '{$map['start_time']}' AS start_time,
  898. '{$map['end_time']}' AS end_time,
  899. IFNULL(
  900. (
  901. SELECT
  902. count(business_id)
  903. FROM
  904. {$prefix}crm_business
  905. WHERE
  906. create_time BETWEEN {$map['start_time']} AND {$map['end_time']}
  907. AND owner_user_id IN ({$map['owner_user_id']})
  908. AND is_end = 1
  909. ),
  910. 0
  911. ) AS business_end,
  912. IFNULL(
  913. count(business_id),
  914. 0
  915. ) AS business_num
  916. FROM
  917. {$prefix}crm_business
  918. WHERE
  919. create_time BETWEEN {$map['start_time']} AND {$map['end_time']}
  920. AND owner_user_id IN ({$map['owner_user_id']})";
  921. return $sql;
  922. }
  923. /**
  924. * 获取系统信息
  925. *
  926. * @param $id
  927. * @return array
  928. * @throws \think\db\exception\DataNotFoundException
  929. * @throws \think\db\exception\ModelNotFoundException
  930. * @throws \think\exception\DbException
  931. */
  932. public function getSystemInfo($id)
  933. {
  934. # 商机
  935. $business = Db::name('crm_business')->field(['create_user_id' ,'owner_user_id', 'create_time', 'update_time', 'last_time'])->where('business_id', $id)->find();
  936. # 创建人
  937. $realname = Db::name('admin_user')->where('id', $business['create_user_id'])->value('realname');
  938. # zjf 20210726
  939. $userModel = new \app\admin\model\User();
  940. $ownerUserInfo = $userModel->getUserById($business['owner_user_id']);
  941. # 负责人部门
  942. $ownerStructureName = $ownerUserInfo['structure_name'];
  943. # 负责人
  944. $ownerUserName = $ownerUserInfo['realname'];
  945. return [
  946. 'create_user_id' => $realname,
  947. 'owner_user_id' => $ownerUserName,
  948. 'create_time' => date('Y-m-d H:i:s', $business['create_time']),
  949. 'update_time' => date('Y-m-d H:i:s', $business['update_time']),
  950. 'last_time' => !empty($business['last_time']) ? date('Y-m-d H:i:s', $business['last_time']) : '',
  951. 'owner_user_structure_name' => $ownerStructureName
  952. ];
  953. }
  954. /**
  955. * 判断联系人详情权限 todo 客户模块也在用,以后抽成一个公共的方法
  956. *
  957. * @param $contactsId
  958. * @return bool
  959. */
  960. private function getContactsAuth($contactsId)
  961. {
  962. $ownerUserId = db('crm_contacts')->where('contacts_id', $contactsId)->value('owner_user_id');
  963. $authUserIds = (new \app\admin\model\User())->getUserByPer('crm', 'contacts', 'read');
  964. return in_array($ownerUserId, $authUserIds);
  965. }
  966. }