package com.ltkj.web.config.timer; import com.ltkj.framework.datasource.DynamicDataSourceContextHolder; import com.ltkj.hosp.domain.TjJcycxm; import com.ltkj.hosp.mapper.TestMapper; import com.ltkj.hosp.service.*; import com.ltkj.hosp.sqlDomain.LtkjExamJcbgd; import com.ltkj.hosp.sqlDomain.LtkjExamJcsqd; import com.ltkj.hosp.sqlDomain.LtkjHybgd; import com.ltkj.hosp.sqlDomain.LtkjHysqd; import com.ltkj.system.service.ISysConfigService; import jodd.util.StringUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.time.LocalTime; import java.time.ZonedDateTime; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; /** * @Company: 西安路泰科技有限公司 * @Author: zjh * @Date: 2024/7/25 15:05 */ @Slf4j @Component @Profile("!dev & !prod & !test") public class DefaultScheduled { @Resource private ITjOrderService orderService; @Autowired private ISysConfigService configService; @Resource private LtkjExamJcbgdService jcbgdService; @Resource private LtkjExamJcsqdService jcsqdService; @Resource private LtkjHybgdService ltkjHybgdService; @Resource private LtkjHysqdService ltkjHysqdService; @Resource private TestMapper testMapper; @Resource private TjAsyncService asyncService; @Autowired private TjJcycxmService jcycxmService; private final ReentrantLock reentrantLock = new ReentrantLock(); private ScheduledExecutorService scheduledExecutorService; private ScheduledFuture scheduledFuture; private static final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @PostConstruct private void init() { scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); scheduleNextTask(0); // 初次启动立即执行 } private void scheduleNextTask(long delayInMinutes) { if (scheduledFuture != null) { scheduledFuture.cancel(false); } Date nextExecutionTime = new Date(System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(delayInMinutes)); log.info("下次执行任务时间 -> {}", dateTimeFormat.format(nextExecutionTime)); scheduledFuture = scheduledExecutorService.schedule(this::exec, delayInMinutes, TimeUnit.MINUTES); } private void exec() { String dsqkssj = configService.selectConfigByKey("dsqkssj"); String dsqjssj = configService.selectConfigByKey("dsqjssj"); String yqbm = configService.selectConfigByKey("common_api_service_hospbm"); boolean panduandangqianshijian = panduandangqianshijian(dsqkssj, dsqjssj); log.info("当前时间是否在 " + dsqkssj + " 点到 " + dsqjssj + " 点之间: " + panduandangqianshijian); String dsrwsjjg = configService.selectConfigByKey("dsrwsjjg"); boolean lock = reentrantLock.tryLock(); try { if (!panduandangqianshijian) { if (lock) { log.info("任务开始执行时间 -> {}", dateTimeFormat.format(new Date())); long start = System.currentTimeMillis(); // 查询任务数据 log.info("开始执行同步 执行数量->{}", 0); List jy = orderService.getDingShiTongBuJianYanTjNum(); List jc = orderService.getDingShiTongBuJianChaTjNum(); if (null != jy && !jy.isEmpty()) { log.info("同步检验人员 执行数量->{}", jy.size()); for (String s : jy) { extractedjianyan(s); } } if (null != jc && !jc.isEmpty()) { log.info("同步检查人员 执行数量->{}", jc.size()); for (String s : jc) { extractedjiancha(s); } } Map map = new HashMap<>(); testMapper.tjplUpdateDetatilByVi(map); Integer object = (Integer) map.get("total"); log.info("执行存储过程后出参->{}", object); if (null != object && object.equals(1)) { if (null != jc && !jc.isEmpty()) { for (String s : jc) { jcycxmService.deletedByTjh(s); List list = jcsqdService.getLtkjJcsqdByTjh(s); if(null !=list && !list.isEmpty()){ for (LtkjExamJcsqd jcsqd : list) { if(jcsqd.getJgbx().contains("。")){ String[] split = jcsqd.getJgzt().split("。"); for (String jg : split) { if(StringUtil.isNotBlank(jg) && !jg.contains("未见异常") && !jg.contains("未见明显异常") && !jg.contains("未见占位") && !jg.contains("未见明显")&& !jg.contains("正常")&& !jg.contains("双侧椎间孔无狭窄")){ TjJcycxm jcycxm=new TjJcycxm(); jcycxm.setTjh(s); jcycxm.setYqid(yqbm); String[] split1 = jcsqd.getJcxmid().split(";"); jcycxm.setProId(split1[0]); jcycxm.setProName(jcsqd.getJcxmmc()); jcycxm.setCreateTime(new Date()); jcycxm.setJcjg(jg); jcycxmService.save(jcycxm); } } } } } } } long end = System.currentTimeMillis(); log.info("同步结束 耗时:{}秒", (end - start) / 1000); // 动态获取下次任务的间隔 int minute = Integer.parseInt(dsrwsjjg); if (minute < 5) minute = 5; if (minute > 59) minute = 59; scheduleNextTask(minute); }else { log.info("执行存储过程出参失败"); scheduleNextTask(Long.parseLong(dsrwsjjg)); } } }else { log.info("不在定时器执行时间范围之内!!!"); scheduleNextTask(Long.parseLong(dsrwsjjg)); } } catch (Exception e) { scheduleNextTask(Long.parseLong(dsrwsjjg)); log.error("定时器执行报错"); e.printStackTrace(); } finally { reentrantLock.unlock(); } } private boolean panduandangqianshijian(String kssj, String jssj) { ZonedDateTime now = ZonedDateTime.now(); LocalTime time = now.toLocalTime(); return time.isAfter(LocalTime.of(Integer.parseInt(kssj), 0)) && time.isBefore(LocalTime.of(Integer.parseInt(jssj), 0)); } private void extractedjianyan(String s) { try { List hysqdList = testMapper.getCcXZxYyLtkjHysqdByLisVi(s); List wssq = testMapper.getWsxmLtkjHysqdByLisVi(s); if(null !=wssq && !wssq.isEmpty())hysqdList.addAll(wssq); if (null != hysqdList && !hysqdList.isEmpty()) { for (LtkjHysqd hysqd : hysqdList) { LtkjHysqd jybgid = ltkjHysqdService.getLtkjHysqdByTjhAndTmh(hysqd.getTjh(), hysqd.getTmh()); if (null != jybgid) { ltkjHysqdService.deletedLtkjHysqdByTjhAndTmh(hysqd.getTjh(), hysqd.getTmh()); ltkjHybgdService.deletedLtkjHybgdByTjhAndTmh(hysqd.getTmh()); } if (ltkjHysqdService.save(hysqd)) { List hybgdList = testMapper.getCcXZxYyLtkjHybgdByLisVi(hysqd.getTmh()); List wsbg = testMapper.getWsxmLtkjHybgdByLisVi(s); if(null !=wsbg && !wsbg.isEmpty())hybgdList.addAll(wsbg); ltkjHybgdService.saveBatch(hybgdList); } } DynamicDataSourceContextHolder.clearDataSourceType(); } } catch (Exception e) { log.info("同步检验数据失败"); throw new RuntimeException(e); } } private void extractedjiancha(String s) { try { List jcsqdList = testMapper.getCcXZxYyPacsLtkjExamJcsqd(s); if (null != jcsqdList && !jcsqdList.isEmpty()) { DynamicDataSourceContextHolder.clearDataSourceType(); jcsqdService.deletedLtkjJcsqdByTjhAndTmh(s); jcbgdService.deletedLtkjJcbgdByTjhAndTmh(s); jcsqdService.saveBatch(jcsqdList); List jcbgdList = testMapper.getCcXZxYyPacsLtkjExamJcbgd(s); DynamicDataSourceContextHolder.clearDataSourceType(); if (null != jcbgdList && !jcbgdList.isEmpty()) jcbgdService.saveBatch(jcbgdList); DynamicDataSourceContextHolder.clearDataSourceType(); } } catch (Exception e) { log.info("同步检查数据失败"); throw new RuntimeException(e); } } }