ReleaseUserTask.java 16 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.NoticeMapper;
  7. import com.goafanti.common.dao.RestrictProjectMapper;
  8. import com.goafanti.common.dao.UserMapper;
  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.Notice;
  13. import com.goafanti.common.model.User;
  14. import com.goafanti.common.model.UserTransferLog;
  15. import com.goafanti.common.utils.AsyncUtils;
  16. import com.goafanti.common.utils.LoggerUtils;
  17. import com.goafanti.common.utils.StringUtils;
  18. import com.goafanti.customer.bo.LockingReleaseBo;
  19. import com.goafanti.customer.service.CustomerService;
  20. import com.goafanti.user.service.UserService;
  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.ArrayList;
  28. import java.util.Date;
  29. import java.util.List;
  30. import java.util.UUID;
  31. @Component
  32. //@RestController
  33. public class ReleaseUserTask {
  34. @Resource
  35. private CustomerService customerService;
  36. @Resource
  37. private UserService userService;
  38. @Resource
  39. private NoticeMapper noticeMapper;
  40. @Resource
  41. private AsyncUtils asyncUtils;
  42. @Value(value = "${dev.name}")
  43. private String devName=null;
  44. int pointsDataLimit = 50;
  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 48 11 * * ?")
  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(AFTConstants.CLUE_USER_MAX);
  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. /**
  215. * 客户提醒
  216. * @param userList
  217. * @throws InterruptedException
  218. */
  219. private void pushUserDays(List<User> userList ) throws InterruptedException {
  220. LoggerUtils.debug(getClass(), "==============客户提醒开始============");
  221. List<Notice> ln = new ArrayList<>();
  222. List<userDaysBo> userChannelList = new ArrayList<userDaysBo>();
  223. for (User u : userList) {
  224. if (StringUtils.isNotBlank(u.getId())&&!AFTConstants.CAOJIN_AID.equals(u.getId())) {
  225. List<userDaysBo> userTmpList = customerService.selectReleaseUserDays(u.getId());
  226. for (userDaysBo ub : userTmpList) {
  227. String str=null;
  228. if (ub.getChannel() == 0) {
  229. String shareType="";
  230. if(ub.getNewChannel()==0&&ub.getType()==0)shareType="私有";
  231. else if(ub.getNewChannel()==0&&ub.getType()==2)shareType="签单";
  232. else if(ub.getNewChannel()==1)shareType="渠道";
  233. str = String.format("您的%s客户【%s】剩余天数不足十五天,请及时%s!", shareType, ub.getName(),"跟进");
  234. }else {
  235. str = String.format("您的%s客户【%s】剩余天数不足十五天,请及时签单!", "外联", ub.getName());
  236. }
  237. ln.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, ub.getAid(),
  238. NoticeStatus.CUSTOMER_LOSE_REMINDER.getCode(), str,ub.getUid()));
  239. }
  240. }
  241. //查询15天未跟进,私有跟释放前15天重复,去除
  242. if (StringUtils.isNotBlank(u.getId())) {
  243. userChannelList = customerService.selectChannelNotFollow(u.getId());
  244. }
  245. for (userDaysBo ub : userChannelList) {
  246. String str=null;
  247. if (ub.getChannel() == 1) {
  248. str = String.format("您的%s客户【%s】已经十五天未跟进,请及时跟进或者限时签单!", "外联", ub.getName());
  249. }
  250. ln.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, ub.getAid(),
  251. NoticeStatus.CUSTOMER_PRIVATE_REMINDER.getCode(), str,ub.getUid()));
  252. }
  253. }
  254. addNoticeBatch(ln);
  255. LoggerUtils.debug(getClass(), "==============客户提醒结束============");
  256. }
  257. /**
  258. * 将客户和业务转为待释放
  259. */
  260. public void updateUser(List<User> userList ) {
  261. LoggerUtils.debug(getClass(), "==============客户释放开始============");
  262. Date releaseTime = new Date();
  263. try {
  264. List<LockingReleaseBo> lockUserList = new ArrayList<>();
  265. List<LockingReleaseBo> lockSignList = new ArrayList<>();
  266. if (userList != null && !userList.isEmpty()) {
  267. for (User u : userList) {
  268. System.out.println(u.getId());
  269. if (StringUtils.isNotBlank(u.getId())&&!AFTConstants.CAOJIN_AID.equals(u.getId())) {
  270. // 获取30天释放 释放以锁定表与user_mid最后跟进取时间
  271. List<LockingReleaseBo> userTmpList = customerService.selectWaitReleaseCustomer(u.getId());
  272. customerService.pushReleaseLog(userTmpList);
  273. addUserNotice(userTmpList);
  274. if (!userTmpList.isEmpty()) {
  275. lockUserList.addAll(userTmpList);
  276. }
  277. //获取需要释放的签单客户
  278. List<LockingReleaseBo> users1 = userService.selectSignReleaseUser(u.getId());
  279. customerService.pushReleaseLog(users1);
  280. addUserNotice(users1);
  281. lockSignList.addAll(users1);
  282. }
  283. }
  284. }
  285. //签单客户要单独处理,没有锁,直接释放
  286. pushSignUser(lockSignList);
  287. //处理私有客户
  288. pushPrivateUser(lockUserList);
  289. pushChannel(releaseTime, userList);
  290. //客户释放
  291. List<LockingReleaseBo> lockList = customerService.selectPendinglockUserList();
  292. if (!lockList.isEmpty()){
  293. customerService.updateReleaseLock(releaseTime);
  294. }
  295. } catch (Exception e) {
  296. Notice n = new Notice(UUID.randomUUID().toString(), new Date(), 0, "1",
  297. NoticeStatus.TASK_PATENT_ERROR.getCode(), "==============客户释放失败================",null);
  298. addNotice(n);
  299. EmailBo emailBo = new EmailBo("释放客户失败", AFTConstants.ADMIN_EMAIL, "平台", "超管");
  300. asyncUtils.send(emailBo);
  301. LoggerUtils.error(getClass(), "====================客户释放失败=================");
  302. LoggerUtils.error(getClass(), "客户释放失败", e);
  303. }
  304. LoggerUtils.debug(getClass(), "==============客户释放完成============");
  305. }
  306. private void pushPrivateUser(List<LockingReleaseBo> lockUserList) throws InterruptedException {
  307. List<LockingReleaseBo> newList=new ArrayList<LockingReleaseBo>();
  308. if (!lockUserList.isEmpty()) {
  309. for (int i = 0; i < lockUserList.size(); i++) {
  310. newList.add(lockUserList.get(i));
  311. if (pointsDataLimit == newList.size() || i == lockUserList.size() - 1) {
  312. customerService.updatePendingReleaseLock(newList);
  313. newList.clear();
  314. Thread.sleep(2000);
  315. }
  316. }
  317. }
  318. }
  319. private void pushSignUser(List<LockingReleaseBo> lockSignList) throws InterruptedException {
  320. List<LockingReleaseBo> newList = new ArrayList<LockingReleaseBo>();
  321. if (!lockSignList.isEmpty()) {
  322. for (int i = 0; i < lockSignList.size(); i++) {
  323. newList.add(lockSignList.get(i));
  324. if (pointsDataLimit == newList.size() || i == lockSignList.size() - 1) {
  325. customerService.updateUserShareType(newList);
  326. newList.clear();
  327. Thread.sleep(2000);
  328. }
  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. }
  368. public void addNotice(Notice n ) {
  369. n.setType(NoticeTypes.getType(n.getNoticeType()));
  370. noticeMapper.insertSelective(n);
  371. }
  372. public void addNoticeBatch(List<Notice> ln) {
  373. List<Notice> newList=new ArrayList<Notice>();
  374. if (ln != null && !ln.isEmpty()) {
  375. for (int i = 0; i < ln.size(); i++) {
  376. ln.get(i).setType(NoticeTypes.getType(ln.get(i).getNoticeType()));
  377. newList.add(ln.get(i));
  378. if (50 == newList.size() || i == ln.size() - 1) {
  379. if (!newList.isEmpty()) {
  380. //发送站内信
  381. noticeMapper.insertBatch(newList);
  382. }
  383. newList.clear();
  384. try {
  385. Thread.sleep(2000);
  386. } catch (InterruptedException e) {
  387. e.printStackTrace();
  388. }
  389. }
  390. }
  391. }
  392. }
  393. private void addUserNotice(List<LockingReleaseBo> userTmpList) {
  394. List<Notice> nl=new ArrayList<Notice>();
  395. if (!userTmpList.isEmpty()) {
  396. for (LockingReleaseBo u : userTmpList) {
  397. String str="";
  398. String str2="";
  399. String str3="";
  400. if(u.getNewChannel()==0&&u.getShareType()==0){
  401. str2="私有";
  402. str3="或270天未签单";
  403. }else if(u.getNewChannel()==0&&u.getShareType()==2){
  404. str2="签单";
  405. }else if(u.getNewChannel()==1){
  406. str2= "渠道";
  407. }
  408. str=String.format("您的%s客户【%s】已经30天未跟进%s,已经自动释放!", str2, u.getUserName(),str3);
  409. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, u.getAid(),
  410. NoticeStatus.CUSTOMER_LOSE30.getCode(),
  411. str,
  412. u.getUid()));
  413. }
  414. }
  415. if(!nl.isEmpty())addNoticeBatch(nl);
  416. }
  417. private void addChannelNotice(List<userDaysBo> hsList, List<userDaysBo> sfList) {
  418. List<Notice> nl=new ArrayList<Notice>();
  419. for (userDaysBo hs : hsList) {
  420. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, hs.getAid(),
  421. NoticeStatus.CUSTOMER_CHENNEL_RECOVERY.getCode(),
  422. String.format("您的%s客户【%s】已经90天未跟进,已经自动回收!", "外联", hs.getName()),
  423. hs.getUid()));
  424. }
  425. for (userDaysBo sf : sfList) {
  426. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, sf.getAid(),
  427. NoticeStatus.CUSTOMER_CHENNEL_LOSE.getCode(),
  428. String.format("您的%s客户【%s】已经90天未跟进,已经自动释放!", "外联", sf.getName()),
  429. sf.getUid()));
  430. }
  431. if(!nl.isEmpty())addNoticeBatch(nl);
  432. }
  433. }