|  | @ -1,30 +1,29 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.care.service.hz;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.BaseDeviceElectricRecordDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.BaseHzInterfaceDictDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.PatientDeviceDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.util.DSLUtils;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.util.HttpToolUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.util.MD5Utils;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.device.BaseDeviceElectricRecordDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.device.DevicePatientDevice;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.util.BaseHzInterfaceDictEntity;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.date.DateUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.http.HttpClientUtil;
 | 
	
		
			
				|  |  | import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.data.redis.core.StringRedisTemplate;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.BeanPropertyRowMapper;
 | 
	
		
			
				|  |  | import org.springframework.http.HttpMethod;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.io.BufferedReader;
 | 
	
		
			
				|  |  | import java.io.DataOutputStream;
 | 
	
		
			
				|  |  | import java.io.InputStreamReader;
 | 
	
		
			
				|  |  | import java.net.HttpURLConnection;
 | 
	
		
			
				|  |  | import java.net.URL;
 | 
	
		
			
				|  |  | import java.util.Date;
 | 
	
		
			
				|  |  | import java.util.HashMap;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
	
		
			
				|  | @ -43,55 +42,101 @@ public class HzInterfaceService {
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HttpClientUtil httpClientUtil;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public JSONObject electricityTable(String name,String date) throws Exception {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONObject jsonObject = getAreaApi(date);
 | 
	
		
			
				|  |  |         System.out.println(jsonObject.toJSONString());
 | 
	
		
			
				|  |  |         /*for (Object o : dataArr) {
 | 
	
		
			
				|  |  |             //JSONObject.parseObject();
 | 
	
		
			
				|  |  |         }*/
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PatientDeviceDao patientDeviceDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDeviceElectricRecordDao baseDeviceElectricRecordDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取居民电表数据,没有就去查询
 | 
	
		
			
				|  |  |      * @param queryDate
 | 
	
		
			
				|  |  |      * @param patient
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public synchronized BaseDeviceElectricRecordDO electricityTable(String queryDate,String patient){
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             List<BaseDeviceElectricRecordDO> list = new ArrayList<>();
 | 
	
		
			
				|  |  |             if(StringUtils.isNotBlank(patient)){
 | 
	
		
			
				|  |  |                 list = baseDeviceElectricRecordDao.findByPatientAndStatDate(patient,queryDate);
 | 
	
		
			
				|  |  |                 if(list.size()>0){
 | 
	
		
			
				|  |  |                     return list.get(0);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }else{
 | 
	
		
			
				|  |  |                 list = baseDeviceElectricRecordDao.findByStatDate(queryDate);
 | 
	
		
			
				|  |  |                 if(list.size()>0){
 | 
	
		
			
				|  |  |                     return list.get(0);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             Map<String,Integer> map = new HashMap<>();
 | 
	
		
			
				|  |  |             JSONArray jsonArray = getAreaApi(queryDate);
 | 
	
		
			
				|  |  |             for (int i=0;i<jsonArray.size();i++){
 | 
	
		
			
				|  |  |                 JSONObject json = jsonArray.getJSONObject(i);
 | 
	
		
			
				|  |  |                 String consNo = json.getString("consNo");
 | 
	
		
			
				|  |  |                 if(!map.containsKey(consNo)){
 | 
	
		
			
				|  |  |                     map.put(consNo,0);
 | 
	
		
			
				|  |  |                     BaseDeviceElectricRecordDO recordDO = new BaseDeviceElectricRecordDO();
 | 
	
		
			
				|  |  |                     recordDO.setAddress(json.getString("address"));
 | 
	
		
			
				|  |  |                     recordDO.setConsNo(json.getString("consNo"));
 | 
	
		
			
				|  |  |                     recordDO.setHome(json.getString("home"));
 | 
	
		
			
				|  |  |                     recordDO.setLowerLimit(json.getString("lowerLimit"));
 | 
	
		
			
				|  |  |                     recordDO.setStatDate(json.getString("statDate"));
 | 
	
		
			
				|  |  |                     recordDO.setUpperLimit(json.getString("upperLimit"));
 | 
	
		
			
				|  |  |                     recordDO.setName(json.getString("name"));
 | 
	
		
			
				|  |  |                     DevicePatientDevice patientDevice = patientDeviceDao.findByDeviceSnAndCategoryCodeAndUserType(consNo,"20","-1");
 | 
	
		
			
				|  |  |                     if(patientDevice!=null){
 | 
	
		
			
				|  |  |                         recordDO.setPatient(patientDevice.getUser());
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     list.add(recordDO);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if(list.size()>0){
 | 
	
		
			
				|  |  |                 baseDeviceElectricRecordDao.save(list);
 | 
	
		
			
				|  |  |                 return list.get(0);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }catch (Exception e){
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public long expire(String key) {
 | 
	
		
			
				|  |  |     private long expire(String key) {
 | 
	
		
			
				|  |  |         return redisTemplate.opsForValue().getOperations().getExpire(key);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private JSONObject getAreaApi(String date) throws Exception{
 | 
	
		
			
				|  |  |         BaseHzInterfaceDictEntity dbkey = requestDBKEY();
 | 
	
		
			
				|  |  |         //判断是否过期 或者 是否不存在
 | 
	
		
			
				|  |  |         if(expire(dbkey.getCode()) > 0?false:true||redisTemplate.hasKey(dbkey.getCode())){
 | 
	
		
			
				|  |  |             PopBasicInfo();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         String requestSecret = redisTemplate.opsForValue().get(dbkey.getCode());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //请求接口地址(政务网环境根据接口文档地址改动)
 | 
	
		
			
				|  |  |         String electricityTableSql = "SELECT url FROM base_hz_interface_dict WHERE `status` = 1 AND `code` = 'zfjddjlrdbjk'";
 | 
	
		
			
				|  |  |         String url = jdbcTemplate.queryForObject(electricityTableSql, String.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String requestTime = DSLUtils.dateToLong(new Date()) + "";//时间戳
 | 
	
		
			
				|  |  |     public JSONArray getAreaApi(String date){
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             BaseHzInterfaceDictEntity dbkey = requestDBKEY();
 | 
	
		
			
				|  |  |             //判断是否过期 或者 是否不存在
 | 
	
		
			
				|  |  |             if(expire(dbkey.getCode()) > 0?false:true||redisTemplate.hasKey(dbkey.getCode())){
 | 
	
		
			
				|  |  |                 PopBasicInfo();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             String requestSecret = redisTemplate.opsForValue().get(dbkey.getCode());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String sign = MD5Utils.encoderByMd5(dbkey.getAppKey()+ requestSecret + requestTime);
 | 
	
		
			
				|  |  |             //请求接口地址(政务网环境根据接口文档地址改动)
 | 
	
		
			
				|  |  |             String electricityTableSql = "SELECT url FROM base_hz_interface_dict WHERE `status` = 1 AND `code` = 'zfjddjlrdbjk'";
 | 
	
		
			
				|  |  |             String url = jdbcTemplate.queryForObject(electricityTableSql, String.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONObject json = new JSONObject();
 | 
	
		
			
				|  |  |         json.put("queryDate",date);
 | 
	
		
			
				|  |  |             String requestTime = DSLUtils.dateToLong(new Date()) + "";//时间戳
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         url +="?appKey="+dbkey.getAppKey()+"&sign="+sign+"&requestTime="+requestTime;
 | 
	
		
			
				|  |  |             String sign = MD5Utils.encoderByMd5(dbkey.getAppKey()+ requestSecret + requestTime);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String s = httpURLConnectionPOST(url, json);
 | 
	
		
			
				|  |  |         logger.info("自己封装的请求方式:" + s);
 | 
	
		
			
				|  |  |         String response = httpClientUtil.postBody(url, json);
 | 
	
		
			
				|  |  |         logger.info("电表接口调用结果----> "+response);
 | 
	
		
			
				|  |  |         JSONObject responseBody = JSONObject.parseObject(response);
 | 
	
		
			
				|  |  |             JSONObject json = new JSONObject();
 | 
	
		
			
				|  |  |             json.put("queryDate",date);
 | 
	
		
			
				|  |  |             url +="?appKey="+dbkey.getAppKey()+"&sign="+sign+"&requestTime="+requestTime;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return JSONObject.parseObject(responseBody.get("data").toString());
 | 
	
		
			
				|  |  |             org.springframework.http.HttpEntity<JSONObject> response = httpClientUtil.assesTokenPostHttp(url, json, HttpMethod.POST);
 | 
	
		
			
				|  |  |             logger.info("电表接口调用结果----> "+response);
 | 
	
		
			
				|  |  |             JSONObject responseBody = response.getBody();
 | 
	
		
			
				|  |  |             if("0".equals(responseBody.getString("code"))){
 | 
	
		
			
				|  |  |                 return responseBody.getJSONArray("data");
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }catch (Exception e){
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return new JSONArray();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     // 刷新密钥还存在时可以调用这个方法
 | 
	
		
			
				|  |  |     private String refreshKey(BaseHzInterfaceDictEntity refreshdbkey, BaseHzInterfaceDictEntity requestdbkey){
 | 
	
		
			
				|  |  |         Map<String, String> params = new HashMap<String, String>();
 | 
	
	
		
			
				|  | @ -138,8 +183,8 @@ public class HzInterfaceService {
 | 
	
		
			
				|  |  |         params.put("requestTime", requestTime);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONObject resultObj = HttpToolUtil.httpURLConnection_sendPost(url, params);
 | 
	
		
			
				|  |  |         logger.info("接口返回值:"+resultObj.toString());
 | 
	
		
			
				|  |  |         System.out.println("接口返回值:"+resultObj.toString());
 | 
	
		
			
				|  |  | //        logger.info("接口返回值:"+resultObj.toString());
 | 
	
		
			
				|  |  | //        System.out.println("接口返回值:"+resultObj.toString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //请求密钥
 | 
	
		
			
				|  |  |         JSONObject datasObj = JSONObject.parseObject(resultObj.get("datas").toString());
 | 
	
	
		
			
				|  | @ -159,59 +204,13 @@ public class HzInterfaceService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     // 请求密钥 15分钟过期
 | 
	
		
			
				|  |  |     public BaseHzInterfaceDictEntity requestDBKEY(){
 | 
	
		
			
				|  |  |         String sql = "SELECT * FROM base_hz_interface_dict WHERE `status` = 1 AND `code` = 'dbmyhq'";
 | 
	
		
			
				|  |  |         return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(BaseHzInterfaceDictEntity.class));
 | 
	
		
			
				|  |  |         return baseHzInterfaceDictDao.findByStatusAndCode(1,"dbmyhq");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     // 刷新密钥 48小时过期
 | 
	
		
			
				|  |  |     public BaseHzInterfaceDictEntity refreshDBKEY(){
 | 
	
		
			
				|  |  |         String sql = "SELECT * FROM base_hz_interface_dict WHERE `status` = 1 AND `code` = 'dbmysxhq'";
 | 
	
		
			
				|  |  |         return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(BaseHzInterfaceDictEntity.class));
 | 
	
		
			
				|  |  |         return baseHzInterfaceDictDao.findByStatusAndCode(1,"dbmysxhq");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static String httpURLConnectionPOST(String url, JSONObject data) {
 | 
	
		
			
				|  |  |         StringBuffer strBf = new StringBuffer();
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             URL realUrl = new URL(url);
 | 
	
		
			
				|  |  |             //将realUrl以 open方法返回的urlConnection  连接强转为HttpURLConnection连接  (标识一个url所引用的远程对象连接)
 | 
	
		
			
				|  |  |             //此时cnnection只是为一个连接对象,待连接中
 | 
	
		
			
				|  |  |             HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
 | 
	
		
			
				|  |  |             //设置连接输出流为true,默认false (post请求是以流的方式隐式的传递参数)
 | 
	
		
			
				|  |  |             connection.setDoOutput(true);
 | 
	
		
			
				|  |  |             //设置连接输入流为true
 | 
	
		
			
				|  |  |             connection.setDoInput(true);
 | 
	
		
			
				|  |  |             //设置请求方式为post
 | 
	
		
			
				|  |  |             connection.setRequestMethod("POST");
 | 
	
		
			
				|  |  |             //post请求缓存设为false
 | 
	
		
			
				|  |  |             connection.setUseCaches(false);
 | 
	
		
			
				|  |  |             //设置该HttpURLConnection实例是否自动执行重定向
 | 
	
		
			
				|  |  |             connection.setInstanceFollowRedirects(true);
 | 
	
		
			
				|  |  |             //设置请求头里面的各个属性 (以下为设置内容的类型,设置为经过urlEncoded编码过的from参数)
 | 
	
		
			
				|  |  |             connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
 | 
	
		
			
				|  |  |             //建立连接 (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)
 | 
	
		
			
				|  |  |             connection.connect();
 | 
	
		
			
				|  |  |             //创建输入输出流,用于往连接里面输出携带的参数
 | 
	
		
			
				|  |  |             DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
 | 
	
		
			
				|  |  |             String query = data.toString();
 | 
	
		
			
				|  |  |             //将参数输出到连接
 | 
	
		
			
				|  |  |             dataout.write(query.getBytes("UTF-8"));
 | 
	
		
			
				|  |  |             // 输出完成后刷新并关闭流
 | 
	
		
			
				|  |  |             dataout.flush();
 | 
	
		
			
				|  |  |             dataout.close(); // 关闭流
 | 
	
		
			
				|  |  |             BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
 | 
	
		
			
				|  |  |             String lines;
 | 
	
		
			
				|  |  |             while ((lines = reader.readLine()) != null) {
 | 
	
		
			
				|  |  |                 lines = new String(lines.getBytes(), "utf-8");
 | 
	
		
			
				|  |  |                 strBf.append(lines);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             reader.close();
 | 
	
		
			
				|  |  |             connection.disconnect();
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return strBf.toString();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |