|  | @ -0,0 +1,376 @@
 | 
	
		
			
				|  |  | 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.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.DevicePatientDevice;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.device.DevicePatientHealthIndex;
 | 
	
		
			
				|  |  | import com.yihu.jw.hospital.module.health.controller.Result;
 | 
	
		
			
				|  |  | import com.yihu.jw.hospital.module.iot.service.IotDeviceService;
 | 
	
		
			
				|  |  | import com.yihu.jw.patient.dao.BasePatientDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.date.DateUtil;
 | 
	
		
			
				|  |  | 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.BeanPropertyRowMapper;
 | 
	
		
			
				|  |  | 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 BasePatientDao patientDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DeviceDetailDao deviceDetailDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PatientDeviceDao patientDeviceDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DevicePatientHealthIndexDao patientHealthIndexDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private IotDeviceService iotDeviceService;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public void deviceRegistration(String deviceSn, String deviceModel, String deviceName, String categoryCode, String categoryName) {
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             iotDeviceService.deviceRegistration(deviceSn,deviceModel,deviceName,categoryCode,categoryName);
 | 
	
		
			
				|  |  |         }catch (Exception e){
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     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);
 | 
	
		
			
				|  |  |             iotDeviceService.uploadDeviceData(result.getId());
 | 
	
		
			
				|  |  |         } 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);
 | 
	
		
			
				|  |  |             if(patient!=null){
 | 
	
		
			
				|  |  |                 obj.setName(patient.getName());
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if(deviceDetail!=null){
 | 
	
		
			
				|  |  |                 obj.setHospital(deviceDetail.getGrantOrgCode());
 | 
	
		
			
				|  |  |                 obj.setHospitalName(deviceDetail.getOrgName());
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             obj = patientHealthIndexDao.save(obj);
 | 
	
		
			
				|  |  |             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<>();
 | 
	
		
			
				|  |  |         String sql = "";
 | 
	
		
			
				|  |  |         String startTime = DateUtil.dateToStrLong(timeMin);
 | 
	
		
			
				|  |  |         String endTime = DateUtil.dateToStrLong(timeMax);
 | 
	
		
			
				|  |  |         if (type==1){
 | 
	
		
			
				|  |  |             sql = "select a.* from wlyy_patient_health_index a where a.user = '"+user+"' and a.device_sn = '"+deviceSn+"' " +
 | 
	
		
			
				|  |  |                    "and a.value1 = '"+value1+"' and a.type = "+type+" and a.record_date >= '"+startTime+"' and a.record_date<='"+endTime+"' and a.del = '1' ";
 | 
	
		
			
				|  |  |         }else if (type==2){
 | 
	
		
			
				|  |  |            sql = "select a.* from wlyy_patient_health_index a where a.user = '"+user+"' and a.device_sn = '"+deviceSn+"' " +
 | 
	
		
			
				|  |  |                    "and a.value1 = '"+value1+"' and a.value2='"+value2+"'  and a.value3='"+value3+"' and a.type = "+type+" " +
 | 
	
		
			
				|  |  |                    "and a.record_date >= '"+startTime+"' and a.record_date<='"+endTime+"' and a.del = '1' ";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(DevicePatientHealthIndex.class));
 | 
	
		
			
				|  |  |         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;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |