package org.dromara.pay.job;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.pay.domain.PayNotifyTask;
import org.dromara.pay.service.PayNotifyService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 支付通知重试任务
 *
 * @author pengles
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class PayNotifyJob {

    private final PayNotifyService payNotifyService;

    /**
     * 每30秒检查一次待通知任务
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void retryNotify() {
        List<PayNotifyTask> tasks = payNotifyService.getPendingTasks();
        if (tasks.isEmpty()) {
            return;
        }
        log.info("扫描到待执行回调任务: {}个", tasks.size());
        for (PayNotifyTask task : tasks) {
            payNotifyService.executeTask(task);
        }
    }
}
