|  | @ -3,15 +3,31 @@ package com.yihu.wlyy.service.third.iot;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.device.entity.DevicePatientHealthIndex;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.device.DeviceHealthyInfoMapping;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.device.PatientDevice;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.patient.Patient;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.deviece.DeviceHealthyInfoMappingDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.dict.SystemDictDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.patient.PatientDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.patient.PatientDeviceDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.util.DateUtil;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.util.HttpClientUtil;
 | 
	
		
			
				|  |  | import org.apache.commons.lang.StringUtils;
 | 
	
		
			
				|  |  | import org.apache.http.NameValuePair;
 | 
	
		
			
				|  |  | import org.apache.http.message.BasicNameValuePair;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.dao.DataAccessException;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.ResultSetExtractor;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.sql.ResultSet;
 | 
	
		
			
				|  |  | import java.sql.ResultSetMetaData;
 | 
	
		
			
				|  |  | import java.sql.SQLException;
 | 
	
		
			
				|  |  | import java.util.ArrayList;
 | 
	
		
			
				|  |  | import java.util.Date;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
	
		
			
				|  | @ -21,8 +37,10 @@ import java.util.List;
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | public class IotDeviceService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private Logger logger = LoggerFactory.getLogger(IotDeviceService.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | //    @Value("${}")
 | 
	
		
			
				|  |  |     private String baseUrl = "http://192.168.131.24:8080/svr-iot/";
 | 
	
		
			
				|  |  |     private String baseUrl = "http://192.168.131.24:8080/svr-iot-ysj/";
 | 
	
		
			
				|  |  |     private String grantType = "client_credentials";
 | 
	
		
			
				|  |  |     private String clientId = "Va5yQRHlA4Fq4eR3LT0vuXV4";
 | 
	
		
			
				|  |  |     private String clientSecret = "0rDSjzQ20XUj5itV7WRtznPQSzr5pVw2";
 | 
	
	
		
			
				|  | @ -37,20 +55,401 @@ public class IotDeviceService {
 | 
	
		
			
				|  |  |     private String waistUnit = "cm";
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HttpClientUtil httpClientUtil;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PatientDeviceDao patientDeviceDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PatientDao patientDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private SystemDictDao systemDictDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DeviceHealthyInfoMappingDao deviceHealthyInfoMappingDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 判断是否数据上传到物联网
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public Boolean isUploadIot(){
 | 
	
		
			
				|  |  |         String value = systemDictDao.findByDictNameAndCode("SYSTEM_PARAMS","DEVICE_UPLOAD_IOT");
 | 
	
		
			
				|  |  |         return "1".equals(value)?true:false;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public JSONObject test(){
 | 
	
		
			
				|  |  | //        String sql = "select response from wlyy_http_log_detail where id = 1";
 | 
	
		
			
				|  |  | //        List<String> list = jdbcTemplate.queryForList(sql,String.class);
 | 
	
		
			
				|  |  |         String sql = "SELECT i.id,i.user,i.value1,i.value2,i.value3,i.value4,i.type,i.record_date,i.device_sn,i.status,i.del,d.device_name,d.user_type,p.name,p.idcard " +
 | 
	
		
			
				|  |  |                 "from device.wlyy_patient_health_index i " +
 | 
	
		
			
				|  |  |                 "LEFT JOIN wlyy_patient_device d on i.`user` = d.`user` and i.device_sn = d.device_sn " +
 | 
	
		
			
				|  |  |                 "LEFT JOIN wlyy_patient p on i.`user` = p.code order by i.id limit 1";
 | 
	
		
			
				|  |  |         List<JSONObject> list = jdbcTemplate.query(sql, new ResultSetExtractor<List<JSONObject>>() {
 | 
	
		
			
				|  |  |             @Override
 | 
	
		
			
				|  |  |             public List<JSONObject> extractData(ResultSet resultSet) throws SQLException, DataAccessException {
 | 
	
		
			
				|  |  |                 ResultSetMetaData rsd = resultSet.getMetaData();
 | 
	
		
			
				|  |  |                 int clength = rsd.getColumnCount();
 | 
	
		
			
				|  |  |                 List<JSONObject> li = new ArrayList<JSONObject>();
 | 
	
		
			
				|  |  |                 String columnName;
 | 
	
		
			
				|  |  |                 try {
 | 
	
		
			
				|  |  |                     while (resultSet.next()) {
 | 
	
		
			
				|  |  |                         JSONObject jo = new JSONObject();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                         for (int i = 0; i < clength; i++) {
 | 
	
		
			
				|  |  |                             columnName = rsd.getColumnLabel(i + 1);
 | 
	
		
			
				|  |  |                             jo.put(columnName, resultSet.getObject(i + 1));
 | 
	
		
			
				|  |  |                         }
 | 
	
		
			
				|  |  |                         li.add(jo);
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                 } catch (Exception e) {
 | 
	
		
			
				|  |  |                     e.printStackTrace();
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 return li;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         });
 | 
	
		
			
				|  |  |         if(list.size()>0){
 | 
	
		
			
				|  |  |             JSONObject one = list.get(0);
 | 
	
		
			
				|  |  |             String response = initUpload(one);
 | 
	
		
			
				|  |  |             return JSONObject.parseObject(response);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String getAccessToken(){
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String url = baseUrl+"/oauth/2.0/token";
 | 
	
		
			
				|  |  |         List<NameValuePair> params = new ArrayList<>();
 | 
	
		
			
				|  |  |         params.add(new BasicNameValuePair("grant_type",grantType));
 | 
	
		
			
				|  |  |         params.add(new BasicNameValuePair("client_id",clientId));
 | 
	
		
			
				|  |  |         params.add(new BasicNameValuePair("client_secret",clientSecret));
 | 
	
		
			
				|  |  |         String response = httpClientUtil.post(url, params, "UTF-8");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 设备注册及绑定
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String registedevice(JSONObject json){
 | 
	
		
			
				|  |  |         json.put("access_token",accessToken);
 | 
	
		
			
				|  |  |         json.put("data_source",dataSource);
 | 
	
		
			
				|  |  |         String url = baseUrl+"/dataInput/userBind";
 | 
	
		
			
				|  |  |         String response = httpClientUtil.iotPostBody(url, json.toString());
 | 
	
		
			
				|  |  |         return response;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 数据上传
 | 
	
		
			
				|  |  |      * @param json
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String upload(JSONObject json){
 | 
	
		
			
				|  |  |         json.put("access_token",accessToken);
 | 
	
		
			
				|  |  |         json.put("data_source",dataSource);
 | 
	
		
			
				|  |  |         String url = baseUrl+"/dataInput/input";
 | 
	
		
			
				|  |  |         String response = httpClientUtil.iotPostBody(url, json.toString());
 | 
	
		
			
				|  |  |         return response;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      *  查询重复数据
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String findRepeat(String user,String deviceSn,String value1,String value2,String type,String time){
 | 
	
		
			
				|  |  |         JSONArray jsonArray = new JSONArray();
 | 
	
		
			
				|  |  |         JSONObject jsonUser = new JSONObject();
 | 
	
		
			
				|  |  |         jsonUser.put("andOr","and");
 | 
	
		
			
				|  |  |         jsonUser.put("filed","usercode");
 | 
	
		
			
				|  |  |         jsonUser.put("condition","=");
 | 
	
		
			
				|  |  |         jsonUser.put("value",user);
 | 
	
		
			
				|  |  |         jsonArray.add(jsonUser);
 | 
	
		
			
				|  |  |         JSONObject jsonSn = new JSONObject();
 | 
	
		
			
				|  |  |         jsonSn.put("andOr","and");
 | 
	
		
			
				|  |  |         jsonSn.put("filed","sn");
 | 
	
		
			
				|  |  |         jsonSn.put("condition","=");
 | 
	
		
			
				|  |  |         jsonSn.put("value",deviceSn);
 | 
	
		
			
				|  |  |         jsonArray.add(jsonSn);
 | 
	
		
			
				|  |  |         if("1".equals(type)){
 | 
	
		
			
				|  |  |             //血糖
 | 
	
		
			
				|  |  |             JSONObject jsonValue1 = new JSONObject();
 | 
	
		
			
				|  |  |             jsonValue1.put("andOr","and");
 | 
	
		
			
				|  |  |             jsonValue1.put("filed","blood_sugar");
 | 
	
		
			
				|  |  |             jsonValue1.put("condition","=");
 | 
	
		
			
				|  |  |             jsonValue1.put("value",value1);
 | 
	
		
			
				|  |  |             jsonArray.add(jsonValue1);
 | 
	
		
			
				|  |  |         }else {
 | 
	
		
			
				|  |  |             //血压
 | 
	
		
			
				|  |  |             JSONObject jsonValue1 = new JSONObject();
 | 
	
		
			
				|  |  |             jsonValue1.put("andOr","and");
 | 
	
		
			
				|  |  |             jsonValue1.put("filed","systolic");
 | 
	
		
			
				|  |  |             jsonValue1.put("condition","=");
 | 
	
		
			
				|  |  |             jsonValue1.put("value",value1);
 | 
	
		
			
				|  |  |             JSONObject jsonValue2 = new JSONObject();
 | 
	
		
			
				|  |  |             jsonValue2.put("andOr","and");
 | 
	
		
			
				|  |  |             jsonValue2.put("filed","diastolic");
 | 
	
		
			
				|  |  |             jsonValue2.put("condition","=");
 | 
	
		
			
				|  |  |             jsonValue2.put("value",value2);
 | 
	
		
			
				|  |  |             jsonArray.add(jsonValue1);
 | 
	
		
			
				|  |  |             jsonArray.add(jsonValue2);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         JSONObject jsonTime = new JSONObject();
 | 
	
		
			
				|  |  |         jsonTime.put("andOr","and");
 | 
	
		
			
				|  |  |         jsonTime.put("filed","measure_time");
 | 
	
		
			
				|  |  |         jsonTime.put("condition","=");
 | 
	
		
			
				|  |  |         jsonTime.put("value",time);
 | 
	
		
			
				|  |  |         String url = baseUrl+"/findById";
 | 
	
		
			
				|  |  |         String response = httpClientUtil.iotPostBody(url, jsonArray.toString());
 | 
	
		
			
				|  |  |         return response;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 查询单条
 | 
	
		
			
				|  |  |      * @param rid
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String getById(String rid){
 | 
	
		
			
				|  |  |         JSONObject json = new JSONObject();
 | 
	
		
			
				|  |  |         json.put("andOr","and");
 | 
	
		
			
				|  |  |         json.put("filed","rid");
 | 
	
		
			
				|  |  |         json.put("condition","=");
 | 
	
		
			
				|  |  |         json.put("value",rid);
 | 
	
		
			
				|  |  |         String url = baseUrl+"/getById";
 | 
	
		
			
				|  |  |         String response = httpClientUtil.iotPostBody(url, json.toString());
 | 
	
		
			
				|  |  |         return response;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 查询
 | 
	
		
			
				|  |  |      * filter
 | 
	
		
			
				|  |  |      * - 参数格式:[{"andOr":"and|or","condition":">|=|<|>=|<=|?","field":"<filed>","value":"<value>"},<{...}>]
 | 
	
		
			
				|  |  |      * - 参数说明:andOr跟数据库的中的AND和OR相似;condition指条件匹配程度,?相当于数据库中的like;filed指检索的字段;value为检索的值
 | 
	
		
			
				|  |  |      * page - 参数说明:页码
 | 
	
		
			
				|  |  |      * size - 参数说明:分页大小
 | 
	
		
			
				|  |  |      * @param json
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String searchList(JSONObject json){
 | 
	
		
			
				|  |  |         String url = baseUrl+"/searchList";
 | 
	
		
			
				|  |  |         String response = httpClientUtil.iotPostBody(url, json.toString());
 | 
	
		
			
				|  |  |         return response;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param json
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String listPage(JSONObject json){
 | 
	
		
			
				|  |  |         String url = baseUrl+"/listPage";
 | 
	
		
			
				|  |  |         String response = httpClientUtil.iotPostBody(url, json.toString());
 | 
	
		
			
				|  |  |         return response;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取最近5条数据
 | 
	
		
			
				|  |  |      * @param json
 | 
	
		
			
				|  |  |      * @param sort
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String recent5(JSONObject json,JSONObject sort){
 | 
	
		
			
				|  |  |         json.put("access_token",accessToken);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String url = baseUrl+"/recent5";
 | 
	
		
			
				|  |  |         String response = httpClientUtil.iotPostBody(url, json.toString());
 | 
	
		
			
				|  |  |         return response;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取居民一周内体征数据异常次数
 | 
	
		
			
				|  |  |      * @param json
 | 
	
		
			
				|  |  |      * @param sort
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String recent1(JSONObject json,JSONObject sort){
 | 
	
		
			
				|  |  |         json.put("access_token",accessToken);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String url = baseUrl+"/recent1";
 | 
	
		
			
				|  |  |         String response = httpClientUtil.iotPostBody(url, json.toString());
 | 
	
		
			
				|  |  |         return response;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 初始化迁移数据
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String initData(){
 | 
	
		
			
				|  |  |         Integer page = 1;
 | 
	
		
			
				|  |  |         Integer size = 1000;
 | 
	
		
			
				|  |  |         Integer start = (page-1)*size;
 | 
	
		
			
				|  |  |         String sql = "SELECT i.id,i.user,i.value1,i.value2,i.value3,i.value4,i.type,i.record_date,i.device_sn,i.status,i.del,d.device_name,d.user_type,p.name,p.idcard " +
 | 
	
		
			
				|  |  |                 "from device.wlyy_patient_health_index i " +
 | 
	
		
			
				|  |  |                 "LEFT JOIN wlyy_patient_device d on i.`user` = d.`user` and i.device_sn = d.device_sn " +
 | 
	
		
			
				|  |  |                 "LEFT JOIN wlyy_patient p on i.`user` = p.code order by i.id limit ?,1000";
 | 
	
		
			
				|  |  |         List<JSONObject> list = jdbcTemplate.query(sql,new Object[]{start}, new ResultSetExtractor<List<JSONObject>>() {
 | 
	
		
			
				|  |  |             @Override
 | 
	
		
			
				|  |  |             public List<JSONObject> extractData(ResultSet resultSet) throws SQLException, DataAccessException {
 | 
	
		
			
				|  |  |                 ResultSetMetaData rsd = resultSet.getMetaData();
 | 
	
		
			
				|  |  |                 int clength = rsd.getColumnCount();
 | 
	
		
			
				|  |  |                 List<JSONObject> li = new ArrayList<JSONObject>();
 | 
	
		
			
				|  |  |                 String columnName;
 | 
	
		
			
				|  |  |                 try {
 | 
	
		
			
				|  |  |                     while (resultSet.next()) {
 | 
	
		
			
				|  |  |                         JSONObject jo = new JSONObject();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                         for (int i = 0; i < clength; i++) {
 | 
	
		
			
				|  |  |                             columnName = rsd.getColumnLabel(i + 1);
 | 
	
		
			
				|  |  |                             jo.put(columnName, resultSet.getObject(i + 1));
 | 
	
		
			
				|  |  |                         }
 | 
	
		
			
				|  |  |                         li.add(jo);
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                 } catch (Exception e) {
 | 
	
		
			
				|  |  |                     e.printStackTrace();
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 return li;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         });
 | 
	
		
			
				|  |  |         while (list.size()>0){
 | 
	
		
			
				|  |  |             List<DeviceHealthyInfoMapping> infos = new ArrayList<>();
 | 
	
		
			
				|  |  |             list.stream().forEach(one->{
 | 
	
		
			
				|  |  |                 String response = initUpload(one);
 | 
	
		
			
				|  |  |                 JSONObject re = JSONObject.parseObject(response);
 | 
	
		
			
				|  |  |                 String errorMsg = re.getString("errorMsg");//错误信息(请求失败才有错误消息)
 | 
	
		
			
				|  |  |                 if(StringUtils.isNotBlank(errorMsg)){
 | 
	
		
			
				|  |  |                     logger.error("id:"+one.getInteger("id")+" "+errorMsg);
 | 
	
		
			
				|  |  |                 }else {
 | 
	
		
			
				|  |  |                     //保存映射关系
 | 
	
		
			
				|  |  |                     DeviceHealthyInfoMapping info = new DeviceHealthyInfoMapping();
 | 
	
		
			
				|  |  |                     info.setCreateTime(new Date());
 | 
	
		
			
				|  |  |                     info.setIndexId(one.getLong("id"));
 | 
	
		
			
				|  |  |                     infos.add(info);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             });
 | 
	
		
			
				|  |  |             deviceHealthyInfoMappingDao.save(infos);
 | 
	
		
			
				|  |  |             logger.info("上传成功:"+(page*size));
 | 
	
		
			
				|  |  |             //下一次
 | 
	
		
			
				|  |  |             page++;
 | 
	
		
			
				|  |  |             start = (page-1)*size;
 | 
	
		
			
				|  |  |             list = jdbcTemplate.queryForList(sql,new Object[]{start},JSONObject.class);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 初始化上传
 | 
	
		
			
				|  |  |      * @param jsonObject
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String initUpload(JSONObject jsonObject){
 | 
	
		
			
				|  |  |         JSONObject json = new JSONObject();
 | 
	
		
			
				|  |  |         Integer type = jsonObject.getInteger("type");
 | 
	
		
			
				|  |  |         json.put("sn",StringUtils.trimToEmpty(jsonObject.getString("device_sn")));
 | 
	
		
			
				|  |  |         json.put("ext_code",StringUtils.trimToEmpty(jsonObject.getString("user_type")));
 | 
	
		
			
				|  |  |         json.put("device_name",transfor(jsonObject.getString("device_name"),1,type));
 | 
	
		
			
				|  |  |         json.put("device_model",transfor(jsonObject.getString("device_name"),2,type));
 | 
	
		
			
				|  |  |         json.put("idcard",StringUtils.trimToEmpty(jsonObject.getString("idcard")));
 | 
	
		
			
				|  |  |         json.put("username",StringUtils.trimToEmpty(jsonObject.getString("name")));
 | 
	
		
			
				|  |  |         json.put("usercode",StringUtils.trimToEmpty(jsonObject.getString("user")));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONArray jsonArray = new JSONArray();
 | 
	
		
			
				|  |  |         JSONObject js = new JSONObject();
 | 
	
		
			
				|  |  | //        js.put("id",String.valueOf(jsonObject.getInteger("id")));
 | 
	
		
			
				|  |  |         js.put("measure_time",subStringTime(jsonObject.getString("record_date")));
 | 
	
		
			
				|  |  |         js.put("del",StringUtils.trimToEmpty(jsonObject.getString("del")));
 | 
	
		
			
				|  |  |         js.put("status",StringUtils.trimToEmpty(jsonObject.getString("status")));
 | 
	
		
			
				|  |  |         switch (type){
 | 
	
		
			
				|  |  |             case 1:
 | 
	
		
			
				|  |  |                 //血糖
 | 
	
		
			
				|  |  |                 js.put("blood_sugar",jsonObject.getString("value1"));
 | 
	
		
			
				|  |  |                 js.put("blood_sugar_unit",bloodSugarUnit);
 | 
	
		
			
				|  |  |                 js.put("blood_sugar_result",jsonObject.getString("value2"));
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             case 2:
 | 
	
		
			
				|  |  |                 //血压
 | 
	
		
			
				|  |  |                 js.put("systolic",jsonObject.getString("value1"));
 | 
	
		
			
				|  |  |                 js.put("systolic_unit",bloodPressureUnit);
 | 
	
		
			
				|  |  |                 js.put("diastolic",jsonObject.getString("value2"));
 | 
	
		
			
				|  |  |                 js.put("diastolic_unit",bloodPressureUnit);
 | 
	
		
			
				|  |  |                 if(StringUtils.isNotBlank(jsonObject.getString("value3"))){
 | 
	
		
			
				|  |  |                     js.put("pulse",jsonObject.getString("value3"));
 | 
	
		
			
				|  |  |                     js.put("pulse_unit",pulseUnit);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             case 3:
 | 
	
		
			
				|  |  |                 //体重/身高/BMI
 | 
	
		
			
				|  |  |                 js.put("height",jsonObject.getString("value1"));
 | 
	
		
			
				|  |  |                 js.put("height_unit",heightUnit);
 | 
	
		
			
				|  |  |                 js.put("weight",jsonObject.getString("value2"));
 | 
	
		
			
				|  |  |                 js.put("weight_unit",weightUnit);
 | 
	
		
			
				|  |  |                 if(StringUtils.isNotBlank(jsonObject.getString("value3"))){
 | 
	
		
			
				|  |  |                     js.put("bmi",jsonObject.getString("value3"));
 | 
	
		
			
				|  |  |                     js.put("bmi_unit",bmiUnit);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             case 4:
 | 
	
		
			
				|  |  |                 //腰围
 | 
	
		
			
				|  |  |                 js.put("waist",jsonObject.getString("value1"));
 | 
	
		
			
				|  |  |                 js.put("waist_unit",waistUnit);
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |             default:
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         jsonArray.add(js);
 | 
	
		
			
				|  |  |         json.put("data",jsonArray);
 | 
	
		
			
				|  |  |         //上传
 | 
	
		
			
				|  |  |         return upload(json);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /************************************i健康业务相关 start************************************************************/
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 更新状态
 | 
	
		
			
				|  |  |      * @param id
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void updateStatus(Long id){
 | 
	
		
			
				|  |  |         DeviceHealthyInfoMapping mapping = deviceHealthyInfoMappingDao.findByIndexId(id,id);
 | 
	
		
			
				|  |  |         if(mapping == null){
 | 
	
		
			
				|  |  |             logger.info("未找到映射关系,id:"+id);
 | 
	
		
			
				|  |  |         }else {
 | 
	
		
			
				|  |  |             JSONObject json = new JSONObject();
 | 
	
		
			
				|  |  |             String url = baseUrl+"/dataInput/updateRecord";
 | 
	
		
			
				|  |  |             String response = httpClientUtil.iotPostBody(url, json.toString());
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 将上传数据转换成标准的json传
 | 
	
		
			
				|  |  |      * 查询单条
 | 
	
		
			
				|  |  |      * @param id
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public DevicePatientHealthIndex getById(Long id){
 | 
	
		
			
				|  |  |         DeviceHealthyInfoMapping mapping = deviceHealthyInfoMappingDao.findByIndexId(id,id);
 | 
	
		
			
				|  |  |         if(mapping == null){
 | 
	
		
			
				|  |  |             logger.info("未找到映射关系,id:"+id);
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         }else {
 | 
	
		
			
				|  |  |             return transforOne(getById(mapping.getRid()),id);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 体征上传
 | 
	
		
			
				|  |  |      * @param obj
 | 
	
		
			
				|  |  |      * @param userType
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public DevicePatientHealthIndex upload(DevicePatientHealthIndex obj,String userType,String deviceName,String deviceModel,Patient patient){
 | 
	
		
			
				|  |  |     public DevicePatientHealthIndex save(DevicePatientHealthIndex obj){
 | 
	
		
			
				|  |  |         JSONObject json = new JSONObject();
 | 
	
		
			
				|  |  |         json.put("sn",obj.getDeviceSn());
 | 
	
		
			
				|  |  |         json.put("ext_code",userType);
 | 
	
		
			
				|  |  |         json.put("device_name",deviceName);
 | 
	
		
			
				|  |  |         json.put("device_model",deviceModel);
 | 
	
		
			
				|  |  |         if(StringUtils.isNotBlank(obj.getDeviceSn())){
 | 
	
		
			
				|  |  |             json.put("sn",obj.getDeviceSn());
 | 
	
		
			
				|  |  |             List<PatientDevice> devices = patientDeviceDao.findByPatientAndDeviceSn(obj.getUser(),obj.getDeviceSn());
 | 
	
		
			
				|  |  |             if(devices!=null&&devices.size()>0){
 | 
	
		
			
				|  |  |                 PatientDevice device = devices.get(0);
 | 
	
		
			
				|  |  |                 json.put("ext_code",device.getUserType());
 | 
	
		
			
				|  |  |                 json.put("device_name",transfor(device.getDeviceName(),1,obj.getType()));
 | 
	
		
			
				|  |  |                 json.put("device_model",transfor(device.getDeviceName(),2,obj.getType()));
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         Patient patient = patientDao.findByCode(obj.getUser());
 | 
	
		
			
				|  |  |         json.put("idcard",patient.getIdcard());
 | 
	
		
			
				|  |  |         json.put("username",patient.getName());
 | 
	
		
			
				|  |  |         json.put("usercode",patient.getCode());
 | 
	
	
		
			
				|  | @ -101,67 +500,111 @@ public class IotDeviceService {
 | 
	
		
			
				|  |  |         String response = upload(json);
 | 
	
		
			
				|  |  |         JSONObject re = JSONObject.parseObject(response);
 | 
	
		
			
				|  |  |         String errorMsg = re.getString("errorMsg");//错误信息(请求失败才有错误消息)
 | 
	
		
			
				|  |  |         String successMsg = re.getString("successMsg");//成功信息(请求成功才有成功消息)
 | 
	
		
			
				|  |  |         if(StringUtils.isBlank(errorMsg)){
 | 
	
		
			
				|  |  |             re.getJSONObject("obj").getString("id");
 | 
	
		
			
				|  |  |             String rid = re.getJSONObject("obj").getJSONArray("rid").getString(0);
 | 
	
		
			
				|  |  |             DeviceHealthyInfoMapping mapping = new DeviceHealthyInfoMapping();
 | 
	
		
			
				|  |  |             mapping.setCreateTime(new Date());
 | 
	
		
			
				|  |  |             mapping.setRid(rid);
 | 
	
		
			
				|  |  |             deviceHealthyInfoMappingDao.save(mapping);
 | 
	
		
			
				|  |  |             obj.setId(mapping.getId());
 | 
	
		
			
				|  |  |         }else {
 | 
	
		
			
				|  |  |             logger.error(errorMsg);
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return obj;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String getAccessToken(){
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String url = baseUrl+"/oauth/2.0/token";
 | 
	
		
			
				|  |  |         List<NameValuePair> params = new ArrayList<>();
 | 
	
		
			
				|  |  |         params.add(new BasicNameValuePair("grant_type",grantType));
 | 
	
		
			
				|  |  |         params.add(new BasicNameValuePair("client_id",clientId));
 | 
	
		
			
				|  |  |         params.add(new BasicNameValuePair("client_secret",clientSecret));
 | 
	
		
			
				|  |  |         String response = httpClientUtil.post(url, params, "UTF-8");
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 更新
 | 
	
		
			
				|  |  |      * @param obj
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public DevicePatientHealthIndex update(DevicePatientHealthIndex obj){
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |         return obj;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /************************************i健康业务相关 end  ************************************************************/
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /*****************************************工具方法 start************************************************************/
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 设备注册及绑定
 | 
	
		
			
				|  |  |      * 单条转换
 | 
	
		
			
				|  |  |      * @param res
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String registedevice(JSONObject json){
 | 
	
		
			
				|  |  |         json.put("access_token",accessToken);
 | 
	
		
			
				|  |  |         json.put("data_source",dataSource);
 | 
	
		
			
				|  |  |         String url = baseUrl+"/dataInput/userBind";
 | 
	
		
			
				|  |  |         List<NameValuePair> params = new ArrayList<>();
 | 
	
		
			
				|  |  |         params.add(new BasicNameValuePair("json",json.toString()));
 | 
	
		
			
				|  |  |         String response = httpClientUtil.post(url, params, "UTF-8");
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     public DevicePatientHealthIndex transforOne(String res,Long id){
 | 
	
		
			
				|  |  |         if(StringUtils.isBlank(res)){
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         JSONObject jsonObject = JSONObject.parseObject(res);
 | 
	
		
			
				|  |  |         if(jsonObject.getString("errorMsg")!=null){
 | 
	
		
			
				|  |  |             logger.error(jsonObject.getString("errorMsg"));
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         JSONObject obj = jsonObject.getJSONArray("obj").getJSONObject(0);
 | 
	
		
			
				|  |  |         DevicePatientHealthIndex index = new DevicePatientHealthIndex();
 | 
	
		
			
				|  |  |         index.setId(id);
 | 
	
		
			
				|  |  |         index.setDeviceSn(obj.getString("sn"));
 | 
	
		
			
				|  |  |         index.setUser(obj.getString("usercode"));
 | 
	
		
			
				|  |  |         index.setIdcard(obj.getString("idcard"));
 | 
	
		
			
				|  |  |         index.setRecordDate(DateUtil.strToDate(obj.getString("measure_time")));
 | 
	
		
			
				|  |  |         index.setSortDate(index.getRecordDate());
 | 
	
		
			
				|  |  |         if(obj.getString("blood_sugar")==null){
 | 
	
		
			
				|  |  |             index.setType(1);
 | 
	
		
			
				|  |  |             index.setValue1(obj.getString("blood_sugar"));
 | 
	
		
			
				|  |  |             index.setValue2(obj.getString("blood_sugar_result"));
 | 
	
		
			
				|  |  |         }else if(obj.getString("systolic")==null){
 | 
	
		
			
				|  |  |             index.setType(2);
 | 
	
		
			
				|  |  |             index.setValue1(obj.getString("systolic"));
 | 
	
		
			
				|  |  |             index.setValue2(obj.getString("diastolic"));
 | 
	
		
			
				|  |  |             index.setValue3(obj.getString("pulse"));
 | 
	
		
			
				|  |  |         }else if(obj.getString("height")==null){
 | 
	
		
			
				|  |  |             index.setType(3);
 | 
	
		
			
				|  |  |             index.setValue1(obj.getString("height"));
 | 
	
		
			
				|  |  |             index.setValue2(obj.getString("weight"));
 | 
	
		
			
				|  |  |             index.setValue3(obj.getString("bmi"));
 | 
	
		
			
				|  |  |         }else if(obj.getString("waist")==null){
 | 
	
		
			
				|  |  |             index.setType(4);
 | 
	
		
			
				|  |  |             index.setValue1(obj.getString("waist"));
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return index;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 数据上传
 | 
	
		
			
				|  |  |      * @param json
 | 
	
		
			
				|  |  |      * 字符串分割
 | 
	
		
			
				|  |  |      * @param name
 | 
	
		
			
				|  |  |      * @param type 1品牌,2 型号
 | 
	
		
			
				|  |  |      * @param deviceType 1血糖,2 血压
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String upload(JSONObject json){
 | 
	
		
			
				|  |  |         json.put("access_token",accessToken);
 | 
	
		
			
				|  |  |         json.put("data_source",dataSource);
 | 
	
		
			
				|  |  |         String url = baseUrl+"/dataInput/input";
 | 
	
		
			
				|  |  |         org.json.JSONObject params = new org.json.JSONObject();
 | 
	
		
			
				|  |  |         params.put("json_data",json.toString());
 | 
	
		
			
				|  |  |         String response = httpClientUtil.postBody(url, params);
 | 
	
		
			
				|  |  |         return response;
 | 
	
		
			
				|  |  |     private String transfor(String name,Integer type,Integer deviceType){
 | 
	
		
			
				|  |  |         String re = "";
 | 
	
		
			
				|  |  |         String[] brand = {"康为","爱奥乐","优瑞恩","三诺","云湃"};
 | 
	
		
			
				|  |  |         String[] model = {"A206G","G-777G","U80EH","亲智","RBP-980"};
 | 
	
		
			
				|  |  |         String model2 = "G-426-3";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if(StringUtils.isBlank(name)){
 | 
	
		
			
				|  |  |             return re;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         for(int i=0;i<brand.length;i++){
 | 
	
		
			
				|  |  |             String one = brand[i];
 | 
	
		
			
				|  |  |             if(name.contains(one)){
 | 
	
		
			
				|  |  |                 re = type == 1? one:((i==0&&deviceType==1)?model2:model[i]);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return re;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 查询
 | 
	
		
			
				|  |  |      * @param json
 | 
	
		
			
				|  |  |      * 时间处理
 | 
	
		
			
				|  |  |      * @param time
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String search(JSONObject json){
 | 
	
		
			
				|  |  |         json.put("access_token",accessToken);
 | 
	
		
			
				|  |  |         String url = baseUrl+"/upload";
 | 
	
		
			
				|  |  |         List<NameValuePair> params = new ArrayList<>();
 | 
	
		
			
				|  |  |         params.add(new BasicNameValuePair("json",json.toString()));
 | 
	
		
			
				|  |  |         String response = httpClientUtil.post(url, params, "UTF-8");
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     private String subStringTime(String time){
 | 
	
		
			
				|  |  |         return StringUtils.isBlank(time)? "":time.substring(0,19);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /*****************************************工具方法 end  ************************************************************/
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |