package shop.alien.job.store;

import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import shop.alien.entity.result.R;
import shop.alien.job.feign.AlienStoreFeign;

/**
 * 预订未到店超时定时任务
 * retain_position_flag=1：start_time + retention_duration（分钟）早于当前时间；
 * 否则仅 start_time 早于当前时间；且 user_reservation_order 为「待使用」的预订：
 * - user_reservation.status 置为 4（未到店超时）
 * - user_reservation_order.order_status 置为 3（已过期）
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ReservationTimeoutJob {

    private final AlienStoreFeign alienStoreFeign;

    @XxlJob("reservationTimeoutJob")
    public void reservationTimeoutJob() {
        log.info("【定时任务】预订未到店超时：开始执行");
        try {
            R<Integer> result = alienStoreFeign.markReservationTimeoutByEndTime();
            int count = (result != null && result.getData() != null) ? result.getData() : 0;
            log.info("【定时任务】预订未到店超时：执行完成，本次更新条数={}", count);
        } catch (Exception e) {
            log.error("【定时任务】预订未到店超时：执行异常", e);
            throw e;
        }
    }
}
