ReleaseUserTask.java 9.9 KB

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