PoolConfigLogic.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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')->where('pool_id', $poolId)->delete();
  231. Db::commit();
  232. return true;
  233. } catch (\Exception $e) {
  234. Db::rollback();
  235. $this->error = '删除公海配置失败!';
  236. return false;
  237. }
  238. }
  239. /**
  240. * 转移公海客户
  241. *
  242. * @param array $param source_pool_id 源公海ID,target_pool_id 目标公海ID
  243. * @author fanqi
  244. * @since 2021-03-30
  245. * @return bool
  246. */
  247. public function transferPool($param)
  248. {
  249. if (empty($param['source_pool_id']) || empty($param['target_pool_id'])) {
  250. $this->error = '缺少源ID或目标ID';
  251. return false;
  252. }
  253. # 源
  254. $sourceCustomerIds = Db::name('crm_customer_pool_relation')->where('pool_id', $param['source_pool_id'])->column('customer_id');
  255. # 目标
  256. $targetCustomerIds = Db::name('crm_customer_pool_relation')->where('pool_id', $param['target_pool_id'])->column('customer_id');
  257. # 差异
  258. $diffCustomerIds = array_diff($sourceCustomerIds, $targetCustomerIds);
  259. $data = [];
  260. foreach ($diffCustomerIds AS $key => $value) {
  261. $data[] = [
  262. 'customer_id' => $value,
  263. 'pool_id' => $param['target_pool_id']
  264. ];
  265. }
  266. Db::startTrans();
  267. try {
  268. Db::name('crm_customer_pool_relation')->where('pool_id', $param['source_pool_id'])->delete();
  269. if (!empty($data)) Db::name('crm_customer_pool_relation')->insertAll($data);
  270. Db::commit();
  271. return true;
  272. } catch (\Exception $e) {
  273. Db::rollback();
  274. $this->error = '转移失败!';
  275. return false;
  276. }
  277. }
  278. /**
  279. * 获取客户级别列表
  280. *
  281. * @author fanqi
  282. * @since 2021-04-22
  283. * @return array
  284. */
  285. public function getCustomerLevel()
  286. {
  287. $setting = db('admin_field')->where(['types' => 'crm_customer', 'field' => 'level'])->value('setting');
  288. $data = explode(chr(10), $setting);
  289. return !empty($data) ? $data : [];
  290. }
  291. /**
  292. * 获取公海字段列表
  293. *
  294. * @param array $param pool_id 公海ID
  295. * @author fanqi
  296. * @since 2021-04-29
  297. * @return bool|PDOStatement|string|Collection
  298. */
  299. public function getPoolFieldList($param)
  300. {
  301. if (!empty($param['pool_id'])) {
  302. return db('crm_customer_pool_field_setting')->field(['field_name AS field', 'name', 'form_type', 'is_hidden'])->where('pool_id', $param['pool_id'])->select();
  303. } else {
  304. $data = db('admin_field')->field(['field', 'name', 'form_type', 'is_hidden'])->where(['types' => 'crm_customer'])->select();
  305. $address = [
  306. 'field' => 'address',
  307. 'name' => '省、市、区/县',
  308. 'form_type' => 'customer_address',
  309. 'is_hidden' => 0
  310. ];
  311. $detailAddress = [
  312. 'field' => 'detail_address',
  313. 'name' => '详细地址',
  314. 'form_type' => 'text',
  315. 'is_hidden' => 0
  316. ];
  317. $lastRecord = [
  318. 'field' => 'last_record',
  319. 'name' => '最后跟进记录',
  320. 'form_type' => 'text',
  321. 'is_hidden' => 0
  322. ];
  323. $lastTime = [
  324. 'field' => 'last_time',
  325. 'name' => '最后跟进时间',
  326. 'form_type' => 'datetime',
  327. 'is_hidden' => 0
  328. ];
  329. $beforeOwnerUser = [
  330. 'field' => 'before_owner_user_id',
  331. 'name' => '前负责人',
  332. 'form_type' => 'user',
  333. 'is_hidden' => 0
  334. ];
  335. $intoPoolTime = [
  336. 'field' => 'into_pool_time',
  337. 'name' => '进入公海时间',
  338. 'form_type' => 'datetime',
  339. 'is_hidden' => 0
  340. ];
  341. $createTime = [
  342. 'field' => 'create_time',
  343. 'name' => '创建时间',
  344. 'form_type' => 'datetime',
  345. 'is_hidden' => 0
  346. ];
  347. $updateTime = [
  348. 'field' => 'update_time',
  349. 'name' => '更新时间',
  350. 'form_type' => 'datetime',
  351. 'is_hidden' => 0
  352. ];
  353. $createUser = [
  354. 'field' => 'create_user_id',
  355. 'name' => '创建人',
  356. 'form_type' => 'user',
  357. 'is_hidden' => 0
  358. ];
  359. array_push($data, $address, $detailAddress, $lastRecord, $lastTime, $createTime, $updateTime, $createUser, $beforeOwnerUser, $intoPoolTime);
  360. return $data;
  361. }
  362. }
  363. /**
  364. * 处理公海规则数据
  365. *
  366. * @param array $rules 规则数据
  367. * @param int $poolId 公海ID
  368. * @author fanqi
  369. * @since 2021-03-30
  370. * @return array
  371. */
  372. private function getPoolRule($rules, $poolId)
  373. {
  374. $result = [];
  375. foreach ($rules AS $key => $value) {
  376. $result[] = [
  377. 'pool_id' => $poolId,
  378. 'type' => $value['type'],
  379. 'deal_handle' => $value['deal_handle'],
  380. 'business_handle' => $value['business_handle'],
  381. 'level_conf' => $value['level_conf'],
  382. 'level' => json_encode($value['level']),
  383. 'limit_day' => !empty($value['limit_day']) ? $value['limit_day'] : 0
  384. ];
  385. }
  386. return $result;
  387. }
  388. /**
  389. * 处理公海字段数据
  390. *
  391. * @param array $fields 字段列表
  392. * @param int $poolId 公海ID
  393. * @author fanqi
  394. * @since 2021-03-30
  395. * @return array
  396. */
  397. private function getPoolField($fields, $poolId)
  398. {
  399. $result = [];
  400. foreach ($fields AS $key => $value) {
  401. $result[] = [
  402. 'pool_id' => $poolId,
  403. 'name' => $value['name'],
  404. 'field_name' => $value['field'],
  405. 'form_type' => $value['form_type'],
  406. 'is_hidden' => $value['is_hidden']
  407. ];
  408. }
  409. return $result;
  410. }
  411. }