CusFollowService.java 13 KB

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