| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- package com.goafanti.common.task;
- import com.goafanti.business.bo.OutRestrictProject;
- import com.goafanti.common.bo.EmailBo;
- import com.goafanti.common.bo.userDaysBo;
- import com.goafanti.common.constant.AFTConstants;
- import com.goafanti.common.dao.AdminUserCountMapper;
- import com.goafanti.common.dao.NoticeMapper;
- import com.goafanti.common.dao.RestrictProjectMapper;
- import com.goafanti.common.dao.UserTransferLogMapper;
- import com.goafanti.common.enums.NoticeStatus;
- import com.goafanti.common.enums.NoticeTypes;
- import com.goafanti.common.model.AdminUserCount;
- import com.goafanti.common.model.Notice;
- import com.goafanti.common.model.User;
- import com.goafanti.common.model.UserTransferLog;
- 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;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import javax.annotation.Resource;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.time.temporal.ChronoUnit;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.UUID;
- //@Component
- @RestController
- 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;
- @Autowired
- private AdminUserCountMapper adminUserCountMapper;
- @Autowired
- private RestrictProjectMapper restProjectMapper;
- @Autowired
- private UserTransferLogMapper userTransferLogMapper;
- /**
- * 客户释放
- *
- */
- // @Scheduled(cron = "0 23 9 * * ?")
- @RequestMapping("/open/test")
- // @Scheduled(cron = "0 0 1 * * ?")
- public void startTask() {
- try {
- List<User> userList = userService.selectUserByRoleName("营销员", "营销经理");
- pushUserDays(userList);
- Thread.sleep(2000);
- updateUser(userList);
- Thread.sleep(2000);
- pushRestrictProject(userList);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- /**
- * 限时项目定时任务
- */
- private void pushRestrictProject(List<User> userList) {
- LoggerUtils.debug(getClass(), "==============限时项目丢失开始============");
- List<Notice> ln = new ArrayList<>();
- List<UserTransferLog> lu=new ArrayList<>();
- LocalDateTime now = LocalDateTime.now();
- Date date = new Date();
- List<Integer> list = new ArrayList<>();
- for (User u : userList) {
- if (StringUtils.isNotBlank(u.getId())) {
- List<OutRestrictProject> rpList = restProjectMapper.selectByUid(u.getId());
- for (OutRestrictProject rp : rpList) {
- LocalDateTime lockTime = rp.getLockTime().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDateTime();
- if (rp.getType()==1){
- LocalDateTime endTime2 = lockTime.plusDays(15);
- long between2 = ChronoUnit.DAYS.between(now, endTime2);
- if (between2==0){
- ln.add(new Notice(UUID.randomUUID().toString(),date, 0, rp.getAid(),
- NoticeStatus.RESTRICT_PROJECT_LOSE30_REMIND.getCode(),
- String.format("您的客户[%s]限时私有项目[%s]还有15天即将丢失!",rp.getUserName(), rp.getProjectName())
- ,rp.getUid()));
- }else {
- LocalDateTime endTime = lockTime.plusDays(30);
- long between = ChronoUnit.DAYS.between(now, endTime);
- if (between<1){
- ln.add(new Notice(UUID.randomUUID().toString(),date, 0, rp.getAid(),
- NoticeStatus.RESTRICT_PROJECT_LOSE30.getCode(),
- String.format("您的客户[%s]限时私有项目[%s]已丢失!",rp.getUserName(), rp.getProjectName())
- ,rp.getUid()));
- list.add(rp.getId());
- lu.add(new UserTransferLog(rp.getUid(), rp.getAid(), rp.getPid(), 18, "限时项目释放(30天)",date));
- }
- }
- }else if (rp.getType()==2){
- LocalDateTime endTime2 = lockTime.plusDays(255);
- long between2 = ChronoUnit.DAYS.between(now, endTime2);
- if (between2==0){
- ln.add(new Notice(UUID.randomUUID().toString(),date, 0, rp.getAid(),
- NoticeStatus.RESTRICT_PROJECT_LOSE270_REMIND.getCode(),
- String.format("您的客户[%s]限时签单项目[%s]还有15天即将丢失!",rp.getUserName(), rp.getProjectName())
- ,rp.getUid()));
- }else{
- LocalDateTime endTime = lockTime.plusDays(270);
- long between = ChronoUnit.DAYS.between(now, endTime);
- if (between<1){
- ln.add(new Notice(UUID.randomUUID().toString(),date, 0, rp.getAid(),
- NoticeStatus.RESTRICT_PROJECT_LOSE270.getCode(),
- String.format("您的客户[%s]限时签单项目[%s]已丢失!",rp.getUserName(), rp.getProjectName())
- ,rp.getUid()));
- list.add(rp.getId());
- lu.add(new UserTransferLog(rp.getUid(), rp.getAid(), rp.getPid(), 19, "限时项目释放(270天)",date));
- }
- }
- }
- }
- }
- }
- addNoticeBatch(ln);
- pushReleaseRestrictProject(list);
- addUserTransferLogBatch(lu);
- LoggerUtils.debug(getClass(), "==============限时项目结束============");
- }
- private void addUserTransferLogBatch(List<UserTransferLog> list) {
- List<UserTransferLog> list2 = new ArrayList<>();
- int i=0;
- for (UserTransferLog e : list) {
- i++;
- list2.add(e);
- if(list2.size()==pointsDataLimit||i==list.size()){
- userTransferLogMapper.insertList(list2);
- list2.clear();
- }
- }
- }
- public static void main(String[] args) {
- LocalDateTime now = LocalDateTime.now();
- LocalDateTime endTime = LocalDateTime.of(2024,5,14,0,0);
- endTime= endTime.minusDays(270);
- long between = ChronoUnit.DAYS.between(now, endTime);
- System.out.println(between);
- System.out.println(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
- }
- private void pushReleaseRestrictProject(List<Integer> list) {
- List<Integer> list2 = new ArrayList<>();
- int i=0;
- for (Integer e : list) {
- i++;
- list2.add(e);
- if(list2.size()==pointsDataLimit||i==list.size()){
- restProjectMapper.updateRelease(list2);
- list2.clear();
- }
- }
- }
- private void pushUserDays(List<User> userList ) throws InterruptedException {
- LoggerUtils.debug(getClass(), "==============客户提醒开始============");
- List<Notice> ln = new ArrayList<>();
- List<userDaysBo> userChannelList = new ArrayList<userDaysBo>();
- for (User u : userList) {
- if (StringUtils.isNotBlank(u.getId())&&!AFTConstants.CAOJIN_AID.equals(u.getId())) {
- List<userDaysBo> userTmpList = customerService.selectReleaseUserDays(u.getId());
- for (userDaysBo ub : userTmpList) {
- String str=null;
- if (ub.getChannel() == 0) {
- String shareType="";
- if(ub.getNewChannel()==0&&ub.getType()==0)shareType="私有";
- else if(ub.getNewChannel()==0&&ub.getType()==2)shareType="签单";
- else if(ub.getNewChannel()==1)shareType="渠道";
- str = String.format("您的%s客户【%s】剩余天数不足十五天,请及时%s!", shareType, ub.getName(),"跟进");
- }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天重复,去除
- 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(), "==============客户提醒结束============");
- }
- /**
- * 将客户和业务转为待释放
- */
- public void updateUser(List<User> userList ) {
- LoggerUtils.debug(getClass(), "==============客户释放开始============");
- Date releaseTime = new Date();
- try {
- List<LockingReleaseBo> lockUserList = new ArrayList<LockingReleaseBo>();
- if (userList != null && !userList.isEmpty()) {
- for (User u : userList) {
- System.out.println(u.getId());
- if (StringUtils.isNotBlank(u.getId())&&!AFTConstants.CAOJIN_AID.equals(u.getId())) {
- // 获取30天释放 释放以锁定表与user_mid最后跟进取时间
- List<LockingReleaseBo> userTmpList = customerService.selectWaitReleaseCustomer(u.getId());
- customerService.pushReleaseLog(userTmpList, 0);
- addUserNotice(userTmpList);
- if (userTmpList != null && !userTmpList.isEmpty())
- lockUserList.addAll(userTmpList);
- }
- }
- }
- List<LockingReleaseBo> newList = new ArrayList<LockingReleaseBo>();
- if (lockUserList != null && !lockUserList.isEmpty()) {
- for (int i = 0; i < lockUserList.size(); i++) {
- newList.add(lockUserList.get(i));
- if (pointsDataLimit == newList.size() || i == lockUserList.size() - 1) {
- if (!newList.isEmpty()) {
- customerService.updatePendingReleaseLock(newList);
- }
- newList.clear();
- Thread.sleep(2000);
- }
- }
- }
- //处理每个人重复的天数
- pushSalesmanCount(lockUserList);
- pushChannel(releaseTime, userList);
- } 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+"释放客户失败");
- asyncUtils.send(emailBo);
- LoggerUtils.error(getClass(), "====================客户释放失败=================");
- LoggerUtils.error(getClass(), "客户释放失败", e);
- }
- LoggerUtils.debug(getClass(), "==============客户释放完成============");
- }
- private void pushSalesmanCount(List<LockingReleaseBo> aidList) {
- for (LockingReleaseBo e : aidList) {
- String dates= e.getLockTime();
- AdminUserCount adminUserCount = customerService.getAdminUserCount(e.getAid(), dates);
- if (adminUserCount!=null&&adminUserCount.getPrivateCount()>0){
- AdminUserCount newAdminUserCount = new AdminUserCount();
- newAdminUserCount.setId(adminUserCount.getId());
- newAdminUserCount.setPrivateCount(adminUserCount.getPrivateCount()-1);
- adminUserCountMapper.updateByPrimaryKeySelective(newAdminUserCount);
- }
- }
- }
- /**
- * 处理外联
- */
- private void pushChannel(Date releaseTime, List<User> userList) throws InterruptedException {
- // 获取超过90天客户,判断是否回收过,未回收则回收,已回收则释放为公共客户
- List<userDaysBo> userChannelList =null;
- List<userDaysBo> count =new ArrayList<userDaysBo>();
- if (userList != null && !userList.isEmpty()) {
- for (User u : userList) {
- userChannelList=customerService.selectChannelUserDays(u.getId());
- if (userChannelList!=null) {
- count.addAll(userChannelList);
- }
- }
- }
- List<userDaysBo> hsList=new ArrayList<>();
- List<userDaysBo> sfList=new ArrayList<>();
- if (count != null && !count.isEmpty()) {
- 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<LockingReleaseBo> lockList = customerService.selectPendinglockUserList();
- if (!lockList.isEmpty())
- customerService.updateReleaseLock(releaseTime);
- }
- public void addNotice(Notice n ) {
- n.setType(NoticeTypes.getType(n.getNoticeType()));
- noticeMapper.insertSelective(n);
- }
- public void addNoticeBatch(List<Notice> ln) {
- List<Notice> newList=new ArrayList<Notice>();
- if (ln != null && !ln.isEmpty()) {
- 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.isEmpty()) {
- //发送站内信
- noticeMapper.insertBatch(newList);
- }
- newList.clear();
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- private void addUserNotice(List<LockingReleaseBo> userTmpList) {
- List<Notice> nl=new ArrayList<Notice>();
- if (!userTmpList.isEmpty()) {
- for (LockingReleaseBo u : userTmpList) {
- String str="";
- String str2="";
- String str3="";
- if(u.getNewChannel()==0&&u.getShareType()==0){
- str2="私有";
- str3="或270天未签单";
- }else if(u.getNewChannel()==0&&u.getShareType()==2){
- str2="签单";
- }else if(u.getNewChannel()==1){
- str2= "渠道";
- }
- str=String.format("您的%s客户【%s】已经30天未跟进%s,已经自动释放!", str2, u.getUserName(),str3);
- nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, u.getAid(),
- NoticeStatus.CUSTOMER_LOSE30.getCode(),
- str,
- u.getUid()));
- }
- }
- if(!nl.isEmpty())addNoticeBatch(nl);
- }
- private void addChannelNotice(List<userDaysBo> hsList, List<userDaysBo> sfList) {
- List<Notice> nl=new ArrayList<Notice>();
- 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);
- }
- }
|