|
@ -0,0 +1,454 @@
|
|
|
package com.yihu.jw.hospital.module.health.service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.yihu.jw.device.dao.DeviceDetailDao;
|
|
|
import com.yihu.jw.device.dao.DeviceInfoDao;
|
|
|
import com.yihu.jw.device.dao.DevicePatientHealthIndexDao;
|
|
|
import com.yihu.jw.device.dao.PatientDeviceDao;
|
|
|
import com.yihu.jw.entity.base.patient.BasePatientDO;
|
|
|
import com.yihu.jw.entity.care.device.DeviceDetail;
|
|
|
import com.yihu.jw.entity.care.device.DeviceInfo;
|
|
|
import com.yihu.jw.entity.care.device.DevicePatientDevice;
|
|
|
import com.yihu.jw.entity.care.device.DevicePatientHealthIndex;
|
|
|
import com.yihu.jw.entity.hospital.message.SystemMessageDO;
|
|
|
import com.yihu.jw.hospital.message.dao.SystemMessageDao;
|
|
|
import com.yihu.jw.hospital.module.health.controller.Result;
|
|
|
import com.yihu.jw.hospital.utils.CountDistance;
|
|
|
import com.yihu.jw.hospital.utils.DeviceDataPushLogUtil;
|
|
|
import com.yihu.jw.im.util.ImUtil;
|
|
|
import com.yihu.jw.patient.dao.BasePatientDao;
|
|
|
import com.yihu.jw.util.common.GpsUtil;
|
|
|
import com.yihu.jw.util.date.DateUtil;
|
|
|
import com.yihu.jw.util.healthIndex.HealthIndexUtil;
|
|
|
import com.yihu.jw.utils.StringUtil;
|
|
|
import net.sf.json.JSONObject;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* Created by Bing on 2021/7/7.
|
|
|
*/
|
|
|
@Service
|
|
|
public class DeviceUploadService {
|
|
|
private static Logger logger = LoggerFactory.getLogger(DeviceUploadService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private DeviceInfoDao deviceInfoDao;
|
|
|
@Autowired
|
|
|
private BasePatientDao patientDao;
|
|
|
@Autowired
|
|
|
private DeviceDetailDao deviceDetailDao;
|
|
|
@Autowired
|
|
|
private PatientDeviceDao patientDeviceDao;
|
|
|
@Autowired
|
|
|
private DevicePatientHealthIndexDao patientHealthIndexDao;
|
|
|
@Autowired
|
|
|
private SystemMessageDao systemMessageDao;
|
|
|
@Autowired
|
|
|
private HealthIndexUtil healthIndexUtil;
|
|
|
@Autowired
|
|
|
private ImUtil imUtil;
|
|
|
@Autowired
|
|
|
private CountDistance countDistance;
|
|
|
@Autowired
|
|
|
private GpsUtil gpsUtil;
|
|
|
@Autowired
|
|
|
private JdbcTemplate jdbcTemplate;
|
|
|
@Autowired
|
|
|
private DeviceDataPushLogUtil dataPushLogUtil;
|
|
|
|
|
|
public Result uploadDevicesData(String dataJson, HttpServletRequest request)throws Exception {
|
|
|
try {
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
Map<String, Object> map = objectMapper.readValue(dataJson, Map.class);
|
|
|
|
|
|
String deviceSn = map.get("deviceSn").toString();//设备唯一码
|
|
|
Integer deviceType = Integer.parseInt(map.get("deviceType").toString());//设备类型1:血压 2:血糖
|
|
|
String data = map.get("data").toString();//体征值(血糖:data=血糖值 血压:data=收缩压,舒张压,脉搏)
|
|
|
String uploadTime = map.get("uploadTime").toString();//体征上传时间yyyy-MM-dd HH:mm:ss
|
|
|
String manufacturerCode = map.get("manufacturerCode").toString();//厂商代码
|
|
|
String manufacturerName = map.get("manufacturerName").toString();//厂商名称
|
|
|
String unit = map.get("unit").toString();//单位mmol/L,mmHg
|
|
|
String sendTime = map.get("sendTime").toString();//发送时间yyyy-MM-dd HH:mm:ss
|
|
|
String userType = map.get("button").toString();//按键号 即 userType
|
|
|
String measurementType=null==map.get("measurementType")?"":map.get("measurementType").toString(); //单个设备的测量类型,deviceType=4智能手表:1心率,2血压
|
|
|
String paraString = JSON.toJSONString(request.getParameterMap());
|
|
|
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("deviceSn", deviceSn);
|
|
|
json.put("deviceType", deviceType);
|
|
|
json.put("data", data);
|
|
|
json.put("uploadTime", uploadTime);//三诺的上传时间
|
|
|
json.put("manufacturerCode", manufacturerCode);
|
|
|
json.put("manufacturerName", manufacturerName);
|
|
|
json.put("unit", unit);
|
|
|
json.put("sendTime", sendTime);//三诺的测量时间
|
|
|
json.put("userType", userType);
|
|
|
json.put("measurementType", measurementType);
|
|
|
|
|
|
String type = deviceType + "";
|
|
|
if (1 == deviceType) {
|
|
|
type = "2";
|
|
|
} else if (2 == deviceType) {
|
|
|
type = "1";
|
|
|
userType = "-1";
|
|
|
}
|
|
|
DeviceInfo deviceInfo = new DeviceInfo();
|
|
|
deviceInfo.setDeviceData(dataJson);
|
|
|
deviceInfo.setDeviceType(type);
|
|
|
deviceInfo.setPushDate(DateUtil.getNowTimestamp());
|
|
|
deviceInfo.setStatus("1");
|
|
|
deviceInfoDao.save(deviceInfo);
|
|
|
|
|
|
DevicePatientHealthIndex result = null;
|
|
|
if(type.equalsIgnoreCase("1")){
|
|
|
if (Double.parseDouble(data)>=1){
|
|
|
result = savePatientDeviceData(deviceSn, type, data, uploadTime, userType, json,measurementType);
|
|
|
}else {
|
|
|
logger.info("This blood device data below 1.0:====="+dataJson);
|
|
|
throw new Exception("This blood device data below 1.0 ");
|
|
|
}
|
|
|
}else {
|
|
|
result = savePatientDeviceData(deviceSn, type, data, uploadTime, userType, json,measurementType);
|
|
|
}
|
|
|
if (result == null) {
|
|
|
logger.info("This device is not relate patient!:====="+dataJson);
|
|
|
throw new Exception("This device is not relate patient!");
|
|
|
}
|
|
|
dataPushLogUtil.savePushLog(deviceSn,paraString,"体征数据接收");
|
|
|
dataPushLogUtil.updContactStatus(deviceSn,1,false);
|
|
|
//保存消息
|
|
|
BasePatientDO patientDO = patientDao.findById(result.getUser()).orElse(null);
|
|
|
if (null != patientDO){
|
|
|
SystemMessageDO messageDO = new SystemMessageDO();
|
|
|
String typeName = null;
|
|
|
switch (type){
|
|
|
case "1":
|
|
|
typeName = "您有新的血糖测量记录:血糖浓度:"+result.getValue1()+"mmol/L";
|
|
|
break;
|
|
|
case "2":
|
|
|
typeName = "您有新的血压测量记录:收缩压:"+result.getValue1()+"mmHg,舒张压:"
|
|
|
+result.getValue2()+"mmHg,脉搏:"+result.getValue3()+"次/min";
|
|
|
break;
|
|
|
}
|
|
|
if (StringUtils.isNotBlank(typeName)){
|
|
|
/* String adminSql = "SELECT py_code FROM wlyy_hospital_sys_dict WHERE dict_name = 'pushOnOff' AND dict_code ='"+6+"' ";
|
|
|
String onOff= jdbcTemplate.queryForObject(adminSql,String.class);
|
|
|
Boolean kg = Boolean.parseBoolean(onOff);
|
|
|
String patientOnOffSql = "select on_off from base_patient_pad_pushonoff where patient = '" + patientDO.getId() + "' and type = 3";
|
|
|
Integer integer = jdbcTemplate.queryForObject(patientOnOffSql,Integer.class);
|
|
|
if (kg&&integer==0?false:true) {
|
|
|
messageDO.setTitle(typeName);
|
|
|
messageDO.setType("42");//42体征设备测量
|
|
|
messageDO.setSender("system");
|
|
|
messageDO.setSenderName("养护助手");
|
|
|
messageDO.setRelationCode(result.getId() + "");
|
|
|
messageDO.setReceiver(patientDO.getId());
|
|
|
messageDO.setReceiverName(patientDO.getName());
|
|
|
// messageDO.setUserType(1);
|
|
|
messageDO.setOver("1");
|
|
|
|
|
|
|
|
|
messageDO.setCode(type);//与体征类型对应
|
|
|
JSONArray errorIndex = healthIndexUtil.verifyHealthIndex(Integer.parseInt(type), result.getValue1(), result.getValue2(),
|
|
|
result.getValue3(), result.getValue4(), result.getValue5(), result.getValue6(),
|
|
|
result.getValue7());
|
|
|
messageDO.setData(JSON.toJSONString(errorIndex, SerializerFeature.WriteNullStringAsEmpty));
|
|
|
messageDO.setDel("1");
|
|
|
messageDO.setCreateTime(new Date());
|
|
|
//推送socket
|
|
|
com.alibaba.fastjson.JSONObject message = new com.alibaba.fastjson.JSONObject();
|
|
|
message.put("title", typeName);
|
|
|
message.put("code", messageDO.getCode());
|
|
|
message.put("content", messageDO.getData());
|
|
|
message.put("relation_code", messageDO.getRelationCode());
|
|
|
message.put("content_type", 42);
|
|
|
message.put("content_notice", "");
|
|
|
// message.put("audioUrl", url);
|
|
|
String content_notice = "";
|
|
|
for (int i = 0; i < errorIndex.size(); i++) {
|
|
|
com.alibaba.fastjson.JSONObject tmp = errorIndex.getJSONObject(i);
|
|
|
if (0 != tmp.getInteger("error")) {
|
|
|
result.setStatus(1);
|
|
|
}
|
|
|
if (1 == tmp.getInteger("error")) {
|
|
|
content_notice += tmp.getString("indexName") + "、";
|
|
|
}
|
|
|
}
|
|
|
if (StringUtils.isNotBlank(content_notice)) {
|
|
|
content_notice = content_notice.substring(0, content_notice.length() - 1);
|
|
|
message.put("content_notice", "您的key1过高,请注意饮食,尽量食用少油少盐食物".replace("key1", content_notice));
|
|
|
messageDO.setContent("您的key1过高,请注意饮食,尽量食用少油少盐食物".replace("key1", content_notice));
|
|
|
dataPushLogUtil.updContactStatus(deviceSn, 1, true);
|
|
|
} else {
|
|
|
dataPushLogUtil.updContactStatus(deviceSn, 1, false);
|
|
|
}
|
|
|
patientHealthIndexDao.save(result);
|
|
|
systemMessageDao.save(messageDO);
|
|
|
imUtil.sendPatientSystemMessage(messageDO.getReceiver(), JSON.toJSONString(message, SerializerFeature.WriteMapNullValue));
|
|
|
}*/
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
logger.error(e.getMessage());
|
|
|
}
|
|
|
|
|
|
return Result.success("Device data incoming success");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 保存设备数据
|
|
|
* @param type 设备类型 4 智能手表
|
|
|
* @param userType 用户身份:-1 单用户
|
|
|
* @param measurementType 单设备多功能类型 type=4时,measurementType=1心率,measurementType=2血压
|
|
|
*/
|
|
|
public DevicePatientHealthIndex savePatientDeviceData(String deviceSn, String type, String data, String uploadTime, String userType, JSONObject json, String measurementType) throws Exception {
|
|
|
Date currentTime = DateUtil.getNowTimestamp();
|
|
|
Date time = currentTime;
|
|
|
if (!StringUtil.isEmpty(uploadTime)) {
|
|
|
//设备重新开机启动会有默认时间,相隔很远.
|
|
|
time = DateUtil.toTimestamp(uploadTime, DateUtil.YYYY_MM_DD_HH_MM_SS);
|
|
|
Date monthTime = DateUtil.getNextMonthReturnDate( currentTime,-12);
|
|
|
if (DateUtil.compareDate(time, monthTime) < 0) {
|
|
|
time = currentTime;
|
|
|
}
|
|
|
}
|
|
|
if (StringUtil.isStrEmpty(userType)) {
|
|
|
userType = "-1";
|
|
|
}
|
|
|
DevicePatientDevice device = null;
|
|
|
DeviceDetail deviceDetail = null;
|
|
|
List<DeviceDetail> deviceDetails = deviceDetailDao.findByDeviceCode(deviceSn);
|
|
|
if (deviceDetails != null || deviceDetails.size()!=0){
|
|
|
deviceDetail = deviceDetails.get(0);
|
|
|
if (deviceDetail.getGrantOrgCode() != null&& deviceDetail.getGrantOrgCode().equals("3502050300")){//
|
|
|
List<DevicePatientDevice> patientDeviceList = patientDeviceDao.findByDeviceSnAndCategoryCode(deviceSn, type);
|
|
|
if (patientDeviceList != null&&patientDeviceList.size()==1) {
|
|
|
device = patientDeviceList.get(0);
|
|
|
}else if(patientDeviceList != null&&patientDeviceList.size()==2){
|
|
|
for (int i=0;i<patientDeviceList.size();i++){
|
|
|
if(userType.equals(patientDeviceList.get(i).getUserType())){
|
|
|
device = patientDeviceList.get(i);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}else {
|
|
|
// device = patientDeviceDao.findByDeviceSnAndCategoryCodeAndUserType(deviceSn, type, userType);
|
|
|
List<DevicePatientDevice> devices = patientDeviceDao.findByDeviceSnAndCategoryCode(deviceSn, type);
|
|
|
if (devices.size()>0){
|
|
|
device = devices.get(0);
|
|
|
}else {
|
|
|
device=null;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (device != null) {
|
|
|
|
|
|
/*//根据设备获取患者(设备只绑定一个人的时候,不判断按键)
|
|
|
|
|
|
if (device!=null) {*/
|
|
|
/************设备数据重复推送处理 start**********************/
|
|
|
String value1 = data;
|
|
|
String value2="";
|
|
|
String value3="";
|
|
|
if("2".equals(type)){
|
|
|
String[] value = data.split(",");
|
|
|
if (value.length > 0) {
|
|
|
value1 = value[0]; //收缩压
|
|
|
value2 = value[1];
|
|
|
value3 = value[2];
|
|
|
}
|
|
|
}else if("4".equals(type)){
|
|
|
if(StringUtils.isNotBlank(measurementType)&&"2".equals(measurementType)){
|
|
|
type="2";
|
|
|
String[] value = data.split(",");
|
|
|
if (value.length > 0) {
|
|
|
value1 = value[0]; //收缩压
|
|
|
value2 = value[1];
|
|
|
/* value3 = value[2];*/
|
|
|
}
|
|
|
}else {
|
|
|
//心率
|
|
|
type="5";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (value1.equals(value2) && value2.equals(value3)){
|
|
|
return null;
|
|
|
}
|
|
|
// List<PatientHealthIndex> list = patientHealthIndexDao.findByType(device.getUser(),deviceSn,value1,Integer.parseInt(type),time);
|
|
|
/*long maxtime = time.getTime()+30*1000;
|
|
|
long mintime = time.getTime()-30*1000;*/
|
|
|
Date minDate = DateUtil.getPreDays(time,-3);
|
|
|
Date maxDate = DateUtil.getPreDays(time,3);
|
|
|
List<DevicePatientHealthIndex> list = ioTfindByType(device.getUser(),deviceSn,value1,value2,value3,Integer.parseInt(type),time,minDate,maxDate);
|
|
|
if(list!=null&&list.size()>0){
|
|
|
return null;
|
|
|
}
|
|
|
/************设备数据重复推送处理 end**********************/
|
|
|
|
|
|
DevicePatientHealthIndex obj = new DevicePatientHealthIndex();
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
obj.setCzrq(sdf.parse(json.getString("sendTime")));//创建时间取三诺的测量时间记录
|
|
|
obj.setDeviceSn(deviceSn);
|
|
|
obj.setDel("1");
|
|
|
obj.setRecordDate(sdf.parse(json.getString("uploadTime"))); //记录时间取上传时间
|
|
|
obj.setSortDate(time); //排序时间
|
|
|
// obj.setManageResult(0);//默认数据是医生未干预状态
|
|
|
String user = device.getUser();
|
|
|
|
|
|
obj.setUser(user);
|
|
|
// obj.setButton(userType);
|
|
|
obj.setIdcard(device.getUserIdcard());
|
|
|
obj.setStatus(0);
|
|
|
// 1血糖 2血压 3体重 4腰围,5心率
|
|
|
switch (type) {
|
|
|
case "1":
|
|
|
obj.setType(1);
|
|
|
obj.setValue1(data); //血糖值
|
|
|
obj.setValue2(formatBloodSuger(user,deviceSn,DateUtil.dateToStr(time, DateUtil.YYYY_MM_DD_HH_MM_SS)));
|
|
|
break;
|
|
|
case "2":
|
|
|
obj.setType(2);
|
|
|
String[] value = data.split(",");
|
|
|
if (value.length > 0) {
|
|
|
obj.setValue1(value[0]); //收缩压
|
|
|
}
|
|
|
if (value.length > 1) {
|
|
|
obj.setValue2(value[1]); //舒张压
|
|
|
}
|
|
|
if (value.length > 2) {
|
|
|
obj.setValue3(value[2]); //脉搏
|
|
|
}
|
|
|
if (value.length > 3) {
|
|
|
obj.setValue4(value[3]); //有无心率不齐
|
|
|
}
|
|
|
break;
|
|
|
case "3":
|
|
|
obj.setType(3);
|
|
|
obj.setValue1(data); //体重
|
|
|
break;
|
|
|
case "4":
|
|
|
obj.setType(4);
|
|
|
obj.setValue1(data); //腰围
|
|
|
break;
|
|
|
case "5":
|
|
|
obj.setType(5);
|
|
|
obj.setValue1(data); //心率
|
|
|
break;
|
|
|
default:
|
|
|
throw new Exception("Can not support the metric!");
|
|
|
}
|
|
|
//新增字段处理
|
|
|
BasePatientDO patient = patientDao.findById(user).orElse(null);
|
|
|
if(patient!=null){
|
|
|
obj.setName(patient.getName());
|
|
|
}
|
|
|
if(deviceDetail!=null){
|
|
|
obj.setHospital(deviceDetail.getGrantOrgCode());
|
|
|
obj.setHospitalName(deviceDetail.getOrgName());
|
|
|
}
|
|
|
|
|
|
obj = patientHealthIndexDao.save(obj);
|
|
|
// if ("1".equals(type)||"2".equals(type)){
|
|
|
// dataPushLogUtil.savePatientMonitorData(deviceSn,Integer.parseInt(type),JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue),obj.getId()+"");
|
|
|
// }
|
|
|
return obj;
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查找重复数据
|
|
|
* @param user
|
|
|
* @param deviceSn
|
|
|
* @param value1
|
|
|
* @param type
|
|
|
* @param time
|
|
|
* @return
|
|
|
*/
|
|
|
public List<DevicePatientHealthIndex> ioTfindByType(String user,String deviceSn,String value1,String value2,String value3,Integer type,Date time,Date timeMin,Date timeMax){
|
|
|
|
|
|
List<DevicePatientHealthIndex> list = new ArrayList<>();
|
|
|
if (type==1){
|
|
|
list = patientHealthIndexDao.findByTypeInHalfMinute(user,deviceSn,value1,type,timeMin,timeMax);
|
|
|
}else if (type==2){
|
|
|
list = patientHealthIndexDao.findByTypeInHalfMinuteAllValue(user,deviceSn,value1,type,timeMin,timeMax,value2,value3);
|
|
|
}
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 血糖测量时间段
|
|
|
* @param user
|
|
|
* @param deviceSN
|
|
|
* @param dateTime
|
|
|
* @return
|
|
|
*/
|
|
|
private String formatBloodSuger(String user,String deviceSN,String dateTime) {
|
|
|
String fastingStart = "00:00:00";
|
|
|
String fastingEnd = "07:59:59";
|
|
|
String afterBreakFastStart = "08:00:00";
|
|
|
String afterBreakFastEnd = "09:59:59";
|
|
|
String beforeLunchStart = "10:00:00";
|
|
|
String beforeLunchEnd = "11:59:59";
|
|
|
String afterLunchStart = "12:00:00";
|
|
|
String afterLunchEnd = "13:59:59";
|
|
|
String beforeDinnerStart = "14:00:00";
|
|
|
String beforeDinnerEnd = "17:59:59";
|
|
|
String afterDinnerStart = "18:00:00";
|
|
|
String afterDinnerEnd = "19:59:59";
|
|
|
String beforeSleepStart = "20:00:00";
|
|
|
String beforeSleepEnd = "23:59:59";
|
|
|
if (isInArea(dateTime, fastingStart,fastingEnd)) {
|
|
|
return "1";//"空腹血糖"
|
|
|
}
|
|
|
if (isInArea(dateTime, afterBreakFastStart,afterBreakFastEnd)) {
|
|
|
return "2";//"早餐后血糖"
|
|
|
}
|
|
|
if (isInArea(dateTime, beforeLunchStart,beforeLunchEnd)) {
|
|
|
return "3";//"午餐前血糖"
|
|
|
}
|
|
|
if (isInArea(dateTime, afterLunchStart,afterLunchEnd)) {
|
|
|
return "4";//"午餐后血糖"
|
|
|
}
|
|
|
if (isInArea(dateTime, beforeDinnerStart,beforeDinnerEnd)) {
|
|
|
return "5";//"晚餐前血糖"
|
|
|
}
|
|
|
if (isInArea(dateTime, afterDinnerStart,afterDinnerEnd)) {
|
|
|
return "6";//"晚餐后血糖"
|
|
|
}
|
|
|
if (isInArea(dateTime, beforeSleepStart,beforeSleepEnd)) {
|
|
|
return "7";//"睡前血糖"
|
|
|
}
|
|
|
|
|
|
return "1";//"空腹血糖"
|
|
|
}
|
|
|
|
|
|
// 判断时间是否在对应时间段
|
|
|
private Boolean isInArea(String time,String begin,String end) {
|
|
|
String date = DateUtil.getDateFromDateTime(time);
|
|
|
Long beginTime = DateUtil.compareDateTime(DateUtil.toTimestamp(time), DateUtil.toTimestamp(date + " " + begin ));
|
|
|
Long endTime = DateUtil.compareDateTime(DateUtil.toTimestamp(time), DateUtil.toTimestamp(date + " " + end ));
|
|
|
if (beginTime > 0 && endTime < 0) {
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
}
|