Commit 2bf03f32 authored by caotao's avatar caotao

新增自动处理分页请求及参数

parent d38e3092
package com.yeejoin.amos.api.householdapi.Utils; package com.yeejoin.amos.api.householdapi.Utils;
import cn.hutool.Hutool; import cn.hutool.Hutool;
import cn.hutool.core.util.ObjectUtil;
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.JSONArray;
...@@ -14,6 +15,8 @@ import com.yeejoin.amos.api.householdapi.face.service.impl.HouseholdPvApiService ...@@ -14,6 +15,8 @@ import com.yeejoin.amos.api.householdapi.face.service.impl.HouseholdPvApiService
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.component.influxdb.InfluxDbConnection;
import com.yeejoin.amos.openapi.enums.PVProducerInfoEnum; import com.yeejoin.amos.openapi.enums.PVProducerInfoEnum;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.models.auth.In;
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.typroject.tyboot.component.cache.Redis; import org.typroject.tyboot.component.cache.Redis;
...@@ -84,32 +87,67 @@ public class HouseholdPvUtils { ...@@ -84,32 +87,67 @@ public class HouseholdPvUtils {
* @return String 请求返回的response字符串 * @return String 请求返回的response字符串
*/ */
public String gerResponseByAPiID(Long seq) { public String gerResponseByAPiID(Long seq) {
List cacheParams = new ArrayList();
String respone = "";
String params = "";
JSONArray jsonArray = null;
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());
String baseurl = (String) producerInfo.get("apiurl");
HashMap<String, String> headMap = (HashMap<String, String>) producerInfo.get("header"); HashMap<String, String> headMap = (HashMap<String, String>) producerInfo.get("header");
String orginalAuthorization = headMap.get("Authorization") + ":";
String url = baseurl + householdPvApiDto.getApiUrl();
String appsecret = (String) producerInfo.get("appsecret");
if (ObjectUtil.isNotEmpty(householdPvApiDto.getDependecyApiId())) {
String RedisKey = API_REDIS_PREFIX + householdPvApiDto.getDependecyApiId();
cacheParams = Redis.getRedisTemplate().opsForList().range(RedisKey, 0, -1);
if (cacheParams.size() > 0) {
for (int i = 0; i < cacheParams.size(); i++) {
JSONObject jsonObject = JSON.parseObject(householdPvApiDto.getParamInfo());
if (ObjectUtil.isNotEmpty(householdPvApiDto.getDependApiParamKey())) {
jsonObject.put(householdPvApiDto.getDependApiParamKey(), cacheParams.get(i));
}
params = jsonObject.toJSONString();
if (PVProducerInfoEnum.JLY.getCode().equals(householdPvApiDto.getProducerId())) { if (PVProducerInfoEnum.JLY.getCode().equals(householdPvApiDto.getProducerId())) {
String contentMD5 = GoLangHeaderUtils.getDigest(householdPvApiDto.getParamInfo()); JLYHeaderMapHandler(params, householdPvApiDto, headMap, orginalAuthorization, appsecret, (String) cacheParams.get(i));
String param = "POST" + "\n" + contentMD5 + "\n" + "application/json" + "\n" + headMap.get("Date") + "\n" + householdPvApiDto.getApiUrl(); }
String sign = "";
try { try {
sign = GoLangHeaderUtils.HmacSHA1Encrypt(param, (String) producerInfo.get("appsecret")); Thread.sleep(500);
respone = sendRequest(householdPvApiDto.getRequestMethod(), url, params, headMap);
jsonArray = handlerResponseByResultResolverule(householdPvApiDto.getResultResolveRule(), respone);
handleResponseAndCacheData(String.valueOf(seq), householdPvApiDto.getCacheField(), jsonArray, householdPvApiDto.getMeasurement());
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); e.printStackTrace();
} finally {
continue;
} }
headMap.put("Content-MD5", contentMD5);
headMap.put("Authorization", headMap.get("Authorization") + ":" + sign);
} }
String respone = "";
if (householdPvApiDto.getRequestMethod().equals("POST")) {
respone = HttpUtil.createPost(producerInfo.get("apiurl") + householdPvApiDto.getApiUrl()).headerMap(headMap, false).
body(householdPvApiDto.getParamInfo()).execute().body();
} }
if (householdPvApiDto.getRequestMethod().equals("GET")) { } else {
respone = HttpUtil.createGet(producerInfo.get("apiurl") + householdPvApiDto.getApiUrl()).headerMap(headMap, true). if (PVProducerInfoEnum.JLY.getCode().equals(householdPvApiDto.getProducerId())) {
body(householdPvApiDto.getParamInfo()).execute().body(); JLYHeaderMapHandler(params, householdPvApiDto, headMap, orginalAuthorization, appsecret, null);
}
respone = sendRequest(householdPvApiDto.getRequestMethod(), url, householdPvApiDto.getParamInfo(), headMap);
jsonArray = handlerResponseByResultResolverule(householdPvApiDto.getResultResolveRule(), respone);
handleResponseAndCacheData(String.valueOf(seq), householdPvApiDto.getCacheField(), jsonArray, householdPvApiDto.getMeasurement());
if (ObjectUtil.isNotEmpty(householdPvApiDto.getIsPage())) {
Integer size = getPagesize(householdPvApiDto.getPagesizeResolveRule(), respone);
for (int i = 1; i < size; i++) {
JSONObject jsonObject = JSON.parseObject(householdPvApiDto.getParamInfo());
if (ObjectUtil.isNotEmpty(householdPvApiDto.getPageField())) {
jsonObject.put(householdPvApiDto.getPageField(), i + 1);
}
params = jsonObject.toJSONString();
if (PVProducerInfoEnum.JLY.getCode().equals(householdPvApiDto.getProducerId())) {
JLYHeaderMapHandler(params, householdPvApiDto, headMap, orginalAuthorization, appsecret, null);
}
respone = sendRequest(householdPvApiDto.getRequestMethod(), url, householdPvApiDto.getParamInfo(), headMap);
jsonArray = handlerResponseByResultResolverule(householdPvApiDto.getResultResolveRule(), respone);
handleResponseAndCacheData(String.valueOf(seq), householdPvApiDto.getCacheField(), jsonArray, householdPvApiDto.getMeasurement());
}
}
} }
JSONArray jsonArray = handlerResponseByResultResolverule(householdPvApiDto.getResultResolveRule(), respone);
handleResponseAndCacheData(String.valueOf(seq), householdPvApiDto.getCacheField(), jsonArray,householdPvApiDto.getMeasurement());
return respone; return respone;
} }
...@@ -119,9 +157,10 @@ public class HouseholdPvUtils { ...@@ -119,9 +157,10 @@ public class HouseholdPvUtils {
* @return 解析后的数据 * @return 解析后的数据
*/ */
public JSONArray handlerResponseByResultResolverule(String resultResovle, String response) { public JSONArray handlerResponseByResultResolverule(String resultResovle, String response) {
String[] rules = resultResovle.split(",");
JSONObject jsonObject = JSONObject.parseObject(response); JSONObject jsonObject = JSONObject.parseObject(response);
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
if (ObjectUtil.isNotEmpty(resultResovle)) {
String[] rules = resultResovle.split(",");
if (rules.length > 0) { if (rules.length > 0) {
for (int i = 0; i < rules.length; i++) { for (int i = 0; i < rules.length; i++) {
try { try {
...@@ -134,6 +173,10 @@ public class HouseholdPvUtils { ...@@ -134,6 +173,10 @@ public class HouseholdPvUtils {
} }
} }
} }
if (jsonArray.size() == 0) {
jsonArray.add(jsonObject);
}
}
return jsonArray; return jsonArray;
} }
...@@ -151,6 +194,10 @@ public class HouseholdPvUtils { ...@@ -151,6 +194,10 @@ public class HouseholdPvUtils {
if (jsonArray.size() > 0) { if (jsonArray.size() > 0) {
for (Object o : jsonArray) { for (Object o : jsonArray) {
Map<String, Object> maps1 = (Map<String, Object>) o; Map<String, Object> maps1 = (Map<String, Object>) o;
Set<String> keys = maps1.keySet();
keys.forEach((key) -> {
maps1.put(key, maps1.get(key).toString());
});
if (cacheField != null) { if (cacheField != null) {
list.add(maps1.get(cacheField)); list.add(maps1.get(cacheField));
} }
...@@ -162,15 +209,68 @@ public class HouseholdPvUtils { ...@@ -162,15 +209,68 @@ public class HouseholdPvUtils {
} }
} }
if (cacheField != null) {
saveList = Redis.getRedisTemplate().opsForList().range(apiID, 0, -1); saveList = Redis.getRedisTemplate().opsForList().range(apiID, 0, -1);
if (saveList.size() == 0) { if (saveList.size() == 0) {
Redis.getRedisTemplate().opsForList().leftPushAll(apiID, list); Redis.getRedisTemplate().opsForList().leftPushAll(apiID, list);
} }
saveList = Redis.getRedisTemplate().opsForList().range(apiID,0,-1); saveList = Redis.getRedisTemplate().opsForList().range(apiID, 0, -1);
if (saveList.size() != list.size()) { if (saveList.size() != list.size()) {
Redis.getRedisTemplate().delete(apiID); Redis.getRedisTemplate().delete(apiID);
Redis.getRedisTemplate().opsForList().leftPushAll(apiID, list); Redis.getRedisTemplate().opsForList().leftPushAll(apiID, list);
} }
} }
}
public String sendRequest(String requestMethod, String url, String params, HashMap<String, String> headMap) {
String respone = "";
if (requestMethod.equals("POST")) {
respone = HttpUtil.createPost(url).headerMap(headMap, false).
body(params).execute().body();
}
if (requestMethod.equals("GET")) {
respone = HttpUtil.createGet(url).headerMap(headMap, true).
body(params).execute().body();
}
return respone;
}
/***
*
* @param params 参数字符窜
* @param householdPvApiDto apidto
* @param headMap header头
* @param orginalAuthorization 原始的orginalAuthorization
* @param appsecret appsecret
* @param cacheParams 缓存的参数值
* @desc 锦浪云请求参数及head头处理
*/
public void JLYHeaderMapHandler(String params, HouseholdPvApiDto householdPvApiDto, HashMap<String, String> headMap, String orginalAuthorization, String appsecret, String cacheParams) {
if (PVProducerInfoEnum.JLY.getCode().equals(householdPvApiDto.getProducerId())) {
String contentMD5 = GoLangHeaderUtils.getDigest(params);
headMap.put("Date", GoLangHeaderUtils.getGMTTime());
String param = "POST" + "\n" + contentMD5 + "\n" + "application/json" + "\n" + headMap.get("Date") + "\n" + householdPvApiDto.getApiUrl();
String sign = "";
try {
sign = GoLangHeaderUtils.HmacSHA1Encrypt(param, appsecret);
} catch (Exception e) {
throw new RuntimeException(e);
}
headMap.put("Content-MD5", contentMD5);
headMap.put("Authorization", orginalAuthorization + sign);
}
}
public Integer getPagesize(String pageSizeResovle, String response) {
Integer pageSize = 0;
String[] rules = pageSizeResovle.split(",");
JSONObject jsonObject = JSONObject.parseObject(response);
if (rules.length > 0) {
for (int i = 0; i < rules.length - 1; i++) {
jsonObject = (JSONObject) jsonObject.get(rules[i]);
}
}
pageSize = (Integer) jsonObject.get(rules[rules.length - 1]);
return pageSize;
}
} }
...@@ -71,4 +71,16 @@ public class HouseholdPvApiDto extends BaseModel { ...@@ -71,4 +71,16 @@ public class HouseholdPvApiDto extends BaseModel {
*/ */
@ApiModelProperty(value = "时序库表名") @ApiModelProperty(value = "时序库表名")
private String measurement; private String measurement;
/**
* 页数解析规则
*/
@ApiModelProperty(value = "pagesize_resolve_rule")
private String pagesizeResolveRule;
/**
* 页码对应的请求中的字段
*/
@ApiModelProperty( value = "page_field")
private String pageField;
} }
...@@ -90,4 +90,16 @@ public class HouseholdPvApi extends BaseEntity { ...@@ -90,4 +90,16 @@ public class HouseholdPvApi extends BaseEntity {
*/ */
@TableField("measurement") @TableField("measurement")
private String measurement; private String measurement;
/**
* 页数解析规则
*/
@TableField("pagesize_resolve_rule")
private String pagesizeResolveRule;
/**
* 页码对应的请求中的字段
*/
@TableField("page_field")
private String pageField;
} }
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