CustomerPool.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. /**
  3. * 客户公海
  4. *
  5. * @author fanqi
  6. * @since 2021-04-13
  7. */
  8. namespace app\crm\controller;
  9. use app\admin\controller\ApiCommon;
  10. use app\crm\logic\CustomerPoolLogic;
  11. use think\Hook;
  12. use think\Request;
  13. use think\response\Json;
  14. class CustomerPool extends ApiCommon
  15. {
  16. /**
  17. * 用于判断权限
  18. * @permission 无限制
  19. * @allow 登录用户可访问
  20. * @other 其他根据系统设置
  21. **/
  22. public function _initialize()
  23. {
  24. $action = [
  25. 'permission' => [],
  26. 'allow' => [
  27. 'index',
  28. 'read',
  29. 'pondlist',
  30. 'field',
  31. 'advanced',
  32. 'authority',
  33. 'receive',
  34. 'distribute',
  35. 'delete',
  36. 'fieldconfig',
  37. 'setfieldwidth',
  38. 'setfieldconfig',
  39. 'exceldownload',
  40. 'import',
  41. 'export'
  42. ]
  43. ];
  44. Hook::listen('check_auth', $action);
  45. $request = Request::instance();
  46. $a = strtolower($request->action());
  47. if (!in_array($a, $action['permission'])) {
  48. parent::_initialize();
  49. }
  50. }
  51. /**
  52. * 公海列表
  53. *
  54. * @return Json
  55. * @since 2021-04-14
  56. * @author fanqi
  57. */
  58. public function index()
  59. {
  60. if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID']);
  61. $data = (new CustomerPoolLogic())->getPoolList($this->param);
  62. return resultArray(['data' => $data]);
  63. }
  64. /**
  65. * 详情
  66. *
  67. * @return Json
  68. * @since 2021-04-14
  69. * @author fanqi
  70. */
  71. public function read()
  72. {
  73. if (empty($this->param['pool_id']) || empty($this->param['customer_id'])) return resultArray(['error' => '参数错误!']);
  74. $userInfo = $this->userInfo;
  75. $param = $this->param;
  76. $param['user_id'] = $userInfo['id'];
  77. $data = (new CustomerPoolLogic())->getPoolData($param);
  78. return resultArray(['data' => $data]);
  79. }
  80. /**
  81. * 删除公海客户
  82. *
  83. * @return Json
  84. * @since 2021-04-15
  85. * @author fanqi
  86. */
  87. public function delete()
  88. {
  89. if (empty($this->param['id'])) return resultArray(['error' => '请选择要删除的客户!']);
  90. $this->param['user_id'] = $this->userInfo['id'];
  91. $result = (new CustomerPoolLogic())->deletePoolCustomer($this->param);
  92. if (!empty($result)) return resultArray(['error' => $result]);
  93. return resultArray(['data' => '删除成功!']);
  94. }
  95. /**
  96. * 公海池列表
  97. *
  98. * @return Json
  99. * @since 2021-04-13
  100. * @author fanqi
  101. */
  102. public function pondList()
  103. {
  104. $data = (new CustomerPoolLogic())->getPondList(['user_id' => $this->userInfo['id'], 'structure_id' => $this->userInfo['structure_id']]);
  105. return resultArray(['data' => $data]);
  106. }
  107. /**
  108. * 公海字段
  109. *
  110. * @return Json
  111. * @since 2021-04-13
  112. * @author fanqi
  113. */
  114. public function field()
  115. {
  116. if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID!']);
  117. $userInfo = $this->userInfo;
  118. $param = $this->param;
  119. $param['user_id'] = $userInfo['id'];
  120. $data = (new CustomerPoolLogic())->getFieldList($param);
  121. return resultArray(['data' => $data]);
  122. }
  123. /**
  124. * 高级筛选字段列表
  125. *
  126. * @return Json
  127. * @since 2021-04-14
  128. * @author fanqi
  129. */
  130. public function advanced()
  131. {
  132. if (empty($this->param['types'])) return resultArray(['error' => '缺少模块类型!']);
  133. $data = (new CustomerPoolLogic())->getAdvancedFilterFieldList($this->param);
  134. return resultArray(['data' => $data]);
  135. }
  136. /**
  137. * 领取公海池客户
  138. *
  139. * @return Json
  140. * @since 2021-04-15
  141. * @author fanqi
  142. */
  143. public function receive()
  144. {
  145. if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择要领取的公海客户!']);
  146. $param = $this->param;
  147. $param['user_id'] = $this->userInfo['id'];
  148. $result = (new CustomerPoolLogic())->receiveCustomers($param);
  149. if (!empty($result)) return resultArray(['error' => $result]);
  150. return resultArray(['data' => '领取成功!']);
  151. }
  152. /**
  153. * 分配公海客户
  154. *
  155. * @return Json
  156. * @since 2021-04-15
  157. * @author fanqi
  158. */
  159. public function distribute()
  160. {
  161. if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择要分配的公海客户!']);
  162. if (empty($this->param['user_id'])) return resultArray(['error' => '请选择要分配的员工!']);
  163. $result = (new CustomerPoolLogic())->distributeCustomer($this->param);
  164. if (!empty($result)) return resultArray(['error' => $result]);
  165. return resultArray(['data' => '分配成功!']);
  166. }
  167. // 公海客户导入模板下载
  168. public function excelDownload($save_path = '')
  169. {
  170. $excelModel = new \app\admin\model\Excel();
  171. $param = $this->param;
  172. $field_list = $this->fieldsData($param);
  173. $excelModel->excelImportDownload($field_list, 'crm_pool', $save_path);
  174. }
  175. // 导入
  176. public function import()
  177. {
  178. $param = $this->param;
  179. $userInfo = $this->userInfo;
  180. $excelModel = new \app\admin\model\Excel();
  181. $param['create_user_id'] = $userInfo['id'];
  182. $param['deal_time'] = time();
  183. $param['types'] = 'crm_customer';
  184. $param['deal_status'] = '未成交';
  185. $file = request()->file('file');
  186. // $res = $excelModel->importExcel($file, $param, $this);
  187. $res = $excelModel->batchImportData($file, $param, $this);
  188. RecordActionLog($userInfo['id'], 'crm_customer', 'excel', '导入公海客户', '', '', '导入公海客户');
  189. return resultArray(['data' => $excelModel->getError()]);
  190. }
  191. // 导出
  192. public function export()
  193. {
  194. $param = $this->param;
  195. $userInfo = $this->userInfo;
  196. $action_name = '导出全部';
  197. if ($param['customer_id']) {
  198. $action_name = '导出选中';
  199. }
  200. $param['is_excel'] = 1;
  201. $excelModel = new \app\admin\model\Excel();
  202. // 导出的字段列表
  203. $field_list = $this->fieldsData($param);
  204. // 文件名
  205. $file_name = '5kcrm_customer_' . date('Ymd');
  206. $model = model('Customer');
  207. $temp_file = $param['temp_file'];
  208. unset($param['temp_file']);
  209. $page = $param['page'] ?: 1;
  210. unset($param['page']);
  211. unset($param['export_queue_index']);
  212. // p($field_list);
  213. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list) {
  214. $param['page'] = $page;
  215. $param['limit'] = $limit;
  216. $data = (new CustomerPoolLogic())->getPoolList($param);
  217. $data['list'] = $model->exportHandle($data['list'], $field_list, 'customer');
  218. return $data;
  219. });
  220. RecordActionLog($userInfo['id'], 'crm_customer', 'excelexport', $action_name, '', '', '导出客户');
  221. return resultArray(['error' => 'error']);
  222. }
  223. /**
  224. * 公海权限
  225. *
  226. * @return Json
  227. * @since 2021-04-14
  228. * @author fanqi
  229. */
  230. public function authority()
  231. {
  232. $param = $this->param;
  233. $param['user_id'] = $this->userInfo['id'];
  234. $param['structure_id'] = $this->userInfo['structure_id'];
  235. $data = (new CustomerPoolLogic())->getAuthorityData($param);
  236. return resultArray(['data' => $data]);
  237. }
  238. /**
  239. * 公海字段样式
  240. *
  241. * @return Json
  242. * @since 2021-04-22
  243. * @author fanqi
  244. */
  245. public function fieldConfig()
  246. {
  247. if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID!']);
  248. $param = $this->param;
  249. $userInfo = $this->userInfo;
  250. $param['user_id'] = $userInfo['id'];
  251. $data = (new CustomerPoolLogic())->getFieldConfigIndex($param);
  252. return resultArray(['data' => $data]);
  253. }
  254. /**
  255. * 设置公海字段宽度
  256. *
  257. * @return Json
  258. * @since 2021-04-22
  259. * @author fanqi
  260. */
  261. public function setFieldWidth()
  262. {
  263. if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID!']);
  264. if (empty($this->param['field'])) return resultArray(['error' => '缺少字段名称!']);
  265. if (empty($this->param['width'])) return resultArray(['error' => '缺少宽度值!']);
  266. $param = $this->param;
  267. $userInfo = $this->userInfo;
  268. $param['user_id'] = $userInfo['id'];
  269. (new CustomerPoolLogic())->setFieldWidth($param);
  270. return resultArray(['data' => '操作成功!']);
  271. }
  272. /**
  273. * 设置公海字段样式
  274. *
  275. * @return Json
  276. * @since 2021-04-22
  277. * @author fanqi
  278. */
  279. public function setFieldConfig()
  280. {
  281. if (empty($this->param['pool_id'])) return resultArray(['缺少公海ID!']);
  282. if (empty($this->param['value']) && empty($this->param['hide_value'])) return resultArray(['error' => '字段参数错误!']);
  283. $param = $this->param;
  284. $userInfo = $this->userInfo;
  285. $param['user_id'] = $userInfo['id'];
  286. (new CustomerPoolLogic())->setFieldConfig($param);
  287. return resultArray(['data' => '操作成功!']);
  288. }
  289. /**
  290. * 导入模板
  291. * @param $param
  292. *
  293. * @author alvin guogaobo
  294. * @version 1.0 版本号
  295. * @since 2021/5/17 0017 11:34
  296. */
  297. public function fieldsData($param)
  298. {
  299. $where=['pool_id' => $param['pool_id'], 'is_hidden' => 0,
  300. 'field_name' => ['not in', ['deal_status','create_user_id']]];
  301. if($param['is_excel']==1){
  302. $where=['pool_id' => $param['pool_id'], 'is_hidden' => 0,
  303. 'field_name' => ['not in', ['deal_status']]];
  304. }
  305. $pool_list = db('crm_customer_pool_field_setting')->where($where)->field('field_name as field,form_type,name')->select();
  306. $fieldParam['types'] = 'crm_customer';
  307. $fieldParam['action'] = 'excel';
  308. $setting = [];
  309. $list=[];
  310. foreach ($pool_list as $k => &$v) {
  311. switch ($v['field']) {
  312. case 'address':
  313. if($param['is_excel']!=1){
  314. $v['field'] = 'customer_address';
  315. $v['form_type'] = 'map_address';
  316. $list[]=$v;
  317. unset($pool_list[$k]);
  318. }
  319. break;
  320. case 'detail_address':
  321. if($param['is_excel']!=1){
  322. unset($pool_list[$k]);
  323. }
  324. break;
  325. case ($v['form_type'] == 'select') || ($v['form_type'] == 'checkbox') :
  326. $set= db('admin_field')->where(['types' => 'crm_customer', 'field' => $v['field']])->find();
  327. $pool_list[$k]['setting'] = explode(chr(10), $set['setting']);
  328. break;
  329. default :
  330. if($param['is_excel']!=1){
  331. $pool_list[$k]['setting'] = [];
  332. }
  333. break;
  334. }
  335. // if($param['is_excel']==1){
  336. // foreach ( $pool_list as &$v){
  337. // if($v['field']=='create_user_id'){
  338. // $v['field'] = 'create_user_name';
  339. // }elseif($v['field']=='before_owner_user_id'){
  340. // $v['field'] = 'create_user_name';
  341. // }
  342. // }
  343. // }
  344. }
  345. $pool_list=array_merge($pool_list,$list);
  346. return $pool_list;
  347. }
  348. }