123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. * @author fanqi
  55. * @since 2021-04-14
  56. * @return Json
  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. * @author fanqi
  68. * @since 2021-04-14
  69. * @return Json
  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. * @author fanqi
  84. * @since 2021-04-15
  85. * @return Json
  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. * @author fanqi
  99. * @since 2021-04-13
  100. * @return Json
  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. * @author fanqi
  111. * @since 2021-04-13
  112. * @return Json
  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. * @author fanqi
  127. * @since 2021-04-14
  128. * @return Json
  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. * @author fanqi
  140. * @since 2021-04-15
  141. * @return Json
  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. * @author fanqi
  156. * @since 2021-04-15
  157. * @return Json
  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_customer', $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['owner_user_id'] = $userInfo['id'];
  183. $param['deal_time'] = time();
  184. $param['types'] = 'crm_customer';
  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. $fieldModel = new \app\admin\model\Field();
  204. $field_list=$this->fieldsData($param);
  205. $field=[
  206. 1=>[
  207. 'field'=>'before_owner_user_name',
  208. 'types'=>'crm_customer',
  209. 'name'=>'前负责人',
  210. ],
  211. 2=>[
  212. 'field'=>'into_pool_time',
  213. 'types'=>'crm_customer',
  214. 'name'=>'进入公海时间',
  215. ]
  216. ];
  217. $field_list=array_merge($field_list,$field);
  218. // 文件名
  219. $file_name = '5kcrm_customer_' . date('Ymd');
  220. $model = model('Customer');
  221. $temp_file = $param['temp_file'];
  222. unset($param['temp_file']);
  223. $page = $param['page'] ?: 1;
  224. unset($param['page']);
  225. unset($param['export_queue_index']);
  226. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list) {
  227. $param['page'] = $page;
  228. $param['limit'] = $limit;
  229. $data = (new CustomerPoolLogic())->getPoolList($param);
  230. $data['list'] = $model->exportHandle($data['list'], $field_list, 'customer');
  231. return $data;
  232. });
  233. RecordActionLog($userInfo['id'],'crm_customer','excelexport',$action_name,'','','导出客户');
  234. return resultArray(['error' => 'error']);
  235. }
  236. /**
  237. * 公海权限
  238. *
  239. * @author fanqi
  240. * @since 2021-04-14
  241. * @return Json
  242. */
  243. public function authority()
  244. {
  245. $param = $this->param;
  246. $param['user_id'] = $this->userInfo['id'];
  247. $param['structure_id'] = $this->userInfo['structure_id'];
  248. $data = (new CustomerPoolLogic())->getAuthorityData($param);
  249. return resultArray(['data' => $data]);
  250. }
  251. /**
  252. * 公海字段样式
  253. *
  254. * @author fanqi
  255. * @since 2021-04-22
  256. * @return Json
  257. */
  258. public function fieldConfig()
  259. {
  260. if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID!']);
  261. $param = $this->param;
  262. $userInfo = $this->userInfo;
  263. $param['user_id'] = $userInfo['id'];
  264. $data = (new CustomerPoolLogic())->getFieldConfigIndex($param);
  265. return resultArray(['data' => $data]);
  266. }
  267. /**
  268. * 设置公海字段宽度
  269. *
  270. * @author fanqi
  271. * @since 2021-04-22
  272. * @return Json
  273. */
  274. public function setFieldWidth()
  275. {
  276. if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID!']);
  277. if (empty($this->param['field'])) return resultArray(['error' => '缺少字段名称!']);
  278. if (empty($this->param['width'])) return resultArray(['error' => '缺少宽度值!']);
  279. $param = $this->param;
  280. $userInfo = $this->userInfo;
  281. $param['user_id'] = $userInfo['id'];
  282. (new CustomerPoolLogic())->setFieldWidth($param);
  283. return resultArray(['data' => '操作成功!']);
  284. }
  285. /**
  286. * 设置公海字段样式
  287. *
  288. * @author fanqi
  289. * @since 2021-04-22
  290. * @return Json
  291. */
  292. public function setFieldConfig()
  293. {
  294. if (empty($this->param['pool_id'])) return resultArray(['缺少公海ID!']);
  295. if (empty($this->param['value']) && empty($this->param['hide_value'])) return resultArray(['error' => '字段参数错误!']);
  296. $param = $this->param;
  297. $userInfo = $this->userInfo;
  298. $param['user_id'] = $userInfo['id'];
  299. (new CustomerPoolLogic())->setFieldConfig($param);
  300. return resultArray(['data' => '操作成功!']);
  301. }
  302. public function fieldsData($param){
  303. $pool_list=db('crm_customer_pool_field_setting')->where(['pool_id'=>$param['pool_id'],'is_hidden'=>0])->select();
  304. $fieldModel = new \app\admin\model\Field();
  305. $fieldParam['types'] = 'crm_customer';
  306. $fieldParam['action'] = 'excel';
  307. $merge_list = $fieldModel->field($fieldParam);
  308. $field_list=array_intersect($merge_list,$pool_list);
  309. return $field_list;
  310. }
  311. }