123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 跟进记录
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\model;
  8. use think\Db;
  9. use app\admin\model\Common;
  10. use think\Request;
  11. use think\Validate;
  12. class Record extends Common
  13. {
  14. /**
  15. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  16. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  17. */
  18. protected $name = 'admin_record';
  19. protected $createTime = 'create_time';
  20. protected $updateTime = 'update_time';
  21. protected $autoWriteTimestamp = true;
  22. protected $types_arr = ['crm_leads','crm_customer','crm_contacts','crm_product','crm_business','crm_contract','oa_log','admin_record'];
  23. /**
  24. * [getDataList 跟进记录list]
  25. * @author Michael_xu
  26. * @param [string] $map [查询条件]
  27. * @param [number] $page [当前页数]
  28. * @param [number] $limit [每页数量]
  29. * @param [string] $by [分类]
  30. * @param [string] $types [类别]
  31. * @param [number] $types_id [类别Id]
  32. * @return [array]
  33. */
  34. public function getDataList($request, $by = '')
  35. {
  36. $userModel = new \app\admin\model\User();
  37. $commonModel = new \app\admin\model\Comment();
  38. $fileModel = new \app\admin\model\File();
  39. $structureModel = new \app\admin\model\Structure();
  40. $lableModel = new \app\work\model\WorkLable();
  41. $taskModel = new \app\work\model\Task();
  42. $request = $this->fmtRequest( $request );
  43. $map = $request['map'] ? : [];
  44. if (!$map['types'] || !$map['types_id']) {
  45. $this->error = '参数错误';
  46. return false;
  47. }
  48. switch ($by) {
  49. case 'record' :
  50. $where_record = [];
  51. $where_record['types'] = $map['types'];
  52. $where_record['types_id'] = $map['types_id'];
  53. $contactsWhere = [];
  54. $businessWhere = [];
  55. $contractWhere = [];
  56. //客户模块下包含被转化线索的跟进记录
  57. if ($map['types'] == 'crm_customer') {
  58. if ($leads_id = db('crm_leads')->where(['customer_id' => $map['types_id']])->value('leads_id')) {
  59. $whereOr = [];
  60. $whereOr['types'] = 'crm_leads';
  61. $whereOr['types_id'] = $leads_id;
  62. }
  63. $customerWhere['customer_id'] = $where_record['types_id'];
  64. # 查询客户的联系人ID串
  65. $contacts = Db::name('crm_contacts')->field(['contacts_id'])->where($customerWhere)->select();
  66. if (!empty($contacts)) {
  67. $contactsWhere['types'] = 'crm_contacts';
  68. $contactsWhere['types_id'] = array_reduce($contacts, function ($result, $value) {
  69. return array_merge($result, array_values($value));
  70. }, []);
  71. }
  72. # 查询客户的商机ID串
  73. $business = Db::name('crm_business')->field(['business_id'])->where($customerWhere)->select();
  74. if (!empty($business)) {
  75. $businessWhere['types'] = 'crm_business';
  76. $businessWhere['types_id'] = array_reduce($business, function ($result, $value) {
  77. return array_merge($result, array_values($value));
  78. }, []);
  79. }
  80. # 查询客户的合同ID串
  81. $contract = Db::name('crm_contract')->field(['contract_id'])->where($customerWhere)->select();
  82. if (!empty($contract)) {
  83. $contractWhere['types'] = 'crm_contract';
  84. $contractWhere['types_id'] = array_reduce($contract, function ($result, $value) {
  85. return array_merge($result, array_values($value));
  86. }, []);
  87. }
  88. }
  89. //联系人下包含关联的客户的跟进记录
  90. if ($map['types'] == 'crm_contacts') {
  91. $whereOr = [];
  92. $whereOr['contacts_ids'] = array('like','%,'.$map['types_id'].',%');
  93. }
  94. if ($map['types'] == 'crm_business') {
  95. $whereOr = [];
  96. $whereOr['business_ids'] = array('like','%,'.$map['types_id'].',%');
  97. }
  98. $list = db('admin_record')
  99. ->page($request['page'], $request['limit'])
  100. ->order('create_time desc')
  101. ->select(function($query) use ($where_record, $whereOr, $contractWhere, $businessWhere, $contactsWhere){
  102. $query->where($where_record)
  103. ->whereOr(function ($query) use ($whereOr) {
  104. $query->where($whereOr);
  105. })
  106. ->whereOr(function ($query) use ($contractWhere) {
  107. if (!empty($contractWhere['types_id'])) $query->where('types', $contractWhere['types']);
  108. if (!empty($contractWhere['types_id'])) $query->whereIn('types_id', $contractWhere['types_id']);
  109. })
  110. ->whereOr(function ($query) use ($businessWhere) {
  111. if (!empty($businessWhere['types_id'])) $query->where('types', $businessWhere['types']);
  112. if (!empty($businessWhere['types_id'])) $query->whereIn('types_id', $businessWhere['types_id']);
  113. })
  114. ->whereOr(function ($query) use ($contactsWhere) {
  115. if (!empty($contactsWhere['types_id'])) $query->where('types', $contactsWhere['types']);
  116. if (!empty($contactsWhere['types_id'])) $query->whereIn('types_id', $contactsWhere['types_id']);
  117. });
  118. });
  119. foreach ($list as $k=>$v) {
  120. $list[$k]['id'] = $v['record_id'];
  121. $list[$k]['cate'] = 1;
  122. }
  123. $dataCount = db('admin_record')
  124. ->where(function($query) use ($where_record, $whereOr, $contractWhere, $businessWhere, $contactsWhere){
  125. $query->where($where_record)->whereOr(function ($query) use ($whereOr) {
  126. $query->where($whereOr);
  127. })
  128. ->whereOr(function ($query) use ($contractWhere) {
  129. if (!empty($contractWhere['types'])) $query->where('types', $contractWhere['types']);
  130. if (!empty($contractWhere['types_id'])) $query->whereIn('types_id', $contractWhere['types_id']);
  131. })
  132. ->whereOr(function ($query) use ($businessWhere) {
  133. if (!empty($businessWhere['types'])) $query->where('types', $businessWhere['types']);
  134. if (!empty($businessWhere['types_id'])) $query->whereIn('types_id', $businessWhere['types_id']);
  135. })
  136. ->whereOr(function ($query) use ($contactsWhere) {
  137. if (!empty($contactsWhere['types'])) $query->where('types', $contactsWhere['types']);
  138. if (!empty($contactsWhere['types_id'])) $query->whereIn('types_id', $contactsWhere['types_id']);
  139. });
  140. })->count();
  141. break;
  142. case 'log' :
  143. $where_log = [];
  144. $r_logs = $this->getRelationIdsByType($map['types'], $map['types_id'], 'oa_log') ? : [];
  145. $where_log['log_id'] = ['in',$r_logs];
  146. $list = db('oa_log')
  147. ->page($request['page'], $request['limit'])
  148. ->order('create_time desc')
  149. ->where($where_log)
  150. ->select();
  151. foreach ($list as $k=>$v) {
  152. $list[$k]['id'] = $v['log_id'];
  153. $list[$k]['cate'] = 2;
  154. }
  155. $dataCount = db('oa_log')->where($where_log)->count();
  156. break;
  157. case 'examine' :
  158. $where_examine = [];
  159. $r_logs = $this->getRelationIdsByType($map['types'], $map['types_id'], 'oa_examine') ? : [];
  160. $where_examine['examine_id'] = ['in',$r_logs];
  161. $list = db('oa_examine')
  162. ->page($request['page'], $request['limit'])
  163. ->order('create_time desc')
  164. ->where($where_examine)
  165. ->select();
  166. foreach ($list as $k=>$v) {
  167. $list[$k]['id'] = $v['examine_id'];
  168. $list[$k]['cate'] = 3;
  169. }
  170. $dataCount = db('oa_examine')->where($where_examine)->count();
  171. break;
  172. case 'task' :
  173. $where_task = [];
  174. $r_logs = $this->getRelationIdsByType($map['types'], $map['types_id'], 'task') ? : [];
  175. $where_task['task_id'] = ['in',$r_logs];
  176. $list = db('task')
  177. ->page($request['page'], $request['limit'])
  178. ->order('create_time desc')
  179. ->where($where_task)
  180. ->select();
  181. foreach ($list as $k=>$v) {
  182. $list[$k]['id'] = $v['task_id'];
  183. $list[$k]['cate'] = 4;
  184. }
  185. $dataCount = db('task')->where($where_task)->count();
  186. break;
  187. case 'event' :
  188. $where_event = [];
  189. $r_logs = $this->getRelationIdsByType($map['types'], $map['types_id'], 'oa_event') ? : [];
  190. $where_event['event_id'] = ['in',$r_logs];
  191. $list = db('oa_event')
  192. ->page($request['page'], $request['limit'])
  193. ->order('create_time desc')
  194. ->where($where_event)
  195. ->select();
  196. foreach ($list as $k=>$v) {
  197. $list[$k]['id'] = $v['event_id'];
  198. $list[$k]['cate'] = 5;
  199. }
  200. $dataCount = db('oa_event')->where($where_event)->count();
  201. break;
  202. default :
  203. $where_log = [];
  204. $r_logs = $this->getRelationIdsByType($map['types'], $map['types_id'], 'oa_log') ? : [];
  205. $where_log['log_id'] = ['in',$r_logs];
  206. $sqlArr[] = Db::table('__OA_LOG__')
  207. ->where($where_log)
  208. ->field(['log_id as id,create_time,create_user_id,2 as cate,content'])
  209. ->buildSql();
  210. $where_examine = [];
  211. $r_logs = $this->getRelationIdsByType($map['types'], $map['types_id'], 'oa_examine') ? : [];
  212. $where_examine['examine_id'] = ['in',$r_logs];
  213. $sqlArr[] = Db::table('__OA_EXAMINE__')
  214. ->where($where_examine)
  215. ->field(['examine_id as id,create_time,create_user_id,3 as cate,content'])
  216. ->buildSql();
  217. $where_task = [];
  218. $r_logs = $this->getRelationIdsByType($map['types'], $map['types_id'], 'task') ? : [];
  219. $where_task['task_id'] = ['in',$r_logs];
  220. $sqlArr[] = Db::table('__TASK__')
  221. ->where($where_task)
  222. ->field(['task_id as id,create_time,create_user_id,4 as cate,name as content'])
  223. ->buildSql();
  224. $where_record = [];
  225. $where_record['types'] = $map['types'];
  226. $where_record['types_id'] = $map['types_id'];
  227. //客户模块下包含被转化线索的跟进记录
  228. if ($map['types'] == 'crm_customer') {
  229. if ($leads_id = db('crm_leads')->where(['customer_id' => $map['types_id']])->value('leads_id')) {
  230. $whereOr = [];
  231. $whereOr['types'] = 'crm_leads';
  232. $whereOr['types_id'] = $leads_id;
  233. }
  234. }
  235. $e = Db::table('__ADMIN_RECORD__')
  236. ->alias('record')
  237. ->where($where_record)
  238. ->whereOr($whereOr)
  239. ->field(['record_id as id,create_time,create_user_id,1 as cate,content'])
  240. ->union($sqlArr)
  241. ->buildSql();
  242. $list = Db::table($e.' a')
  243. ->page($request['page'], $request['limit'])
  244. ->order('create_time desc')
  245. ->select();
  246. $dataCount = Db::table($e.' a')->count();
  247. break;
  248. }
  249. $admin_user_ids = $userModel->getAdminId();
  250. foreach ($list as $k=>$v) {
  251. $create_user_info = isset($v['create_user_id']) ? $userModel->getUserById($v['create_user_id']) : [];
  252. $list[$k]['create_user_info'] = $create_user_info;
  253. $content = '';
  254. $fileList = [];
  255. $imgList = [];
  256. $where = [];
  257. $where['module_id'] = $v['id'];
  258. $relation_list = [];
  259. switch ($v['cate']) {
  260. case '1' :
  261. $where['module'] = 'admin_record';
  262. $relation_list = $this->getListByRelationId('record', $v['id']);
  263. $dataInfo = [];
  264. break;
  265. case '2' :
  266. $where['module'] = 'oa_log';
  267. $dataInfo = $v;
  268. $dataInfo['create_user_info'] = $create_user_info;
  269. $dataInfo['sendUserList'] = $userModel->getDataByStr($dataInfo['send_user_ids']) ? : [];
  270. $dataInfo['sendStructList'] = $structureModel->getDataByStr($dataInfo['send_structure_ids']) ? : [];
  271. $param['type_id'] = $dataInfo['log_id'];
  272. $param['type'] = 'oa_log';
  273. $dataInfo['replyList'] = $commonModel->read($param);
  274. $is_update = 0;
  275. $is_delete = 0;
  276. //3天内的日志可删,可修改
  277. if (($dataInfo['create_user_id'] == $user_id) && date('Ymd',$dataInfo['create_time']) > date('Ymd',(strtotime(date('Ymd',time()))-86400*3))) {
  278. $is_update = 1;
  279. $is_delete = 1;
  280. }
  281. $permission['is_update'] = $is_update;
  282. $permission['is_delete'] = $is_delete;
  283. $dataInfo['permission'] = $permission;
  284. $relation_list = $this->getListByRelationId('log', $v['id']);
  285. break;
  286. case '3' :
  287. $where['module'] = 'oa_examine';
  288. $dataInfo = $v;
  289. $dataInfo['category_name'] = db('oa_examine_category')->where(['category_id' => $dataInfo['category_id']])->value('title');
  290. $dataInfo['create_user_info'] = $create_user_info;
  291. $causeCount = 0;
  292. $causeTitle = '';
  293. $duration = $dataInfo['duration'] ? : '0.0';
  294. $money = $dataInfo['money'] ? : '0.00';
  295. if (in_array($dataInfo['category_id'],['3','5'])) {
  296. $causeCount = db('oa_examine_travel')->where(['examine_id' => $dataInfo['examine_id']])->count() ? : 0;
  297. if ($dataInfo['category_id'] == 3) $causeTitle = $causeCount.'个行程,共'.$duration.'天';
  298. if ($dataInfo['category_id'] == 5) $causeTitle = $causeCount.'个报销事项,共'.$money.'元';
  299. }
  300. $dataInfo['causeTitle'] = $causeTitle;
  301. $dataInfo['causeCount'] = $causeCount ? : 0;
  302. //权限
  303. //创建人或管理员有撤销权限
  304. $permission = [];
  305. $is_recheck = 0;
  306. $is_update = 0;
  307. $is_delete = 0;
  308. if (((int)$dataInfo['create_user_id'] == $user_id || !in_array($userr_id, $admin_user_ids)) && (!in_array($dataInfo['check_status'],['2','3']) || (empty($dataInfo['check_status']) && empty($dataInfo['check_user_id'])))) {
  309. $is_recheck = 1;
  310. }
  311. //创建人(待审状态且无审批人时可编辑)
  312. if (($user_id == (int)$dataInfo['create_user_id']) && $dataInfo['check_status'] == 0 && empty($dataInfo['check_user_id'])) {
  313. $is_update = 1;
  314. $is_delete = 1;
  315. }
  316. $permission['is_recheck'] = $is_recheck;
  317. $permission['is_update'] = $is_update;
  318. $permission['is_delete'] = $is_delete;
  319. $dataInfo['permission'] = $permission;
  320. $relation_list = $this->getListByRelationId('examine', $v['id']);
  321. break;
  322. case '4' :
  323. $where['module'] = 'work_task';
  324. $relation_list = $this->getListByRelationId('task', $v['id']);
  325. $dataInfo = $v;
  326. $dataInfo['task_name'] = $dataInfo['name'];
  327. if ($dataInfo['pid'] > 0) {
  328. $p_det = Db::name('Task')->field('task_id,name')->where('task_id ='.$dataInfo['pid'])->find();
  329. $dataInfo['pname'] = $p_det['name'];
  330. } else {
  331. $dataInfo['pname'] = '';
  332. }
  333. $subcount = Db::name('Task')->where(' ishidden =0 and ( status=1 ) and pid ='.$dataInfo['task_id'])->count();
  334. $subdonecount = Db::name('Task')->where(' ishidden = 0 and status = 5 and pid ='.$dataInfo['task_id'])->count();
  335. $dataInfo['subcount'] = $subcount; //子任务
  336. $dataInfo['subdonecount'] = $subdonecount; //已完成子任务
  337. $dataInfo['commentcount'] = Db::name('AdminComment')->where('type=1 and type_id ='.$dataInfo['task_id'])->count();
  338. $dataInfo['filecount'] = Db::name('WorkTaskFile')->where('task_id ='.$dataInfo['task_id'])->count();
  339. if ($dataInfo['lable_id']) {
  340. $dataInfo['lableList'] = $lableModel->getDataByStr($dataInfo['lable_id']);
  341. }else{
  342. $dataInfo['lableList'] = array();
  343. }
  344. //参与人列表数组
  345. //$userlist =$userModel->getDataByStr($value['owner_user_id']);
  346. //$dataInfo['own_list'] = $userlist?$userlist: array();
  347. //负责人信息
  348. $dataInfo['main_user'] = $dataInfo['main_user_id'] ? $userModel->getUserById($dataInfo['main_user_id']) : array();
  349. $dataInfo['relationCount'] = $taskModel->getRelationCount($dataInfo['task_id']);
  350. break;
  351. case '5' :
  352. $where['module'] = 'oa_event';
  353. $relation_list = $this->getListByRelationId('event', $v['id']);
  354. $dataInfo = $v;
  355. $dataInfo['create_user_info'] = $userModel->getUserById($dataInfo['create_user_id']);
  356. $dataInfo['ownerList'] = $userModel->getDataByStr($dataInfo['owner_user_ids']) ? : [];
  357. $dataInfo['remindtype'] = (int)$dataInfo['remindtype'];
  358. $noticeList = Db::name('OaEventNotice')->where('event_id = '.$dataInfo['event_id'])->find();
  359. if (!$noticeList) {
  360. $dataInfo['is_repeat'] = 0;
  361. } else {
  362. $dataInfo['is_repeat'] = 1;
  363. }
  364. $dataInfo['stop_time'] = $noticeList ? $noticeList['stop_time'] : '';
  365. $dataInfo['noticetype'] = $noticeList ? $noticeList['noticetype'] : '';
  366. if ($noticeList['noticetype'] == '2') {
  367. $dataInfo['repeat'] = $noticeList['repeated'] ? explode('|||',$noticeList['repeated']) : [];
  368. } else {
  369. $dataInfo['repeat'] = '';
  370. }
  371. break;
  372. case '6' :
  373. $where['module'] = 'work';
  374. $relation_list = $this->getListByRelationId('work', $v['id']);
  375. break;
  376. }
  377. $newFileList = [];
  378. $newFileList = $fileModel->getDataList($where, 'all');
  379. if ($newFileList['list']) {
  380. foreach ($newFileList['list'] as $val) {
  381. if ($val['types'] == 'file') {
  382. $fileList[] = $val;
  383. } else {
  384. $imgList[] = $val;
  385. }
  386. }
  387. }
  388. $dataInfo['fileList'] = $fileList ? : [];
  389. $dataInfo['imgList'] = $imgList ? : [];
  390. $dataInfo['customerList'] = $relation_list['customerList'] ? : [];
  391. $dataInfo['contactsList'] = $relation_list['contactsList'] ? : [];
  392. $dataInfo['businessList'] = $relation_list['businessList'] ? : [];
  393. $dataInfo['contractList'] = $relation_list['contractList'] ? : [];
  394. if ($v['cate'] != 1) {
  395. $list[$k] = ['dataInfo' => $dataInfo];
  396. } else {
  397. $list[$k]['dataInfo'] = $dataInfo;
  398. }
  399. }
  400. $data = [];
  401. $data['list'] = $list ? : [];
  402. $data['dataCount'] = $dataCount ? : 0;
  403. return $data;
  404. }
  405. /**
  406. * 创建跟进记录信息
  407. * @author Michael_xu
  408. * @param
  409. * @return
  410. */
  411. public function createData($param)
  412. {
  413. $eventModel = new \app\oa\model\Event();
  414. if (!$param['types'] || !$param['types_id'] || !in_array($param['types'], $this->types_arr)) {
  415. $this->error = '参数错误';
  416. return false;
  417. }
  418. //验证
  419. $validate = validate($this->name);
  420. if (!$validate->check($param)) {
  421. $this->error = $validate->getError();
  422. return false;
  423. }
  424. $param['business_ids'] = arrayToString($param['business_ids']);
  425. $param['contacts_ids'] = arrayToString($param['contacts_ids']);
  426. $fileArr = $param['file_id']; //接收表单附件
  427. unset($param['file_id']);
  428. if ($this->data($param)->allowField(true)->save()) {
  429. //下次联系时间
  430. $this->updateNexttime($param['types'], $param['types_id'], $param['next_time']);
  431. //处理附件关系
  432. if ($fileArr) {
  433. $fileModel = new \app\admin\model\File();
  434. $resData = $fileModel->createDataById($fileArr, 'admin_record', $this->record_id);
  435. if ($resData == false) {
  436. $this->error = '附件上传失败';
  437. return false;
  438. }
  439. }
  440. $data = [];
  441. $data['record_id'] = $this->record_id;
  442. return $data;
  443. } else {
  444. $this->error = '添加失败';
  445. return false;
  446. }
  447. }
  448. /**
  449. * 根据主键获取详情
  450. * @param array $param [description]
  451. */
  452. public function getDataById($id = '')
  453. {
  454. $map['record_id'] = $id;
  455. $dataInfo = db('admin_record')->where($map)->find();
  456. if (!$dataInfo) {
  457. $this->error = '暂无此数据';
  458. return false;
  459. }
  460. $userModel = new \app\admin\model\User();
  461. $dataInfo['create_user_info'] = $userModel->getUserById($dataInfo['create_user_id']);
  462. return $dataInfo;
  463. }
  464. /**
  465. * 相关业务ids
  466. * @param $types 相关业务
  467. * @param $types_id 相关业务ID
  468. * @param $relation 相关模块
  469. */
  470. public function getRelationIdsByType($types, $types_id, $relation)
  471. {
  472. $rIds = [];
  473. switch ($relation) {
  474. case 'oa_log' : $dbName = db('oa_log_relation'); $relationId = 'log_id'; break; //相关日志
  475. case 'oa_event' : $dbName = db('oa_event_relation'); $relationId = 'event_id'; break; //相关日程
  476. case 'task' : $dbName = db('task_relation'); $relationId = 'task_id'; break; //相关任务
  477. case 'task_work' : $dbName = db('work_relation'); $relationId = 'work_id'; break; //相关项目
  478. case 'oa_examine' : $dbName = db('oa_examine_relation'); $relationId = 'examine_id'; break; //相关审批
  479. default : return []; break;
  480. }
  481. switch ($types) {
  482. case 'crm_customer' : $rIds = $dbName->where(['customer_ids' => ['like', '%,'.$types_id.',%']])->column($relationId); break;
  483. case 'crm_contacts' : $rIds = $dbName->where(['contacts_ids' => ['like', '%,'.$types_id.',%']])->column($relationId); break;
  484. case 'crm_business' : $rIds = $dbName->where(['business_ids' => ['like', '%,'.$types_id.',%']])->column($relationId); break;
  485. case 'crm_contract' : $rIds = $dbName->where(['contract_ids' => ['like', '%,'.$types_id.',%']])->column($relationId); break;
  486. }
  487. return $rIds ? : [];
  488. }
  489. /**
  490. * 相关业务list
  491. * @param $types 相关业务
  492. * @param $types_id 相关业务ID
  493. * @param $relation 相关模块
  494. */
  495. public function getListByRelationId($relation, $relation_id)
  496. {
  497. $BusinessModel = new \app\crm\model\Business();
  498. $ContactsModel = new \app\crm\model\Contacts();
  499. $ContractModel = new \app\crm\model\Contract();
  500. $CustomerModel = new \app\crm\model\Customer();
  501. $LeadsModel = new \app\crm\model\Leads();
  502. $data = [];
  503. switch ($relation) {
  504. case 'log' : $data = db('oa_log_relation')->where(['log_id' => $relation_id])->find(); break;
  505. case 'event' : $data = db('oa_event_relation')->where(['event_id' => $relation_id])->find(); break;
  506. case 'task' : $data = db('task_relation')->where(['task_id' => $relation_id])->find(); break;
  507. case 'work' : $data = db('work_relation')->where(['work_id' => $relation_id])->find(); break;
  508. case 'examine' : $data = db('oa_examine_relation')->where(['examine_id' => $relation_id])->find(); break;
  509. case 'record' : $data = db('admin_record')->where(['record_id' => $relation_id])->find(); break;
  510. case 'activity' : $data = db('crm_activity')->where(['activity_id' => $relation_id])->find(); break;
  511. default : $data = []; break;
  512. }
  513. $data['customerList'] = $data['customer_ids'] ? $CustomerModel->getDataByStr($data['customer_ids']) : [];
  514. $data['contactsList'] = $data['contacts_ids'] ? $ContactsModel->getDataByStr($data['contacts_ids']) : [];
  515. $data['businessList'] = $data['business_ids'] ? $BusinessModel->getDataByStr($data['business_ids']) : [];
  516. $data['contractList'] = $data['contract_ids'] ? $ContractModel->getDataByStr($data['contract_ids']) : [];
  517. $data['leadsList'] = $data['leads_ids'] ? $LeadsModel->getDataByStr($data['leads_ids']) : [];
  518. return $data ? : [];
  519. }
  520. /**
  521. * 多标签list
  522. * @param $types 相关业务
  523. * @param $types_id 相关业务ID
  524. * @param $relation 相关模块
  525. */
  526. public function getListByLableId($relation, $relation_id)
  527. {
  528. $TaskModel = new \app\work\model\Task();
  529. $data = [];
  530. switch ($relation) {
  531. case 'task' : $data = db('task')->where(['task_id' => $relation_id])->field('lable_id')->find(); break;
  532. case 'work' : $data = db('work_relation')->where(['work_id' => $relation_id])->find(); break;
  533. default : $data = []; break;
  534. }
  535. $data['lable'] = $data['lable_id'] ? $TaskModel->getDataByStr($data['lable_id']) : [];
  536. return $data ? : [];
  537. }
  538. /**
  539. * 相关模块下次联系时间
  540. * @param types 类型
  541. * @param types 类型ID
  542. * @param next_time 下次联系时间
  543. */
  544. public function updateNexttime($types, $types_id, $next_time)
  545. {
  546. switch ($types) {
  547. case 'crm_customer' : $dbName = db('crm_customer'); $dbId = 'customer_id'; break;
  548. case 'crm_leads' : $dbName = db('crm_leads'); $dbId = 'leads_id'; break;
  549. case 'crm_contacts' : $dbName = db('crm_contacts'); $dbId = 'contacts_id'; break;
  550. case 'crm_business' : $dbName = db('crm_business'); $dbId = 'business_id'; break;
  551. default : break;
  552. }
  553. if (!$dbName || !$dbId) {
  554. return true;
  555. }
  556. $data = [];
  557. if ($next_time) {
  558. $data['next_time'] = $next_time;
  559. } else {
  560. // 如果未填写下次联系时间,并且 原下次联系时间为当天,则把下次联系时间置空
  561. $next_time = $dbName->where([$dbId => $types_id])->value('next_time');
  562. list($start, $end) = getTimeByType();
  563. if ($next_time >= $start && $next_time <= $end) {
  564. $data['next_time'] = 0;
  565. }
  566. }
  567. $data['update_time'] = time();
  568. if (in_array($types,['crm_customer','crm_leads'])) $data['follow'] = '已跟进';
  569. $dbName->where([$dbId => $types_id])->update($data);
  570. return true;
  571. }
  572. /**
  573. * 跟进记录删除
  574. * @param types 类型
  575. * @param types 类型ID数组
  576. * @param
  577. */
  578. public function delDataByTypes($types, $types_id)
  579. {
  580. if (!is_array($types_id)) {
  581. $types_id[] = $types_id;
  582. }
  583. $fileModel = new \app\admin\model\File();
  584. $record_ids = db('crm_activity')->where(['activity_type' => $types,'activity_type_id' => ['in',$types_id]])->column('activity_id');
  585. db('crm_activity')->where(['activity_type' => $types,'activity_type_id' => ['in',$types_id]])->delete();
  586. //删除关联附件
  587. $fileModel->delRFileByModule('crm_activity',$record_ids);
  588. return true;
  589. }
  590. /**
  591. * 查询最近更进记录
  592. *
  593. * @param string $types 关联类型
  594. * @param array $types_id_list 类型ID
  595. * @return array
  596. * @author Ymob
  597. * @datetime 2019-12-11 10:43:04
  598. */
  599. public static function getLastRecord($types, $types_id_list)
  600. {
  601. $prefix = config('database.prefix');
  602. $types_ids = implode(',', $types_id_list) ?: '-1';
  603. $list = self::field(['types_id', 'content'])
  604. ->where("
  605. `record_id` IN (
  606. SELECT
  607. MAX(`record_id`)
  608. FROM
  609. `{$prefix}admin_record`
  610. WHERE
  611. `types` = '{$types}'
  612. AND `types_id` IN ({$types_ids})
  613. GROUP BY
  614. `types_id`
  615. )
  616. ")
  617. ->select();
  618. $res = [];
  619. foreach ($list as $val) {
  620. $res[$val['types_id']] = $val['content'];
  621. }
  622. return $res;
  623. }
  624. }