ReleaseUserTask.java 9.8 KB

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