package com.example.scheudleds; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.example.domain.DictHosp; import com.example.factory.ServiceFactory; import com.example.service.DictHospService; import com.example.service.HisService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.Collectors; @Slf4j @Component public class DictSyncCommon { private final ServiceFactory serviceFactory; @Autowired public DictSyncCommon(ServiceFactory serviceFactory) { this.serviceFactory = serviceFactory; } private final ExecutorService executorService = Executors.newFixedThreadPool(40); @Autowired private DictHospService dictHospService; @Scheduled(cron = "0 0 22 * * ?") public void executeTasks() { // List list = Arrays.asList("shanxiqinxamjyy","shanxiqinpbkwyy","shanxiqinjdczgzyy","shanxiqinsqyy"); List list = dictHospService.list(new LambdaQueryWrapper().isNotNull(DictHosp::getBeanName)) .stream().map(DictHosp::getBeanName).collect(Collectors.toList()); log.info("开始执行定时任务:{}", System.currentTimeMillis()); try { for (String hosp : list) { executorService.submit(() -> { try { log.info("开始同步医院数据:{}", hosp); HisService hisService = serviceFactory.getService(hosp); hisService.syncDict(hosp); log.info("完成同步医院数据:{}", hosp); } catch (Exception e) { log.error("同步医院数据失败,医院:{},异常:{}", hosp, e.getMessage()); } }); } } catch (Exception e) { log.error("定时任务执行异常:", e); } } }