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;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的使用Spring Boot开发优惠券的代码示例: 1. 创建一个Spring Boot项目 2. 添加依赖 在`pom.xml`文件中添加以下依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> </dependencies> ``` 3. 创建一个 Coupon 实体类 ```java import java.util.Date; public class Coupon { private String id; private String name; private Date startDate; private Date endDate; private double discount; // getters and setters } ``` 4. 创建一个 CouponRepository 接口 ```java import org.springframework.data.mongodb.repository.MongoRepository; public interface CouponRepository extends MongoRepository<Coupon, String> { Coupon findByName(String name); } ``` 5. 创建一个 CouponController 控制器 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/coupons") public class CouponController { @Autowired private CouponRepository couponRepository; @RequestMapping(value = "", method = RequestMethod.POST) public ResponseEntity<Coupon> createCoupon(@RequestBody Coupon coupon) { couponRepository.save(coupon); return new ResponseEntity<Coupon>(coupon, HttpStatus.CREATED); } @RequestMapping(value = "/{name}", method = RequestMethod.GET) public ResponseEntity<Coupon> getCouponByName(@PathVariable("name") String name) { Coupon coupon = couponRepository.findByName(name); if (coupon == null) { return new ResponseEntity<Coupon>(HttpStatus.NOT_FOUND); } return new ResponseEntity<Coupon>(coupon, HttpStatus.OK); } } ``` 6. 配置 MongoDB 在`application.properties`文件中添加以下配置: ``` spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=coupons ``` 7. 运行应用程序 运行`CouponApplication.java`文件,应用程序将在端口8080上启动。 现在,您可以使用Postman或其他REST客户端测试API,例如: - 创建一个新优惠券: `POST http://localhost:8080/coupons` 请求体: ```json { "name": "10% off", "startDate": "2022-01-01", "endDate": "2022-12-31", "discount": 0.1 } ``` - 按名称检索优惠券: `GET http://localhost:8080/coupons/10%25%20off` 响应体: ```json { "id": "61e6a4f8e1f20b7dcf7d3f91", "name": "10% off", "startDate": "2022-01-01T00:00:00.000+00:00", "endDate": "2022-12-31T00:00:00.000+00:00", "discount": 0.1 } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

虚构的乌托邦

如果可以请打赏鼓励

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

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

打赏作者

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

抵扣说明:

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

余额充值