2024-02-22 18:13:08 +08:00
|
|
|
|
package com.rax.vital.timer;
|
|
|
|
|
|
2024-03-01 13:01:35 +08:00
|
|
|
|
import com.rax.common.security.util.SecurityUtils;
|
|
|
|
|
import com.rax.vital.datasource.MongoDBSource;
|
|
|
|
|
import com.rax.vital.medicine.service.VitalSignsService;
|
2024-02-22 18:13:08 +08:00
|
|
|
|
import lombok.RequiredArgsConstructor;
|
2024-03-01 13:01:35 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
|
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
|
|
import org.springframework.data.mongodb.core.query.Query;
|
2024-02-22 18:13:08 +08:00
|
|
|
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
2024-03-01 13:01:35 +08:00
|
|
|
|
import java.util.*;
|
2024-02-22 18:13:08 +08:00
|
|
|
|
|
2024-03-01 13:01:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* 生命体征和用药信息推送
|
|
|
|
|
*
|
|
|
|
|
* @author zhaoyz
|
|
|
|
|
* @date 2024/2/29
|
|
|
|
|
*/
|
|
|
|
|
@RefreshScope
|
2024-02-22 18:13:08 +08:00
|
|
|
|
@Component
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class VitalSignTimer {
|
|
|
|
|
|
|
|
|
|
private final SimpMessagingTemplate simpMessagingTemplate;
|
|
|
|
|
|
2024-03-01 13:01:35 +08:00
|
|
|
|
private final VitalSignsService vitalSignsService;
|
|
|
|
|
|
|
|
|
|
// 定时任务容器
|
|
|
|
|
private static final Map<String, TimerTask> timerTaskMap = new HashMap<>(300);
|
|
|
|
|
|
|
|
|
|
// mongoDB链接工具类容器
|
|
|
|
|
private static final Map<String, MongoDBSource> mongoDBSourceMap = new HashMap<>(300);
|
|
|
|
|
|
|
|
|
|
// MongoDB的地址
|
|
|
|
|
@Value("${vital-sign.mongodb.host}")
|
|
|
|
|
private String mongoDBHost;
|
2024-02-22 18:13:08 +08:00
|
|
|
|
|
2024-03-01 13:01:35 +08:00
|
|
|
|
// MongoDB的用户名
|
|
|
|
|
@Value("${vital-sign.mongodb.username}")
|
|
|
|
|
private String username;
|
|
|
|
|
|
|
|
|
|
// MongoDB的用户的密码
|
|
|
|
|
@Value("${vital-sign.mongodb.password}")
|
|
|
|
|
private String password;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据当前用户和患者数据库进行查询生命体征和用药信息并推送
|
|
|
|
|
*
|
|
|
|
|
* @author zhaoyz
|
|
|
|
|
*/
|
|
|
|
|
public void createAndSendMessage(String database) {
|
|
|
|
|
String account = SecurityUtils.getUser().getUsername();
|
|
|
|
|
TimerTask task = timerTaskMap.get(account + "-" + database);
|
2024-02-22 18:13:08 +08:00
|
|
|
|
if (task != null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 13:01:35 +08:00
|
|
|
|
MongoDBSource mongoDBSource = mongoDBSourceMap.get(database);
|
|
|
|
|
if (mongoDBSource == null) {
|
|
|
|
|
mongoDBSource = new MongoDBSource(mongoDBHost, password, username, database);
|
|
|
|
|
mongoDBSourceMap.put(database, mongoDBSource);
|
|
|
|
|
mongoDBSource.increaseCount();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MongoDBSource finalMongoDBSource = mongoDBSource;
|
2024-02-22 18:13:08 +08:00
|
|
|
|
TimerTask timerTask = new TimerTask() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2024-03-01 13:01:35 +08:00
|
|
|
|
List<Map> vitalSignsList = vitalSignsService.getVitalSignsList(finalMongoDBSource);
|
|
|
|
|
HashMap<String, Object> result = new HashMap();
|
|
|
|
|
result.put("vitalSignsList", vitalSignsList);
|
|
|
|
|
simpMessagingTemplate.convertAndSendToUser(account, "/doctorMedicine", vitalSignsList);
|
|
|
|
|
|
2024-02-22 18:13:08 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
2024-03-01 13:01:35 +08:00
|
|
|
|
// 定时任务,设置1秒
|
|
|
|
|
Timer timer = new Timer();
|
2024-02-22 18:13:08 +08:00
|
|
|
|
timer.schedule(timerTask, 0, 1000);
|
2024-03-01 13:01:35 +08:00
|
|
|
|
|
|
|
|
|
timerTaskMap.put(account + "-" + database, timerTask);
|
2024-02-22 18:13:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 13:01:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* 停止指定的某个用户查询的患者数据库定时器
|
|
|
|
|
*
|
|
|
|
|
* @param database
|
|
|
|
|
* @param user
|
|
|
|
|
* @author zhaoyz
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void stopTimerTask(String database, String user) {
|
|
|
|
|
TimerTask timerTask = timerTaskMap.get(user + "-" + database);
|
2024-02-22 18:13:08 +08:00
|
|
|
|
if (timerTask != null) {
|
|
|
|
|
timerTask.cancel();
|
2024-03-01 13:01:35 +08:00
|
|
|
|
timerTaskMap.remove(user + "-" + database);
|
|
|
|
|
|
|
|
|
|
MongoDBSource mongoDBSource = mongoDBSourceMap.get(database);
|
|
|
|
|
mongoDBSource.decreaseCount();
|
|
|
|
|
int count = mongoDBSource.getCount();
|
|
|
|
|
if (count == 0) {
|
|
|
|
|
mongoDBSource.close();
|
|
|
|
|
mongoDBSourceMap.remove(database);
|
|
|
|
|
}
|
2024-02-22 18:13:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|