package com.czg.task;

import com.czg.market.entity.MkDiscountActivity;
import com.czg.market.entity.MkLimitTimeDiscount;
import com.czg.market.service.MkDiscountActivityService;
import com.czg.market.service.MkLimitTimeDiscountService;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 满减活动定时任务
 *
 * @author ww
 * @description
 */
@Slf4j
@Component
public class ActivityStatusTask {
    @Resource
    private MkDiscountActivityService discountActivityService;
    @Resource
    private MkLimitTimeDiscountService limitTimeDiscountService;

    /**
     * 满减活动/限时折扣 任务状态更新任务
     */
    public void task() {
        //满减活动
        discountActivityService.update(new MkDiscountActivity().setStatus(2),
                new QueryWrapper()
                        .eq(MkDiscountActivity::getStatus, 1)
                        .le(MkDiscountActivity::getValidStartTime, LocalDateTime.now())
                        .gt(MkDiscountActivity::getValidEndTime, LocalDateTime.now())
        );
        discountActivityService.update(new MkDiscountActivity().setStatus(3),
                new QueryWrapper().le(MkDiscountActivity::getValidEndTime, LocalDateTime.now())
        );

        //限时折扣
        limitTimeDiscountService.update(new MkLimitTimeDiscount().setStatus(2),
                new QueryWrapper()
                        .eq(MkLimitTimeDiscount::getStatus, 1)
                        .le(MkLimitTimeDiscount::getValidStartTime, LocalDateTime.now())
                        .gt(MkLimitTimeDiscount::getValidEndTime, LocalDateTime.now())
        );

        limitTimeDiscountService.update(new MkLimitTimeDiscount().setStatus(3),
                new QueryWrapper().le(MkLimitTimeDiscount::getValidEndTime, LocalDateTime.now())
        );
    }
}
