123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. namespace app\admin\logic;
  3. use PDOStatement;
  4. use think\Collection;
  5. use think\Db;
  6. class PoolConfigLogic
  7. {
  8. public $error = '操作失败!';
  9. /**
  10. * 公海配置列表
  11. *
  12. * @param array $param page 页码,limit 每页条数
  13. * @author fanqi
  14. * @since 2021-03-30
  15. * @return array
  16. */
  17. public function getPoolList($param)
  18. {
  19. $page = !empty($param['page']) ? $param['page'] : 1;
  20. $limit = !empty($param['limit']) ? $param['limit'] : 15;
  21. $count = db('crm_customer_pool')->count();
  22. # 公海列表
  23. $list = db('crm_customer_pool')->field([
  24. 'pool_id', 'pool_name', 'admin_user_ids', 'user_ids', 'department_ids', 'status'
  25. ])->limit(($page - 1) * $limit, $limit)->select();
  26. # 统计公海下的客户数量
  27. $customerData = [];
  28. $customerList = db('crm_customer_pool_relation')->field(['pool_id', 'count(customer_id) AS customer_count'])->group('pool_id')->select();
  29. foreach ($customerList AS $key => $value) {
  30. $customerData[$value['pool_id']] = $value['customer_count'];
  31. }
  32. foreach ($list AS $key => $value) {
  33. # 公海管理员
  34. $adminUserIds = trim($value['admin_user_ids'], ',');
  35. $adminUserNames = db('admin_user')->whereIn('id', $adminUserIds)->column('realname');
  36. # 公海成员
  37. $userIds = trim($value['user_ids'], ',');
  38. $userNames = db('admin_user')->whereIn('id', $userIds)->column('realname');
  39. # 部门
  40. $structureIds = trim($value['department_ids'], ',');
  41. $structureNames = db('admin_structure')->whereIn('id', $structureIds)->column('name');
  42. # 公海成员
  43. $poolMembers = array_merge($structureNames, $userNames);
  44. $list[$key]['admin_user_names'] = implode(',', $adminUserNames);
  45. $list[$key]['user_names'] = implode(',', array_unique($poolMembers));
  46. $list[$key]['customer_count'] = !empty($customerData[$value['pool_id']]) ? $customerData[$value['pool_id']] : 0;
  47. }
  48. return ['count' => $count, 'list' => !empty($list) ? $list : []];
  49. }
  50. /**
  51. * 设置多公海配置
  52. *
  53. * @param $param
  54. * @author fanqi
  55. * @since 2021-03-30
  56. * @return bool
  57. */
  58. public function setPoolConfig($param)
  59. {
  60. if (empty($param['pool_name'])) {
  61. $this->error = '请填写公海名称!';
  62. return false;
  63. }
  64. if (!empty($param['pool_name']) && mb_strlen($param['pool_name']) > 100) {
  65. $this->error = '公海名称最多只能输入100个字符!';
  66. return false;
  67. }
  68. if (empty($param['admin_user_ids'])) {
  69. $this->error = '请选择公海管理员!';
  70. return false;
  71. }
  72. if (empty($param['user_ids']) && empty($param['department_ids'])) {
  73. $this->error = '请选择公海成员!';
  74. return false;
  75. }
  76. if (!empty($param['recycle_conf']) && empty($param['rule'])) {
  77. $this->error = '请设置回收规则!';
  78. return false;
  79. }
  80. $repeatWhere['pool_name'] = $param['pool_name'];
  81. if (!empty($param['pool_id'])) $repeatWhere['pool_id'] = ['neq', $param['pool_id']];
  82. if (db('crm_customer_pool')->where($repeatWhere)->value('pool_id')) {
  83. $this->error = '公海名称重复';
  84. return false;
  85. }
  86. $poolData = [
  87. 'pool_name' => $param['pool_name'],
  88. 'admin_user_ids' => ',' . $param['admin_user_ids'] . ',',
  89. 'user_ids' => ',' . $param['user_ids'] . ',',
  90. 'department_ids' => !empty($param['department_ids']) ? ',' . $param['department_ids'] . ',' : '',
  91. 'status' => 1,
  92. 'before_owner_conf' => $param['before_owner_conf'],
  93. 'before_owner_day' => $param['before_owner_day'],
  94. 'receive_conf' => $param['receive_conf'],
  95. 'receive_count' => $param['receive_count'],
  96. 'remind_conf' => $param['remind_conf'],
  97. 'remain_day' => $param['remain_day'],
  98. 'recycle_conf' => $param['recycle_conf'],
  99. 'create_user_id' => $param['user_id'],
  100. 'create_time' => time()
  101. ];
  102. Db::startTrans();
  103. try {
  104. if (!empty($param['pool_id'])) {
  105. # 编辑
  106. $poolId = $param['pool_id'];
  107. Db::name('crm_customer_pool')->where('pool_id', $poolId)->update($poolData);
  108. } else {
  109. # 创建
  110. $poolId = Db::name('crm_customer_pool')->insert($poolData, false, true);
  111. }
  112. # 公海字段
  113. $fieldData = $this->getPoolField($param['field'], $poolId);
  114. if (!empty($fieldData)) {
  115. Db::name('crm_customer_pool_field_setting')->where('pool_id', $poolId)->delete();
  116. Db::name('crm_customer_pool_field_setting')->insertAll($fieldData);
  117. }
  118. # 公海规则
  119. $ruleData = $this->getPoolRule($param['rule'], $poolId);
  120. if (!empty($ruleData)) {
  121. Db::name('crm_customer_pool_rule')->where('pool_id', $poolId)->delete();
  122. Db::name('crm_customer_pool_rule')->insertAll($ruleData);
  123. }
  124. Db::commit();
  125. return true;
  126. } catch (\Exception $e) {
  127. Db::rollback();
  128. $this->error = '创建公海失败!';
  129. return false;
  130. }
  131. }
  132. /**
  133. * 公海配置详情
  134. *
  135. * @param int $poolId 公海ID
  136. * @author fanqi
  137. * @since 2021-03-30
  138. * @return array|bool
  139. */
  140. public function readPool($poolId)
  141. {
  142. $data = db('crm_customer_pool')->where('pool_id', $poolId)->find();
  143. if (empty($data['pool_id'])) {
  144. $this->error = '没有查询到数据!';
  145. return false;
  146. }
  147. # 时间格式
  148. $data['create_time'] = date('Y-m-d H:i:s', $data['create_time']);
  149. # 公海管理员
  150. $adminUserIds = trim($data['admin_user_ids'], ',');
  151. $data['admin_user_ids'] = $adminUserIds;
  152. $data['admin_user_info'] = db('admin_user')->field(['id', 'realname', 'thumb_img'])->whereIn('id', $adminUserIds)->select();
  153. foreach ($data['admin_user_info'] AS $key => $value) {
  154. $data['admin_user_info'][$key]['thumb_img'] = getFullPath($value['thumb_img']);
  155. }
  156. # 公海成员
  157. $userIds = trim($data['user_ids'], ',');
  158. $data['user_ids'] = $userIds;
  159. $data['user_info'] = db('admin_user')->field(['id', 'realname', 'thumb_img'])->whereIn('id', $userIds)->select();
  160. foreach ($data['user_info'] AS $key => $value) {
  161. $data['user_info'][$key]['thumb_img'] = getFullPath($value['thumb_img']);
  162. }
  163. # 公海部门
  164. $departmentIds = trim($data['department_ids'], ',');
  165. $data['department_ids'] = $departmentIds;
  166. $data['department_info'] = db('admin_structure')->field(['id', 'name'])->whereIn('id', $departmentIds)->select();
  167. # 公海字段
  168. $data['field'] = db('crm_customer_pool_field_setting')->where('pool_id', $data['pool_id'])->select();
  169. # 公海规则
  170. $data['rule'] = db('crm_customer_pool_rule')->where('pool_id', $data['pool_id'])->select();
  171. foreach ($data['rule'] AS $key => $value) {
  172. if (!empty($value['level'])) {
  173. $data['rule'][$key]['level'] = json_decode($value['level'], true);
  174. $data['rule'][$key]['level_setting'] = json_decode($value['level'], true);
  175. }
  176. }
  177. # 客户数量
  178. $data['customer_count'] = db('crm_customer_pool_relation')->where('pool_id', $poolId)->count();
  179. return $data;
  180. }
  181. /**
  182. * 变更公海配置状态
  183. *
  184. * @param array $param pool_id 公海ID, status 状态(1启用、0停用)
  185. * @author fanqi
  186. * @since 2021-03-30
  187. * @return false|int|string
  188. */
  189. public function changePoolStatus($param)
  190. {
  191. $poolId = $param['pool_id'];
  192. $status = $param['status'];
  193. if ($status == 0 && db('crm_customer_pool_relation')->where('pool_id', $poolId)->count() > 0) {
  194. $this->error = '公海内有客户,不能停用!';
  195. return false;
  196. }
  197. if ($status == 0 && db('crm_customer_pool')->where(['pool_id' => ['neq', $poolId], 'status' => 1])->count() < 1) {
  198. $this->error = '至少要开启一个公海!';
  199. return false;
  200. }
  201. return db('crm_customer_pool')->where('pool_id', $poolId)->update(['status' => $status]);
  202. }
  203. /**
  204. * 删除公海配置
  205. *
  206. * @param int $poolId 公海ID
  207. * @author fanqi
  208. * @since 2021-03-30
  209. * @return bool
  210. */
  211. public function deletePool($poolId)
  212. {
  213. if (db('crm_customer_pool_relation')->where('pool_id', $poolId)->count() > 0) {
  214. $this->error = '公海内有客户,不能删除!';
  215. return false;
  216. }
  217. if (db('crm_customer_pool')->where(['pool_id' => ['neq', $poolId], 'status' => 1])->count() < 1) {
  218. $this->error = '至少要保留一个开启的公海!';
  219. return false;
  220. }
  221. Db::startTrans();
  222. try {
  223. # 删除公海规则数据
  224. Db::name('crm_customer_pool_rule')->where('pool_id', $poolId)->delete();
  225. # 删除公海字段数据
  226. Db::name('crm_customer_pool_field_setting')->where('pool_id', $poolId)->delete();
  227. # 删除用户保存的公海字段数据
  228. Db::name('crm_customer_pool_field_style')->where('pool_id', $poolId)->delete();
  229. # 删除公海操作记录数据
  230. Db::name('crm_customer_pool_record')->where('pool_id', $poolId)->delete();
  231. # 删除公海数据
  232. Db::name('crm_customer_pool')->where('pool_id', $poolId)->delete();
  233. Db::commit();
  234. return true;
  235. } catch (\Exception $e) {
  236. Db::rollback();
  237. $this->error = '删除公海配置失败!';
  238. return false;
  239. }
  240. }
  241. /**
  242. * 转移公海客户
  243. *
  244. * @param array $param source_pool_id 源公海ID,target_pool_id 目标公海ID
  245. * @author fanqi
  246. * @since 2021-03-30
  247. * @return bool
  248. */
  249. public function transferPool($param)
  250. {
  251. if (empty($param['source_pool_id']) || empty($param['target_pool_id'])) {
  252. $this->error = '缺少源ID或目标ID';
  253. return false;
  254. }
  255. # 源
  256. $sourceCustomerIds = Db::name('crm_customer_pool_relation')->where('pool_id', $param['source_pool_id'])->column('customer_id');
  257. # 目标
  258. $targetCustomerIds = Db::name('crm_customer_pool_relation')->where('pool_id', $param['target_pool_id'])->column('customer_id');
  259. # 差异
  260. $diffCustomerIds = array_diff($sourceCustomerIds, $targetCustomerIds);
  261. $data = [];
  262. foreach ($diffCustomerIds AS $key => $value) {
  263. $data[] = [
  264. 'customer_id' => $value,
  265. 'pool_id' => $param['target_pool_id']
  266. ];
  267. }
  268. Db::startTrans();
  269. try {
  270. Db::name('crm_customer_pool_relation')->where('pool_id', $param['source_pool_id'])->delete();
  271. if (!empty($data)) Db::name('crm_customer_pool_relation')->insertAll($data);
  272. Db::commit();
  273. return true;
  274. } catch (\Exception $e) {
  275. Db::rollback();
  276. $this->error = '转移失败!';
  277. return false;
  278. }
  279. }
  280. /**
  281. * 获取客户级别列表
  282. *
  283. * @author fanqi
  284. * @since 2021-04-22
  285. * @return array
  286. */
  287. public function getCustomerLevel()
  288. {
  289. $setting = db('admin_field')->where(['types' => 'crm_customer', 'field' => 'level'])->value('setting');
  290. $data = explode(chr(10), $setting);
  291. return !empty($data) ? $data : [];
  292. }
  293. /**
  294. * 获取公海字段列表
  295. *
  296. * @param array $param pool_id 公海ID
  297. * @author fanqi
  298. * @since 2021-04-29
  299. * @return bool|PDOStatement|string|Collection
  300. */
  301. public function getPoolFieldList($param)
  302. {
  303. if (!empty($param['pool_id'])) {
  304. return db('crm_customer_pool_field_setting')->field(['field_name AS field', 'name', 'form_type', 'is_hidden', 'is_null', 'is_unique'])->where('pool_id', $param['pool_id'])->select();
  305. } else {
  306. $data = db('admin_field')->field(['field', 'name', 'form_type', 'is_hidden', 'is_null', 'is_unique'])->where(['types' => 'crm_customer'])->select();
  307. $address = [
  308. 'field' => 'address',
  309. 'name' => '省、市、区/县',
  310. 'form_type' => 'customer_address',
  311. 'is_hidden' => 0,
  312. 'is_null' => 0,
  313. 'is_unique' => 0,
  314. ];
  315. $detailAddress = [
  316. 'field' => 'detail_address',
  317. 'name' => '详细地址',
  318. 'form_type' => 'text',
  319. 'is_hidden' => 0,
  320. 'is_null' => 0,
  321. 'is_unique' => 0,
  322. ];
  323. $lastRecord = [
  324. 'field' => 'last_record',
  325. 'name' => '最后跟进记录',
  326. 'form_type' => 'text',
  327. 'is_hidden' => 0,
  328. 'is_null' => 0,
  329. 'is_unique' => 0,
  330. ];
  331. $lastTime = [
  332. 'field' => 'last_time',
  333. 'name' => '最后跟进时间',
  334. 'form_type' => 'datetime',
  335. 'is_hidden' => 0,
  336. 'is_null' => 0,
  337. 'is_unique' => 0,
  338. ];
  339. $beforeOwnerUser = [
  340. 'field' => 'before_owner_user_id',
  341. 'name' => '前负责人',
  342. 'form_type' => 'user',
  343. 'is_hidden' => 0,
  344. 'is_null' => 0,
  345. 'is_unique' => 0,
  346. ];
  347. $intoPoolTime = [
  348. 'field' => 'into_pool_time',
  349. 'name' => '进入公海时间',
  350. 'form_type' => 'datetime',
  351. 'is_hidden' => 0,
  352. 'is_null' => 0,
  353. 'is_unique' => 0,
  354. ];
  355. $createTime = [
  356. 'field' => 'create_time',
  357. 'name' => '创建时间',
  358. 'form_type' => 'datetime',
  359. 'is_hidden' => 0,
  360. 'is_null' => 0,
  361. 'is_unique' => 0,
  362. ];
  363. $updateTime = [
  364. 'field' => 'update_time',
  365. 'name' => '更新时间',
  366. 'form_type' => 'datetime',
  367. 'is_hidden' => 0,
  368. 'is_null' => 0,
  369. 'is_unique' => 0,
  370. ];
  371. $createUser = [
  372. 'field' => 'create_user_id',
  373. 'name' => '创建人',
  374. 'form_type' => 'user',
  375. 'is_hidden' => 0,
  376. 'is_null' => 0,
  377. 'is_unique' => 0,
  378. ];
  379. array_push($data, $address, $detailAddress, $lastRecord, $lastTime, $createTime, $updateTime, $createUser, $beforeOwnerUser, $intoPoolTime);
  380. return $data;
  381. }
  382. }
  383. /**
  384. * 处理公海规则数据
  385. *
  386. * @param array $rules 规则数据
  387. * @param int $poolId 公海ID
  388. * @author fanqi
  389. * @since 2021-03-30
  390. * @return array
  391. */
  392. private function getPoolRule($rules, $poolId)
  393. {
  394. $result = [];
  395. foreach ($rules AS $key => $value) {
  396. $result[] = [
  397. 'pool_id' => $poolId,
  398. 'type' => $value['type'],
  399. 'deal_handle' => $value['deal_handle'],
  400. 'business_handle' => $value['business_handle'],
  401. 'level_conf' => $value['level_conf'],
  402. 'level' => json_encode($value['level']),
  403. 'limit_day' => !empty($value['limit_day']) ? $value['limit_day'] : 0
  404. ];
  405. }
  406. return $result;
  407. }
  408. /**
  409. * 处理公海字段数据
  410. *
  411. * @param array $fields 字段列表
  412. * @param int $poolId 公海ID
  413. * @author fanqi
  414. * @since 2021-03-30
  415. * @return array
  416. */
  417. private function getPoolField($fields, $poolId)
  418. {
  419. $result = [];
  420. foreach ($fields AS $key => $value) {
  421. $result[] = [
  422. 'pool_id' => $poolId,
  423. 'name' => $value['name'],
  424. 'field_name' => $value['field'],
  425. 'form_type' => $value['form_type'],
  426. 'is_hidden' => $value['is_hidden'],
  427. 'is_null' => $value['is_null'],
  428. 'is_unique' => $value['is_unique']
  429. ];
  430. }
  431. return $result;
  432. }
  433. }