package com.goafanti.common.task; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import javax.annotation.Resource; import javax.mail.MessagingException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.goafanti.common.bo.EmailBo; import com.goafanti.common.bo.userDaysBo; import com.goafanti.common.constant.AFTConstants; import com.goafanti.common.dao.NoticeMapper; import com.goafanti.common.enums.NoticeStatus; import com.goafanti.common.enums.NoticeTypes; import com.goafanti.common.model.Notice; import com.goafanti.common.model.User; import com.goafanti.common.utils.AsyncUtils; import com.goafanti.common.utils.LoggerUtils; import com.goafanti.common.utils.StringUtils; import com.goafanti.customer.bo.LockingReleaseBo; import com.goafanti.customer.service.CustomerService; import com.goafanti.user.service.UserService; @Component @Controller public class ReleaseUserTask { @Resource private CustomerService customerService; @Resource private UserService userService; @Autowired private NoticeMapper noticeMapper; @Autowired private AsyncUtils asyncUtils; @Value(value = "${dev.name}") private String devName=null; int pointsDataLimit = 50; /** * 每天凌晨一点 * * @throws InterruptedException */ // @Scheduled(cron = "0 55 15 * * ?") @Scheduled(cron = "0 0 1 * * ?") public void startTask() { try { pushUserDays(); Thread.sleep(2000); updateUser(); } catch (InterruptedException e) { e.printStackTrace(); } } @ResponseBody @RequestMapping(value = "/open/remindUser", method = RequestMethod.GET) private void pushUserDays() throws InterruptedException { LoggerUtils.debug(getClass(), "==============客户提醒开始============"); List userList = userService.selectUserByRoleName("营销员", "营销经理"); List userTmpList = null; List ln = new ArrayList<>(); for (User u : userList) { if (StringUtils.isNotBlank(u.getId())) { userTmpList = customerService.selectReleaseUserDays(u.getId()); for (userDaysBo ub : userTmpList) { String str=null; if (ub.getChannel() == 0) { str = String.format("您的%s客户【%s】剩余天数不足十五天,请及时%s!", "私有", ub.getName(),ub.getType()==0?"跟进":"签单"); }else { str = String.format("您的%s客户【%s】剩余天数不足十五天,请及时签单!", "渠道", ub.getName()); } ln.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, ub.getAid(), NoticeStatus.CUSTOMER_LOSE_REMINDER.getCode(), str,ub.getUid())); } } } //查询15天未跟进,私有跟释放前15天重复,去除 List userChannelList = new ArrayList(); for (User u : userList) { if (StringUtils.isNotBlank(u.getId())) { userChannelList = customerService.selectChannelNotFollow(u.getId()); } for (userDaysBo ub : userChannelList) { String str=null; if (ub.getChannel() == 1) { str = String.format("您的%s客户【%s】已经十五天未跟进,请及时跟进或者限时签单!", "渠道", ub.getName()); } ln.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, ub.getAid(), NoticeStatus.CUSTOMER_PRIVATE_REMINDER.getCode(), str,ub.getUid())); } } addNoticeBatch(ln); LoggerUtils.debug(getClass(), "==============客户提醒结束============"); } /** * 将客户和业务转为待释放 */ @RequestMapping(value = "/open/updateUser", method = RequestMethod.GET) public void updateUser() { LoggerUtils.debug(getClass(), "==============客户释放开始============"); Date releaseTime = new Date(); try { List userList = userService.selectUserByRoleName("营销员", "营销经理"); List lockUserList = new ArrayList(); if (userList != null && !userList.isEmpty()) { for (User u : userList) { List userTmpList = null; List userTmpList2 = null; if (StringUtils.isNotBlank(u.getId())) { // 获取30天释放 userTmpList = customerService.selectWaitReleaseCustomer(u.getId(), 0); // 获取270天释放 if(!u.getId().equals("734092ad-6564-4021-b7ab-aab5af3a1537")){ userTmpList2 = customerService.selectWaitReleaseCustomer(u.getId(), 1); customerService.pushReleaseLog(userTmpList2, 1); } addUserNotice(userTmpList,userTmpList2); // customerService.pushReleaseLog(userTmpList, 0); if (userTmpList != null && !userTmpList.isEmpty()) lockUserList.addAll(userTmpList); if (userTmpList2 != null && !userTmpList2.isEmpty()) lockUserList.addAll(userTmpList2); } } } List newList = new ArrayList(); if (lockUserList != null && lockUserList.size() > 0) { for (int i = 0; i < lockUserList.size(); i++) { newList.add(lockUserList.get(i)); if (pointsDataLimit == newList.size() || i == lockUserList.size() - 1) { if (newList.size() > 0) { customerService.updatePendingReleaseLock(newList); } newList.clear(); Thread.sleep(2000); } } } // 获取超过90天客户,判断是否回收过,未回收则回收,已回收则释放为公共客户 List userChannelList =null; List count =new ArrayList(); if (userList != null && !userList.isEmpty()) { for (User u : userList) { userChannelList=customerService.selectChannelUserDays(u.getId()); if (userChannelList!=null) { count.addAll(userChannelList); } } } List hsList=new ArrayList<>(); List sfList=new ArrayList<>(); if (count != null && count.size() > 0) { for (int i = 0; i < count.size(); i++) { //回收 if (count.get(i).getRecovery()==0) { hsList.add(count.get(i)); }else { sfList.add(count.get(i)); } if (pointsDataLimit == (hsList.size()+sfList.size()) || i == count.size() - 1) { if(!hsList.isEmpty())userService.pushReleaseUserChannel(hsList,0); if(!sfList.isEmpty())userService.pushReleaseUserChannel(sfList,1); addChannelNotice(hsList,sfList); hsList.clear(); sfList.clear(); Thread.sleep(2000); } } } List lockList = customerService.selectPendinglockUserList(); if (lockList.size() > 0) customerService.updateReleaseLock(releaseTime); } catch (Exception e) { Notice n = new Notice(UUID.randomUUID().toString(), new Date(), 0, "1", NoticeStatus.TASK_PATENT_ERROR.getCode(), "==============客户释放失败================",null); addNotice(n); EmailBo emailBo = new EmailBo("释放客户失败", AFTConstants.ADMIN_EMAIL, "超管", "平台", "系统", devName+"释放客户失败"); try { asyncUtils.send(emailBo); } catch (UnsupportedEncodingException | MessagingException e1) { e1.printStackTrace(); } LoggerUtils.error(getClass(), "====================客户释放失败================="); LoggerUtils.error(getClass(), "客户释放失败", e); } LoggerUtils.debug(getClass(), "==============客户释放完成============"); } public void addNotice(Notice n ) { n.setType(NoticeTypes.getType(n.getNoticeType())); noticeMapper.insertSelective(n); } public void addNoticeBatch(List ln) { List newList=new ArrayList(); if (ln != null && ln.size() > 0) { for (int i = 0; i < ln.size(); i++) { ln.get(i).setType(NoticeTypes.getType(ln.get(i).getNoticeType())); newList.add(ln.get(i)); if (50 == newList.size() || i == ln.size() - 1) { if (newList.size() > 0) noticeMapper.insertBatch(newList); newList.clear(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } } } private void addUserNotice(List userTmpList, List userTmpList2) { List nl=new ArrayList(); if (!userTmpList.isEmpty()) { for (LockingReleaseBo u : userTmpList) { nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, u.getAid(), NoticeStatus.CUSTOMER_LOSE30.getCode(), String.format("您的%s客户【%s】已经30天未跟进,已经自动释放!", "私有", u.getUserName()), u.getUid())); } } if (!userTmpList2.isEmpty()) { for (LockingReleaseBo u : userTmpList2) { nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, u.getAid(), NoticeStatus.CUSTOMER_LOSE270.getCode(), String.format("您的%s客户【%s】已经270天未签单,已经自动释放!", "私有", u.getUserName()), u.getUid())); } } if(!nl.isEmpty())addNoticeBatch(nl); } private void addChannelNotice(List hsList, List sfList) { List nl=new ArrayList(); for (userDaysBo hs : hsList) { nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, hs.getAid(), NoticeStatus.CUSTOMER_CHENNEL_RECOVERY.getCode(), String.format("您的%s客户【%s】已经90天未跟进,已经自动回收!", "渠道", hs.getName()), hs.getUid())); } for (userDaysBo sf : sfList) { nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, sf.getAid(), NoticeStatus.CUSTOMER_CHENNEL_LOSE.getCode(), String.format("您的%s客户【%s】已经90天未跟进,已经自动释放!", "渠道", sf.getName()), sf.getUid())); } if(!nl.isEmpty())addNoticeBatch(nl); } }