package com.jeesharp.business.modules.mall.application;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 商城待支付订单超时自动取消定时任务（释放库存与冻结优惠券）。
 *
 * @author jeesharp
 * @since 2026-06-05
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MallOrderPayTimeoutCancelJob {

    private final MallOrderApplicationService mallOrderApplicationService;

    @XxlJob("mallOrderPayTimeoutCancelJob")
    public void execute() {
        XxlJobHelper.log("param={}", XxlJobHelper.getJobParam());
        int count = mallOrderApplicationService.cancelExpiredPendingOrders();
        XxlJobHelper.log("cancelled={}", count);
        if (count > 0) {
            log.info("商城支付超时自动取消 {} 笔待支付订单", count);
        }
    }
}
