ReleaseUserTask.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 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(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. 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> lockSignList = 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. lockSignList.addAll(users1);
  277. }
  278. }
  279. }
  280. //签单客户要单独处理,没有锁,直接释放
  281. pushSignUser(lockSignList);
  282. //处理私有客户
  283. pushPrivateUser(lockUserList);
  284. pushChannel(releaseTime, userList);
  285. //客户释放
  286. List<LockingReleaseBo> lockList = customerService.selectPendinglockUserList();
  287. if (!lockList.isEmpty()){
  288. customerService.updateReleaseLock(releaseTime);
  289. }
  290. } catch (Exception e) {
  291. Notice n = new Notice(UUID.randomUUID().toString(), new Date(), 0, "1",
  292. NoticeStatus.TASK_PATENT_ERROR.getCode(), "==============客户释放失败================",null);
  293. addNotice(n);
  294. EmailBo emailBo = new EmailBo("释放客户失败", AFTConstants.ADMIN_EMAIL, "平台", "超管");
  295. asyncUtils.send(emailBo);
  296. LoggerUtils.error(getClass(), "====================客户释放失败=================");
  297. LoggerUtils.error(getClass(), "客户释放失败", e);
  298. }
  299. LoggerUtils.debug(getClass(), "==============客户释放完成============");
  300. }
  301. private void pushPrivateUser(List<LockingReleaseBo> lockUserList) throws InterruptedException {
  302. List<LockingReleaseBo> newList=new ArrayList<LockingReleaseBo>();
  303. if (!lockUserList.isEmpty()) {
  304. for (int i = 0; i < lockUserList.size(); i++) {
  305. newList.add(lockUserList.get(i));
  306. if (pointsDataLimit == newList.size() || i == lockUserList.size() - 1) {
  307. customerService.updatePendingReleaseLock(newList);
  308. newList.clear();
  309. Thread.sleep(2000);
  310. }
  311. }
  312. }
  313. }
  314. private void pushSignUser(List<LockingReleaseBo> lockSignList) throws InterruptedException {
  315. List<LockingReleaseBo> newList = new ArrayList<LockingReleaseBo>();
  316. if (!lockSignList.isEmpty()) {
  317. for (int i = 0; i < lockSignList.size(); i++) {
  318. newList.add(lockSignList.get(i));
  319. if (pointsDataLimit == newList.size() || i == lockSignList.size() - 1) {
  320. customerService.updateUserShareType(newList);
  321. newList.clear();
  322. Thread.sleep(2000);
  323. }
  324. }
  325. }
  326. }
  327. /**
  328. * 处理外联
  329. */
  330. private void pushChannel(Date releaseTime, List<User> userList) throws InterruptedException {
  331. // 获取超过90天客户,判断是否回收过,未回收则回收,已回收则释放为公共客户
  332. List<userDaysBo> userChannelList =null;
  333. List<userDaysBo> count =new ArrayList<userDaysBo>();
  334. if (userList != null && !userList.isEmpty()) {
  335. for (User u : userList) {
  336. userChannelList=customerService.selectChannelUserDays(u.getId());
  337. if (userChannelList!=null) {
  338. count.addAll(userChannelList);
  339. }
  340. }
  341. }
  342. List<userDaysBo> hsList=new ArrayList<>();
  343. List<userDaysBo> sfList=new ArrayList<>();
  344. if (count != null && !count.isEmpty()) {
  345. for (int i = 0; i < count.size(); i++) {
  346. //回收
  347. if (count.get(i).getRecovery()==0) {
  348. hsList.add(count.get(i));
  349. }else {
  350. sfList.add(count.get(i));
  351. }
  352. if (pointsDataLimit == (hsList.size()+sfList.size()) || i == count.size() - 1) {
  353. if(!hsList.isEmpty())userService.pushReleaseUserChannel(hsList,0);
  354. if(!sfList.isEmpty())userService.pushReleaseUserChannel(sfList,1);
  355. addChannelNotice(hsList,sfList);
  356. hsList.clear();
  357. sfList.clear();
  358. Thread.sleep(2000);
  359. }
  360. }
  361. }
  362. }
  363. public void addNotice(Notice n ) {
  364. n.setType(NoticeTypes.getType(n.getNoticeType()));
  365. noticeMapper.insertSelective(n);
  366. }
  367. public void addNoticeBatch(List<Notice> ln) {
  368. List<Notice> newList=new ArrayList<Notice>();
  369. if (ln != null && !ln.isEmpty()) {
  370. for (int i = 0; i < ln.size(); i++) {
  371. ln.get(i).setType(NoticeTypes.getType(ln.get(i).getNoticeType()));
  372. newList.add(ln.get(i));
  373. if (50 == newList.size() || i == ln.size() - 1) {
  374. if (!newList.isEmpty()) {
  375. //发送站内信
  376. noticeMapper.insertBatch(newList);
  377. }
  378. newList.clear();
  379. try {
  380. Thread.sleep(2000);
  381. } catch (InterruptedException e) {
  382. e.printStackTrace();
  383. }
  384. }
  385. }
  386. }
  387. }
  388. private void addUserNotice(List<LockingReleaseBo> userTmpList) {
  389. List<Notice> nl=new ArrayList<Notice>();
  390. if (!userTmpList.isEmpty()) {
  391. for (LockingReleaseBo u : userTmpList) {
  392. String str="";
  393. String str2="";
  394. String str3="";
  395. if(u.getNewChannel()==0&&u.getShareType()==0){
  396. str2="私有";
  397. str3="或270天未签单";
  398. }else if(u.getNewChannel()==0&&u.getShareType()==2){
  399. str2="签单";
  400. }else if(u.getNewChannel()==1){
  401. str2= "渠道";
  402. }
  403. str=String.format("您的%s客户【%s】已经30天未跟进%s,已经自动释放!", str2, u.getUserName(),str3);
  404. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, u.getAid(),
  405. NoticeStatus.CUSTOMER_LOSE30.getCode(),
  406. str,
  407. u.getUid()));
  408. }
  409. }
  410. if(!nl.isEmpty())addNoticeBatch(nl);
  411. }
  412. private void addChannelNotice(List<userDaysBo> hsList, List<userDaysBo> sfList) {
  413. List<Notice> nl=new ArrayList<Notice>();
  414. for (userDaysBo hs : hsList) {
  415. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, hs.getAid(),
  416. NoticeStatus.CUSTOMER_CHENNEL_RECOVERY.getCode(),
  417. String.format("您的%s客户【%s】已经90天未跟进,已经自动回收!", "外联", hs.getName()),
  418. hs.getUid()));
  419. }
  420. for (userDaysBo sf : sfList) {
  421. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, sf.getAid(),
  422. NoticeStatus.CUSTOMER_CHENNEL_LOSE.getCode(),
  423. String.format("您的%s客户【%s】已经90天未跟进,已经自动释放!", "外联", sf.getName()),
  424. sf.getUid()));
  425. }
  426. if(!nl.isEmpty())addNoticeBatch(nl);
  427. }
  428. }