ReleaseUserTask.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.NoticeMapper;
  6. import com.goafanti.common.enums.NoticeStatus;
  7. import com.goafanti.common.enums.NoticeTypes;
  8. import com.goafanti.common.model.Notice;
  9. import com.goafanti.common.model.User;
  10. import com.goafanti.common.utils.AsyncUtils;
  11. import com.goafanti.common.utils.LoggerUtils;
  12. import com.goafanti.common.utils.StringUtils;
  13. import com.goafanti.customer.bo.LockingReleaseBo;
  14. import com.goafanti.customer.service.CustomerService;
  15. import com.goafanti.user.service.UserService;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.scheduling.annotation.Scheduled;
  19. import org.springframework.stereotype.Component;
  20. import javax.annotation.Resource;
  21. import java.util.ArrayList;
  22. import java.util.Date;
  23. import java.util.List;
  24. import java.util.UUID;
  25. @Component
  26. //@RestController
  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 31 15 * * ?")
  45. @Scheduled(cron = "0 0 1 * * ?")
  46. public void startTask() {
  47. try {
  48. List<User> userList = userService.selectUserByRoleName("营销员", "营销经理");
  49. pushUserDays(userList);
  50. Thread.sleep(2000);
  51. updateUser(userList);
  52. Thread.sleep(2000);
  53. } catch (InterruptedException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. private void pushUserDays(List<User> userList ) throws InterruptedException {
  58. LoggerUtils.debug(getClass(), "==============客户提醒开始============");
  59. List<Notice> ln = new ArrayList<>();
  60. for (User u : userList) {
  61. if (StringUtils.isNotBlank(u.getId())) {
  62. List<userDaysBo> userTmpList = customerService.selectReleaseUserDays(u.getId());
  63. for (userDaysBo ub : userTmpList) {
  64. String str=null;
  65. if (ub.getChannel() == 0) {
  66. String shareType="";
  67. if(ub.getNewChannel()==0)shareType="私有";
  68. else if(ub.getNewChannel()==1)shareType="渠道";
  69. str = String.format("您的%s客户【%s】剩余天数不足十五天,请及时%s!", shareType, ub.getName(),ub.getType()==0?"跟进":"签单");
  70. }else {
  71. str = String.format("您的%s客户【%s】剩余天数不足十五天,请及时签单!", "外联", ub.getName());
  72. }
  73. ln.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, ub.getAid(),
  74. NoticeStatus.CUSTOMER_LOSE_REMINDER.getCode(), str,ub.getUid()));
  75. }
  76. }
  77. }
  78. //查询15天未跟进,私有跟释放前15天重复,去除
  79. List<userDaysBo> userChannelList = new ArrayList<userDaysBo>();
  80. for (User u : userList) {
  81. if (StringUtils.isNotBlank(u.getId())) {
  82. userChannelList = customerService.selectChannelNotFollow(u.getId());
  83. }
  84. for (userDaysBo ub : userChannelList) {
  85. String str=null;
  86. if (ub.getChannel() == 1) {
  87. str = String.format("您的%s客户【%s】已经十五天未跟进,请及时跟进或者限时签单!", "外联", ub.getName());
  88. }
  89. ln.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, ub.getAid(),
  90. NoticeStatus.CUSTOMER_PRIVATE_REMINDER.getCode(), str,ub.getUid()));
  91. }
  92. }
  93. addNoticeBatch(ln);
  94. LoggerUtils.debug(getClass(), "==============客户提醒结束============");
  95. }
  96. /**
  97. * 将客户和业务转为待释放
  98. */
  99. public void updateUser(List<User> userList ) {
  100. LoggerUtils.debug(getClass(), "==============客户释放开始============");
  101. Date releaseTime = new Date();
  102. try {
  103. List<LockingReleaseBo> lockUserList = new ArrayList<LockingReleaseBo>();
  104. if (userList != null && !userList.isEmpty()) {
  105. for (User u : userList) {
  106. if (StringUtils.isNotBlank(u.getId())) {
  107. // 获取30天释放
  108. List<LockingReleaseBo> userTmpList = customerService.selectWaitReleaseCustomer(u.getId());
  109. customerService.pushReleaseLog(userTmpList, 0);
  110. addUserNotice(userTmpList);
  111. if (userTmpList != null && !userTmpList.isEmpty())
  112. lockUserList.addAll(userTmpList);
  113. }
  114. }
  115. }
  116. List<LockingReleaseBo> newList = new ArrayList<LockingReleaseBo>();
  117. List<String> aidList=new ArrayList<>();
  118. if (lockUserList != null && lockUserList.size() > 0) {
  119. for (int i = 0; i < lockUserList.size(); i++) {
  120. newList.add(lockUserList.get(i));
  121. if (pointsDataLimit == newList.size() || i == lockUserList.size() - 1) {
  122. if (newList.size() > 0) {
  123. customerService.updatePendingReleaseLock(newList);
  124. }
  125. newList.clear();
  126. Thread.sleep(2000);
  127. }
  128. //设置释放客户修改客户数
  129. String aid=lockUserList.get(i).getAid();
  130. if (!aidList.contains(aid)){
  131. aidList.add(aid);
  132. }
  133. }
  134. }
  135. for (String aid : aidList) {
  136. customerService.updateAdminUserCountByAid(aid);
  137. }
  138. pushChannel(releaseTime, userList);
  139. } catch (Exception e) {
  140. Notice n = new Notice(UUID.randomUUID().toString(), new Date(), 0, "1",
  141. NoticeStatus.TASK_PATENT_ERROR.getCode(), "==============客户释放失败================",null);
  142. addNotice(n);
  143. EmailBo emailBo = new EmailBo("释放客户失败", AFTConstants.ADMIN_EMAIL, "超管", "平台", "系统", devName+"释放客户失败");
  144. asyncUtils.send(emailBo);
  145. LoggerUtils.error(getClass(), "====================客户释放失败=================");
  146. LoggerUtils.error(getClass(), "客户释放失败", e);
  147. }
  148. LoggerUtils.debug(getClass(), "==============客户释放完成============");
  149. }
  150. /**
  151. * 处理外联
  152. * @param releaseTime
  153. * @param userList
  154. * @throws InterruptedException
  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) {
  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(!nl.isEmpty())addNoticeBatch(nl);
  231. }
  232. private void addChannelNotice(List<userDaysBo> hsList, List<userDaysBo> sfList) {
  233. List<Notice> nl=new ArrayList<Notice>();
  234. for (userDaysBo hs : hsList) {
  235. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, hs.getAid(),
  236. NoticeStatus.CUSTOMER_CHENNEL_RECOVERY.getCode(),
  237. String.format("您的%s客户【%s】已经90天未跟进,已经自动回收!", "外联", hs.getName()),
  238. hs.getUid()));
  239. }
  240. for (userDaysBo sf : sfList) {
  241. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, sf.getAid(),
  242. NoticeStatus.CUSTOMER_CHENNEL_LOSE.getCode(),
  243. String.format("您的%s客户【%s】已经90天未跟进,已经自动释放!", "外联", sf.getName()),
  244. sf.getUid()));
  245. }
  246. if(!nl.isEmpty())addNoticeBatch(nl);
  247. }
  248. }