123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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\StarTrait;
  10. use think\Db;
  11. use think\Hook;
  12. use think\Request;
  13. class Leads extends ApiCommon
  14. {
  15. use StarTrait;
  16. /**
  17. * 用于判断权限
  18. * @permission 无限制
  19. * @allow 登录用户可访问
  20. * @other 其他根据系统设置
  21. **/
  22. public function _initialize()
  23. {
  24. $action = [
  25. 'permission' => ['exceldownload', 'setfollow'],
  26. 'allow' => ['system', 'count']
  27. ];
  28. Hook::listen('check_auth', $action);
  29. $request = Request::instance();
  30. $a = strtolower($request->action());
  31. if (!in_array($a, $action['permission'])) {
  32. parent::_initialize();
  33. }
  34. }
  35. /**
  36. * 线索列表
  37. * @return
  38. * @author Michael_xu
  39. */
  40. public function index()
  41. {
  42. $leadsModel = model('Leads');
  43. $param = $this->param;
  44. $userInfo = $this->userInfo;
  45. $param['user_id'] = $userInfo['id'];
  46. $data = $leadsModel->getDataList($param);
  47. return resultArray(['data' => $data]);
  48. }
  49. /**
  50. * 线索公海
  51. * @return
  52. * @author Michael_xu
  53. */
  54. public function pool()
  55. {
  56. $leadsModel = model('Leads');
  57. $param = $this->param;
  58. //线索公海条件(没有负责人或已经到期)
  59. $end_time = '';
  60. $param['update_time'] = array();
  61. $data = $leadsModel->getDataList($param);
  62. return resultArray(['data' => $data]);
  63. }
  64. /**
  65. * 添加线索
  66. * @param \think\Request $request
  67. * @return
  68. * @author Michael_xu
  69. */
  70. public function save()
  71. {
  72. $leadsModel = model('Leads');
  73. $param = $this->param;
  74. $userInfo = $this->userInfo;
  75. $param['create_user_id'] = $userInfo['id'];
  76. $param['owner_user_id'] = $userInfo['id'];
  77. if ($leadsModel->createData($param)) {
  78. return resultArray(['data' => '添加成功']);
  79. } else {
  80. return resultArray(['error' => $leadsModel->getError()]);
  81. }
  82. }
  83. /**
  84. * 线索详情
  85. * @param
  86. * @return
  87. * @author Michael_xu
  88. */
  89. public function read()
  90. {
  91. $leadsModel = model('Leads');
  92. $userModel = new \app\admin\model\User();
  93. $param = $this->param;
  94. $userInfo = $this->userInfo;
  95. $data = $leadsModel->getDataById($param['id'], $userInfo['id']);
  96. //判断权限
  97. $auth_user_ids = $userModel->getUserByPer('crm', 'leads', 'read');
  98. if (!in_array($data['owner_user_id'], $auth_user_ids)) {
  99. //无权限
  100. $authData['dataAuth'] = (int)0;
  101. return resultArray(['data' => $authData]);
  102. }
  103. if (!$data) {
  104. return resultArray(['error' => $leadsModel->getError()]);
  105. }
  106. return resultArray(['data' => $data]);
  107. }
  108. /**
  109. * 编辑线索
  110. * @param
  111. * @return
  112. * @author Michael_xu
  113. */
  114. public function update()
  115. {
  116. $leadsModel = model('Leads');
  117. $param = $this->param;
  118. $userInfo = $this->userInfo;
  119. $param['user_id'] = $userInfo['id'];
  120. if ($leadsModel->updateDataById($param, $param['id'])) {
  121. return resultArray(['data' => '编辑成功']);
  122. } else {
  123. return resultArray(['error' => $leadsModel->getError()]);
  124. }
  125. }
  126. /**
  127. * 删除线索
  128. * @param
  129. * @return
  130. * @author Michael_xu
  131. */
  132. public function delete()
  133. {
  134. $param = $this->param;
  135. $leadsModel = model('Leads');
  136. $recordModel = new \app\admin\model\Record();
  137. $fileModel = new \app\admin\model\File();
  138. $actionRecordModel = new \app\admin\model\ActionRecord();
  139. if (!is_array($param['id'])) {
  140. $leads_id[] = $param['id'];
  141. } else {
  142. $leads_id = $param['id'];
  143. }
  144. $delIds = [];
  145. $errorMessage = [];
  146. //数据权限判断
  147. $userModel = new \app\admin\model\User();
  148. $auth_user_ids = $userModel->getUserByPer('crm', 'leads', 'delete');
  149. foreach ($leads_id as $k => $v) {
  150. $isDel = true;
  151. //数据详情
  152. $data = $leadsModel->getDataById($v);
  153. if (!$data) {
  154. $isDel = false;
  155. $errorMessage[] = 'id为' . $v . '的线索删除失败,错误原因:' . $leadsModel->getError();
  156. }
  157. if (!in_array($data['owner_user_id'], $auth_user_ids)) {
  158. $isDel = false;
  159. $errorMessage[] = '名称为' . $data['name'] . '的线索删除失败,错误原因:无权操作';
  160. }
  161. if ($isDel) {
  162. $delIds[] = $v;
  163. }
  164. }
  165. $dataInfo = $leadsModel->where('leads_id',['in',$delIds])->select();
  166. if ($delIds) {
  167. $data = $leadsModel->delDatas($delIds);
  168. if (!$data) {
  169. return resultArray(['error' => $leadsModel->getError()]);
  170. }
  171. // 删除线索扩展数据
  172. db('crm_leads_data')->whereIn('leads_id', $delIds)->delete();
  173. //删除跟进记录
  174. $recordModel->delDataByTypes(1, $delIds);
  175. //删除关联附件
  176. $fileModel->delRFileByModule('crm_leads', $delIds);
  177. //删除关联操作记录
  178. $actionRecordModel->delDataById(['types' => 'crm_leads', 'action_id' => $delIds]);
  179. $userInfo = $this->userInfo;
  180. foreach ($dataInfo as $k => $v) {
  181. RecordActionLog($userInfo['id'], 'crm_leads', 'delete', $v['name'], '', '', '删除了线索:' . $v['name']);
  182. }
  183. }
  184. if ($errorMessage) {
  185. return resultArray(['error' => $errorMessage]);
  186. } else {
  187. return resultArray(['data' => '删除成功']);
  188. }
  189. }
  190. /**
  191. * 线索转化为客户
  192. * @param
  193. * @return
  194. * @author Michael_xu
  195. */
  196. public function transform()
  197. {
  198. $leadsModel = model('Leads');
  199. $customerModel = model('Customer');
  200. $fieldModel = new \app\admin\model\Field();
  201. $param = $this->param;
  202. $userInfo = $this->userInfo;
  203. $userModel = new \app\admin\model\User();
  204. $authIds = $userModel->getUserByPer(); //权限范围的user_id
  205. if (!$param['leads_id'] || !is_array($param['leads_id'])) {
  206. return resultArray(['error' => '请选择需要转化的线索']);
  207. }
  208. $errorMessage = [];
  209. foreach ($param['leads_id'] as $leads_id) {
  210. $data = [];
  211. $leadsInfo = db('crm_leads')->where(['leads_id' => $leads_id])->find();
  212. //字段对照关系处理
  213. $data = $fieldModel->getRelevantData('crm_leads', $leadsInfo) ?: [];
  214. $data['create_user_id'] = $userInfo['id'];
  215. $data['owner_user_id'] = $userInfo['id'];
  216. $data['deal_status'] = '未成交';
  217. $data['deal_time'] = time();
  218. $data['create_time'] = time();
  219. $data['update_time'] = time();
  220. $data['next_time'] = $leadsInfo['next_time'];
  221. if (empty($data['telephone'])) $data['telephone'] = 0;
  222. # 获取客户的时间
  223. $data['obtain_time'] = time();
  224. //权限判断
  225. if (!$leadsInfo) {
  226. $errorMessage[] = '线索《' . $leadsInfo['name'] . '》转化失败,错误原因:数据不存在;';
  227. continue;
  228. }
  229. if (!in_array($leadsInfo['owner_user_id'], $authIds)) {
  230. $errorMessage[] = '线索《' . $leadsInfo['name'] . '》转化失败,错误原因:无权限;';
  231. continue;
  232. }
  233. $resCustomer = $customerModel->createData($data);
  234. if (!$resCustomer) {
  235. $errorMessage[] = '线索《' . $leadsInfo['name'] . '》转化失败,错误原因:' . $customerModel->getError();
  236. continue;
  237. }
  238. $leadsData = [];
  239. $leadsData['is_transform'] = 1; //标记为已转化
  240. $leadsData['customer_id'] = $resCustomer['customer_id'];
  241. db('crm_leads')->where(['leads_id' => $leads_id])->update($leadsData);
  242. //修改记录
  243. updateActionLog($userInfo['id'], 'crm_customer', $resCustomer['customer_id'], '', '', '将线索"' . $leadsInfo['name'] . '转化为客户');
  244. RecordActionLog($userInfo['id'],'crm_leads','save',$leadsInfo['name'],'','','将线索'.$leadsInfo['name'].'转化为客户');
  245. # 将线索下的跟进记录同步至客户下
  246. $record = db('crm_activity')->field('activity_id', true)->where(['type' => 1, 'activity_type' => 1, 'activity_type_id' => $leads_id])->select();
  247. foreach ($record as $key1 => $value1) {
  248. $record[$key1]['activity_type'] = 2;
  249. $record[$key1]['activity_type_id'] = $resCustomer['customer_id'];
  250. $record[$key1]['create_time'] = time() + 1;
  251. $record[$key1]['update_time'] = time() + 1;
  252. }
  253. if (!empty($record)) db('crm_activity')->insertAll($record);
  254. }
  255. if (!$errorMessage) {
  256. return resultArray(['data' => '转化成功']);
  257. } else {
  258. return resultArray(['error' => $errorMessage]);
  259. }
  260. }
  261. /**
  262. * 线索转移
  263. * @param owner_user_id 变更负责人
  264. * @param is_remove 1移出,2转为团队成员
  265. * @param type 权限 1只读2读写
  266. * @return
  267. * @author Michael_xu
  268. */
  269. public function transfer()
  270. {
  271. $param = $this->param;
  272. $userInfo = $this->userInfo;
  273. $leadsModel = model('Leads');
  274. $settingModel = model('Setting');
  275. $userModel = new \app\admin\model\User();
  276. $authIds = $userModel->getUserByPer(); //权限范围的user_id
  277. if (!$param['owner_user_id']) {
  278. return resultArray(['error' => '变更负责人不能为空']);
  279. }
  280. if (!$param['leads_id'] || !is_array($param['leads_id'])) {
  281. return resultArray(['error' => '请选择需要转移的线索']);
  282. }
  283. $is_remove = $param['is_remove'] == 2 ?: 1;
  284. $type = $param['type'] == 2 ?: 1;
  285. $data = [];
  286. $data['owner_user_id'] = $param['owner_user_id'];
  287. $data['update_time'] = time();
  288. $data['follow'] = '待跟进';
  289. $ownerUserName = $userModel->getUserNameById($param['owner_user_id']);
  290. $errorMessage = [];
  291. foreach ($param['leads_id'] as $leads_id) {
  292. $leadsInfo = $leadsModel->getDataById($leads_id);
  293. if (!$leadsInfo) {
  294. $errorMessage[] = '名称:为《' . $leadsInfo['name'] . '》的线索转移失败,错误原因:数据不存在;';
  295. continue;
  296. }
  297. //权限判断
  298. if (!in_array($leadsInfo['owner_user_id'], $authIds)) {
  299. $errorMessage[] = '"' . $leadsInfo['name'] . '"转移失败,错误原因:无权限;';
  300. continue;
  301. }
  302. # 处理分配标识,待办事项专用
  303. $data['is_allocation'] = 1;
  304. $resLeads = db('crm_leads')->where(['leads_id' => $leads_id])->update($data);
  305. if (!$resLeads) {
  306. $errorMessage[] = '"' . $leadsInfo['name'] . '"转移失败,错误原因:数据出错;';
  307. continue;
  308. }
  309. //修改记录
  310. updateActionLog($userInfo['id'], 'crm_leads', $leads_id, '', '', '将线索转移给:' . $ownerUserName);
  311. RecordActionLog($userInfo['id'], 'crm_leads', 'transfer',$leadsInfo['name'], '','','将线索:'.$leadsInfo['name'].'转移给:' . $ownerUserName);
  312. }
  313. if (!$errorMessage) {
  314. return resultArray(['data' => '转移成功']);
  315. } else {
  316. return resultArray(['error' => $errorMessage]);
  317. }
  318. }
  319. /**
  320. * 线索导入模板
  321. * @param string $save_path 本地保存路径 用于错误数据导出,在 Admin\Model\Excel::importExcel()调用
  322. * @return
  323. * @author Michael_xu
  324. */
  325. public function excelDownload($save_path = '')
  326. {
  327. # 下次升级使用
  328. $param = $this->param;
  329. $userInfo = $this->userInfo;
  330. $excelModel = new \app\admin\model\Excel();
  331. // 导出的字段列表
  332. $fieldModel = new \app\admin\model\Field();
  333. $fieldParam['types'] = 'crm_leads';
  334. $fieldParam['action'] = 'excel';
  335. $field_list = $fieldModel->field($fieldParam);
  336. $field=[1=>[
  337. 'field'=>'owner_user_id',
  338. 'types'=>'crm_leads',
  339. 'name'=>'负责人',
  340. 'form_type'=>'user',
  341. 'default_value'=>'',
  342. 'is_null' => 1,
  343. 'input_tips' =>'',
  344. 'setting' => Array(),
  345. 'is_hidden'=>0,
  346. 'writeStatus' => 1,
  347. 'value' => '']
  348. ];
  349. $first_array = array_splice($field_list, 2,0, $field);
  350. $excelModel->excelImportDownload($field_list, 'crm_leads', $save_path);
  351. }
  352. /**
  353. * 线索导出
  354. * @param
  355. * @return
  356. * @author Michael_xu
  357. */
  358. public function excelExport()
  359. {
  360. $param = $this->param;
  361. $userInfo = $this->userInfo;
  362. $param['user_id'] = $userInfo['id'];
  363. $action_name='导出全部';
  364. if ($param['leads_id']) {
  365. $param['leads_id'] = ['condition' => 'in', 'value' => $param['leads_id'], 'form_type' => 'text', 'name' => ''];
  366. $param['is_excel'] = 1;
  367. $action_name='导出选中';
  368. }
  369. $excelModel = new \app\admin\model\Excel();
  370. // 导出的字段列表
  371. $fieldModel = new \app\admin\model\Field();
  372. $field_list = $fieldModel->getIndexFieldConfig('crm_leads', $userInfo['id'],'','excel');
  373. // 文件名
  374. $file_name = '5kcrm_leads_' . date('Ymd');
  375. $model = model('Leads');
  376. $temp_file = $param['temp_file'];
  377. unset($param['temp_file']);
  378. $page = $param['page'] ?: 1;
  379. unset($param['page']);
  380. unset($param['export_queue_index']);
  381. RecordActionLog($userInfo['id'],'crm_leads','excelexport',$action_name,'','','导出线索');
  382. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $field_list, $param) {
  383. $param['page'] = $page;
  384. $param['limit'] = $limit;
  385. $data = $model->getDataList($param);
  386. $data['list'] = $model->exportHandle($data['list'], $field_list, 'leads');
  387. return $data;
  388. });
  389. }
  390. /**
  391. * 线索数据导入
  392. * @param
  393. * @return
  394. * @author Michael_xu
  395. */
  396. public function excelImport()
  397. {
  398. $param = $this->param;
  399. $userInfo = $this->userInfo;
  400. $excelModel = new \app\admin\model\Excel();
  401. $param['types'] = 'crm_leads';
  402. $param['create_user_id'] = $userInfo['id'];
  403. $file = request()->file('file');
  404. // $res = $excelModel->importExcel($file, $param);
  405. $res = $excelModel->batchImportData($file, $param, $this);
  406. if (!$res) {
  407. return resultArray(['error' => $excelModel->getError()]);
  408. }
  409. RecordActionLog($userInfo['id'],'crm_leads','excel','导入线索','','','导入线索');
  410. return resultArray(['data' => $excelModel->getError()]);
  411. }
  412. /**
  413. * 线索标记为已跟进
  414. * @param
  415. * @return
  416. * @author Michael_xu
  417. */
  418. public function setFollow()
  419. {
  420. $param = $this->param;
  421. $leadsIds = input('post.id/a') ?: [];
  422. if (!$leadsIds || !is_array($leadsIds)) {
  423. return resultArray(['error' => '参数错误']);
  424. }
  425. $data['follow'] = '已跟进';
  426. $data['update_time'] = time();
  427. $res = db('crm_leads')->where(['leads_id' => ['in', $leadsIds]])->update($data);
  428. if (!$res) {
  429. return resultArray(['error' => '操作失败,请重试']);
  430. }
  431. return resultArray(['data' => '跟进成功']);
  432. }
  433. /**
  434. * 设置关注
  435. *
  436. * @return \think\response\Json
  437. * @throws \think\Exception
  438. * @throws \think\exception\PDOException
  439. */
  440. public function star()
  441. {
  442. $userId = $this->userInfo['id'];
  443. $targetId = $this->param['target_id'];
  444. $type = $this->param['type'];
  445. if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']);
  446. if (!$this->setStar($type, $userId, $targetId)) {
  447. return resultArray(['error' => '设置关注失败!']);
  448. }
  449. return resultArray(['data' => '设置关注成功!']);
  450. }
  451. /**
  452. * 系统信息
  453. *
  454. * @return \think\response\Json
  455. * @throws \think\db\exception\DataNotFoundException
  456. * @throws \think\db\exception\ModelNotFoundException
  457. * @throws \think\exception\DbException
  458. */
  459. public function system()
  460. {
  461. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  462. $leadsModel = new \app\crm\model\Leads();
  463. $data = $leadsModel->getSystemInfo($this->param['id']);
  464. return resultArray(['data' => $data]);
  465. }
  466. /**
  467. * table标签栏数量
  468. *
  469. * @return \think\response\Json
  470. * @throws \think\Exception
  471. */
  472. public function count()
  473. {
  474. if (empty($this->param['leads_id'])) return resultArray(['error' => '参数错误!']);
  475. # 附件
  476. $fileCount = Db::name('crm_leads_file')->alias('leads')->join('__ADMIN_FILE__ file', 'file.file_id = leads.file_id')->where('leads_id', $this->param['leads_id'])->count();
  477. return resultArray(['data' => ['fileCount' => $fileCount]]);
  478. }
  479. }