ReleaseUserTask.java 10 KB

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