package com.gree.mall.manager.schedule;

import com.gree.mall.manager.enums.CouponFlagEnum;
import com.gree.mall.manager.plus.entity.Coupon;
import com.gree.mall.manager.plus.service.CouponService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;

@ConditionalOnProperty(name = "schedule.enable", havingValue = "true", matchIfMissing = true)
@Component
public class CouponEndServiceSchedule {

    @Resource
    CouponService couponService;

    /**
     * 优惠券超领取日期改变flag标志
     */
    @Scheduled(cron = "0 0 0 * * ?")
    @PostConstruct
    @Transactional
    public void task() {
        couponService.lambdaUpdate()
                .eq(Coupon::getFlag, CouponFlagEnum.START.toString())
                .le(Coupon::getReleaseEndTime, new Date())
                .set(Coupon::getFlag, CouponFlagEnum.CANCEL.toString())
                .update();
    }
}
