ReleaseUserTask.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package com.goafanti.common.task;
  2. import com.goafanti.common.bo.EmailBo;
  3. import com.goafanti.common.bo.userDaysBo;
  4. import com.goafanti.common.constant.AFTConstants;
  5. import com.goafanti.common.dao.AdminUserCountMapper;
  6. import com.goafanti.common.dao.NoticeMapper;
  7. import com.goafanti.common.enums.NoticeStatus;
  8. import com.goafanti.common.enums.NoticeTypes;
  9. import com.goafanti.common.model.AdminUserCount;
  10. import com.goafanti.common.model.Notice;
  11. import com.goafanti.common.model.User;
  12. import com.goafanti.common.utils.AsyncUtils;
  13. import com.goafanti.common.utils.LoggerUtils;
  14. import com.goafanti.common.utils.StringUtils;
  15. import com.goafanti.customer.bo.LockingReleaseBo;
  16. import com.goafanti.customer.service.CustomerService;
  17. import com.goafanti.user.service.UserService;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.beans.factory.annotation.Value;
  20. import org.springframework.scheduling.annotation.Scheduled;
  21. import org.springframework.stereotype.Component;
  22. import javax.annotation.Resource;
  23. import java.util.ArrayList;
  24. import java.util.Date;
  25. import java.util.List;
  26. import java.util.UUID;
  27. @Component
  28. //@RestController
  29. public class ReleaseUserTask {
  30. @Resource
  31. private CustomerService customerService;
  32. @Resource
  33. private UserService userService;
  34. @Autowired
  35. private NoticeMapper noticeMapper;
  36. @Autowired
  37. private AsyncUtils asyncUtils;
  38. @Value(value = "${dev.name}")
  39. private String devName=null;
  40. int pointsDataLimit = 50;
  41. @Autowired
  42. private AdminUserCountMapper adminUserCountMapper;
  43. /**
  44. * 每天凌晨一点
  45. *
  46. * @throws InterruptedException
  47. */
  48. // @Scheduled(cron = "0 52 10 * * ?")
  49. // @RequestMapping("/open/test")
  50. @Scheduled(cron = "0 0 1 * * ?")
  51. public void startTask() {
  52. try {
  53. List<User> userList = userService.selectUserByRoleName("营销员", "营销经理");
  54. pushUserDays(userList);
  55. Thread.sleep(2000);
  56. updateUser(userList);
  57. Thread.sleep(2000);
  58. } catch (InterruptedException e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. private void pushUserDays(List<User> userList ) throws InterruptedException {
  63. LoggerUtils.debug(getClass(), "==============客户提醒开始============");
  64. List<Notice> ln = new ArrayList<>();
  65. for (User u : userList) {
  66. if (StringUtils.isNotBlank(u.getId())) {
  67. List<userDaysBo> userTmpList = customerService.selectReleaseUserDays(u.getId());
  68. for (userDaysBo ub : userTmpList) {
  69. String str=null;
  70. if (ub.getChannel() == 0) {
  71. String shareType="";
  72. if(ub.getNewChannel()==0)shareType="私有";
  73. else if(ub.getNewChannel()==1)shareType="渠道";
  74. str = String.format("您的%s客户【%s】剩余天数不足十五天,请及时%s!", shareType, ub.getName(),ub.getType()==0?"跟进":"签单");
  75. }else {
  76. str = String.format("您的%s客户【%s】剩余天数不足十五天,请及时签单!", "外联", ub.getName());
  77. }
  78. ln.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, ub.getAid(),
  79. NoticeStatus.CUSTOMER_LOSE_REMINDER.getCode(), str,ub.getUid()));
  80. }
  81. }
  82. }
  83. //查询15天未跟进,私有跟释放前15天重复,去除
  84. List<userDaysBo> userChannelList = new ArrayList<userDaysBo>();
  85. for (User u : userList) {
  86. if (StringUtils.isNotBlank(u.getId())) {
  87. userChannelList = customerService.selectChannelNotFollow(u.getId());
  88. }
  89. for (userDaysBo ub : userChannelList) {
  90. String str=null;
  91. if (ub.getChannel() == 1) {
  92. str = String.format("您的%s客户【%s】已经十五天未跟进,请及时跟进或者限时签单!", "外联", ub.getName());
  93. }
  94. ln.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, ub.getAid(),
  95. NoticeStatus.CUSTOMER_PRIVATE_REMINDER.getCode(), str,ub.getUid()));
  96. }
  97. }
  98. addNoticeBatch(ln);
  99. LoggerUtils.debug(getClass(), "==============客户提醒结束============");
  100. }
  101. /**
  102. * 将客户和业务转为待释放
  103. */
  104. public void updateUser(List<User> userList ) {
  105. LoggerUtils.debug(getClass(), "==============客户释放开始============");
  106. Date releaseTime = new Date();
  107. try {
  108. List<LockingReleaseBo> lockUserList = new ArrayList<LockingReleaseBo>();
  109. if (userList != null && !userList.isEmpty()) {
  110. for (User u : userList) {
  111. if (StringUtils.isNotBlank(u.getId())) {
  112. // 获取30天释放
  113. List<LockingReleaseBo> userTmpList = customerService.selectWaitReleaseCustomer(u.getId());
  114. customerService.pushReleaseLog(userTmpList, 0);
  115. addUserNotice(userTmpList);
  116. if (userTmpList != null && !userTmpList.isEmpty())
  117. lockUserList.addAll(userTmpList);
  118. }
  119. }
  120. }
  121. List<LockingReleaseBo> newList = new ArrayList<LockingReleaseBo>();
  122. if (lockUserList != null && lockUserList.size() > 0) {
  123. for (int i = 0; i < lockUserList.size(); i++) {
  124. newList.add(lockUserList.get(i));
  125. if (pointsDataLimit == newList.size() || i == lockUserList.size() - 1) {
  126. if (newList.size() > 0) {
  127. customerService.updatePendingReleaseLock(newList);
  128. }
  129. newList.clear();
  130. Thread.sleep(2000);
  131. }
  132. }
  133. }
  134. //处理每个人重复的天数
  135. pushSalesmanCount(lockUserList);
  136. pushChannel(releaseTime, userList);
  137. } catch (Exception e) {
  138. Notice n = new Notice(UUID.randomUUID().toString(), new Date(), 0, "1",
  139. NoticeStatus.TASK_PATENT_ERROR.getCode(), "==============客户释放失败================",null);
  140. addNotice(n);
  141. EmailBo emailBo = new EmailBo("释放客户失败", AFTConstants.ADMIN_EMAIL, "超管", "平台", "系统", devName+"释放客户失败");
  142. asyncUtils.send(emailBo);
  143. LoggerUtils.error(getClass(), "====================客户释放失败=================");
  144. LoggerUtils.error(getClass(), "客户释放失败", e);
  145. }
  146. LoggerUtils.debug(getClass(), "==============客户释放完成============");
  147. }
  148. private void pushSalesmanCount(List<LockingReleaseBo> aidList) {
  149. for (LockingReleaseBo e : aidList) {
  150. String dates= e.getLockTime();
  151. AdminUserCount adminUserCount = customerService.getAdminUserCount(e.getAid(), dates);
  152. if (adminUserCount!=null&&adminUserCount.getPrivateCount()>0){
  153. AdminUserCount newAdminUserCount = new AdminUserCount();
  154. newAdminUserCount.setId(adminUserCount.getId());
  155. newAdminUserCount.setPrivateCount(adminUserCount.getPrivateCount()-1);
  156. adminUserCountMapper.updateByPrimaryKeySelective(newAdminUserCount);
  157. }
  158. }
  159. }
  160. /**
  161. * 处理外联
  162. * @param releaseTime
  163. * @param userList
  164. * @throws InterruptedException
  165. */
  166. private void pushChannel(Date releaseTime, List<User> userList) throws InterruptedException {
  167. // 获取超过90天客户,判断是否回收过,未回收则回收,已回收则释放为公共客户
  168. List<userDaysBo> userChannelList =null;
  169. List<userDaysBo> count =new ArrayList<userDaysBo>();
  170. if (userList != null && !userList.isEmpty()) {
  171. for (User u : userList) {
  172. userChannelList=customerService.selectChannelUserDays(u.getId());
  173. if (userChannelList!=null) {
  174. count.addAll(userChannelList);
  175. }
  176. }
  177. }
  178. List<userDaysBo> hsList=new ArrayList<>();
  179. List<userDaysBo> sfList=new ArrayList<>();
  180. if (count != null && count.size() > 0) {
  181. for (int i = 0; i < count.size(); i++) {
  182. //回收
  183. if (count.get(i).getRecovery()==0) {
  184. hsList.add(count.get(i));
  185. }else {
  186. sfList.add(count.get(i));
  187. }
  188. if (pointsDataLimit == (hsList.size()+sfList.size()) || i == count.size() - 1) {
  189. if(!hsList.isEmpty())userService.pushReleaseUserChannel(hsList,0);
  190. if(!sfList.isEmpty())userService.pushReleaseUserChannel(sfList,1);
  191. addChannelNotice(hsList,sfList);
  192. hsList.clear();
  193. sfList.clear();
  194. Thread.sleep(2000);
  195. }
  196. }
  197. }
  198. List<LockingReleaseBo> lockList = customerService.selectPendinglockUserList();
  199. if (lockList.size() > 0)
  200. customerService.updateReleaseLock(releaseTime);
  201. }
  202. public void addNotice(Notice n ) {
  203. n.setType(NoticeTypes.getType(n.getNoticeType()));
  204. noticeMapper.insertSelective(n);
  205. }
  206. public void addNoticeBatch(List<Notice> ln) {
  207. List<Notice> newList=new ArrayList<Notice>();
  208. if (ln != null && ln.size() > 0) {
  209. for (int i = 0; i < ln.size(); i++) {
  210. ln.get(i).setType(NoticeTypes.getType(ln.get(i).getNoticeType()));
  211. newList.add(ln.get(i));
  212. if (50 == newList.size() || i == ln.size() - 1) {
  213. if (newList.size() > 0) {
  214. //发送站内信
  215. noticeMapper.insertBatch(newList);
  216. }
  217. newList.clear();
  218. try {
  219. Thread.sleep(2000);
  220. } catch (InterruptedException e) {
  221. e.printStackTrace();
  222. }
  223. }
  224. }
  225. }
  226. }
  227. private void addUserNotice(List<LockingReleaseBo> userTmpList) {
  228. List<Notice> nl=new ArrayList<Notice>();
  229. if (!userTmpList.isEmpty()) {
  230. for (LockingReleaseBo u : userTmpList) {
  231. String shareType="";
  232. if(u.getNewChannel()==0)shareType="私有";
  233. else if(u.getNewChannel()==1)shareType="渠道";
  234. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, u.getAid(),
  235. NoticeStatus.CUSTOMER_LOSE30.getCode(),
  236. String.format("您的%s客户【%s】已经30天未跟进,已经自动释放!", shareType, u.getUserName()),
  237. u.getUid()));
  238. }
  239. }
  240. if(!nl.isEmpty())addNoticeBatch(nl);
  241. }
  242. private void addChannelNotice(List<userDaysBo> hsList, List<userDaysBo> sfList) {
  243. List<Notice> nl=new ArrayList<Notice>();
  244. for (userDaysBo hs : hsList) {
  245. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, hs.getAid(),
  246. NoticeStatus.CUSTOMER_CHENNEL_RECOVERY.getCode(),
  247. String.format("您的%s客户【%s】已经90天未跟进,已经自动回收!", "外联", hs.getName()),
  248. hs.getUid()));
  249. }
  250. for (userDaysBo sf : sfList) {
  251. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, sf.getAid(),
  252. NoticeStatus.CUSTOMER_CHENNEL_LOSE.getCode(),
  253. String.format("您的%s客户【%s】已经90天未跟进,已经自动释放!", "外联", sf.getName()),
  254. sf.getUid()));
  255. }
  256. if(!nl.isEmpty())addNoticeBatch(nl);
  257. }
  258. }