package com.example.scheudleds; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.example.config.db.DataSourceConfig; import com.example.config.db.DataSourceContextHolder; import com.example.domain.DictHosp; import com.example.factory.ServiceFactory; import com.example.mapper.SqlMapper; 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; @Autowired private SqlMapper sqlMapper; @Autowired private DataSourceConfig dataSourceConfig; @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()); List list = dictHospService.list(new LambdaQueryWrapper().isNotNull(DictHosp::getBeanName)); log.info("开始执行定时任务:{}", System.currentTimeMillis()); try { for (DictHosp hosp : list) { executorService.submit(() -> { try { String beanName = hosp.getBeanName(); log.info("开始同步医院数据:{}", beanName); HisService hisService = serviceFactory.getService(beanName); hisService.syncDict(beanName); log.info("完成同步医院数据:{}", beanName); } catch (Exception e) { log.error("同步医院数据失败,医院:{}", hosp.getBeanName(), e); } if (hosp.getIsTbHisProject() != null && hosp.getIsTbHisProject() == 1) { try { dataSourceConfig.addDataSource(hosp.getDbname()); DataSourceContextHolder.setDataSourceKey(hosp.getDbname()); sqlMapper.pro_tb_his_project(); } catch (Exception e) { log.error("调用同步存储过程失败,医院:{}", hosp.getBeanName(), e); }finally { DataSourceContextHolder.clear(); } } }); } } catch (Exception e) { log.error("定时任务执行异常:", e); } } }