|  | @ -1,28 +1,37 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.care.service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.config.AqgConfig;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.DevicePatientHealthIndexDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.DeviceSosLogDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.PatientDeviceDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.patient.BasePatientDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.device.DevicePatientHealthIndex;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.device.DevicePatientDevice;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.device.DeviceSosLogDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.util.AesEncryptUtils;
 | 
	
		
			
				|  |  | import com.yihu.jw.patient.dao.BasePatientDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.common.GpsUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.common.LatitudeUtils;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.date.DateUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.http.HttpClientUtil;
 | 
	
		
			
				|  |  | import org.apache.commons.lang.StringUtils;
 | 
	
		
			
				|  |  | 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.data.redis.core.StringRedisTemplate;
 | 
	
		
			
				|  |  | import org.springframework.http.HttpEntity;
 | 
	
		
			
				|  |  | import org.springframework.http.HttpHeaders;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.scheduling.annotation.Async;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.util.LinkedMultiValueMap;
 | 
	
		
			
				|  |  | import org.springframework.util.MultiValueMap;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import javax.servlet.http.HttpServletRequest;
 | 
	
		
			
				|  |  | import java.util.Date;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
	
		
			
				|  | @ -36,6 +45,8 @@ import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  | public class DeviceService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static Logger logger = LoggerFactory.getLogger(DeviceService.class);
 | 
	
		
			
				|  |  |     @Value("${cloudCare.url}")
 | 
	
		
			
				|  |  |     private String cloudCareUrl;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HttpClientUtil httpClientUtil;
 | 
	
	
		
			
				|  | @ -45,6 +56,14 @@ public class DeviceService {
 | 
	
		
			
				|  |  |     private PatientDeviceDao patientDeviceDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DevicePatientHealthIndexDao healthIndexDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BasePatientDao patientDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DeviceSosLogDao sosLogDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private GpsUtil gpsUtil;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取爱牵挂管理员cookie
 | 
	
	
		
			
				|  | @ -96,12 +115,67 @@ public class DeviceService {
 | 
	
		
			
				|  |  |     @Async
 | 
	
		
			
				|  |  |     public void aqgsos(String imei,String label_mac,String time_begin, HttpServletRequest request) {
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |             String address= request.getParameter("address");
 | 
	
		
			
				|  |  |             Double lat= Double.parseDouble(request.getParameter("lat"));
 | 
	
		
			
				|  |  |             Double lon= Double.parseDouble(request.getParameter("lon"));
 | 
	
		
			
				|  |  |             List<DevicePatientDevice> devicePatientDeviceDos = patientDeviceDao.findByDeviceSn(imei);
 | 
	
		
			
				|  |  |             DevicePatientDevice deviceDO = devicePatientDeviceDos.get(0);
 | 
	
		
			
				|  |  |             BasePatientDO patientDO = patientDao.findById(deviceDO.getUser());
 | 
	
		
			
				|  |  |             DeviceSosLogDO logDO = new DeviceSosLogDO();
 | 
	
		
			
				|  |  |             logDO.setPatient(patientDO.getIdcard());
 | 
	
		
			
				|  |  |             logDO.setPatientName(patientDO.getName());
 | 
	
		
			
				|  |  |             logDO.setIdcard(patientDO.getIdcard());
 | 
	
		
			
				|  |  |             logDO.setCategoryCode(deviceDO.getCategoryCode());
 | 
	
		
			
				|  |  |             logDO.setDeviceSn(deviceDO.getDeviceSn());
 | 
	
		
			
				|  |  |             logDO.setCreateTime(new Date());
 | 
	
		
			
				|  |  |             if (deviceDO.getCategoryCode().equals("4")) {
 | 
	
		
			
				|  |  |                 JSONObject position = gpsUtil.gcj02_To_Bd09(lat,lon);
 | 
	
		
			
				|  |  |                 logDO.setSosAddress(address);
 | 
	
		
			
				|  |  |                 logDO.setSosLat(position.getDouble("lat")+"");
 | 
	
		
			
				|  |  |                 logDO.setSosLon(position.getDouble("lon")+"");
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if (deviceDO.getCategoryCode().equals("7")) {
 | 
	
		
			
				|  |  |                 logDO.setSosAddress(deviceDO.getSosAddress());
 | 
	
		
			
				|  |  |                 Map<String, String> json = null;
 | 
	
		
			
				|  |  |                 if (StringUtils.isNotBlank(deviceDO.getSosAddress())) {
 | 
	
		
			
				|  |  |                     json = LatitudeUtils.getGeocoderLatitude(deviceDO.getSosAddress().replace("G.", "").replace("(糖友网)", "").replace("(高友网)", ""));
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 if (json != null) {
 | 
	
		
			
				|  |  |                     logDO.setSosLat(json.get("lat").toString());
 | 
	
		
			
				|  |  |                     logDO.setSosLon(json.get("lat").toString());
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             //发送紧急救助
 | 
	
		
			
				|  |  |             String sql =" select Distinct item.org_code,item.org_name\n" +
 | 
	
		
			
				|  |  |                     "from base_service_package_sign_record sr INNER JOIN base_service_package_record pr\n" +
 | 
	
		
			
				|  |  |                     "on sr.id = pr.sign_id and sr.status=1 INNER JOIN base_service_package_item item on pr.service_package_id = item.service_package_id and item.del=1 \n" +
 | 
	
		
			
				|  |  |                     "where item.code='emergencyAssistance' and sr.patient='"+patientDO.getId()+"'";
 | 
	
		
			
				|  |  |             List<Map<String,Object>> sqlResult = jdbcTemplate.queryForList(sql);
 | 
	
		
			
				|  |  |             if (sqlResult.size()>0){
 | 
	
		
			
				|  |  |                 JSONObject jsonObject = new JSONObject();
 | 
	
		
			
				|  |  |                 jsonObject.put("serveAddress",logDO.getSosAddress());
 | 
	
		
			
				|  |  |                 jsonObject.put("serveLon",logDO.getSosLon());
 | 
	
		
			
				|  |  |                 jsonObject.put("serveLat",logDO.getSosLat());
 | 
	
		
			
				|  |  |                 jsonObject.put("orgCode",sqlResult.get(0).get("org_code").toString());
 | 
	
		
			
				|  |  |                 jsonObject.put("orgName",sqlResult.get(0).get("org_name").toString());
 | 
	
		
			
				|  |  |                 jsonObject.put("patient",logDO.getPatient());
 | 
	
		
			
				|  |  |                 jsonObject.put("patientName",logDO.getPatientName());
 | 
	
		
			
				|  |  |                 String url = cloudCareUrl+"cloudCare/noLogin/emergency_assistance/newOrder";
 | 
	
		
			
				|  |  |                 Map map = new HashMap<String, String>();
 | 
	
		
			
				|  |  |                 map.put("patientId", logDO.getPatient());
 | 
	
		
			
				|  |  |                 map.put("jsonData", jsonObject.toJSONString());
 | 
	
		
			
				|  |  |                 String content = com.alibaba.fastjson.JSONObject.toJSONString(map);
 | 
	
		
			
				|  |  |                 String postParams = AesEncryptUtils.agEncrypt(content);
 | 
	
		
			
				|  |  |                 String response = httpClientUtil.postBodyRawForm(url,postParams);
 | 
	
		
			
				|  |  |                 JSONObject result = com.alibaba.fastjson.JSONObject.parseObject(response);
 | 
	
		
			
				|  |  |                 JSONObject jsonObjectData =JSONObject.parseObject(AesEncryptUtils.agDecrypt(result.getString("data")));
 | 
	
		
			
				|  |  |                 logDO.setSosSendStatus(jsonObjectData.getInteger("status").toString());
 | 
	
		
			
				|  |  |                 logDO.setSosSendMessage(jsonObjectData.getString("message"));
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             sosLogDao.save(logDO);
 | 
	
		
			
				|  |  |         }catch(Exception e){
 | 
	
		
			
				|  |  |                 e.printStackTrace();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
	
		
			
				|  | @ -195,7 +269,9 @@ public class DeviceService {
 | 
	
		
			
				|  |  |                 List<DevicePatientDevice> devicePatientDeviceDos = patientDeviceDao.findByDeviceSn(imei);
 | 
	
		
			
				|  |  |                 if (devicePatientDeviceDos.size()>0){
 | 
	
		
			
				|  |  |                     DevicePatientDevice devicePatientDevice = devicePatientDeviceDos.get(0);
 | 
	
		
			
				|  |  |                     BasePatientDO patientDO = patientDao.findById(devicePatientDevice.getUser());
 | 
	
		
			
				|  |  |                     DevicePatientHealthIndex patientHealthIndex = new DevicePatientHealthIndex();
 | 
	
		
			
				|  |  |                     patientHealthIndex.setName(patientDO.getName());
 | 
	
		
			
				|  |  |                     patientHealthIndex.setDeviceSn(imei);
 | 
	
		
			
				|  |  |                     patientHealthIndex.setUser(devicePatientDevice.getUser());
 | 
	
		
			
				|  |  |                     patientHealthIndex.setIdcard(devicePatientDevice.getUserIdcard());
 | 
	
	
		
			
				|  | @ -208,6 +284,10 @@ public class DeviceService {
 | 
	
		
			
				|  |  |                     if (heartrate>=theshold_heartrate_h||heartrate<=theshold_heartrate_l){
 | 
	
		
			
				|  |  |                         patientHealthIndex.setStatus(1);
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     else {
 | 
	
		
			
				|  |  |                         patientHealthIndex.setStatus(0);
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     patientHealthIndex.setDel("1");
 | 
	
		
			
				|  |  |                     healthIndexDao.save(patientHealthIndex);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
	
		
			
				|  | @ -233,7 +313,9 @@ public class DeviceService {
 | 
	
		
			
				|  |  |                 List<DevicePatientDevice> devicePatientDeviceDos = patientDeviceDao.findByDeviceSn(imei);
 | 
	
		
			
				|  |  |                 if (devicePatientDeviceDos.size()>0){
 | 
	
		
			
				|  |  |                     DevicePatientDevice devicePatientDevice = devicePatientDeviceDos.get(0);
 | 
	
		
			
				|  |  |                     BasePatientDO patientDO = patientDao.findById(devicePatientDevice.getUser());
 | 
	
		
			
				|  |  |                     DevicePatientHealthIndex patientHealthIndex = new DevicePatientHealthIndex();
 | 
	
		
			
				|  |  |                     patientHealthIndex.setName(patientDO.getName());
 | 
	
		
			
				|  |  |                     patientHealthIndex.setDeviceSn(imei);
 | 
	
		
			
				|  |  |                     patientHealthIndex.setUser(devicePatientDevice.getUser());
 | 
	
		
			
				|  |  |                     patientHealthIndex.setIdcard(devicePatientDevice.getUserIdcard());
 | 
	
	
		
			
				|  | @ -247,6 +329,10 @@ public class DeviceService {
 | 
	
		
			
				|  |  |                     if (sbp>=sbp_h||dbp<=dbp_l){
 | 
	
		
			
				|  |  |                         patientHealthIndex.setStatus(1);
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     else {
 | 
	
		
			
				|  |  |                         patientHealthIndex.setStatus(0);
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     patientHealthIndex.setDel("1");
 | 
	
		
			
				|  |  |                     healthIndexDao.save(patientHealthIndex);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
	
		
			
				|  | @ -278,29 +364,6 @@ public class DeviceService {
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 柏颐sos 接收
 | 
	
		
			
				|  |  |      * @param imei  15位设备唯一序号
 | 
	
		
			
				|  |  |      * @param time_begin  发生时间YYYY-MM-DD HH:mm:SS
 | 
	
		
			
				|  |  |      * @param heartrate   心率
 | 
	
		
			
				|  |  |      * @param city  城市
 | 
	
		
			
				|  |  |      * @param address  地址
 | 
	
		
			
				|  |  |      * @param lon  经度
 | 
	
		
			
				|  |  |      * @param lat  纬度
 | 
	
		
			
				|  |  |      * @param type  类型   0:Gps定位; 1:基站定位
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @Async
 | 
	
		
			
				|  |  |     public void bySos(String imei,String time_begin,int heartrate,String city,String address,double lon,double lat,String type) {
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             if(StringUtils.isNotBlank(imei)){
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 柏颐步数数据接收
 | 
	
		
			
				|  |  |      * @param imei 15位设备唯一序号
 | 
	
	
		
			
				|  | @ -340,42 +403,4 @@ public class DeviceService {
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 柏颐开关机数据接收
 | 
	
		
			
				|  |  |      * @param imei 15位设备唯一序号
 | 
	
		
			
				|  |  |      * @param time_begin 发生时间YYYY-MM-DD HH:mm:SS
 | 
	
		
			
				|  |  |      * @param type 开/关机类型  0:开机; 1:关机; 2:电量上报; 3:低电通知
 | 
	
		
			
				|  |  |      * @param remaining_power 剩余电量(%)
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @Async
 | 
	
		
			
				|  |  |     public void bySwitch(String imei,String time_begin,String type,int remaining_power) {
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             if(StringUtils.isNotBlank(imei)){
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 柏颐数据消息通知
 | 
	
		
			
				|  |  |      * @param type  type=1 SOS,type=2 fall,type=3 new 新成员加入 ,type=4 电子围栏触发, type=5 设备低电,type=6 环境音
 | 
	
		
			
				|  |  |      * @param deviceid 15位设备唯一序号
 | 
	
		
			
				|  |  |      * @param communityid 机构ID
 | 
	
		
			
				|  |  |      * @param url 环境音下载地址 只有环境音(type=6)为必填,其他type都为非必填
 | 
	
		
			
				|  |  |      * @param name 设备名称
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @Async
 | 
	
		
			
				|  |  |     public void byPushData(int type,String deviceid,String communityid,String url,String name) {
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             if(StringUtils.isNotBlank(deviceid)){
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |