Commit d88a8293 authored by caotao's avatar caotao

更新户用光伏相关代码

parent 87cf8b25
package com.yeejoin.amos.api.householdapi.Utils; package com.yeejoin.amos.api.householdapi.Utils;
import cn.hutool.Hutool;
import cn.hutool.http.HttpUtil; import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qiniu.util.Json;
import com.yeejoin.amos.api.householdapi.face.model.HouseholdPvApiDto; import com.yeejoin.amos.api.householdapi.face.model.HouseholdPvApiDto;
import com.yeejoin.amos.api.householdapi.face.model.HouseholdPvProducerInfoDto; import com.yeejoin.amos.api.householdapi.face.model.HouseholdPvProducerInfoDto;
import com.yeejoin.amos.api.householdapi.face.orm.entity.HouseholdPvProducerInfo; import com.yeejoin.amos.api.householdapi.face.orm.entity.HouseholdPvProducerInfo;
import com.yeejoin.amos.api.householdapi.face.service.impl.HouseholdPvApiServiceImpl; import com.yeejoin.amos.api.householdapi.face.service.impl.HouseholdPvApiServiceImpl;
import com.yeejoin.amos.api.householdapi.face.service.impl.HouseholdPvProducerInfoServiceImpl; import com.yeejoin.amos.api.householdapi.face.service.impl.HouseholdPvProducerInfoServiceImpl;
import com.yeejoin.amos.component.influxdb.InfluxDbConnection;
import com.yeejoin.amos.openapi.enums.PVProducerInfoEnum; import com.yeejoin.amos.openapi.enums.PVProducerInfoEnum;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody; import org.typroject.tyboot.component.cache.Redis;
import java.util.Date; import java.util.*;
import java.util.HashMap;
@Component @Component
public class HouseholdPvUtils { public class HouseholdPvUtils {
@Autowired @Autowired
private HouseholdPvProducerInfoServiceImpl householdPvProducerInfoServiceImpl; private HouseholdPvProducerInfoServiceImpl householdPvProducerInfoServiceImpl;
@Autowired @Autowired
private HouseholdPvApiServiceImpl householdPvApiServiceImpl; private HouseholdPvApiServiceImpl householdPvApiServiceImpl;
@Autowired
InfluxDbConnection influxDbConnection;
private final static String API_REDIS_PREFIX = "HOUSEHOLD_API_CACHE_" +
"";
/** /**
* @deprecated 根据厂商编码获取厂商的hearer
* @param code 厂商code * @param code 厂商code
* @return HashMap<String, Object> 发送请求前的准备 准备header信息 * @return HashMap<String, Object> 发送请求前的准备 准备header信息
* @deprecated 根据厂商编码获取厂商的hearer
*/ */
public HashMap<String, Object> getHeaderByProducerCode(String code) { public HashMap<String, Object> getHeaderByProducerCode(String code) {
HashMap<String, Object> hashMap = new HashMap<>(); HashMap<String, Object> hashMap = new HashMap<>();
HashMap<String, String> hashMaphead = new HashMap<>(); HashMap<String, String> hashMaphead = new HashMap<>();
HouseholdPvProducerInfo householdPvProducerInfo =this.householdPvProducerInfoServiceImpl.queryForHouseholdPvProducerInfoByCode(code); HouseholdPvProducerInfo householdPvProducerInfo = this.householdPvProducerInfoServiceImpl.queryForHouseholdPvProducerInfoByCode(code);
String url = householdPvProducerInfo.getProdcerUrl() + householdPvProducerInfo.getTokenUrl(); String url = householdPvProducerInfo.getProdcerUrl() + householdPvProducerInfo.getTokenUrl();
hashMap.put("apiurl", householdPvProducerInfo.getProdcerUrl()); hashMap.put("apiurl", householdPvProducerInfo.getProdcerUrl());
if (PVProducerInfoEnum.GDW.getCode().equals(code)) { if (PVProducerInfoEnum.GDW.getCode().equals(code)) {
...@@ -59,45 +67,93 @@ public class HouseholdPvUtils { ...@@ -59,45 +67,93 @@ public class HouseholdPvUtils {
String response = HttpUtil.createPost(url).body(body).execute().body(); String response = HttpUtil.createPost(url).body(body).execute().body();
JSONObject jsonObject = JSON.parseObject(response); JSONObject jsonObject = JSON.parseObject(response);
hashMaphead.put("Authorization", (String) jsonObject.get("data")); hashMaphead.put("Authorization", (String) jsonObject.get("data"));
hashMap.put("header",hashMaphead); hashMap.put("header", hashMaphead);
}else if(PVProducerInfoEnum.JLY.getCode().equals(code)){ } else if (PVProducerInfoEnum.JLY.getCode().equals(code)) {
hashMaphead.put("Content-type","application/json;charset=UTF-8"); hashMaphead.put("Content-type", "application/json;charset=UTF-8");
hashMaphead.put("Authorization","API "+householdPvProducerInfo.getProdcerAppid()); hashMaphead.put("Authorization", "API " + householdPvProducerInfo.getProdcerAppid());
hashMaphead.put("Content-MD5",null); hashMaphead.put("Content-MD5", null);
hashMaphead.put("Date", GoLangHeaderUtils.getGMTTime()); hashMaphead.put("Date", GoLangHeaderUtils.getGMTTime());
hashMap.put("header",hashMaphead); hashMap.put("header", hashMaphead);
hashMap.put("appsecret", householdPvProducerInfo.getProdcerAppsecret()); hashMap.put("appsecret", householdPvProducerInfo.getProdcerAppsecret());
} }
return hashMap; return hashMap;
} }
/** @return String 请求返回的response字符串
/**
* @param seq apiId 数据库中维护的的apiId * @param seq apiId 数据库中维护的的apiId
* */ * @return String 请求返回的response字符串
public String gerResponseByAPiID(Long seq){ */
public String gerResponseByAPiID(Long seq) {
HouseholdPvApiDto householdPvApiDto = this.householdPvApiServiceImpl.queryBySeq(seq); HouseholdPvApiDto householdPvApiDto = this.householdPvApiServiceImpl.queryBySeq(seq);
HashMap<String,Object> producerInfo = this.getHeaderByProducerCode(householdPvApiDto.getProducerId()); HashMap<String, Object> producerInfo = this.getHeaderByProducerCode(householdPvApiDto.getProducerId());
HashMap<String,String> headMap = (HashMap<String, String>) producerInfo.get("header"); HashMap<String, String> headMap = (HashMap<String, String>) producerInfo.get("header");
if(PVProducerInfoEnum.JLY.getCode().equals(householdPvApiDto.getProducerId())){ if (PVProducerInfoEnum.JLY.getCode().equals(householdPvApiDto.getProducerId())) {
String contentMD5 = GoLangHeaderUtils.getDigest(householdPvApiDto.getParamInfo()); String contentMD5 = GoLangHeaderUtils.getDigest(householdPvApiDto.getParamInfo());
String param = "POST"+"\n"+contentMD5+"\n"+"application/json"+"\n"+headMap.get("Date")+"\n"+householdPvApiDto.getApiUrl(); String param = "POST" + "\n" + contentMD5 + "\n" + "application/json" + "\n" + headMap.get("Date") + "\n" + householdPvApiDto.getApiUrl();
String sign=""; String sign = "";
try { try {
sign = GoLangHeaderUtils.HmacSHA1Encrypt(param, (String) producerInfo.get("appsecret")); sign = GoLangHeaderUtils.HmacSHA1Encrypt(param, (String) producerInfo.get("appsecret"));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
headMap.put("Content-MD5",contentMD5); headMap.put("Content-MD5", contentMD5);
headMap.put("Authorization",headMap.get("Authorization")+":"+sign); headMap.put("Authorization", headMap.get("Authorization") + ":" + sign);
} }
String respone=""; String respone = "";
if(householdPvApiDto.getRequestMethod().equals("POST")){ if (householdPvApiDto.getRequestMethod().equals("POST")) {
respone = HttpUtil.createPost(producerInfo.get("apiurl")+householdPvApiDto.getApiUrl()).headerMap(headMap,false). respone = HttpUtil.createPost(producerInfo.get("apiurl") + householdPvApiDto.getApiUrl()).headerMap(headMap, false).
body(householdPvApiDto.getParamInfo()).execute().body(); body(householdPvApiDto.getParamInfo()).execute().body();
} }
if(householdPvApiDto.getRequestMethod().equals("GET")){ if (householdPvApiDto.getRequestMethod().equals("GET")) {
respone = HttpUtil.createGet(producerInfo.get("apiurl")+householdPvApiDto.getApiUrl()).headerMap(headMap,true). respone = HttpUtil.createGet(producerInfo.get("apiurl") + householdPvApiDto.getApiUrl()).headerMap(headMap, true).
body(householdPvApiDto.getParamInfo()).execute().body(); body(householdPvApiDto.getParamInfo()).execute().body();
} }
JSONArray jsonArray = handlerResponseByResultResolverule(householdPvApiDto.getResultResolveRule(), respone);
handleResponseAndCacheData(String.valueOf(seq), householdPvApiDto.getCacheField(), jsonArray);
return respone; return respone;
} }
public JSONArray handlerResponseByResultResolverule(String resultResovle, String response) {
String[] rules = resultResovle.split(",");
JSONObject jsonObject = JSONObject.parseObject(response);
JSONArray jsonArray = new JSONArray();
if (rules.length > 0) {
for (int i = 0; i < rules.length; i++) {
try {
jsonObject = (JSONObject) jsonObject.get(rules[i]);
if (jsonObject == null) {
jsonArray = (JSONArray) jsonObject.get(rules[i]);
}
} catch (Exception exception) {
jsonArray = (JSONArray) jsonObject.get(rules[i]);
}
}
}
return jsonArray;
}
public void handleResponseAndCacheData(String seq, String cacheField, JSONArray jsonArray) {
String apiID = API_REDIS_PREFIX + seq;
List list = new ArrayList();
List saveList = new ArrayList();
if (jsonArray.size() > 0) {
for (Object o : jsonArray) {
Map<String, Object> maps1 = (Map<String, Object>) o;
list.add(maps1.get(cacheField));
Map<String, String> maps2 = new HashMap<>();
maps2.put("key", UUID.randomUUID().toString());
// influxDbConnection.insert("SH_DZ",maps2,maps1);
}
}
saveList = Redis.getRedisTemplate().opsForList().range(apiID, 0, -1);
if (saveList.size() == 0) {
Redis.getRedisTemplate().opsForList().leftPushAll(apiID, list);
}
// saveList = Redis.getRedisTemplate().opsForList().range(apiID,0,-1);
if (saveList.size() != list.size()) {
Redis.getRedisTemplate().delete(apiID);
Redis.getRedisTemplate().opsForList().leftPushAll(apiID, list);
}
list = Redis.getRedisTemplate().opsForList().range(apiID, 0, -1);
}
} }
package com.yeejoin.amos.api.householdapi.face.model; package com.yeejoin.amos.api.householdapi.face.model;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
...@@ -16,10 +17,10 @@ import java.util.Date; ...@@ -16,10 +17,10 @@ import java.util.Date;
*/ */
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ApiModel(value="HouseholdPvApiDto", description="户用光伏-厂商API haders") @ApiModel(value = "HouseholdPvApiDto", description = "户用光伏-厂商API haders")
public class HouseholdPvApiDto extends BaseModel { public class HouseholdPvApiDto extends BaseModel {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "厂商id") @ApiModelProperty(value = "厂商id")
...@@ -40,4 +41,30 @@ public class HouseholdPvApiDto extends BaseModel { ...@@ -40,4 +41,30 @@ public class HouseholdPvApiDto extends BaseModel {
@ApiModelProperty(value = "参数信息") @ApiModelProperty(value = "参数信息")
private String paramInfo; private String paramInfo;
/**
* 依赖的apiId
*/
@ApiModelProperty(value = "依赖的apiId")
private String dependecyApiId;
/**
* 缓存的属性
*/
@ApiModelProperty(value = "缓存的属性")
private String cacheField;
/**
* 依赖的api属性
*/
@ApiModelProperty(value = "依赖的api属性")
private String dependApiParamKey;
/**
* result解析结果
*/
@ApiModelProperty(value = "result解析结果")
private String resultResolveRule;
/**
* 是否分页
*/
@ApiModelProperty(value = "是否分页")
private Integer isPage;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment