ReleaseUserTask.java 17 KB

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