package com.czg.task;

import cn.hutool.core.date.DateUtil;
import com.czg.order.entity.OrderInfo;
import com.czg.order.service.OrderInfoService;
import com.czg.service.order.enums.OrderStatusEnums;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 订单定时任务
 *
 * @author ww
 * @description
 */
@Slf4j
@Component
public class OTimeTask {
    @Resource
    private OrderInfoService orderInfoService;

    /**
     * order 过期
     */
    @Scheduled(cron = "0 0 1 * * ? ")
    public void run() {
        orderInfoService.updateChain()
                .set(OrderInfo::getStatus, OrderStatusEnums.CANCELLED.getCode())
                .eq(OrderInfo::getStatus, OrderStatusEnums.UNPAID.getCode())
                .lt(OrderInfo::getTradeDay, DateUtil.format(DateUtil.yesterday(), "yyyy-MM-dd"))
                .update();
    }
}
