package com.fs.task.service;

import com.fs.provider.mapper.ServiceOrderMapper;
import com.fs.provider.service.IServiceOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component("serviceOrderQuartzTask")
public class ServiceOrderQuartzTask {
    @Resource
    private ServiceOrderMapper serviceOrderMapper;

    @Resource
    private IServiceOrderService serviceOrderService;

    /**
     * 扫描超时未确认的服务单，自动确认结算
     * 注意：这个方法不要带参数，最适合 sys_job 调用
     */
    public void scanAutoConfirm() {
        List<Long> ids = serviceOrderMapper.selectNeedAutoConfirmIds(200);
        log.info("[serviceOrderQuartzTask] scan size={}", ids.size());

        for (Long id : ids) {
            try {
                serviceOrderService.autoConfirmAndSettleIfNeeded(id);
            } catch (Exception e) {
                log.error("[serviceOrderQuartzTask] autoConfirm failed, orderId={}", id, e);
            }
        }
    }
}
