请求模拟代码提交

This commit is contained in:
wr
2023-06-08 16:06:18 +08:00
parent 67d8c8b256
commit 6f859a234f
10 changed files with 666 additions and 0 deletions

20
pom.xml
View File

@@ -111,6 +111,25 @@
<version>${commons-codec.version}</version>
</dependency>
<!--网络请求-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<dependency>
<groupId>com.huawei.apigateway</groupId>
<artifactId>java-sdk-core</artifactId>
<version>3.0.10</version>
</dependency>
<dependency>
<groupId>com.njcn</groupId>
<artifactId>common-db</artifactId>
@@ -127,6 +146,7 @@
</exclusions>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,64 @@
package com.njcn.syncdata.controller;
import com.njcn.syncdata.service.IPowerDistributionareaService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/dataImport")
@RequiredArgsConstructor
public class DataImportController {
private final IPowerDistributionareaService service;
/**
* 登录获取token
* @return
*/
@GetMapping("/token")
public String token(){
return service.LoginToken();
}
/**
* 获取数据中台数据
* @return
*/
@GetMapping("/getAdsList")
public String adsList(){
return service.adsList();
}
/**
* 获取数据中台返回体
* @param token
* @param psrType
* @param fieldName
* @param fieldValue
* @return
*/
@GetMapping("/getResource")
public Map getResource(String token,String psrType, String fieldName,String fieldValue){
return service.staticMethod(token,psrType,fieldName,fieldValue);
}
/**
* 获取数据中台返回数据
* @param token
* @param psrType
* @param fieldName
* @param fieldValue
* @return
*/
@GetMapping("/getResourceData")
public List getResourceData(String token,String psrType, String fieldName,String fieldValue){
Map map = service.staticMethod(token, psrType, fieldName, fieldValue);
List data = (List) map.get("data");
return data;
}
}

View File

@@ -0,0 +1,19 @@
package com.njcn.syncdata.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.njcn.syncdata.pojo.po.PowerDistributionarea;
/**
* <p>
* Mapper 台区
* </p>
*
* @author hongawen
* @since 2022-10-14
*/
public interface PowerDistributionareaMapper extends BaseMapper<PowerDistributionarea> {
}

View File

@@ -0,0 +1,76 @@
package com.njcn.syncdata.pojo.po;
import lombok.Data;
import java.math.BigDecimal;
/**
* @author wr
* @description
* @date 2023/6/7 8:57
*/
@Data
public class AdsAstXbjcptGetFctgDi {
/**
* 台区标识
*/
private String tg_id;
/**
* PMS侧变压器ID
*/
private String trans_pms_id;
/**
* 计量点号
*/
private Integer id;
/**
* 台区名称
*/
private String tg_name;
/**
* 台区编号
*/
private String tg_no;
/**
* 台区区域划分类别,01、市中心区02、市区,03、 城镇,04、农村
*/
private String area_type;
/**
* 公变专变标志台区是0.公变或者1.专变
*/
private String pub_priv_flag;
/**
* 台区管理单位编号
*/
private String org_no;
/**
* 供电单位名称
*/
private String org_name;
/**
* 供电单位类别:单位类别国网公司、省公司、地市公司、区县公司、分公司、供电所等。01 国网公司、02 省公司、03 地市公司 、04 区县公司、05 分公司、06 供电所
*/
private String org_type;
/**
* 上级供电单位编号
*/
private String p_org_no;
/**
* 运行状态台区运行状态
*/
private String run_status_code;
/**
* 容量台区容量,为可并列运行的变压器容量之和
*/
private BigDecimal tg_cap;
/**
* 分布式光伏用户数
*/
private Integer fc_num;
/**
* 分布式光伏用户总容量MW
*/
private BigDecimal fc_sum_cap;
}

View File

@@ -0,0 +1,50 @@
package com.njcn.syncdata.pojo.po;
import lombok.Data;
import java.math.BigDecimal;
/**
* @author wr
* @description
* @date 2023/6/7 11:18
*/
@Data
public class PWFeederLinePSR {
/**
* 设备id
*/
private String ID;
/**
* 起点电站id
*/
private String start_station_id;
/**
* 设备名称
*/
private String Name;
/**
* 组织机构
*/
private String DEPART;
/**
* 维护班组
*/
private String MAINTENANCE_TEAM;
/**
* 供电半径
*/
private BigDecimal SUPPLY_RADIUS;
/**
* 供电线路总长度
*/
private BigDecimal LENGTH;
}

View File

@@ -0,0 +1,38 @@
package com.njcn.syncdata.pojo.po;
import lombok.Data;
/**
* @author wr
* @description
* @date 2023/6/7 10:25
*/
@Data
public class PWOPTransformerPSR {
/**
* 外部系统id
*/
private String ACCOUNT_ID;
/**
* 根设备id
*/
private String ROOT_ID;
/**
* 是否是农网
*/
private Integer IS_RURAL_GRID;
/**
* 使用性质
*/
private String use_nature;
/**
* 运行状态
*/
private String RUN_STATUS;
}

