BindPushJob.java 3.93 KB
package com.sincere.quartz.job;

import com.sincere.common.dto.smartCampus.BindPushDto;
import com.sincere.common.dto.smartCampus.ParentDto;
import com.sincere.common.enums.PushTypeEnums;
import com.sincere.common.util.DateUtils;
import com.sincere.quartz.feign.ScFeign;
import com.sincere.quartz.model.ShortMsg;
import com.sincere.quartz.service.SmsService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户注册激活提醒
 * @author chen
 * @version 1.0
 * @date 2019/12/10 0010 8:31
 */
@Service
public class BindPushJob {

    private Logger logger = LoggerFactory.getLogger(BindPushJob.class);


    @Autowired
    ScFeign scFeign;

    @Autowired
    SmsService smsService;

    private static Map<String , String> intervalDaysMap = new HashMap<>();  //redis  持久化   替换 可避免重启导致间隔的错误

    @Scheduled(cron = "0 0-59 * * * ? ")
    public void bindPush(){
        Date nowDate = new Date();
        List<BindPushDto> schoolList = scFeign.selectBindPushSchool();
        for(BindPushDto school : schoolList){
            if(school.getBeginDate().compareTo(nowDate) <= 0 && school.getEndDate().compareTo(nowDate) >= 0){
                String lastDate = intervalDaysMap.get(school.getSchoolId()+"_"+school.getType());
                if(StringUtils.isBlank(lastDate)){
                    //下发推送
                    bindPush(school,nowDate);
                }else {
                   int day =  DateUtils.getDateDifference(nowDate,DateUtils.string2Date(lastDate,DateUtils.format1),"day") ;
                   if(day == school.getIntervalDays() + 1) {
                       //下发推送
                       bindPush(school,nowDate);
                   }
                }
            }
        }
    }

    private void bindPush(BindPushDto bindPushDto , Date nowDate){
        if(bindPushDto.getPushTime().equals(DateUtils.date2String(nowDate,DateUtils.format4))){
            intervalDaysMap.put(bindPushDto.getSchoolId()+"_"+bindPushDto.getType(),DateUtils.date2String(nowDate,DateUtils.format1));
            //未关注
            List<ParentDto> unFollowList = scFeign.selectNotFollow(bindPushDto.getSchoolId());
            logger.info(bindPushDto.getSchoolName()+"未关注人数"+unFollowList.size());
            for(ParentDto parentDto : unFollowList){
                sendMessage(parentDto,bindPushDto.getMsg());
            }
            //未绑定
            List<ParentDto> unBindList =scFeign.selectNotBind(bindPushDto.getSchoolId(),getThirdType(bindPushDto.getType()));
            logger.info(bindPushDto.getSchoolName()+"未绑定人数"+unBindList.size());
            for(ParentDto parentDto : unBindList){
                sendMessage(parentDto,bindPushDto.getMsg());
            }
        }
    }

    private void sendMessage(ParentDto parentDto ,  String message){
        //手机号为空
        if(StringUtils.isNotBlank(parentDto.getMobile())){
            String tableSuffix = DateUtils.date2String(new Date(), DateUtils.format);
            ShortMsg shortMsg = new ShortMsg();
            shortMsg.setTableName("smsNew"+tableSuffix);
            shortMsg.setSchoolId(parentDto.getSchoolId());
            shortMsg.setMobile(parentDto.getMobile());
            shortMsg.setMsg(message);
            smsService.insertSMS(shortMsg);
            logger.info("----学校Id---"+parentDto.getSchoolId() + "----手机号----"+parentDto.getMobile()+"---"+message);
        }
    }

    // 0是企业号,1是钉钉
    private int getThirdType(int type){
        if(type == 0){
            return PushTypeEnums.QIYEHAO.getType() ;
        }else {
            return PushTypeEnums.DING.getType() ;
        }
    }

}