CusFollowService.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. package com.dk.mdm.service.mst;
  2. import com.dk.common.exception.BaseBusinessException;
  3. import com.dk.common.infrastructure.annotaiton.Pagination;
  4. import com.dk.common.infrastructure.constant.Constant;
  5. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  6. import com.dk.common.model.pojo.PageList;
  7. import com.dk.common.model.vo.core.StaffEntity;
  8. import com.dk.common.response.ResponseCodeEnum;
  9. import com.dk.common.response.ResponseResultUtil;
  10. import com.dk.common.response.ResponseResultVO;
  11. import com.dk.mdm.infrastructure.convert.mst.CusFollowConvert;
  12. import com.dk.mdm.infrastructure.util.AuthUtils;
  13. import com.dk.mdm.mapper.mst.CusFollowStaffMapper;
  14. import com.dk.mdm.mapper.mst.CustomerMapper;
  15. import com.dk.mdm.mapper.mst.StaffMapper;
  16. import com.dk.mdm.model.pojo.mst.*;
  17. import com.dk.mdm.mapper.mst.CusFollowMapper;
  18. import com.dk.common.service.BaseService;
  19. import com.dk.common.mapper.BaseMapper;
  20. import com.dk.mdm.model.query.mst.CusFollowQuery;
  21. import com.dk.mdm.model.query.mst.CustomerQuery;
  22. import com.dk.mdm.model.response.mst.CusFollowResponse;
  23. import com.dk.mdm.model.response.mst.CustomerResponse;
  24. import com.dk.common.model.response.mst.StaffResponse;
  25. import com.dk.mdm.model.vo.mst.CusFollowVO;
  26. import com.dk.mdm.service.common.CommonService;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import java.time.LocalDate;
  31. import java.time.LocalDateTime;
  32. import java.util.*;
  33. @Service
  34. @Transactional
  35. public class CusFollowService extends BaseService<CusFollow> {
  36. @Override
  37. public BaseMapper<CusFollow> getRepository() {
  38. return cusFollowMapper;
  39. }
  40. @Autowired
  41. private CusFollowMapper cusFollowMapper;
  42. @Autowired
  43. private CustomerMapper customerMapper;
  44. @Autowired
  45. private CustomerService customerService;
  46. @Autowired
  47. private CusFollowStaffMapper cusFollowStaffMapper;
  48. @Autowired
  49. private CommonService commonService;
  50. @Autowired
  51. private CusFollowConvert cusFollowConvert;
  52. @Autowired
  53. private StaffMapper staffMapper;
  54. @Autowired
  55. private AuthUtils authUtils;
  56. @Autowired
  57. private MeasureRoomService measureRoomService;
  58. @Autowired
  59. private MeasureReceiptService measureReceiptService;
  60. /**
  61. * @desc : 重写主键
  62. * @author : 于继渤
  63. * @date : 2024/2/29 20:29
  64. */
  65. @Override
  66. public String getPrimaryKey() {
  67. return "follow_id";
  68. }
  69. /**
  70. * @desc : 查询
  71. * @author : 于继渤
  72. * @date : 2023/1/5 9:39
  73. */
  74. @Pagination
  75. public ResponseResultVO<PageList<CusFollowResponse>> selectByCond(CusFollowQuery cusFollowQuery) {
  76. return super.mergeListWithCount(cusFollowQuery, cusFollowMapper.selectByCond(cusFollowQuery),
  77. cusFollowMapper.countByCond(cusFollowQuery));
  78. }
  79. public ResponseResultVO<List<CusFollowResponse>> selectByList(CusFollowQuery cusFollowQuery) {
  80. return ResponseResultUtil.success(cusFollowMapper.selectByList(cusFollowQuery));
  81. }
  82. /**
  83. * @desc : 新建
  84. * @author : 于继渤
  85. * @date : 2023/1/5 9:39
  86. */
  87. @Transactional(
  88. rollbackFor = {Exception.class}
  89. )
  90. public ResponseResultVO<?> insert(CusFollowVO cusFollowVO) {
  91. CusFollow cusFollow = cusFollowConvert.convertToPo(cusFollowVO);
  92. //设置ID
  93. Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.CUSTOMERFOLLOW.getName(), true);
  94. cusFollow.setFollowId(uniqueNoteCode.get("outId").toString());
  95. //跟进时间
  96. cusFollow.setFollowTime(LocalDateTime.now());
  97. //跟进人
  98. cusFollow.setFollowStaff(authUtils.getStaff().getStaffId());
  99. //跟进部门
  100. cusFollow.setFollowOrg(authUtils.getStaff().getOrgId());
  101. if (cusFollowVO.getCusId() != null) {
  102. //查询客户
  103. Customer customer = customerMapper.selectById(cusFollowVO.getCusId());
  104. // 查询跟进人是否存在
  105. CusFollowStaff followStaff = cusFollowStaffMapper.selectByCusFollowStaffId(cusFollowVO.getCusId(), authUtils.getStaff().getStaffId());
  106. //追加跟进人表
  107. if (followStaff == null) {
  108. cusFollowStaffMapper.insert(new CusFollowStaff()
  109. .setCusId(cusFollowVO.getCusId())
  110. .setFollowStaff(authUtils.getStaff().getStaffId())
  111. .setLastFollowId(authUtils.getStaff().getStaffId())
  112. .setLastFollowTime(LocalDateTime.now())
  113. .setCpId(authUtils.getStaff().getCpId())
  114. .setLastFollowStatus(cusFollowVO.getFollowStatus())
  115. .setFollowCount(1)
  116. );
  117. }
  118. //追加跟进人
  119. List<Long> users = new ArrayList<>();
  120. Object o = customerMapper.selectFollowStaffs(cusFollowVO.getCusId());
  121. if (o != null && o instanceof Long[]) {
  122. users = Arrays.asList((Long[]) o);
  123. }
  124. if (users == null || users.size() == 0 || (users != null && users.stream().allMatch(Objects::isNull))) {
  125. users = new ArrayList<>();
  126. }
  127. //公海客户跟进后 变成潜客状态
  128. if (Constant.saleCustomerStatusConstant.SALE_STATUS_INTE.getName().equals(customer.getSaleStatus())) {
  129. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_POTE.getName());
  130. }
  131. //已流失客户重新接待后应该变成潜客
  132. if (Constant.saleCustomerStatusConstant.SALE_STATUS_LOST.getName().equals(customer.getSaleStatus())) {
  133. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_POTE.getName());
  134. }
  135. //放入公海客户,清除跟进人
  136. if (Constant.IntentionConstant.SEA.getValue().equals(cusFollowVO.getIntention())) {
  137. customer.setFollowStaffs(new ArrayList<>());
  138. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_INTE.getName());
  139. } else {
  140. if (!users.contains(authUtils.getStaff().getStaffId())) {
  141. List arrList = new ArrayList(users);
  142. arrList.add(authUtils.getStaff().getStaffId());
  143. customer.setFollowStaffs(arrList);
  144. }
  145. }
  146. //已流失客户,修改状态为流失
  147. if (Constant.IntentionConstant.LOST.getValue().equals(cusFollowVO.getIntention())) {
  148. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_LOST.getName());
  149. }
  150. //如果不是留资接待 修改客户信息
  151. if (!Constant.BasicDataConstant.FOLLOW_STATUS_4.getValue().equals(cusFollowVO.getFollowStatus())) {
  152. customer.setCusName(cusFollowVO.getCusName());
  153. customer.setCusPhone(cusFollowVO.getCusPhone());
  154. customer.setAddressName(cusFollowVO.getAddressName());
  155. customer.setAddressNo(cusFollowVO.getAddressNo());
  156. customer.setAddressGcj02(cusFollowVO.getAddressGcj02());
  157. customer.setAddressFull(cusFollowVO.getAddressFull());
  158. customer.setAddressArea(cusFollowVO.getAddressArea());
  159. }
  160. customer.setCpId(authUtils.getStaff().getCpId());
  161. // 同商户 电话不同
  162. List<CustomerResponse> cusList = customerMapper.selectByCond(
  163. new CustomerQuery().setCpId(customer.getCpId()).setCusPhone(customer.getCusPhone()).setFlgValid(true)
  164. );
  165. if (cusList != null && cusList.size() > 0 && cusList.stream().anyMatch(a -> !a.getCusId().equals(customer.getCusId()))) {
  166. throw new BaseBusinessException(ErrorCodeEnum.CUSTOMER_SAME_COMPANY_TELEPHONE.getCode(),
  167. ErrorCodeEnum.CUSTOMER_SAME_COMPANY_TELEPHONE.getMessage());
  168. }
  169. customer.setLastFollowId(cusFollow.getFollowId());
  170. customer.setLastFollowTime(cusFollow.getFollowTime());
  171. customer.setLastFollowStaff(cusFollow.getFollowStaff());
  172. //修改跟进次数
  173. customer.setFollowCount(customer.getFollowCount() + 1);
  174. customerService.updateByUuid(customer);
  175. } else if (cusFollowVO.getCusPhone() != null && cusFollowVO.getCusName() != null) {
  176. //留资接待 新建客户
  177. Customer customer = new Customer();
  178. //设置编码
  179. Map<String, Object> customerNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.CUSTOMER.getName(), true);
  180. customer.setCusId(customerNoteCode.get("outId").toString());
  181. customer.setCusCode(customerNoteCode.get("outNote").toString());
  182. //TODO 渠道 取当前登录人员工的渠道
  183. customer.setChannelId("10112024-0302-0000-0000-00000759d8a4");
  184. customer.setCusName(cusFollowVO.getCusName());
  185. customer.setCusPhone(cusFollowVO.getCusPhone());
  186. customer.setAddressName(cusFollowVO.getAddressName());
  187. customer.setAddressNo(cusFollowVO.getAddressNo());
  188. customer.setAddressGcj02(cusFollowVO.getAddressGcj02());
  189. customer.setAddressFull(cusFollowVO.getAddressFull());
  190. customer.setAddressArea(cusFollowVO.getAddressArea());
  191. customer.setReportStaff(authUtils.getStaff().getStaffId());
  192. customer.setReportTime(LocalDateTime.now());
  193. customer.setCpId(authUtils.getStaff().getCpId());
  194. customer.setStaffId(authUtils.getStaff().getStaffId());
  195. customer.setOrgId(authUtils.getStaff().getOrgId());
  196. customer.setFollowCount(1);
  197. customer.setLastFollowId(cusFollow.getFollowId());
  198. customer.setLastFollowTime(cusFollow.getFollowTime());
  199. customer.setLastFollowStaff(cusFollow.getFollowStaff());
  200. cusFollow.setCusId(customer.getCusId());
  201. // 同商户 电话不同
  202. Long count = customerMapper.countByCond(
  203. new CustomerQuery().setCpId(customer.getCpId()).setCusPhone(customer.getCusPhone()).setFlgValid(true)
  204. );
  205. if (count > 0) {
  206. return ResponseResultUtil.error(ErrorCodeEnum.CUSTOMER_SAME_COMPANY_TELEPHONE.getCode(),
  207. ErrorCodeEnum.CUSTOMER_SAME_COMPANY_TELEPHONE.getMessage());
  208. }
  209. //放入公海客户,清除跟进人
  210. if (Constant.IntentionConstant.SEA.getValue().equals(cusFollowVO.getIntention())) {
  211. customer.setFollowStaffs(new ArrayList<>());
  212. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_INTE.getName());
  213. } else {
  214. //当前跟进人(业务员)
  215. customer.setFollowStaffs(new ArrayList<String>() {{
  216. add(authUtils.getStaff().getStaffId());
  217. }});
  218. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_POTE.getName());
  219. }
  220. //新建客户或编辑客户
  221. customerMapper.insert(customer);
  222. }
  223. // 约量尺
  224. if (Constant.BasicDataConstant.FOLLOW_STATUS_3.getValue().equals(cusFollow.getFollowStatus())) {
  225. // 量尺状态:待量尺
  226. cusFollow.setMeasureStatus(Constant.BasicDataConstant.MEASURE_STATUS_2.getValue());
  227. }
  228. if (cusFollow.getFollowData() == null) {
  229. cusFollow.setFollowData("无");
  230. }
  231. super.insert(cusFollow);
  232. //设计师
  233. StaffResponse designStaff = null;
  234. if (cusFollow.getDesignStaff() != null) {
  235. designStaff = staffMapper.selectById(cusFollow.getDesignStaff());
  236. }
  237. // 约尺 同意邀约 有设计师 设计师绑定企微
  238. boolean pushFlag = Constant.BasicDataConstant.FOLLOW_STATUS_3.getValue().equals(cusFollow.getFollowStatus()) &&
  239. Constant.BasicDataConstant.FOLLOW_INVITE_RESULT.getValue().equals(cusFollow.getInviteResult()) &&
  240. designStaff != null;
  241. if (cusFollow.getNextFollowTime() != null || pushFlag) {
  242. // 保存跟进信息
  243. // saveToXxlJob(cusFollow);
  244. }
  245. return ResponseResultUtil.success();
  246. }
  247. /**
  248. * @desc : 保存跟进信息到xxl job
  249. * @author : 于继渤海
  250. * @date : 2022-05-27 09:53
  251. */
  252. // @Transactional(rollbackFor = {Exception.class})
  253. // public void saveToXxlJob(CusFollow cusFollow) {
  254. //
  255. // Long nextId = followPlanMapper.getNextId();
  256. //
  257. //
  258. // // 约量尺 && 接受邀约
  259. //// 预约量尺,邀约结果接受,通知设计师提醒时间是(服务时间);邀约结果拒绝,通知创建者提醒时间是(提醒时间)
  260. // if (Constant.BasicDataConstant.FOLLOW_STATUS_3.getValue().equals(follow.getFollowStatus()) &&
  261. // Constant.BasicDataConstant.FOLLOW_INVITE_RESULT.getValue().equals(follow.getInviteResult())) {
  262. // follow.setNextFollowTime(follow.getInviteTime()).setFollowUser(follow.getDesignUser());
  263. // }
  264. //
  265. // int id = xxlJobUtils.create(Math.toIntExact(
  266. // (follow.getNextFollowTime().toInstant(ZoneOffset.of("+8")).toEpochMilli() - System.currentTimeMillis()) / 1000
  267. // ), nextId, Constant.XxlJobInfo.FOLLOW.getValue());
  268. //
  269. //
  270. // // 生成主键
  271. // followPlanMapper.insert(
  272. // new FollowPlan().setPlanId(nextId).setCustomerId(follow.getCustomerId())
  273. // .setPlanType(Constant.IntegerConstant.PLAN_TYPE_FOLLOW.getValue())
  274. // .setFollowId(follow.getFollowId())
  275. // .setFollowUser(follow.getFollowUser())
  276. // .setNextFollowPlan(follow.getNextFollowPlan())
  277. // .setNextFollowTime(follow.getNextFollowTime())
  278. // .setXxlJobId(id)
  279. // .setCpId(follow.getCpId())
  280. // );
  281. // }
  282. /**
  283. * @desc : 新建量尺回执
  284. * @author : 于继渤
  285. * @date : 2023/1/5 9:39
  286. */
  287. @Transactional(
  288. rollbackFor = {Exception.class}
  289. )
  290. public ResponseResultVO<?> updateMeasure(CusFollowVO cusFollowVO) {
  291. CusFollow cusFollow = cusFollowConvert.convertToPo(cusFollowVO);
  292. //更新总单
  293. // int update = cusFollowMapper.updateMeasure(cusFollow);
  294. //更新客户信息
  295. Customer customer = new Customer();
  296. customer.setCusId(cusFollowVO.getCusId());
  297. customer.setCusName(cusFollowVO.getCusName());
  298. customer.setCusPhone(cusFollowVO.getCusPhone());
  299. customer.setAddressName(cusFollowVO.getAddressName());
  300. customer.setAddressNo(cusFollowVO.getAddressNo());
  301. customer.setAddressFull(cusFollowVO.getAddressFull()); //全路径地址
  302. customer.setAddressArea(cusFollowVO.getAddressArea()); //省市区
  303. customer.setAddressGcj02(cusFollowVO.getAddressGcj02());//经纬度
  304. customerService.updateByUuid(customer);
  305. CusFollow Follow = cusFollowMapper.selectById(cusFollowVO.getFollowId());
  306. if (Follow == null) {
  307. return ResponseResultUtil.error(ResponseCodeEnum.SELECT_NULL);
  308. } else if (Constant.BasicDataConstant.MEASURE_STATUS_1.getValue().equals(Follow.getMeasureStatus())) {
  309. return ResponseResultUtil.error(ErrorCodeEnum.CUSTOMER_FOLLOW_MEASURE_STATUS_DONE.getCode(),
  310. ErrorCodeEnum.CUSTOMER_FOLLOW_MEASURE_STATUS_DONE.getMessage());
  311. }
  312. StaffEntity staff = authUtils.getStaff();
  313. //设置id
  314. Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.RECEIPT.getName(), true);
  315. // 新建一条量尺记录
  316. MeasureReceipt measureReceipt = new MeasureReceipt();
  317. if (cusFollowVO.getFlgAgainMeasure() != null) {
  318. measureReceipt.setFlgAgainMeasure(cusFollowVO.getFlgAgainMeasure());
  319. } else {
  320. measureReceipt.setFlgAgainMeasure(false);
  321. }
  322. measureReceipt.setReceiptId(uniqueNoteCode.get("outId").toString());
  323. measureReceipt.setCusId(cusFollowVO.getCusId());
  324. measureReceipt.setFollowId(cusFollowVO.getFollowId());
  325. measureReceipt.setMeasureStatus(Constant.BasicDataConstant.MEASURE_STATUS_1.getValue());
  326. measureReceipt.setMeasureTime(LocalDateTime.now());
  327. //剩下参数有前台传
  328. measureReceiptService.insert(measureReceipt);
  329. // 新建空间
  330. if (cusFollowVO.getMeasureRoomList() != null && cusFollowVO.getMeasureRoomList().size() > 0) {
  331. for (MeasureRoom measureRoom : cusFollowVO.getMeasureRoomList()) {
  332. //设置ID
  333. Map<String, Object> uniqueNoteCode1 = commonService.getUniqueNoteCode(Constant.docNameConstant.ROOM.getName(), true);
  334. measureRoom.setRoomId(uniqueNoteCode1.get("outId").toString());
  335. measureRoom.setFollowId(cusFollowVO.getFollowId());
  336. measureRoom.setCusId(cusFollowVO.getCusId());
  337. measureRoom.setCpId(authUtils.getStaff().getCpId());
  338. measureRoom.setReceiptId(measureReceipt.getReceiptId());
  339. measureRoomService.insert(measureRoom);
  340. }
  341. }
  342. //修改量尺状态为 已量尺
  343. cusFollow.setMeasureStatus(Constant.BasicDataConstant.MEASURE_STATUS_1.getValue());
  344. //修改跟进
  345. super.updateByUuid(cusFollow);
  346. return ResponseResultUtil.success();
  347. }
  348. /**
  349. * @desc : 编辑
  350. * @author : 于继渤
  351. * @date : 2024-03-13 17:03
  352. */
  353. @Transactional(rollbackFor = {Exception.class})
  354. public ResponseResultVO<?> update(CusFollowVO cusFollowVO) {
  355. CusFollow cusFollow = cusFollowConvert.convertToPo(cusFollowVO);
  356. //编辑时更改客户意向要把对应客户也需要更改
  357. if (cusFollowVO.getSaleStatus() != null && (cusFollowVO.getSaleStatus().equals(Constant.saleCustomerStatusConstant.SALE_STATUS_INTE.getName()) || cusFollowVO.getSaleStatus().equals(Constant.saleCustomerStatusConstant.SALE_STATUS_LOST.getName()))) {
  358. //公海客户清空跟进人
  359. if (cusFollowVO.getSaleStatus().equals(Constant.saleCustomerStatusConstant.SALE_STATUS_INTE.getName())) {
  360. customerMapper.updateFollowStaffs(new Customer().setCusId(cusFollowVO.getCusId()));
  361. }
  362. }
  363. if (cusFollowVO.getCusId() != null) {
  364. Customer customer = new Customer();
  365. customer.setCusId(cusFollowVO.getCusId());
  366. customer.setAddressName(cusFollowVO.getAddressName());
  367. customer.setAddressNo(cusFollowVO.getAddressNo());
  368. customer.setCusPhone(cusFollowVO.getCusPhone());
  369. customer.setCusName(cusFollowVO.getCusName());
  370. customer.setOrgId(cusFollowVO.getFollowOrg());
  371. customer.setAddressFull(cusFollowVO.getAddressFull());
  372. customer.setAddressArea(cusFollowVO.getAddressArea());
  373. customer.setSaleStatus(cusFollowVO.getSaleStatus());
  374. customer.setAddressGcj02(cusFollowVO.getAddressGcj02());
  375. //更新客户信息
  376. customerService.updateByUuid(customer);
  377. } else {
  378. StaffEntity staff = authUtils.getStaff();
  379. Customer customer = new Customer();
  380. customer.setCusName(cusFollowVO.getCusName());
  381. customer.setCusPhone(cusFollowVO.getCusPhone());
  382. customer.setAddressName(cusFollowVO.getAddressName());
  383. customer.setAddressNo(cusFollowVO.getAddressNo());
  384. customer.setOrgId(cusFollowVO.getFollowOrg());
  385. customer.setAddressFull(cusFollowVO.getAddressFull());
  386. customer.setAddressArea(cusFollowVO.getAddressArea());
  387. customer.setSaleStatus(cusFollowVO.getSaleStatus());
  388. customer.setAddressGcj02(cusFollowVO.getAddressGcj02());
  389. customer.setReportStaff(cusFollowVO.getFollowStaff());
  390. customer.setReportTime(LocalDateTime.now());
  391. customer.setCpId(staff.getCpId());
  392. List<String> list = new ArrayList<>();
  393. list.add(staff.getStaffId());
  394. customer.setFollowStaffs(list);
  395. customer.setSaleStatus("客成状态-潜客");
  396. customer.setChannelId(cusFollowVO.getChannelId());
  397. Map<String, Object> customerNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.CUSTOMER.getName(), true);
  398. customer.setCusId(customerNoteCode.get("outId").toString());
  399. //新建客户
  400. customerMapper.insert(customer);
  401. cusFollow.setCusId(customer.getCusId());
  402. }
  403. //更新总单
  404. super.updateByUuid(cusFollow);
  405. return ResponseResultUtil.success();
  406. }
  407. }