springboot定时任务 修改优惠券是否有效 和过期后7天不展示优惠券 及单表条件构造器的使用

 */
@Configuration
@EnableScheduling
public class TaskManger {


    @Autowired
    ZyCouponRecordServiceImpl zyCouponRecorService;


/*
* 每天0点0分0秒运行 更改 失效状态
* */
    @Scheduled(cron = "0 0 0 * * ?")
    public void billTask( ) {
        List<ZyCouponRecord> zyCouponRecords =new ArrayList<>();
        List<ZyCouponRecord> zyCouponRecordlist = zyCouponRecorService.lambdaQuery().eq(ZyCouponRecord::getRecordState, 0).list();
        System.out.println(zyCouponRecordlist.size()+"zyCouponRecordlist");
        for (int a=0;a<zyCouponRecordlist.size();a++){

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formatDate = sdf.format(zyCouponRecordlist.get(a).getCtime());
          if( DateUtil.getdatas(zyCouponRecordlist.get(a).getRecordCouponTerm()).equals(formatDate) ){
              zyCouponRecordlist.get(a).setRecordState(2);
              zyCouponRecords.add(zyCouponRecordlist.get(a));
          }
        }

        zyCouponRecorService.updateBatchById(zyCouponRecords);

    }




    /*
     * 每天0点30分0秒运行 更改 失效状态
     * */
    @Scheduled(cron = "0 30 0 * * ?")
    public void Task( ) {

        zyCouponRecorService.lambdaUpdate()
                .ne(ZyCouponRecord::getRecordState,0)
                .eq(ZyCouponRecord::getRecordDisplay, 0)
                .eq(ZyCouponRecord::getEtime,DateUtil.getSevenAgo())
                .set(ZyCouponRecord::getRecordDisplay, 1).update();



    }
}

时间转换工具

import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 时间获取工具类(用于首页数据统计使用)
 */
public class DateUtil {
    public static void main(String[] args) {
        System.out.println(getSixMonth());
    }

    public static String getToday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());
        return format;
    }

    public static String getBillId() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(new Date());
        return format;
    }

    public static String getSevenAgo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = DateUtils.addDays(new Date(), -2);
        String formatDate = sdf.format(date);
        return formatDate;
    }

    public static boolean getDayBetweenTwoDate(String startDateStr, String endDateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        boolean b = false;
        try {
            Date startDate = simpleDateFormat.parse(startDateStr);
            Date endDate = simpleDateFormat.parse(endDateStr);
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(startDate);
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(endDate);
            //计算两个日期相差的天数
            //startCalendar.getTime().getTime()返回long毫秒数形式,毫秒转为秒所以除以1000
            //1天=24小时,1小时=60分,1分=60秒,所以两个时间的差再除以60 * 60 * 24换算成天的形式
            int days = ((int) (startCalendar.getTime().getTime() / 1000) - (int) (endCalendar.getTime().getTime() / 1000)) / (60 * 60 * 24);
            if (days < 7) {
                b = true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return b;
    }

    //获取近七天日期
    public static List<String> getSevenDate() {
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < 7; i++) {
            Date date = DateUtils.addDays(new Date(), -i);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
        }
        return dateList;
    }

    //获取近一个月日期
    public static List<String> getOneMonth() {
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < 30; i++) {
            Date date = DateUtils.addDays(new Date(), -i);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
        }
        return dateList;
    }

    //获取近半年内月份
    public static List<String> getSixMonth() {
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        for (int i = 0; i < 6; i++) {
            Date date = DateUtils.addMonths(new Date(), -i);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
        }
        return dateList;
    }

    public static String getLastMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = DateUtils.addMonths(new Date(), -1);
        String formatDate = sdf.format(date);
        return formatDate;
    }


    public static String getdatas(int a) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = DateUtils.addDays(new Date(), -a);
        String formatDate = sdf.format(date);
        return formatDate;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虚构的乌托邦

如果可以请打赏鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值