package com.whdc.component; import com.whdc.mapper.AutoCallConfigMapper; import com.whdc.model.entity.AutoCallPerson; import com.whdc.service.AutoCallTaskService2; import com.whdc.utils.AutoCallHelper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.annotation.Profile; import org.springframework.context.event.EventListener; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; /** * @author lyf * @since 2025-06-20 */ @Component @Slf4j @Profile("default") public class AutoCallTaskScheduled { @Autowired private AutoCallTaskService2 autoCallTaskService; @Autowired private AutoCallConfigMapper configMapper; @Autowired private AutoCallHelper autoCallHelper; @Autowired private TaskScheduler taskScheduler; private AtomicBoolean initialized = new AtomicBoolean(false); @EventListener(ApplicationReadyEvent.class) public void initialize() throws Exception { autoCallHelper.getToken(); initialized.set(true); log.info("AutoCallTaskScheduled初始化完成"); } @Scheduled(cron = "*/3 * * * * ?") public void generateLoop() { if (configMapper.isScheduled()) { autoCallTaskService.step1GenerateTask(); } } @Scheduled(cron = "*/3 * * * * ?") public void callLoop() { if (!initialized.get()) { return; } if (!configMapper.isScheduled()) { return; } log.info("AutoCallTaskScheduled callLoop"); List personList = autoCallTaskService.step2GetOneUnUploadedPerson(); log.info("AutoCallTaskScheduled {}个外呼人, {}", personList.size(), personList.stream().map(AutoCallPerson::getUploadCustName).collect(Collectors.toList())); try { for (AutoCallPerson person : personList) { autoCallTaskService.step3UploadAICCTask(person); int pendingDuration = 60 * 1000 * 2; int loopGap = 1000; boolean success = false; while (pendingDuration > 0) { try { Thread.sleep(loopGap); } catch (InterruptedException ignore) { Thread.currentThread().interrupt(); return; } pendingDuration -= loopGap; success = autoCallTaskService.step4QueryAICCTaskResult(person); if (success) break; } if (!success) { autoCallTaskService.markPersonDetailQueryTimeout(person); } } } catch (Exception e) { log.error("AutoCallTaskScheduled callLoop error", e); } } }