package com.ltkj.web.config.timer; import com.ltkj.framework.datasource.DynamicDataSourceContextHolder; import com.ltkj.hosp.domain.TjOrder; import com.ltkj.hosp.mapper.TestMapper; import com.ltkj.hosp.mapper.TjOrderMapper; 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 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.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") 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; 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() { log.info("任务开始执行时间 -> {}", dateTimeFormat.format(new Date())); String dsrwsjjg = configService.selectConfigByKey("dsrwsjjg"); boolean lock = reentrantLock.tryLock(); try { if (lock) { 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) { // TjOrder order = orderService.getOrderByTjNum(s); extractedjianyan(s); } } if(null !=jc && !jc.isEmpty()){ log.info("同步检查人员 执行数量->{}", jc.size()); for (String s : jc) { // TjOrder order = orderService.getOrderByTjNum(s); extractedjiancha(s); } } Map map=new HashMap<>(); testMapper.tjplUpdateDetatilByVi(map); Integer object = (Integer) map.get("total"); log.info("执行存储过程后出参->{}",object); if(null !=object && object.equals(1)){ 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); } } } catch (Exception e) { log.error("定时器执行报错"); e.printStackTrace(); } finally { scheduleNextTask(Long.parseLong(dsrwsjjg)); reentrantLock.unlock(); } } private void extractedjianyan(String s) { try { List hysqdList = testMapper.getCcXZxYyLtkjHysqdByLisVi(s); 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()); 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); } } }