ReleaseUserTask.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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.dao.UserTransferLogMapper;
  10. import com.goafanti.common.enums.NoticeStatus;
  11. import com.goafanti.common.enums.NoticeTypes;
  12. import com.goafanti.common.model.AdminUserCount;
  13. import com.goafanti.common.model.Notice;
  14. import com.goafanti.common.model.User;
  15. import com.goafanti.common.model.UserTransferLog;
  16. import com.goafanti.common.utils.AsyncUtils;
  17. import com.goafanti.common.utils.LoggerUtils;
  18. import com.goafanti.common.utils.StringUtils;
  19. import com.goafanti.customer.bo.LockingReleaseBo;
  20. import com.goafanti.customer.service.CustomerService;
  21. import com.goafanti.user.service.UserService;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.beans.factory.annotation.Value;
  24. import org.springframework.scheduling.annotation.Scheduled;
  25. import org.springframework.stereotype.Component;
  26. import javax.annotation.Resource;
  27. import java.time.LocalDateTime;
  28. import java.time.format.DateTimeFormatter;
  29. import java.time.temporal.ChronoUnit;
  30. import java.util.ArrayList;
  31. import java.util.Date;
  32. import java.util.List;
  33. import java.util.UUID;
  34. import java.util.stream.Collectors;
  35. @Component
  36. //@RestController
  37. public class ReleaseUserTask {
  38. @Resource
  39. private CustomerService customerService;
  40. @Resource
  41. private UserService userService;
  42. @Autowired
  43. private NoticeMapper noticeMapper;
  44. @Autowired
  45. private AsyncUtils asyncUtils;
  46. @Value(value = "${dev.name}")
  47. private String devName=null;
  48. int pointsDataLimit = 50;
  49. @Autowired
  50. private AdminUserCountMapper adminUserCountMapper;
  51. @Autowired
  52. private RestrictProjectMapper restProjectMapper;
  53. @Autowired
  54. private UserTransferLogMapper userTransferLogMapper;
  55. /**
  56. * 客户释放
  57. *
  58. */
  59. @Scheduled(cron = "0 30 16 * * ?")
  60. // @RequestMapping("/open/test")
  61. // @Scheduled(cron = "0 0 1 * * ?")
  62. public void startTask() {
  63. try {
  64. List<User> userList = userService.selectUserByRoleName("营销员", "营销经理");
  65. pushUserDays(userList);
  66. Thread.sleep(2000);
  67. updateUser(userList);
  68. Thread.sleep(2000);
  69. pushRestrictProject(userList);
  70. } catch (InterruptedException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. /**
  75. * 限时项目定时任务
  76. */
  77. private void pushRestrictProject(List<User> userList) {
  78. LoggerUtils.debug(getClass(), "==============限时项目丢失开始============");
  79. List<Notice> ln = new ArrayList<>();
  80. List<UserTransferLog> lu=new ArrayList<>();
  81. LocalDateTime now = LocalDateTime.now();
  82. Date date = new Date();
  83. List<Integer> list = new ArrayList<>();
  84. for (User u : userList) {
  85. if (StringUtils.isNotBlank(u.getId())) {
  86. List<OutRestrictProject> rpList = restProjectMapper.selectByUid(u.getId());
  87. for (OutRestrictProject rp : rpList) {
  88. LocalDateTime lockTime = rp.getLockTime().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDateTime();
  89. if (rp.getType()==1){
  90. LocalDateTime endTime2 = lockTime.plusDays(15);
  91. long between2 = ChronoUnit.DAYS.between(now, endTime2);
  92. if (between2==0){
  93. ln.add(new Notice(UUID.randomUUID().toString(),date, 0, rp.getAid(),
  94. NoticeStatus.RESTRICT_PROJECT_LOSE30_REMIND.getCode(),
  95. String.format("您的客户[%s]限时私有项目[%s]还有15天即将丢失!",rp.getUserName(), rp.getProjectName())
  96. ,rp.getUid()));
  97. }else {
  98. LocalDateTime endTime = lockTime.plusDays(30);
  99. long between = ChronoUnit.DAYS.between(now, endTime);
  100. if (between<1){
  101. ln.add(new Notice(UUID.randomUUID().toString(),date, 0, rp.getAid(),
  102. NoticeStatus.RESTRICT_PROJECT_LOSE30.getCode(),
  103. String.format("您的客户[%s]限时私有项目[%s]已丢失!",rp.getUserName(), rp.getProjectName())
  104. ,rp.getUid()));
  105. list.add(rp.getId());
  106. lu.add(new UserTransferLog(rp.getUid(), rp.getAid(), rp.getPid(), 18, "限时项目释放(30天)",date));
  107. }
  108. }
  109. }else if (rp.getType()==2){
  110. LocalDateTime endTime2 = lockTime.plusDays(255);
  111. long between2 = ChronoUnit.DAYS.between(now, endTime2);
  112. if (between2==0){
  113. ln.add(new Notice(UUID.randomUUID().toString(),date, 0, rp.getAid(),
  114. NoticeStatus.RESTRICT_PROJECT_LOSE270_REMIND.getCode(),
  115. String.format("您的客户[%s]限时签单项目[%s]还有15天即将丢失!",rp.getUserName(), rp.getProjectName())
  116. ,rp.getUid()));
  117. }else{
  118. LocalDateTime endTime = lockTime.plusDays(270);
  119. long between = ChronoUnit.DAYS.between(now, endTime);
  120. if (between<1){
  121. ln.add(new Notice(UUID.randomUUID().toString(),date, 0, rp.getAid(),
  122. NoticeStatus.RESTRICT_PROJECT_LOSE270.getCode(),
  123. String.format("您的客户[%s]限时签单项目[%s]已丢失!",rp.getUserName(), rp.getProjectName())
  124. ,rp.getUid()));
  125. list.add(rp.getId());
  126. lu.add(new UserTransferLog(rp.getUid(), rp.getAid(), rp.getPid(), 19, "限时项目释放(270天)",date));
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }
  133. addNoticeBatch(ln);
  134. pushReleaseRestrictProject(list);
  135. addUserTransferLogBatch(lu);
  136. LoggerUtils.debug(getClass(), "==============限时项目结束============");
  137. }
  138. private void addUserTransferLogBatch(List<UserTransferLog> list) {
  139. List<UserTransferLog> list2 = new ArrayList<>();
  140. int i=0;
  141. for (UserTransferLog e : list) {
  142. i++;
  143. list2.add(e);
  144. if(list2.size()==pointsDataLimit||i==list.size()){
  145. userTransferLogMapper.insertList(list2);
  146. list2.clear();
  147. }
  148. }
  149. }
  150. public static void main(String[] args) {
  151. LocalDateTime now = LocalDateTime.now();
  152. LocalDateTime endTime = LocalDateTime.of(2024,5,14,0,0);
  153. endTime= endTime.minusDays(270);
  154. long between = ChronoUnit.DAYS.between(now, endTime);
  155. System.out.println(between);
  156. System.out.println(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  157. }
  158. private void pushReleaseRestrictProject(List<Integer> list) {
  159. List<Integer> list2 = new ArrayList<>();
  160. int i=0;
  161. for (Integer e : list) {
  162. i++;
  163. list2.add(e);
  164. if(list2.size()==pointsDataLimit||i==list.size()){
  165. restProjectMapper.updateRelease(list2);
  166. list2.clear();
  167. }
  168. }
  169. }
  170. private void pushUserDays(List<User> userList ) throws InterruptedException {
  171. LoggerUtils.debug(getClass(), "==============客户提醒开始============");
  172. List<Notice> ln = new ArrayList<>();
  173. List<userDaysBo> userChannelList = new ArrayList<userDaysBo>();
  174. for (User u : userList) {
  175. if (StringUtils.isNotBlank(u.getId())&&AFTConstants.CAOJIN_AID.equals(u.getId())) {
  176. List<userDaysBo> userTmpList = customerService.selectReleaseUserDays(u.getId());
  177. for (userDaysBo ub : userTmpList) {
  178. String str=null;
  179. if (ub.getChannel() == 0) {
  180. String shareType="";
  181. if(ub.getNewChannel()==0&&ub.getType()==0)shareType="私有";
  182. else if(ub.getNewChannel()==0&&ub.getType()==2)shareType="签单";
  183. else if(ub.getNewChannel()==1)shareType="渠道";
  184. str = String.format("您的%s客户【%s】剩余天数不足十五天,请及时%s!", shareType, ub.getName(),"跟进");
  185. }else {
  186. str = String.format("您的%s客户【%s】剩余天数不足十五天,请及时签单!", "外联", ub.getName());
  187. }
  188. ln.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, ub.getAid(),
  189. NoticeStatus.CUSTOMER_LOSE_REMINDER.getCode(), str,ub.getUid()));
  190. }
  191. }
  192. //查询15天未跟进,私有跟释放前15天重复,去除
  193. if (StringUtils.isNotBlank(u.getId())) {
  194. userChannelList = customerService.selectChannelNotFollow(u.getId());
  195. }
  196. for (userDaysBo ub : userChannelList) {
  197. String str=null;
  198. if (ub.getChannel() == 1) {
  199. str = String.format("您的%s客户【%s】已经十五天未跟进,请及时跟进或者限时签单!", "外联", ub.getName());
  200. }
  201. ln.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, ub.getAid(),
  202. NoticeStatus.CUSTOMER_PRIVATE_REMINDER.getCode(), str,ub.getUid()));
  203. }
  204. }
  205. addNoticeBatch(ln);
  206. LoggerUtils.debug(getClass(), "==============客户提醒结束============");
  207. }
  208. /**
  209. * 将客户和业务转为待释放
  210. */
  211. public void updateUser(List<User> userList ) {
  212. LoggerUtils.debug(getClass(), "==============客户释放开始============");
  213. Date releaseTime = new Date();
  214. try {
  215. List<LockingReleaseBo> lockUserList = new ArrayList<>();
  216. List<LockingReleaseBo> lockUserList2 = new ArrayList<>();
  217. if (userList != null && !userList.isEmpty()) {
  218. for (User u : userList) {
  219. System.out.println(u.getId());
  220. if (StringUtils.isNotBlank(u.getId())&&!AFTConstants.CAOJIN_AID.equals(u.getId())) {
  221. // 获取30天释放 释放以锁定表与user_mid最后跟进取时间
  222. List<LockingReleaseBo> userTmpList = customerService.selectWaitReleaseCustomer(u.getId());
  223. customerService.pushReleaseLog(userTmpList);
  224. addUserNotice(userTmpList);
  225. if (!userTmpList.isEmpty()) {
  226. //30天私有和270天签单业务释放不改变
  227. List<LockingReleaseBo> collect = userTmpList.stream().filter(e ->
  228. (e.getShareType() == 0&& e.getType()==0)||e.getType()==1
  229. ).collect(Collectors.toList());
  230. lockUserList.addAll(collect);
  231. //30天签单单独只释放签签单
  232. List<LockingReleaseBo> collect2 = userTmpList.stream().filter(e ->
  233. e.getShareType() == 2&& e.getType()==0).collect(Collectors.toList());
  234. lockUserList2.addAll(collect2);
  235. }
  236. }
  237. }
  238. }
  239. List<LockingReleaseBo> newList = new ArrayList<LockingReleaseBo>();
  240. //处理私有客户
  241. if (!lockUserList.isEmpty()) {
  242. for (int i = 0; i < lockUserList.size(); i++) {
  243. newList.add(lockUserList.get(i));
  244. if (pointsDataLimit == newList.size() || i == lockUserList.size() - 1) {
  245. customerService.updatePendingReleaseLock(newList);
  246. newList.clear();
  247. Thread.sleep(2000);
  248. }
  249. }
  250. }
  251. if (!lockUserList2.isEmpty()) {
  252. for (int i = 0; i < lockUserList2.size(); i++) {
  253. newList.add(lockUserList2.get(i));
  254. System.out.println(lockUserList2.get(i).getUserName());
  255. if (pointsDataLimit == newList.size() || i == lockUserList.size() - 1) {
  256. userService.updateUserShareType(newList);
  257. newList.clear();
  258. Thread.sleep(2000);
  259. }
  260. }
  261. }
  262. //处理每个人重复的天数
  263. pushSalesmanCount(lockUserList);
  264. pushChannel(releaseTime, userList);
  265. } catch (Exception e) {
  266. Notice n = new Notice(UUID.randomUUID().toString(), new Date(), 0, "1",
  267. NoticeStatus.TASK_PATENT_ERROR.getCode(), "==============客户释放失败================",null);
  268. addNotice(n);
  269. EmailBo emailBo = new EmailBo("释放客户失败", AFTConstants.ADMIN_EMAIL, "平台", "超管");
  270. asyncUtils.send(emailBo);
  271. LoggerUtils.error(getClass(), "====================客户释放失败=================");
  272. LoggerUtils.error(getClass(), "客户释放失败", e);
  273. }
  274. LoggerUtils.debug(getClass(), "==============客户释放完成============");
  275. }
  276. private void pushSalesmanCount(List<LockingReleaseBo> aidList) {
  277. for (LockingReleaseBo e : aidList) {
  278. String dates= e.getLockTime();
  279. AdminUserCount adminUserCount = customerService.getAdminUserCount(e.getAid(), dates);
  280. if (adminUserCount!=null&&adminUserCount.getPrivateCount()>0){
  281. AdminUserCount newAdminUserCount = new AdminUserCount();
  282. newAdminUserCount.setId(adminUserCount.getId());
  283. newAdminUserCount.setPrivateCount(adminUserCount.getPrivateCount()-1);
  284. adminUserCountMapper.updateByPrimaryKeySelective(newAdminUserCount);
  285. }
  286. }
  287. }
  288. /**
  289. * 处理外联
  290. */
  291. private void pushChannel(Date releaseTime, List<User> userList) throws InterruptedException {
  292. // 获取超过90天客户,判断是否回收过,未回收则回收,已回收则释放为公共客户
  293. List<userDaysBo> userChannelList =null;
  294. List<userDaysBo> count =new ArrayList<userDaysBo>();
  295. if (userList != null && !userList.isEmpty()) {
  296. for (User u : userList) {
  297. userChannelList=customerService.selectChannelUserDays(u.getId());
  298. if (userChannelList!=null) {
  299. count.addAll(userChannelList);
  300. }
  301. }
  302. }
  303. List<userDaysBo> hsList=new ArrayList<>();
  304. List<userDaysBo> sfList=new ArrayList<>();
  305. if (count != null && !count.isEmpty()) {
  306. for (int i = 0; i < count.size(); i++) {
  307. //回收
  308. if (count.get(i).getRecovery()==0) {
  309. hsList.add(count.get(i));
  310. }else {
  311. sfList.add(count.get(i));
  312. }
  313. if (pointsDataLimit == (hsList.size()+sfList.size()) || i == count.size() - 1) {
  314. if(!hsList.isEmpty())userService.pushReleaseUserChannel(hsList,0);
  315. if(!sfList.isEmpty())userService.pushReleaseUserChannel(sfList,1);
  316. addChannelNotice(hsList,sfList);
  317. hsList.clear();
  318. sfList.clear();
  319. Thread.sleep(2000);
  320. }
  321. }
  322. }
  323. List<LockingReleaseBo> lockList = customerService.selectPendinglockUserList();
  324. if (!lockList.isEmpty())
  325. customerService.updateReleaseLock(releaseTime);
  326. }
  327. public void addNotice(Notice n ) {
  328. n.setType(NoticeTypes.getType(n.getNoticeType()));
  329. noticeMapper.insertSelective(n);
  330. }
  331. public void addNoticeBatch(List<Notice> ln) {
  332. List<Notice> newList=new ArrayList<Notice>();
  333. if (ln != null && !ln.isEmpty()) {
  334. for (int i = 0; i < ln.size(); i++) {
  335. ln.get(i).setType(NoticeTypes.getType(ln.get(i).getNoticeType()));
  336. newList.add(ln.get(i));
  337. if (50 == newList.size() || i == ln.size() - 1) {
  338. if (!newList.isEmpty()) {
  339. //发送站内信
  340. noticeMapper.insertBatch(newList);
  341. }
  342. newList.clear();
  343. try {
  344. Thread.sleep(2000);
  345. } catch (InterruptedException e) {
  346. e.printStackTrace();
  347. }
  348. }
  349. }
  350. }
  351. }
  352. private void addUserNotice(List<LockingReleaseBo> userTmpList) {
  353. List<Notice> nl=new ArrayList<Notice>();
  354. if (!userTmpList.isEmpty()) {
  355. for (LockingReleaseBo u : userTmpList) {
  356. String str="";
  357. String str2="";
  358. String str3="";
  359. if(u.getNewChannel()==0&&u.getShareType()==0){
  360. str2="私有";
  361. str3="或270天未签单";
  362. }else if(u.getNewChannel()==0&&u.getShareType()==2){
  363. str2="签单";
  364. }else if(u.getNewChannel()==1){
  365. str2= "渠道";
  366. }
  367. str=String.format("您的%s客户【%s】已经30天未跟进%s,已经自动释放!", str2, u.getUserName(),str3);
  368. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, u.getAid(),
  369. NoticeStatus.CUSTOMER_LOSE30.getCode(),
  370. str,
  371. u.getUid()));
  372. }
  373. }
  374. if(!nl.isEmpty())addNoticeBatch(nl);
  375. }
  376. private void addChannelNotice(List<userDaysBo> hsList, List<userDaysBo> sfList) {
  377. List<Notice> nl=new ArrayList<Notice>();
  378. for (userDaysBo hs : hsList) {
  379. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, hs.getAid(),
  380. NoticeStatus.CUSTOMER_CHENNEL_RECOVERY.getCode(),
  381. String.format("您的%s客户【%s】已经90天未跟进,已经自动回收!", "外联", hs.getName()),
  382. hs.getUid()));
  383. }
  384. for (userDaysBo sf : sfList) {
  385. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, sf.getAid(),
  386. NoticeStatus.CUSTOMER_CHENNEL_LOSE.getCode(),
  387. String.format("您的%s客户【%s】已经90天未跟进,已经自动释放!", "外联", sf.getName()),
  388. sf.getUid()));
  389. }
  390. if(!nl.isEmpty())addNoticeBatch(nl);
  391. }
  392. }