View File

@@ -0,0 +1,156 @@
package com.njcn.syncdata.pojo.po;
import com.baomidou.mybatisplus.annotation.TableName;
import com.njcn.db.bo.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* <p>
*
* </p>
*
* @author hongawen
* @since 2022-10-14
*/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("pms_power_distributionarea")
public class PowerDistributionarea extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* 台区编号
*/
private String id;
/**
* 台区名称
*/
private String name;
/**
* 组织机构名称
*/
private String orgName;
/**
* 组织机构ID(外键)
*/
private String orgId;
/**
* 运维单位名称
*/
private String operationName;
/**
* 运维单位ID(外键)
*/
private String operationId;
/**
* 变电站名称
*/
private String powerrName;
/**
* 电站ID(外键)
*/
private String powerStationId;
/**
* 监测线路名称
*/
private String lineName;
/**
* 所属线路ID(外键)
*/
private String lineId;
/**
* 电压等级
*/
private String voltageLevel;
/**
* 配变容量
*/
private Float pCapacity;
/**
* 地区特征(字典)
*/
private String regionalism;
/**
* 是否农网0-否1
*/
private Integer ifRuralPowerGrid;
/**
* 使用性质
*/
private String natureOfUse;
/**
* 供电半径
*/
private Float powerSupplyRadius;
/**
* 供电线路总长度
*/
private Float lineLength;
/**
* 运行状态(字典)
*/
private String state;
/**
* 分布式光伏用户数
*/
private Integer distributedPhotovoltaicNum;
/**
* 分布式光伏总装机容量
*/
private Float photovoltaicCapacity;
/**
* 是否有电动汽车接入0-否1
*/
private Integer ifBevAp;
/**
* 接入负荷类型(字典)
*/
private String apLoadType;
/**
* 是否是上送国网监测点,0-否 1-是
*/
private Integer isUpToGrid;
/**
* 经度
*/
private Double longitude;
/**
* 维度
*/
private Double latitude;
/**
* 数据状态0-删除1-正常;
*/
private Integer status;
}

View File

@@ -0,0 +1,27 @@
package com.njcn.syncdata.pojo.po;
import lombok.Data;
/**
* @author wr
* @description
* @date 2023/6/7 11:15
*/
@Data
public class Substation {
/**
* 设备id
*/
private String ID;
/**
* 设备名称
*/
private String NAME;
/**
* 电压等级
*/
private String VOLTAGELEVEL_ID;
}

View File

@@ -0,0 +1,39 @@
package com.njcn.syncdata.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.njcn.syncdata.pojo.po.PowerDistributionarea;
import java.util.Map;
/**
* <p>
* 服务类
* </p>
*
* @author hongawen
* @since 2022-10-14
*/
public interface IPowerDistributionareaService extends IService<PowerDistributionarea> {
/**
* 登录获取token
* @return
*/
String LoginToken();
/**
* 获取数据中台数据
* @return
*/
String adsList();
/**
* 通用资源数据中台数据查询
* @param token 数据token
* @param psrType 对应需要查询的表
* @param fieldName 数据的列名
* @param fieldValue 数据id集合
* @return
*/
Map staticMethod(String token,String psrType, String fieldName,String fieldValue);
}

View File

