| 
					
				 | 
			
			
				@ -1,10 +1,13 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				package com.yihu.jw.care.service.device; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.alibaba.fastjson.JSON; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.alibaba.fastjson.serializer.SerializerFeature; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.care.config.AqgConfig; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.care.dao.device.DeviceDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.care.dao.device.DeviceDetailDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.care.dao.device.PatientDeviceDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.care.service.contacts.ContactsService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.care.service.wechat.WeiXinAccessTokenUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.care.util.ConcealUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.care.util.MyJdbcTemplate; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -15,9 +18,12 @@ import com.yihu.jw.entity.care.device.Device; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.entity.care.device.DeviceDetail; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.entity.care.device.DevicePatientDevice; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.patient.dao.BasePatientDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.restmodel.ResponseContant; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.restmodel.web.PageEnvelop; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.util.common.GpsUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.util.common.IdCardUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.util.date.DateUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.util.healthIndex.HealthIndexUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.util.http.HttpClientUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.mysql.query.BaseJpaService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.apache.commons.collections.map.HashedMap; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -79,6 +85,8 @@ public class PatientDeviceService extends BaseJpaService<DevicePatientDevice, Pa 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private StringRedisTemplate redisTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private HttpClientUtil httpClientUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private YsDeviceService ysDeviceService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    WeiXinAccessTokenUtils tokenUtils; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -91,6 +99,10 @@ public class PatientDeviceService extends BaseJpaService<DevicePatientDevice, Pa 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private String esIndex; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private GpsUtil gpsUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private ContactsService contactsService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private HealthIndexUtil healthIndexUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @PostConstruct 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public void init() { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -761,13 +773,141 @@ public class PatientDeviceService extends BaseJpaService<DevicePatientDevice, Pa 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return resultMap; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 根据设备sn及居民code获取居民有效的设备绑定信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param deviceSn 设备sn码 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public JSONObject getPatientDeviceDetail(String patient,String deviceSn){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        JSONObject result = new JSONObject(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        BasePatientDO patientDO = patientDao.findById(patient); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (null==patientDO){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            result.put(ResponseContant.resultFlag,ResponseContant.fail); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            result.put(ResponseContant.resultMsg,"当前居民不存在"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<DevicePatientDevice> devices = patientDeviceDao.findByDeviceSn(deviceSn); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (devices.size()==0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            result.put(ResponseContant.resultFlag,ResponseContant.fail); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            result.put(ResponseContant.resultMsg,"未查询到该设备"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            DevicePatientDevice device = devices.get(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if (!patient.equals(device.getUser())){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                result.put(ResponseContant.resultFlag,ResponseContant.fail); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                result.put(ResponseContant.resultMsg,"居民未绑定该设备"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            }else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String sql = "select dd.photo,pd.device_sn,dd.brands,dd.category_code,dd.model,pd.device_name,date_format(pd.czrq,'%Y-%m-%d %H:%i:%S' ) deviceTime " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "from dm_device dd INNER JOIN wlyy_patient_device pd on dd.category_code = pd.category_code INNER JOIN wlyy_devices wd on dd.model = wd.device_model and pd.device_sn = wd.device_code \n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "where 1=1 and  pd.del=0 and pd.device_sn ='"+deviceSn+"' "; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                Map<String,Object> devInfo = jdbcTemplate.queryForMap(sql); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                devInfo.put("patient",patient); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                devInfo.put("patientName",patientDO.getName()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                devInfo.put("sosContactsDOS",new ArrayList<>()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                if ("4".equals(device.getCategoryCode())||"7".equals(device.getCategoryCode())){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    devInfo.put("sosContactsDOS",contactsService.getPatientSosContacts(patient)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                result.put(ResponseContant.resultFlag,ResponseContant.success); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                result.put(ResponseContant.resultMsg,JSON.parseObject(JSON.toJSONString(devInfo, SerializerFeature.WriteMapNullValue))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 根据设备sn及居民code获取居民有效的设备绑定数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param deviceSn 设备sn码 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public List<DevicePatientDevice> findByDeviceSn(String deviceSn){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       return  patientDeviceDao.findByDeviceSn(deviceSn); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public JSONObject getPatientDeviceData(String patient,String deviceSn,Integer page,Integer pageSize)throws Exception{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        page = page>0?page-1:0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        JSONObject result = new JSONObject(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<DevicePatientDevice> devices = patientDeviceDao.findByDeviceSn(deviceSn); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (devices.size()>0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            DevicePatientDevice device = devices.get(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if (!patient.equals(device.getUser())){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                throw new Exception("居民未绑定该设备"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            }else{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                switch (device.getCategoryCode()){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    case "1"://血压设备 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        result = getHealthIndex(result,deviceSn,patient,page,pageSize); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    case "2"://血糖设备 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        result = getHealthIndex(result,deviceSn,patient,page,pageSize); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    case "4"://手表 紧急工单与安防工单 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        result = getEmeWarn(result,deviceSn,patient,page,pageSize); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    case "7"://报警器 紧急工单 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        result = getEmeWarn(result,deviceSn,patient,page,pageSize); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    case "12"://监控 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        com.alibaba.fastjson.JSONObject tmp =ysDeviceService.getDeviceLiveAddress(patient,deviceSn,1,null); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        result.put("monitorInfoStatus",tmp.getIntValue(ResponseContant.resultFlag)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        if (tmp.getIntValue(ResponseContant.resultFlag) == ResponseContant.fail){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            result.put("monitorInfo",tmp.getString(ResponseContant.resultMsg)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            result.put("monitorInfo",tmp.getJSONObject(ResponseContant.resultMsg)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    default://安防设备 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        result = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            throw new Exception("设备未被绑定"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public JSONObject getHealthIndex(JSONObject result,String deviceSn,String patient,Integer page,Integer pageSize){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String sqlCount = "select count(id) from wlyy_patient_health_index where user ='"+patient+"' and device_sn='"+deviceSn+"' " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                " and del=1 "; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String sql = "select *,CAST(DATE_FORMAT(record_date,'%Y-%m-%d %H:%i:%S') as char) sign_status from wlyy_patient_health_index where user ='"+patient+"' and device_sn='"+deviceSn+"' " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                " and del=1 order by record_date desc limit "+page*pageSize+","+pageSize; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        long count = jdbcTemplate.queryForObject(sqlCount,long.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<Map<String,Object>> list = jdbcTemplate.queryForList(sql); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        for (Map<String,Object> tmp:list){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            Integer type =Integer.parseInt(tmp.get("type").toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String value1 = tmp.get("value1")==null?null:tmp.get("value1").toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String value2 = tmp.get("value2")==null?null:tmp.get("value2").toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String value3 = tmp.get("value3")==null?null:tmp.get("value3").toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String value4 = tmp.get("value4")==null?null:tmp.get("value4").toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String value5 = tmp.get("value5")==null?null:tmp.get("value5").toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String value6 = tmp.get("value6")==null?null:tmp.get("value6").toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String value7 = tmp.get("value7")==null?null:tmp.get("value7").toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            com.alibaba.fastjson.JSONArray errorInfo = healthIndexUtil.verifyHealthIndex(type,value1,value2,value3,value4,value5,value6,value7); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            tmp.put("errorInfo",errorInfo); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        result.put("total",count); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        result.put("page",page+1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        result.put("pageSize",pageSize); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        result.put("dataList",list); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public JSONObject getEmeWarn(JSONObject result,String deviceSn,String patient,Integer page,Integer pageSize){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String sqlCount = "select SUM(total) from( \n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                "select count(ord.id) as total from base_emergency_assistance_order ord  where ord.device_sn='"+deviceSn+"' " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                " and ord.patient='"+patient+"' " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                "UNION All " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                "select count(ord.id)  as total from base_security_monitoring_order ord  where ord.device_sn='"+deviceSn+"' " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                " and ord.patient='"+patient+"' )A "; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        long count = jdbcTemplate.queryForObject(sqlCount,long.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String sql = "select '20' as OrderType,ord.id,ord.patient,ord.patient_name,ord.doctor,ord.status,ord.doctor_name," + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                "'紧急呼叫' as serve_desc,DATE_FORMAT(ord.create_time,'%Y-%m-%d %H:%i:%S') create_time " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                "from base_emergency_assistance_order ord  where ord.device_sn='"+deviceSn+"' and ord.patient='"+patient+"' " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                "UNION " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                "select  '22' as 'OrderType',ord.id,ord.patient,ord.patient_name,ord.doctor,ord.status,ord.doctor_name," + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                "ord.serve_desc,DATE_FORMAT(ord.create_time,'%Y-%m-%d %H:%i:%S') create_time from base_security_monitoring_order ord " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                " where ord.device_sn='"+deviceSn+"' and ord.patient='"+patient+"'  order by create_time desc limit "+page*pageSize+","+pageSize; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<Map<String,Object>> list = jdbcTemplate.queryForList(sql); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        result.put("total",count); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        result.put("page",page+1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        result.put("pageSize",pageSize); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        result.put("dataList",list); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     /******************************************* 爱牵挂设备start *****************************************************/ 
			 |