Kaynağa Gözat

代码修改

yeshijie 7 yıl önce
ebeveyn
işleme
1bd4ca2e22

+ 29 - 0
patient-co-service/wlyy_device/src/main/java/com/yihu/hos/device/common/dao/SystemDictDao.java

@ -0,0 +1,29 @@
package com.yihu.hos.device.common.dao;
import com.yihu.hos.device.model.SystemDict;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
/**
 * Created by Administrator on 2016/8/13.
 */
public interface SystemDictDao extends PagingAndSortingRepository<SystemDict, Long>, JpaSpecificationExecutor<SystemDict> {
    @Query("from SystemDict s where s.dictName=?1 order by sort asc ")
    List<SystemDict> findByDictName(String name);
    @Query("select s.value from SystemDict s where s.dictName=?1 and s.code =?2")
    String findByDictNameAndCode(String dictName, String code);
    @Query(" from SystemDict s where s.dictName=?1 and s.value =?2")
    SystemDict findByDictNameAndValue(String dictName, String value);
    @Query("select s from SystemDict s where s.value like ?1 ")
    List<SystemDict> findByLikeName(String name);
    @Query("select s from SystemDict s where s.dictName like ?1 ")
    List<SystemDict> findByLikeDictName(String name);
}

+ 100 - 0
patient-co-service/wlyy_device/src/main/java/com/yihu/hos/device/model/SystemDict.java

@ -0,0 +1,100 @@
package com.yihu.hos.device.model;
import javax.persistence.*;
/**
 * SystemDict entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "system_dict")
public class SystemDict implements java.io.Serializable {
	// Fields
	private String dictName;
	private String code;
	private String value;
	private String pyCode;
	private Integer sort;
	private Integer id;
	// Constructors
	/** default constructor */
	public SystemDict() {
	}
	/** minimal constructor */
	public SystemDict(String dictName, String code, String value) {
		this.dictName = dictName;
		this.code = code;
		this.value = value;
	}
	// Property accessors
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	/** full constructor */
	public SystemDict(String dictName, String code, String value,
                      String pyCode, Integer sort) {
		this.dictName = dictName;
		this.code = code;
		this.value = value;
		this.pyCode = pyCode;
		this.sort = sort;
	}
	@Column(name = "dict_name", nullable = false, length = 50)
	public String getDictName() {
		return this.dictName;
	}
	public void setDictName(String dictName) {
		this.dictName = dictName;
	}
	@Column(name = "code", nullable = false, length = 50)
	public String getCode() {
		return this.code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	@Column(name = "value", nullable = false, length = 50)
	public String getValue() {
		return this.value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	@Column(name = "py_code", length = 50)
	public String getPyCode() {
		return this.pyCode;
	}
	public void setPyCode(String pyCode) {
		this.pyCode = pyCode;
	}
	@Column(name = "sort")
	public Integer getSort() {
		return this.sort;
	}
	public void setSort(Integer sort) {
		this.sort = sort;
	}
}

+ 25 - 106
patient-co-service/wlyy_device/src/main/java/com/yihu/hos/device/service/DeviceService.java

@ -35,8 +35,8 @@ public class DeviceService extends BaseService{
    private PatientDeviceDao patientDeviceDao;
    @Autowired
    private DeviceDetailDao deviceDetailDao;
    @Autowired
    private PatientHealthIndexDao patientHealthIndexDao;
//    @Autowired
//    private PatientHealthIndexDao patientHealthIndexDao;
    /*@Autowired
    private BloodSuggerConfiguration bloodSuggerConfiguration;*/
    @Autowired
@ -79,6 +79,8 @@ public class DeviceService extends BaseService{
    private PatientAimBloodSuggerDao patientAimBloodSuggerDao;
    @Autowired
    private PatientAimBloodPressureDao patientAimBloodPressureDao;
    @Autowired
    private IotDeviceService iotDeviceService;
    private ObjectMapper objectMapper = new ObjectMapper();
    private Integer aStart;
@ -507,8 +509,10 @@ public class DeviceService extends BaseService{
            List<PatientHealthIndex> bloodPressurepatientHealthIndices = new ArrayList<>();
            List<PatientHealthIndex> bloodSuggurpatientHealthIndices = new ArrayList<>();
            bloodPressurepatientHealthIndices = patientHealthIndexDao.findByPatientAndTypeByPage(patientCode,2);
            bloodSuggurpatientHealthIndices = patientHealthIndexDao.findByPatientAndTypeByPage(patientCode,1);
//            bloodPressurepatientHealthIndices = patientHealthIndexDao.findByPatientAndTypeByPage(patientCode,2);
//            bloodSuggurpatientHealthIndices = patientHealthIndexDao.findByPatientAndTypeByPage(patientCode,1);
            bloodPressurepatientHealthIndices = iotDeviceService.findByPatientAndTypeByPage(patientCode,2);
            bloodSuggurpatientHealthIndices = iotDeviceService.findByPatientAndTypeByPage(patientCode,1);
            if(!bloodPressurepatientHealthIndices.isEmpty()){
                stdbloodPressureBbnormalCount = bloodPressurepatientHealthIndices.size();
@ -538,7 +542,8 @@ public class DeviceService extends BaseService{
                Date end = new Date();
                Date start = DateUtil.setDateTime(end,-7);
                //计算血糖或者血压一周内的异常记录数量
                int errorCount = patientHealthIndexDao.getCountByTimeAndStatus(start,end,1,patientCode);
//                int errorCount = patientHealthIndexDao.getCountByTimeAndStatus(start,end,1,patientCode);
                int errorCount = iotDeviceService.getCountByTimeAndStatus(start,end,1,patientCode);
                if(errorCount >= 5){//超过5次,记为预警状态
                    patient.setStandardStatus(1);
                    //1.4.2加入重点关注逻辑
@ -776,7 +781,8 @@ public class DeviceService extends BaseService{
                    value1 = value[0];     //收缩压
                }
            }
            List<PatientHealthIndex> list = patientHealthIndexDao.findByType(device.getUser(),deviceSn,value1,Integer.parseInt(type),time);
//            List<PatientHealthIndex> list = patientHealthIndexDao.findByType(device.getUser(),deviceSn,value1,Integer.parseInt(type),time);
            List<PatientHealthIndex> list = iotDeviceService.findByType(device.getUser(),deviceSn,value1,Integer.parseInt(type),time);
            if(list!=null&&list.size()>0){
                DeviceInfo deviceInfo = new DeviceInfo();
                deviceInfo.setDeviceData(json.toString());
@ -833,7 +839,8 @@ public class DeviceService extends BaseService{
                default:
                    throw new Exception("Can not support the metric!");
            }
            obj = patientHealthIndexDao.save(obj);
//            obj = patientHealthIndexDao.save(obj);
            obj = iotDeviceService.save(obj);
            return obj;
        } else {
            DeviceInfo deviceInfo = new DeviceInfo();
@ -909,7 +916,8 @@ public class DeviceService extends BaseService{
            throw new Exception("This device is not relate patient!");
        }
        patientHealthIndexDao.save(obj);
//        patientHealthIndexDao.save(obj);
        iotDeviceService.save(obj);
        return obj;
    }
@ -956,16 +964,18 @@ public class DeviceService extends BaseService{
                    msgContent += patient.getName() + "血糖异常(" + value1 + "mmol/L),请处理";
                    //体征异常,更新体征数据状态
                    data.setStatus(1);
                    patientHealthIndexDao.save(data);
//                    data.setStatus(1);
//                    patientHealthIndexDao.save(data);
                    iotDeviceService.updateStatus(data);
                }
            } else { //餐前
                if (!checkHealthIndex(NumberUtils.toDouble(value1), maxValueBefore, minValueBefore)) {
                    msgContent += patient.getName() + "血糖异常(" + value1 + "mmol/L),请处理";
                    //体征异常,更新体征数据状态
                    data.setStatus(1);
                    patientHealthIndexDao.save(data);
//                    data.setStatus(1);
//                    patientHealthIndexDao.save(data);
                    iotDeviceService.updateStatus(data);
                }
            }
        }
@ -998,8 +1008,9 @@ public class DeviceService extends BaseService{
                msgContent = patient.getName() + "血压异常(舒张压 " + value2 + "mmHg、收缩压 " + value1 + "mmHg),请处理";
                //体征异常,更新体征数据状态
                data.setStatus(1);
                patientHealthIndexDao.save(data);
//                data.setStatus(1);
//                patientHealthIndexDao.save(data);
                iotDeviceService.updateStatus(data);
            }
        }
@ -1205,96 +1216,4 @@ public class DeviceService extends BaseService{
        }
    }
    /******************************体征上传 start**************************************************/
    private String bloodSugarUnit = "mmol/L";
    private String bloodPressureUnit = "mmHg";
    private String pulseUnit = "bpm";
    private String heightUnit = "cm";
    private String weightUnit = "kg";
    private String bmiUnit = "kg/m^2";
    private String waistUnit = "cm";
    public void upload(PatientDevice patientDevice,PatientHealthIndex obj,String userType){
        com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
        if(patientDevice!=null){
            //未绑定居民
            Patient patient = patientDao.findByCode(patientDevice.getUser());
            json.put("idcard",patient.getIdcard());
            json.put("username",patient.getName());
            json.put("usercode",patient.getCode());
            json.put("device_name",patientDevice.getDeviceName());
            json.put("device_model",patientDevice.getDeviceName());
        }
        json.put("sn",obj.getDeviceSn());
        json.put("ext_code",userType);
        com.alibaba.fastjson.JSONArray jsonArray = new com.alibaba.fastjson.JSONArray();
        com.alibaba.fastjson.JSONObject js = new com.alibaba.fastjson.JSONObject();
        js.put("measure_time",obj.getRecordDate());
        switch (obj.getType()){
            case 1:
                //血糖
                js.put("blood_sugar",obj.getValue1());
                js.put("blood_sugar_unit",bloodSugarUnit);
                js.put("blood_sugar_result",obj.getValue2());
                break;
            case 2:
                //血压
                js.put("systolic",obj.getValue1());
                js.put("systolic_unit",bloodPressureUnit);
                js.put("diastolic",obj.getValue2());
                js.put("diastolic_unit",bloodPressureUnit);
                if(StringUtils.isNotBlank(obj.getValue3())){
                    js.put("pulse",obj.getValue3());
                    js.put("pulse_unit",pulseUnit);
                }
                break;
            case 3:
                //体重/身高/BMI
                js.put("height",obj.getValue1());
                js.put("height_unit",heightUnit);
                js.put("weight",obj.getValue2());
                js.put("weight_unit",weightUnit);
                if(StringUtils.isNotBlank(obj.getValue3())){
                    js.put("bmi",obj.getValue3());
                    js.put("bmi_unit",bmiUnit);
                }
                break;
            case 4:
                //腰围
                js.put("waist",obj.getValue1());
                js.put("waist_unit",waistUnit);
                break;
            default:
                break;
        }
        jsonArray.add(js);
        json.put("data",jsonArray);
        try {
            String url = wlyyService + "/iot/upload";
            Map<String, Object> params = new HashedMap();
            params.put("jsonStr", json.toString());
            HttpResponse response = httpHelper.post(url, params);
            if (response != null && response.getStatusCode() == 200) {
                JSONObject jsonObject = JSONObject.fromObject(response.getBody());
                if (!"200".equals(jsonObject.optString("status"))) {
                    throw new Exception(jsonObject.optString("msg"));
                }
            } else {
                throw new Exception("接口调用错误!" + response.getBody());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    /******************************体征上传 end****************************************************/
}

+ 270 - 0
patient-co-service/wlyy_device/src/main/java/com/yihu/hos/device/service/IotDeviceService.java

@ -0,0 +1,270 @@
package com.yihu.hos.device.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.device.common.dao.PatientDao;
import com.yihu.hos.device.common.dao.PatientHealthIndexDao;
import com.yihu.hos.device.common.dao.SystemDictDao;
import com.yihu.hos.device.common.http.HttpHelper;
import com.yihu.hos.device.common.http.HttpResponse;
import com.yihu.hos.device.common.util.DateUtil;
import com.yihu.hos.device.model.PatientHealthIndex;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * @author yeshijie on 2018/1/26.
 */
@Service
public class IotDeviceService extends BaseService{
    private static Logger logger = LoggerFactory.getLogger(IotDeviceService.class);
    @Value("${systemConfig.server_url}")
    private String wlyyService;
    @Autowired
    private SystemDictDao systemDictDao;
    @Autowired
    private PatientHealthIndexDao patientHealthIndexDao;
    @Autowired
    private HttpHelper httpHelper;
    @Autowired
    private PatientDao patientDao;
    private ObjectMapper objectMapper = new ObjectMapper();
    /**
     * 判断是否数据上传到物联网
     * @return
     */
    public Boolean isUploadIot(){
        String value = systemDictDao.findByDictNameAndCode("SYSTEM_PARAMS","DEVICE_UPLOAD_IOT");
        return "1".equals(value)?true:false;
    }
    /**
     * 修改体征异常状态
     * @param data
     */
    public void updateStatus(PatientHealthIndex data){
        if(isUploadIot()){
            //物联网更新
            updateStatus(data.getId(),"1");
        }else {
            data.setStatus(1);
            patientHealthIndexDao.save(data);
        }
    }
    /**
     * 按id查找体征记录
     * @param id
     * @return
     */
    public PatientHealthIndex findOne(Long id){
        if(isUploadIot()){
            return getById(id);
        }else {
            return patientHealthIndexDao.findOne(id);
        }
    }
    /**
     * 体征保存
     * @param data
     * @return
     */
    public PatientHealthIndex save(PatientHealthIndex data){
        if(isUploadIot()){
            return saveDevice(data);
        }else {
            return patientHealthIndexDao.save(data);
        }
    }
    /**
     * 按类型查找最近5次的体征记录
     * @param patientCode
     * @param type
     * @return
     */
    public List<PatientHealthIndex> findByPatientAndTypeByPage(String patientCode, Integer type){
        if(isUploadIot()){
            return findByPatientAndType(patientCode,type);
        }else {
            return patientHealthIndexDao.findByPatientAndTypeByPage(patientCode,type);
        }
    }
    /**
     * 统计时间段内的体征异常次数
     * @param start
     * @param end
     * @param status
     * @param patientCode
     * @return
     */
    public int getCountByTimeAndStatus(Date start,Date end,Integer status,String patientCode){
        if(isUploadIot()){
            return getCountByTimeAndStatusAndPatient(start,end,status,patientCode);
        }else {
            return patientHealthIndexDao.getCountByTimeAndStatus(start,end,status,patientCode);
        }
    }
    /**
     * 查找重复数据
     * @param user
     * @param deviceSn
     * @param value1
     * @param type
     * @param time
     * @return
     */
    public List<PatientHealthIndex> findByType(String user,String deviceSn,String value1,Integer type,Date time){
        if(isUploadIot()){
            return findRepeat(user,deviceSn,value1,type,time);
        }else {
            return  patientHealthIndexDao.findByType(user,deviceSn,value1,type,time);
        }
    }
    private List<PatientHealthIndex> findRepeat(String user,String deviceSn,String value1,Integer type,Date time){
        List<PatientHealthIndex> list = null;
        try {
            String url = wlyyService + "/iot/findRepeat";
            Map<String, Object> params = new HashedMap();
            params.put("user", user);
            params.put("deviceSn", deviceSn);
            params.put("value1", value1);
            params.put("type", user);
            params.put("time", DateUtil.dateToStrLong(time));
            String re = transfor(url,params);
            JSONObject json = JSONObject.parseObject(re);
            JSONArray jsonArray = json.getJSONObject("data").getJSONArray("obj");
            if(jsonArray!=null){
                list = new ArrayList<>(jsonArray.size());
                for (Object o:jsonArray) {
                    PatientHealthIndex index = new PatientHealthIndex();
                    list.add(index);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }
    private int getCountByTimeAndStatusAndPatient(Date start,Date end,Integer status,String patientCode){
        try {
            String url = wlyyService + "/iot/getCountByTimeAndStatusAndPatient";
            Map<String, Object> params = new HashedMap();
            params.put("start", DateUtil.dateToStrLong(start));
            params.put("end", DateUtil.dateToStrLong(end));
            params.put("status", status);
            params.put("patientCode", patientCode);
            String response = transfor(url,params);
            JSONObject json = JSONObject.parseObject(response);
            if(json.getInteger("status")==200){
                return json.getInteger("data");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return 0;
    }
    private List<PatientHealthIndex> findByPatientAndType(String patientCode, Integer type){
        return null;
    }
    /**
     * 更新状态
     * @param id
     */
    private void updateStatus(Long id,String status){
        try {
            String url = wlyyService + "/iot/updateStatus";
            Map<String, Object> params = new HashedMap();
            params.put("id", id);
            params.put("status", status);
            transfor(url,params);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    /**
     * 保存体征数据
     * @param obj
     */
    private PatientHealthIndex saveDevice(PatientHealthIndex obj){
        PatientHealthIndex index = null;
        try {
            String url = wlyyService + "/iot/upload";
            Map<String, Object> params = new HashedMap();
            params.put("jsonStr", JSONObject.toJSONString(obj));
            String re = transfor(url,params);
            JSONObject json = JSONObject.parseObject(re);
            index = objectMapper.readValue(json.getJSONObject("obj").toString(), PatientHealthIndex.class);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return index;
    }
    /**
     * 查询单条
     * @param id
     * @return
     */
    private PatientHealthIndex getById(Long id){
        return null;
    }
    /**
     * 结果处理
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    private String transfor(String url,Map<String, Object> params) throws Exception{
        String re = null;
        HttpResponse response = httpHelper.post(url, params);
        if (response != null && response.getStatusCode() == 200) {
            JSONObject jsonObject = JSONObject.parseObject(response.getBody());
            if (jsonObject.getInteger("status")!=null) {
                throw new Exception(jsonObject.getString("msg"));
            }
            re = response.getBody();
        } else {
            throw new Exception("接口调用错误!" + response.getBody());
        }
        return re;
    }
}