@@ -0,0 +1,177 @@
package com.njcn.syncdata.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.apigateway.sdk.utils.Client;
import com.cloud.apigateway.sdk.utils.Request;
import com.njcn.syncdata.mapper.PowerDistributionareaMapper;
import com.njcn.syncdata.pojo.po.AdsAstXbjcptGetFctgDi;
import com.njcn.syncdata.pojo.po.PowerDistributionarea;
import com.njcn.syncdata.service.IPowerDistributionareaService;
import com.njcn.syncdata.utils.RestTemplateUtil;
import lombok.RequiredArgsConstructor;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* <p>
* 台区信息
* </p>
*
* @author hongawen
* @since 2022-10-14
*/
@Service
@RequiredArgsConstructor
public class PowerDistributionareaServiceImpl extends ServiceImpl<PowerDistributionareaMapper, PowerDistributionarea> implements IPowerDistributionareaService {
private static Logger logger = LoggerFactory.getLogger(PowerDistributionareaServiceImpl.class);
@Override
public String LoginToken() {
String token = null;
String userName = "04ba64fc640040efba9c9e76d434fbe3f06e91f17fc1a863fd47ce1efba69e1f44d5948560499999e21bc21b3ec2cdf565534e74e227fb79545c88637c5d16ad893090e941b97aa64b5db28153903c4f11412b3466950bdd8c623b1250be802939e5450b5282f5ae7032e22c";
String password = "048d108b7d88345094ca5278e7361e4bfb6d6d2864a855a1c36afe33f50c4eb609dd08b6a73a89f976a6fc6d428d635b10183a8844db94d1f1697b83ba5beac26130e343a81b1b4ba12ee4212310e8833de39d7032409dff3848c10e654efb2ce4136a0c0bdd02097b48e792e66a7723154afa4977";
String clientId = "3DEB086AF4E9638EC2F6909A2EBAE559";
String clientSecret = "7E974718AB96E7EDC09F9E184F7F25BC";
String userUrl = "http://dwzyywzt.com/baseCenter/oauth2/accessToken";
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(userUrl)
.queryParam("grant_type", "password")
.queryParam("username", userName)
.queryParam("password", password)
.queryParam("client_id", clientId)
.queryParam("client_secret", clientSecret);
URI uri = builder.build().encode().toUri();
ResponseEntity<Map> userEntity = RestTemplateUtil.post(uri, Map.class);
if (userEntity.getStatusCodeValue() == 200) {
logger.debug(Thread.currentThread().getName() + "数据:" + userEntity.getBody() + "结束----");
//获取返回体
Map body = userEntity.getBody();
Map data = (Map) body.get("result");
token = data.get("access_token").toString();
}
return token;
}
@Override
public String adsList() {
String s=null;
String key = "0e3bac160fd246f181ad4fd47da6929a";
String secret = "383b4b2536234d84ac909cd605762061";
String url = "http://0f92aaf3da9b4dd2ab80a337495ccf09.apigw.he-region-2.sgic.sgcc.com.cn/ast/yxywyyxt/dws/get_e_mp_fctg_zl";
String apiId = "4e829410fea4513cd0fab51a12278760";
String apiName = "get_电能质量谐波监测系统_分布式光伏台区台账_e_mp_fctg_zl_单位编码";
Request request = new Request();
CloseableHttpClient client = null;
try {
request.setKey(key);
request.setSecret(secret);
//接口访问类型
request.setMethod("GET");
//接口访问url
request.setUrl(url);
//API中 header头传输的参数
request.addHeader("x-api-id", apiId);
request.addHeader("Content-Type", "Application/json");
//API中 Query传输的参数
//request.addQueryStringParam("name", "value");
//API中 Body体参数
//request.setBody("demo");
} catch (Exception e) {
logger.error(Thread.currentThread().getName() + "链接" + apiName + "失败");
e.printStackTrace();
}
try {
HttpRequestBase signedRequest = Client.sign(request);
Header[] authorization = signedRequest.getHeaders("Authorization");
signedRequest.addHeader("x-Authorization", authorization[0].getValue());
//Send the request.
client = HttpClients.custom().build();
HttpResponse response = client.execute(signedRequest);
//Print the status line of the response.
logger.debug(response.getStatusLine().toString());
//Print the header fields of the response.
Header[] resHeaders = response.getAllHeaders();
for (Header h : resHeaders) {
logger.debug(h.getName() + ":" + h.getValue());
}
//Print the body of the response.
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
s = EntityUtils.toString(resEntity, "UTF-8");
logger.debug(System.getProperty("line.separator") + s);
// JSONObject jsonObject = JSON.parseObject(s);
// JSONArray data = jsonObject.getJSONArray("data");
// logger.debug(System.getProperty("line.separator") + data);
// List<AdsAstXbjcptGetFctgDi> aas = JSONArray.parseArray(s, AdsAstXbjcptGetFctgDi.class);
}
} catch (Exception e) {
logger.error(Thread.currentThread().getName() + "链接" + apiName + "失败");
e.printStackTrace();
} finally {
try {
if (client != null) {
client.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return s;
}
@Override
public Map staticMethod(String token,String psrType, String fieldName,String fieldValue) {
String userUrl = "http://dwzyywzt.com/baseCenter/oauth2/accessToken";
Map<String, String> mapHeader = new HashMap<>();
mapHeader.put("Content-Type", "application/json; charset=utf-8");
mapHeader.put("x-token", token);
mapHeader.put("x-date", System.currentTimeMillis() + "");
mapHeader.put("x-signature", "123");
Map<String, Object> map1 = new HashMap<>();
map1.put("psrType", psrType);
map1.put("distribution", 1);
Map<String, Object> mapSubset = new HashMap<>();
mapSubset.put("perPage", 100);
//sql查询
List<Map<String, Object>> info =new ArrayList<>();
Map<String, Object> mapSubsets = new HashMap<>();
mapSubsets.put("fieldName", fieldName);
mapSubsets.put("compare", "in");
mapSubsets.put("fieldValue",fieldValue);
info.add(mapSubsets);
mapSubset.put("filters", info);
map1.put("params", mapSubset);
ResponseEntity<Map> userEntity = RestTemplateUtil.post(userUrl, mapHeader, map1, Map.class, new ArrayList<>());
return userEntity.getBody();
}
}