定时任务

package com.jeeplus.modules.pay.web.payment;


import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.PropertiesLoader;
import com.jeeplus.modules.pay.entity.payment.PayPaymentRecord;
import com.jeeplus.modules.pay.service.payment.PayPaymentRecordService;
import org.apache.shiro.authc.Account;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.logging.Logger;
import java.util.stream.Collectors;


/**
 * Created by weq on 2018/4/11.
 */
@Service
@Lazy(false)
public class ScheduledPayInfo {
    @Autowired
    private PayPaymentRecordService payPaymentRecordService;


    private final String cron2="0/5 * * * * ? ";//设置定时任务的时间频率
    @Value("${payBeginTime}")
    private String payBeginTime;//设置自动缴费开始时间
    @Value("${payEndTime}")
    private String payEndTime ="";//设置自动缴费结束时间


    Boolean IsFilling =true;//如果没有填充过数据则为true


    @Scheduled(cron = "") //使用 cron表达式,每个月的几号几点执行一次
    public void checkTime(){
//        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH );
//        Date d = new Date();
//        String str = sdf.format(d);
        //2018-04-13 12:00:00
        String format = "yyyy-MM-dd HH:mm:ss";
        Date nowTime= new Date();;
        Date startTime;
        Date endTime;
        Boolean timeJudge=false;


        try {
//            nowTime = new SimpleDateFormat(format).parse(str);
            startTime  = new SimpleDateFormat(format).parse(payBeginTime.substring(1,20));
            endTime= new SimpleDateFormat(format).parse(payEndTime.substring(1,20));
            timeJudge=isEffectiveDate(nowTime, startTime, endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(IsFilling){
            System.out.println("-------------需要填充-----------");
        }else{
            System.out.println("-------------不用填充-----------");
        }


        //如果实时时间在我们设定的范围内才能开始填充
        if(timeJudge){
            AutomaticPay();
        }


    }


    //填充缴费记录
    public void AutomaticPay(){


        System.out.println("自动缴费定时任务开始,当前时间:" + new Date());
        long begin = System.currentTimeMillis();//开始计时
        //缴费记录对象
        PayPaymentRecord payPaymentRecord=new PayPaymentRecord();
        //获取当前时间
        java.sql.Date timePara  = null;
        try {
            timePara = new java.sql.Date(new Date().getTime());
            System.out.println(timePara);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //查询当月所有的缴费记录
        payPaymentRecord.setPaymentMonth(timePara);
        List<PayPaymentRecord> paylist=payPaymentRecordService.findAllListByMonth(payPaymentRecord);


        //将list转为map储存
        Map<String, PayPaymentRecord> mapp = paylist.stream().collect(Collectors.toMap(PayPaymentRecord::getIdNumber, Function.identity()));
//        System.out.println("mapp的1值:"+mapp);
        Map<String, Date> map = paylist.stream().collect(Collectors.toMap(PayPaymentRecord::getIdNumber, PayPaymentRecord::getPaymentMonth));
//        System.out.println("map的2值:"+map);






        //查询上个月所有的缴费记录
        payPaymentRecord.setPaymentMonth(timePara);
        List<PayPaymentRecord> prewPaylist = payPaymentRecordService.findAllListByNextMonth(payPaymentRecord);


        //在进行填充之前判断一下当月缴费记录条数是否和上个月一样:不一样就开始填充
        if(paylist.size() != prewPaylist.size()) {
            IsFilling=true;


            PayPaymentRecord payEntity = new PayPaymentRecord();
            if(IsFilling) {
                for (int i = 0; i < prewPaylist.size(); i++) {
                    payEntity.setIdNumber(prewPaylist.get(i).getIdNumber());//身份证号码
//                    System.out.println("身份证号码:" + payEntity.getIdNumber());
                    payEntity.setPaymentMonth(timePara);//缴费月份


                    //把数据取出来放到map里面去对比
                    if (map.get(payEntity.getIdNumber()) != null) {
                        continue;
                    }
                    payEntity.setUuid(IdGen.uuid());//uuid
                    payEntity.setPartyMoney(prewPaylist.get(i).getPartyMoney());//党费
                    payEntity.setPaymentState("0");//缴费状态
                    payEntity.setRemark(prewPaylist.get(i).getRemark());//备注


                    payPaymentRecordService.insertPay(payEntity);
                }
            }
        }else{
            IsFilling=false;


        }
        long end = System.currentTimeMillis();//结束计时
        System.out.println("定时任务结束,共耗时:[" + (end-begin)+ "]毫秒");
    }




    /**
     * 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
     *
     * @param nowTime 当前时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }


        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);


        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);


        Calendar end = Calendar.getInstance();
        end.setTime(endTime);


        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值