|
|
@@ -1,23 +1,14 @@
|
|
|
package com.nb.web.service.bus.hospital;
|
|
|
|
|
|
-import cn.hutool.core.bean.BeanUtil;
|
|
|
-import cn.hutool.core.collection.CollUtil;
|
|
|
-import cn.hutool.core.collection.CollectionUtil;
|
|
|
-import cn.hutool.core.date.DateUnit;
|
|
|
-import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
-import cn.hutool.core.util.RandomUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import cn.hutool.cron.CronUtil;
|
|
|
import cn.hutool.extra.spring.SpringUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.nb.core.utils.CronExpressionUtil;
|
|
|
import com.nb.web.api.bean.Script;
|
|
|
import com.nb.web.api.entity.BusHospitalConfigEntity;
|
|
|
-import com.nb.web.api.entity.BusInfusionHistoryEntity;
|
|
|
-import com.nb.web.api.feign.query.PatientMonitorQuery;
|
|
|
-import com.nb.web.api.feign.result.PatientMonitorResult;
|
|
|
+import com.nb.web.api.entity.common.BusDeviceRunningEntity;
|
|
|
import com.nb.web.service.bus.entity.*;
|
|
|
import com.nb.web.service.bus.hospital.config.*;
|
|
|
import com.nb.web.service.bus.hospital.config.bean.*;
|
|
|
@@ -27,16 +18,14 @@ import com.nb.web.service.bus.hospital.script.ScriptManager;
|
|
|
import com.nb.web.service.bus.service.*;
|
|
|
import com.nb.core.cache.ConfigStorage;
|
|
|
import com.nb.core.cache.manager.ConfigStorageManager;
|
|
|
-import com.nb.web.api.entity.common.BusDeviceRunningEntity;
|
|
|
import lombok.Data;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.quartz.*;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.Executors;
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
import java.util.concurrent.ScheduledFuture;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author lifang
|
|
|
@@ -87,6 +76,13 @@ public class HospitalManager {
|
|
|
private LocalBusPatientService patientService;
|
|
|
|
|
|
private LocalBusInfusionHistoryService infusionHistoryService;
|
|
|
+
|
|
|
+ // 注入Quartz调度器
|
|
|
+ private Scheduler quartzScheduler;
|
|
|
+
|
|
|
+ // 任务组名
|
|
|
+ private static final String JOB_GROUP = "HIS_DATA_PULL_GROUP";
|
|
|
+
|
|
|
public HospitalManager(String hospitalId,
|
|
|
ScriptManager scriptManager,
|
|
|
ConfigStorageManager configStorageManager) {
|
|
|
@@ -102,36 +98,8 @@ public class HospitalManager {
|
|
|
this.codeHandler=new HospitalPatientCodeHandler(storage,hospitalId);
|
|
|
this.infusionHistoryService=SpringUtil.getBean(LocalBusInfusionHistoryService.class);
|
|
|
this.patientService=SpringUtil.getBean(LocalBusPatientService.class);
|
|
|
+ this.quartzScheduler = SpringUtil.getBean(Scheduler.class);
|
|
|
init(configStorageManager);
|
|
|
-
|
|
|
-// singleOffsetsDeviceExecutor.scheduleWithFixedDelay(()->{
|
|
|
-// try {
|
|
|
-// PatientMonitorQuery query = new PatientMonitorQuery();
|
|
|
-// query.setTenantId(this.getHospitalId());
|
|
|
-// query.setInfusionFinished(true);
|
|
|
-// query.setDeviceStatus(Arrays.asList(0,5));
|
|
|
-// List<PatientMonitorResult> patientMonitorResults = patientService.selectAll(query);
|
|
|
-// if(CollectionUtil.isEmpty(patientMonitorResults)){
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// patientMonitorResults
|
|
|
-// .forEach(infusionInfo->{
|
|
|
-// BusDeviceRunningEntity source = BeanUtil.toBean(infusionInfo, BusDeviceRunningEntity.class);
|
|
|
-// source.setRunState(infusionInfo.getDeviceRunState());
|
|
|
-// source.setAlarm(infusionInfo.getDeviceAlarm());
|
|
|
-// source.setInfusionId(infusionInfo.getInfusionId());
|
|
|
-// source.setUploadTime(infusionInfo.getLastUploadTime());
|
|
|
-// source.setTenantId(this.getHospitalId());
|
|
|
-// extraConfigHandler.handler(source);
|
|
|
-// autoUndoConfigHandler.handler(source);
|
|
|
-// finishMonitorConfigHandler.handler(source);
|
|
|
-// });
|
|
|
-// }catch (Exception e){
|
|
|
-// e.printStackTrace();
|
|
|
-// }
|
|
|
-// },
|
|
|
-// //1-10分钟内启动,间隔1天启动
|
|
|
-// RandomUtil.randomInt(1,10), Math.addExact(1440, RandomUtil.randomInt(1,12)),TimeUnit.MINUTES);
|
|
|
}
|
|
|
|
|
|
private void init(ConfigStorageManager configStorageManager){
|
|
|
@@ -150,7 +118,7 @@ public class HospitalManager {
|
|
|
*/
|
|
|
public void refreshInfo(){
|
|
|
BusHospitalEntity hospital = hospitalService.getById(hospitalId);
|
|
|
- log.info("医院【{}】,刷新信息,info【{}】",hospitalId,hospital);
|
|
|
+ log.info("医院【{}】,刷新信息,info【{}】",hospitalId,hospital.getName());
|
|
|
storage.setConfig("info",hospital);
|
|
|
storage.setConfig("strategy",hospital.getStrategy());
|
|
|
|
|
|
@@ -254,53 +222,78 @@ public class HospitalManager {
|
|
|
return;
|
|
|
}
|
|
|
if(!Boolean.TRUE.equals(updateConfig.getEnable())||updateConfig.getInterval()<0){
|
|
|
- if(hisSchedule !=null&&!hisSchedule.isCancelled()){
|
|
|
- //如果有定时任务正在运行,则关闭
|
|
|
- hisSchedule.cancel(true);
|
|
|
- }
|
|
|
+ // 如果有定时任务正在运行,则关闭
|
|
|
+ cancelQuartzJob();
|
|
|
return;
|
|
|
}
|
|
|
//参数未改变
|
|
|
if(ObjectUtil.isNotNull(this.getUpdateConfig())&&ObjectUtil.equals(this.getUpdateConfig().getInterval(),updateConfig.getInterval())){
|
|
|
return;
|
|
|
}else {
|
|
|
- if(hisSchedule!=null){
|
|
|
- //取消上次循环
|
|
|
- hisSchedule.cancel(true);
|
|
|
- }
|
|
|
- hisSchedule = singleHisExecutor.scheduleAtFixedRate(this::scheduleHis, 0, updateConfig.getInterval(),TimeUnit.MINUTES);
|
|
|
+ // 取消之前的任务
|
|
|
+ cancelQuartzJob();
|
|
|
+
|
|
|
+ // 创建新的Quartz任务
|
|
|
+ createQuartzJob(updateConfig);
|
|
|
}
|
|
|
this.updateConfig=updateConfig;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
- * 描述: 定时从his拉取数据
|
|
|
- * @author lifang
|
|
|
- * @date 2022/6/18 15:20
|
|
|
- * @param
|
|
|
- * @return void
|
|
|
+ * 取消Quartz任务
|
|
|
*/
|
|
|
- private void scheduleHis(){
|
|
|
- log.info("[定时拉取病人数据],hospitalId:{}",hospitalId);
|
|
|
- //拉取最新的50条输注信息,更新病人数据
|
|
|
- Page<BusInfusionHistoryEntity> infusionHistoryPage = new Page<>(0,50,false);
|
|
|
- infusionHistoryPage= infusionHistoryService.page(infusionHistoryPage, new QueryWrapper<BusInfusionHistoryEntity>().lambda().eq(BusInfusionHistoryEntity::getTenantId, hospitalId)
|
|
|
- .eq(BusInfusionHistoryEntity::getFinished,false)
|
|
|
- .orderByDesc(BusInfusionHistoryEntity::getLastUploadTime));
|
|
|
- List<BusInfusionHistoryEntity> records = infusionHistoryPage.getRecords();
|
|
|
- if(CollUtil.isNotEmpty(records)){
|
|
|
- records.stream().map(BusInfusionHistoryEntity::getPatientCode).collect(Collectors.toSet())
|
|
|
- .forEach(patientCode->{
|
|
|
- log.info("[拉取病人信息],hospitalId:{},patientCode:{}",hospitalId,patientCode);
|
|
|
- try {
|
|
|
- this.getScriptSession()
|
|
|
- .asyncGetPatientInfo(patientCode,3,TimeUnit.SECONDS,false);
|
|
|
- }catch (Exception e){
|
|
|
- log.warn("[拉取病人信息失败],",e);
|
|
|
- }
|
|
|
-
|
|
|
- });
|
|
|
+ private void cancelQuartzJob() {
|
|
|
+ try {
|
|
|
+ String jobName = "HIS_DATA_PULL_" + hospitalId;
|
|
|
+ JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP);
|
|
|
+
|
|
|
+ // 检查任务是否存在
|
|
|
+ if (quartzScheduler.checkExists(jobKey)) {
|
|
|
+ // 暂停并删除任务
|
|
|
+ quartzScheduler.deleteJob(jobKey);
|
|
|
+ log.info("成功取消HIS数据拉取任务,hospitalId:{}", hospitalId);
|
|
|
+ }
|
|
|
+ } catch (SchedulerException e) {
|
|
|
+ log.warn("取消HIS数据拉取任务失败,hospitalId:{}", hospitalId, e);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建Quartz任务
|
|
|
+ * @param updateConfig 更新配置
|
|
|
+ */
|
|
|
+ private void createQuartzJob(HisUpdateEntity updateConfig) {
|
|
|
+ try {
|
|
|
+ String jobName = "HIS_DATA_PULL_" + hospitalId;
|
|
|
+ JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP);
|
|
|
+
|
|
|
+ // 先删除已存在的任务
|
|
|
+ if (quartzScheduler.checkExists(jobKey)) {
|
|
|
+ quartzScheduler.deleteJob(jobKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建JobDetail
|
|
|
+ JobDetail jobDetail = JobBuilder.newJob(HospitalHisDataPullJob.class)
|
|
|
+ .withIdentity(jobKey)
|
|
|
+ .storeDurably()
|
|
|
+ .usingJobData("hospitalId", hospitalId)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 创建CronTrigger
|
|
|
+ // 根据间隔时间生成cron表达式(每X分钟执行一次)
|
|
|
+ String cronExpression = CronExpressionUtil.minutesToCronExpression(updateConfig.getInterval());
|
|
|
+ CronTrigger trigger = TriggerBuilder.newTrigger()
|
|
|
+ .withIdentity("trigger_" + jobName, JOB_GROUP)
|
|
|
+ .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)
|
|
|
+ .withMisfireHandlingInstructionIgnoreMisfires()) // 立即执行
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 调度任务
|
|
|
+ quartzScheduler.scheduleJob(jobDetail, trigger);
|
|
|
+
|
|
|
+ log.info("成功创建HIS数据拉取任务,hospitalId:{},cron表达式:{}", hospitalId, cronExpression);
|
|
|
+ } catch (SchedulerException e) {
|
|
|
+ log.error("创建HIS数据拉取任务失败,hospitalId:{}", hospitalId, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|