ReleaseUserTask.java 14 KB

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