CusFollowService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.response.ResponseResultUtil;
  8. import com.dk.common.response.ResponseResultVO;
  9. import com.dk.mdm.infrastructure.convert.mst.CusFollowConvert;
  10. import com.dk.mdm.infrastructure.util.AuthUtils;
  11. import com.dk.mdm.mapper.mst.CusFollowStaffMapper;
  12. import com.dk.mdm.mapper.mst.CustomerMapper;
  13. import com.dk.mdm.mapper.mst.StaffMapper;
  14. import com.dk.mdm.model.pojo.mst.CusFollow;
  15. import com.dk.mdm.mapper.mst.CusFollowMapper;
  16. import com.dk.common.service.BaseService;
  17. import com.dk.common.mapper.BaseMapper;
  18. import com.dk.mdm.model.pojo.mst.CusFollowStaff;
  19. import com.dk.mdm.model.pojo.mst.Customer;
  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.LocalDateTime;
  31. import java.util.*;
  32. @Service
  33. @Transactional
  34. public class CusFollowService extends BaseService<CusFollow> {
  35. @Override
  36. public BaseMapper<CusFollow> getRepository() {
  37. return cusFollowMapper;
  38. }
  39. @Autowired
  40. private CusFollowMapper cusFollowMapper;
  41. @Autowired
  42. private CustomerMapper customerMapper;
  43. @Autowired
  44. private CustomerService customerService;
  45. @Autowired
  46. private CusFollowStaffMapper cusFollowStaffMapper;
  47. @Autowired
  48. private CommonService commonService;
  49. @Autowired
  50. private CusFollowConvert cusFollowConvert;
  51. @Autowired
  52. private StaffMapper staffMapper;
  53. @Autowired
  54. private AuthUtils authUtils;
  55. /**
  56. * @desc : 重写主键
  57. * @author : 于继渤
  58. * @date : 2024/2/29 20:29
  59. */
  60. @Override
  61. public String getPrimaryKey() {
  62. return "follow_id";
  63. }
  64. /**
  65. * @desc : 查询
  66. * @author : 于继渤
  67. * @date : 2023/1/5 9:39
  68. */
  69. @Pagination
  70. public ResponseResultVO<PageList<CusFollowResponse>> selectByCond(CusFollowQuery cusFollowQuery) {
  71. return super.mergeListWithCount(cusFollowQuery, cusFollowMapper.selectByCond(cusFollowQuery),
  72. cusFollowMapper.countByCond(cusFollowQuery));
  73. }
  74. /**
  75. * @desc : 新建
  76. * @author : 于继渤
  77. * @date : 2023/1/5 9:39
  78. */
  79. @Transactional(
  80. rollbackFor = {Exception.class}
  81. )
  82. public ResponseResultVO<?> insert(CusFollowVO cusFollowVO) {
  83. if (cusFollowVO.getCusId() != null) {
  84. //查询客户
  85. Customer customer = customerMapper.selectById(cusFollowVO.getCusId());
  86. // 查询跟进人是否存在
  87. CusFollowStaff followStaff = cusFollowStaffMapper.selectByCusFollowStaffId(cusFollowVO.getCusId(), authUtils.getStaff().getStaffId());
  88. //追加跟进人表
  89. if (followStaff == null) {
  90. cusFollowStaffMapper.insert(new CusFollowStaff()
  91. .setCusId(cusFollowVO.getCusId())
  92. .setFollowStaff(authUtils.getStaff().getStaffId())
  93. .setLastFollowId(authUtils.getStaff().getStaffId())
  94. .setLastFollowTime(LocalDateTime.now())
  95. .setCpId(authUtils.getStaff().getCpId())
  96. .setLastFollowStatus(cusFollowVO.getFollowStatus())
  97. .setFollowCount(1)
  98. );
  99. }
  100. //追加跟进人
  101. List<Long> users = new ArrayList<>();
  102. Object o = customerMapper.selectFollowStaffs(cusFollowVO.getCusId());
  103. if (o != null && o instanceof Long[]) {
  104. users = Arrays.asList((Long[]) o);
  105. }
  106. if (users == null || users.size() == 0 || (users != null && users.stream().allMatch(Objects::isNull))) {
  107. users = new ArrayList<>();
  108. }
  109. //公海客户跟进后 变成潜客状态
  110. if (Constant.saleCustomerStatusConstant.SALE_STATUS_INTE.getName().equals(customer.getSaleStatus())) {
  111. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_POTE.getName());
  112. }
  113. //已流失客户重新接待后应该变成潜客
  114. if (Constant.saleCustomerStatusConstant.SALE_STATUS_LOST.getName().equals(customer.getSaleStatus())) {
  115. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_POTE.getName());
  116. }
  117. //放入公海客户,清除跟进人
  118. if (Constant.IntentionConstant.SEA.getValue().equals(cusFollowVO.getIntention())) {
  119. customer.setFollowStaffs(new ArrayList<>());
  120. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_INTE.getName());
  121. } else {
  122. if (!users.contains(authUtils.getStaff().getStaffId())) {
  123. List arrList = new ArrayList(users);
  124. arrList.add(authUtils.getStaff().getStaffId());
  125. customer.setFollowStaffs(arrList);
  126. }
  127. }
  128. //已流失客户,修改状态为流失
  129. if (Constant.IntentionConstant.LOST.getValue().equals(cusFollowVO.getIntention())) {
  130. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_LOST.getName());
  131. }
  132. //如果不是留资接待 修改客户信息
  133. if (!Constant.BasicDataConstant.FOLLOW_STATUS_4.getValue().equals(cusFollowVO.getFollowStatus())) {
  134. customer.setCusName(cusFollowVO.getCusName());
  135. customer.setCusPhone(cusFollowVO.getCusPhone());
  136. customer.setAddressName(cusFollowVO.getAddressName());
  137. customer.setAddressNo(cusFollowVO.getAddressNo());
  138. customer.setAddressGcj02(cusFollowVO.getAddressGcj02());
  139. customer.setAddressFull(cusFollowVO.getAddressFull());
  140. customer.setAddressArea(cusFollowVO.getAddressArea());
  141. }
  142. customer.setCpId(authUtils.getStaff().getCpId());
  143. // 同商户 电话不同
  144. List<CustomerResponse> cusList = customerMapper.selectByCond(
  145. new CustomerQuery().setCpId(customer.getCpId()).setCusPhone(customer.getCusPhone()).setFlgValid(true)
  146. );
  147. if (cusList != null && cusList.size() > 0 && cusList.stream().anyMatch(a -> !a.getCusId().equals(customer.getCusId()))) {
  148. throw new BaseBusinessException(ErrorCodeEnum.CUSTOMER_SAME_COMPANY_TELEPHONE.getCode(),
  149. ErrorCodeEnum.CUSTOMER_SAME_COMPANY_TELEPHONE.getMessage());
  150. }
  151. customerService.updateByUuid(customer);
  152. } else if (cusFollowVO.getCusPhone() != null && cusFollowVO.getCusName() != null) {
  153. //留资接待 新建客户
  154. Customer customer = new Customer();
  155. customer.setCusName(cusFollowVO.getCusName());
  156. customer.setCusPhone(cusFollowVO.getCusPhone());
  157. customer.setAddressName(cusFollowVO.getAddressName());
  158. customer.setAddressNo(cusFollowVO.getAddressNo());
  159. customer.setAddressGcj02(cusFollowVO.getAddressGcj02());
  160. customer.setAddressFull(cusFollowVO.getAddressFull());
  161. customer.setAddressArea(cusFollowVO.getAddressArea());
  162. customer.setReportStaff(authUtils.getStaff().getStaffId());
  163. customer.setReportTime(LocalDateTime.now());
  164. customer.setCpId(authUtils.getStaff().getCpId());
  165. customer.setOrgId(authUtils.getStaff().getOrgId());
  166. // customer.setCustomerFrom(-1L);
  167. // 同商户 电话不同
  168. Long count = customerMapper.countByCond(
  169. new CustomerQuery().setCpId(customer.getCpId()).setCusPhone(customer.getCusPhone()).setFlgValid(true)
  170. );
  171. if (count > 0) {
  172. return ResponseResultUtil.error(ErrorCodeEnum.CUSTOMER_SAME_COMPANY_TELEPHONE.getCode(),
  173. ErrorCodeEnum.CUSTOMER_SAME_COMPANY_TELEPHONE.getMessage());
  174. }
  175. //放入公海客户,清除跟进人
  176. if (Constant.IntentionConstant.SEA.getValue().equals(cusFollowVO.getIntention())) {
  177. customer.setFollowStaffs(new ArrayList<>());
  178. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_INTE.getName());
  179. } else {
  180. //当前跟进人(业务员)
  181. customer.setFollowStaffs(new ArrayList<String>() {{
  182. add(authUtils.getStaff().getStaffId());
  183. }});
  184. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_POTE.getName());
  185. }
  186. customer.setFollowCount(0);
  187. //新建客户或编辑客户
  188. customerMapper.insert(customer);
  189. cusFollowVO.setCusId(customer.getCusId());
  190. }
  191. CusFollow cusFollow = cusFollowConvert.convertToPo(cusFollowVO);
  192. //设置ID
  193. Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.CUSTOMERFOLLOW.getName(), true);
  194. cusFollow.setFollowId(uniqueNoteCode.get("outId").toString());
  195. //跟进时间
  196. cusFollow.setFollowTime(LocalDateTime.now());
  197. //跟进人
  198. cusFollow.setFollowStaff(authUtils.getStaff().getStaffId());
  199. //跟进部门
  200. cusFollow.setFollowOrg(authUtils.getStaff().getOrgId());
  201. // 约量尺
  202. if (Constant.BasicDataConstant.FOLLOW_STATUS_3.getValue().equals(cusFollow.getFollowStatus())) {
  203. // 量尺状态:待量尺
  204. cusFollow.setMeasureStatus(Constant.BasicDataConstant.MEASURE_STATUS_2.getValue());
  205. }
  206. if (cusFollow.getFollowData() == null) {
  207. cusFollow.setFollowData("无");
  208. }
  209. super.insert(cusFollow);
  210. //设计师
  211. StaffResponse designStaff = null;
  212. if (cusFollow.getDesignStaff() != null) {
  213. designStaff = staffMapper.selectById(cusFollow.getDesignStaff());
  214. }
  215. // 约尺 同意邀约 有设计师 设计师绑定企微
  216. boolean pushFlag = Constant.BasicDataConstant.FOLLOW_STATUS_3.getValue().equals(cusFollow.getFollowStatus()) &&
  217. Constant.BasicDataConstant.FOLLOW_INVITE_RESULT.getValue().equals(cusFollow.getInviteResult()) &&
  218. designStaff != null;
  219. if (cusFollow.getNextFollowTime() != null || pushFlag) {
  220. // 保存跟进信息
  221. // saveToXxlJob(cusFollow);
  222. }
  223. return ResponseResultUtil.success();
  224. }
  225. /**
  226. * @desc : 保存跟进信息到xxl job
  227. * @author : 于继渤海
  228. * @date : 2022-05-27 09:53
  229. */
  230. // @Transactional(rollbackFor = {Exception.class})
  231. // public void saveToXxlJob(CusFollow cusFollow) {
  232. //
  233. // Long nextId = followPlanMapper.getNextId();
  234. //
  235. //
  236. // // 约量尺 && 接受邀约
  237. //// 预约量尺,邀约结果接受,通知设计师提醒时间是(服务时间);邀约结果拒绝,通知创建者提醒时间是(提醒时间)
  238. // if (Constant.BasicDataConstant.FOLLOW_STATUS_3.getValue().equals(follow.getFollowStatus()) &&
  239. // Constant.BasicDataConstant.FOLLOW_INVITE_RESULT.getValue().equals(follow.getInviteResult())) {
  240. // follow.setNextFollowTime(follow.getInviteTime()).setFollowUser(follow.getDesignUser());
  241. // }
  242. //
  243. // int id = xxlJobUtils.create(Math.toIntExact(
  244. // (follow.getNextFollowTime().toInstant(ZoneOffset.of("+8")).toEpochMilli() - System.currentTimeMillis()) / 1000
  245. // ), nextId, Constant.XxlJobInfo.FOLLOW.getValue());
  246. //
  247. //
  248. // // 生成主键
  249. // followPlanMapper.insert(
  250. // new FollowPlan().setPlanId(nextId).setCustomerId(follow.getCustomerId())
  251. // .setPlanType(Constant.IntegerConstant.PLAN_TYPE_FOLLOW.getValue())
  252. // .setFollowId(follow.getFollowId())
  253. // .setFollowUser(follow.getFollowUser())
  254. // .setNextFollowPlan(follow.getNextFollowPlan())
  255. // .setNextFollowTime(follow.getNextFollowTime())
  256. // .setXxlJobId(id)
  257. // .setCpId(follow.getCpId())
  258. // );
  259. // }
  260. }