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;
|
2024-03-12 09:10:03 +08:00
|
|
|
|
import com.rax.vital.datasource.MySQLSource;
|
2024-03-15 13:30:31 +08:00
|
|
|
|
import com.rax.vital.medicine.service.*;
|
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;
|
2024-02-22 18:13:08 +08:00
|
|
|
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
2024-03-12 09:10:03 +08:00
|
|
|
|
import java.sql.Connection;
|
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;
|
|
|
|
|
|
2024-03-12 09:10:03 +08:00
|
|
|
|
private final AIMedicineService aiMedicineService;
|
|
|
|
|
|
|
|
|
|
private final DoctorMedicineService doctorMedicineService;
|
|
|
|
|
|
|
|
|
|
private final FlagService flagService;
|
|
|
|
|
|
2024-03-15 13:30:31 +08:00
|
|
|
|
private final RevulsionService revulsionService;
|
|
|
|
|
|
2024-03-12 09:10:03 +08:00
|
|
|
|
// mongoDB定时任务容器
|
|
|
|
|
private static final Map<String, TimerTask> timerMongoTaskMap = new HashMap<>(300);
|
2024-03-01 13:01:35 +08:00
|
|
|
|
|
|
|
|
|
// mongoDB链接工具类容器
|
|
|
|
|
private static final Map<String, MongoDBSource> mongoDBSourceMap = new HashMap<>(300);
|
|
|
|
|
|
2024-03-12 09:10:03 +08:00
|
|
|
|
// mysql定时任务容器
|
|
|
|
|
private static final Map<String, TimerTask> timerMysqlTaskMap = new HashMap<>(300);
|
|
|
|
|
|
|
|
|
|
// mysql链接容器
|
|
|
|
|
private static final Map<String, MySQLSource> mysqlConnectionMap = new HashMap(300);
|
|
|
|
|
|
2024-03-01 13:01:35 +08:00
|
|
|
|
// 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}")
|
2024-03-12 09:10:03 +08:00
|
|
|
|
private String mongoUsername;
|
2024-03-01 13:01:35 +08:00
|
|
|
|
|
|
|
|
|
// MongoDB的用户的密码
|
|
|
|
|
@Value("${vital-sign.mongodb.password}")
|
2024-03-12 09:10:03 +08:00
|
|
|
|
private String mongoPassword;
|
|
|
|
|
|
|
|
|
|
// mysql地址
|
|
|
|
|
@Value("${vital-sign.mysql.host}")
|
|
|
|
|
private String mysqlHost;
|
|
|
|
|
|
|
|
|
|
// mysql用户名
|
|
|
|
|
@Value("${vital-sign.mysql.username}")
|
|
|
|
|
private String mysqlUsername;
|
|
|
|
|
|
|
|
|
|
// mysql用户密码
|
|
|
|
|
@Value("${vital-sign.mysql.password}")
|
|
|
|
|
private String mysqlPassword;
|
2024-03-01 13:01:35 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2024-03-12 09:10:03 +08:00
|
|
|
|
* 根据当前用户和患者数据库进行查询生命体征和用药信息并推送,数据库类型是MongoDB
|
2024-03-01 13:01:35 +08:00
|
|
|
|
*
|
|
|
|
|
* @author zhaoyz
|
|
|
|
|
*/
|
2024-03-19 09:26:50 +08:00
|
|
|
|
public void createAndSendMessageMongo(String database, String username, String simpSessionId) {
|
|
|
|
|
TimerTask task = timerMongoTaskMap.get(simpSessionId);
|
2024-02-22 18:13:08 +08:00
|
|
|
|
if (task != null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 09:26:50 +08:00
|
|
|
|
MongoDBSource mongoDBSource = mongoDBSourceMap.get(simpSessionId);
|
2024-03-01 13:01:35 +08:00
|
|
|
|
if (mongoDBSource == null) {
|
2024-03-12 09:10:03 +08:00
|
|
|
|
mongoDBSource = new MongoDBSource(mongoDBHost, mongoPassword, mongoUsername, database);
|
2024-03-19 09:26:50 +08:00
|
|
|
|
mongoDBSourceMap.put(simpSessionId, mongoDBSource);
|
2024-03-12 09:10:03 +08:00
|
|
|
|
mongoDBSource.open();
|
2024-03-01 13:01:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MongoDBSource finalMongoDBSource = mongoDBSource;
|
2024-02-22 18:13:08 +08:00
|
|
|
|
TimerTask timerTask = new TimerTask() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2024-03-15 13:30:31 +08:00
|
|
|
|
|
2024-03-12 09:10:03 +08:00
|
|
|
|
MongoTemplate template = finalMongoDBSource.getTemplate();
|
2024-03-01 13:01:35 +08:00
|
|
|
|
HashMap<String, Object> result = new HashMap();
|
2024-03-15 13:30:31 +08:00
|
|
|
|
List vitalSignsList = vitalSignsService.getVitalSignsList(template);
|
2024-03-01 13:01:35 +08:00
|
|
|
|
result.put("vitalSignsList", vitalSignsList);
|
2024-03-15 13:30:31 +08:00
|
|
|
|
List aiMedicineList = aiMedicineService.getAIMedicine(template);
|
|
|
|
|
result.put("aiMedicineList", aiMedicineList);
|
|
|
|
|
List docMedicineList = doctorMedicineService.getDocMedicine(template);
|
|
|
|
|
result.put("docMedicineList", docMedicineList);
|
|
|
|
|
List revulsionList = revulsionService.getRevulsionServiceList(template);
|
|
|
|
|
result.put("revulsionList", revulsionList);
|
|
|
|
|
List flags = flagService.getFlags(template);
|
|
|
|
|
result.put("flags", flags);
|
|
|
|
|
|
2024-03-19 09:26:50 +08:00
|
|
|
|
simpMessagingTemplate.convertAndSendToUser(username + ":" + database, "/surgeryData", result);
|
2024-03-01 13:01:35 +08:00
|
|
|
|
|
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-19 09:26:50 +08:00
|
|
|
|
timerMongoTaskMap.put(simpSessionId, timerTask);
|
2024-03-12 09:10:03 +08:00
|
|
|
|
}
|
2024-03-01 13:01:35 +08:00
|
|
|
|
|
2024-03-15 13:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
* 根据当前用户和患者数据库进行查询生命体征和用药信息并推送,数据库类型是MySQL
|
|
|
|
|
*
|
|
|
|
|
* @param database
|
|
|
|
|
*/
|
2024-03-12 09:10:03 +08:00
|
|
|
|
public void createAndSendMessageMySQL(String database) {
|
|
|
|
|
String account = SecurityUtils.getUser().getUsername();
|
|
|
|
|
TimerTask task = timerMysqlTaskMap.get(account + "-" + database);
|
|
|
|
|
if (task != null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MySQLSource mySQLSource = mysqlConnectionMap.get(database);
|
|
|
|
|
if (mySQLSource == null) {
|
|
|
|
|
mySQLSource = new MySQLSource(mysqlHost, mysqlPassword, mysqlUsername, database);
|
|
|
|
|
mysqlConnectionMap.put(database, mySQLSource);
|
|
|
|
|
mySQLSource.open();
|
|
|
|
|
mySQLSource.increaseCount();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MySQLSource finalMySQLSource = mySQLSource;
|
|
|
|
|
TimerTask timerTask = new TimerTask() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
HashMap<String, Object> result = new HashMap();
|
|
|
|
|
Connection connection = finalMySQLSource.getConnection();
|
|
|
|
|
List<Map> vitalSignsList = vitalSignsService.getVitalSignsList(connection);
|
|
|
|
|
result.put("vitalSignsList", vitalSignsList);
|
2024-03-15 13:30:31 +08:00
|
|
|
|
List<Map> aiMedicineList = aiMedicineService.getAIMedicine(connection);
|
2024-03-12 09:10:03 +08:00
|
|
|
|
result.put("aiMedicineList", aiMedicineList);
|
2024-03-15 13:30:31 +08:00
|
|
|
|
List<Map> docMedicineList = doctorMedicineService.getDocMedicine(connection);
|
2024-03-12 09:10:03 +08:00
|
|
|
|
result.put("docMedicineList", docMedicineList);
|
2024-03-15 13:30:31 +08:00
|
|
|
|
Map flag = flagService.getFlag(connection);
|
2024-03-12 09:10:03 +08:00
|
|
|
|
result.put("flag", flag);
|
2024-03-15 13:30:31 +08:00
|
|
|
|
simpMessagingTemplate.convertAndSendToUser(account, "/doctorMedicine", result);
|
2024-03-12 09:10:03 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// 定时任务,设置1秒
|
|
|
|
|
Timer timer = new Timer();
|
|
|
|
|
timer.schedule(timerTask, 0, 1000);
|
|
|
|
|
timerMysqlTaskMap.put(account + "-" + database, timerTask);
|
2024-02-22 18:13:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 13:01:35 +08:00
|
|
|
|
/**
|
2024-03-12 09:10:03 +08:00
|
|
|
|
* 停止指定的某个用户查询的患者数据库定时器,数据库类型是MongoDB
|
2024-03-01 13:01:35 +08:00
|
|
|
|
*
|
|
|
|
|
* @author zhaoyz
|
|
|
|
|
*/
|
2024-03-19 09:26:50 +08:00
|
|
|
|
public synchronized void stopTimerTaskMongo(String simpSessionId) {
|
|
|
|
|
TimerTask timerTask = timerMongoTaskMap.get(simpSessionId);
|
2024-02-22 18:13:08 +08:00
|
|
|
|
if (timerTask != null) {
|
|
|
|
|
timerTask.cancel();
|
2024-03-19 09:26:50 +08:00
|
|
|
|
MongoDBSource mongoDBSource = mongoDBSourceMap.get(simpSessionId);
|
|
|
|
|
mongoDBSource.close();
|
|
|
|
|
timerMongoTaskMap.remove(simpSessionId);
|
|
|
|
|
mongoDBSourceMap.remove(simpSessionId);
|
2024-02-22 18:13:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-15 13:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
* 停止指定的某个用户查询的患者数据库定时器,数据库类型是MySQL
|
|
|
|
|
*
|
|
|
|
|
* @param database
|
|
|
|
|
* @param user
|
|
|
|
|
*/
|
2024-03-12 09:10:03 +08:00
|
|
|
|
public synchronized void stopTimerTaskMySQL(String database, String user) {
|
|
|
|
|
TimerTask timerTask = timerMysqlTaskMap.get(user + "-" + database);
|
|
|
|
|
if (timerTask != null) {
|
|
|
|
|
timerTask.cancel();
|
|
|
|
|
timerMysqlTaskMap.remove(user + "-" + database);
|
|
|
|
|
|
|
|
|
|
MySQLSource mySQLSource = mysqlConnectionMap.get(database);
|
|
|
|
|
mySQLSource.decreaseCount();
|
|
|
|
|
int count = mySQLSource.getCount();
|
|
|
|
|
if (count == 0) {
|
|
|
|
|
mySQLSource.close();
|
|
|
|
|
mysqlConnectionMap.remove(database);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-22 18:13:08 +08:00
|
|
|
|
|
|
|
|
|
}
|