ReleaseUserTask.java 12 KB

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