作者 [wgf]

DEMO接入升级

package com.aukey.example.constant;
/**
* @author: wgf
* @create: 2020-05-21 10:36
* @description: 这里面配置的API一定要是授权过的,否则获取不了数据
**/
public interface DwApi {
/**
* 汇率API
*/
String CURRENCY_SET_API = "/base/currency_set";
/**
* FBA-库存快照
*/
String FULFILLMENT_CURRENT_INVENTORY = "/stock/fulfillment_current_inventory";
}
package com.aukey.example.constant;
/**
* @author: wgf
* @create: 2020-05-21 10:36
* @description: 这里面配置的API一定要是授权过的,否则获取不了数据
**/
public enum ServiceCodeEnum {
logistics_stock_id_mapping("11101000");
private String serviceCode;
ServiceCodeEnum(String serviceCode) {
this.serviceCode = serviceCode;
}
public String getServiceCode() {
return serviceCode;
}
}
... ...
package com.aukey.example.entity;
import java.math.BigDecimal;
import java.util.Date;
/**
* @author: wgf
* @create: 2020-05-13 14:38
* @description: 汇率实体
**/
public class CurrencySet {
private Integer currencyId;
private String currencyCode;
private String currencyName;
private BigDecimal currencyRate;
private String baseCurrency;
private Integer createBy;
private Date createTime;
private String updateBy;
private Date updateTime;
private String dataStatus;
private Integer auditBy;
private String auditStatus;
private Date auditTime;
private String currencySymbol;
public Integer getCurrencyId() {
return currencyId;
}
public void setCurrencyId(Integer currencyId) {
this.currencyId = currencyId;
}
public String getCurrencyCode() {
return currencyCode;
}
public void setCurrencyCode(String currencyCode) {
this.currencyCode = currencyCode;
}
public String getCurrencyName() {
return currencyName;
}
public void setCurrencyName(String currencyName) {
this.currencyName = currencyName;
}
public BigDecimal getCurrencyRate() {
return currencyRate;
}
public void setCurrencyRate(BigDecimal currencyRate) {
this.currencyRate = currencyRate;
}
public String getBaseCurrency() {
return baseCurrency;
}
public void setBaseCurrency(String baseCurrency) {
this.baseCurrency = baseCurrency;
}
public Integer getCreateBy() {
return createBy;
}
public void setCreateBy(Integer createBy) {
this.createBy = createBy;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getUpdateBy() {
return updateBy;
}
public void setUpdateBy(String updateBy) {
this.updateBy = updateBy;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getDataStatus() {
return dataStatus;
}
public void setDataStatus(String dataStatus) {
this.dataStatus = dataStatus;
}
public Integer getAuditBy() {
return auditBy;
}
public void setAuditBy(Integer auditBy) {
this.auditBy = auditBy;
}
public String getAuditStatus() {
return auditStatus;
}
public void setAuditStatus(String auditStatus) {
this.auditStatus = auditStatus;
}
public Date getAuditTime() {
return auditTime;
}
public void setAuditTime(Date auditTime) {
this.auditTime = auditTime;
}
public String getCurrencySymbol() {
return currencySymbol;
}
public void setCurrencySymbol(String currencySymbol) {
this.currencySymbol = currencySymbol;
}
}
package com.aukey.example.entity;
/**
* @author: wgf
* @create: 2020-05-13 14:38
* @description:
**/
public class LogisticsStockIdMapping {
private Integer id;
private Integer baiyiStockId;
private Integer logisticsStockId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getBaiyiStockId() {
return baiyiStockId;
}
public void setBaiyiStockId(Integer baiyiStockId) {
this.baiyiStockId = baiyiStockId;
}
public Integer getLogisticsStockId() {
return logisticsStockId;
}
public void setLogisticsStockId(Integer logisticsStockId) {
this.logisticsStockId = logisticsStockId;
}
}
... ...
... ... @@ -3,6 +3,7 @@ package com.aukey.example.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONReader;
import com.alibaba.fastjson.TypeReference;
import com.aukey.example.constant.ServiceCodeEnum;
import com.aukey.example.vo.DwParamVo;
import com.aukey.example.web.TokenController;
import okhttp3.OkHttpClient;
... ... @@ -46,13 +47,14 @@ public class DwHelperUtil {
/**
* http请求默认超时为7200秒
* 可根据同步数据大小自行设置超时时间
*
* @return
*/
public static OkHttpClient getClient() {
if (client == null) {
synchronized (DwHelperUtil.class) {
if (client == null) {
client = new OkHttpClient.Builder()
client = new OkHttpClient.Builder()
.callTimeout(60 * 60 * 2, TimeUnit.SECONDS)
.writeTimeout(60 * 2, TimeUnit.SECONDS)
.readTimeout(60 * 2, TimeUnit.SECONDS)
... ... @@ -133,13 +135,13 @@ public class DwHelperUtil {
/**
* @param dwDoMain 数据仓库域名
* @param api 数据仓库申请的API
* @param params 参数
* @param dwDoMain 数据仓库域名
* @param serviceCodeEnum 服务编码
* @param params 参数
* @return
*/
public static String doGet(String dwDoMain, String api, Map<String, Object> params) {
return doGet(dwDoMain + api, params);
public static String doGet(String dwDoMain, ServiceCodeEnum serviceCodeEnum, Map<String, Object> params) {
return doGet(String.format("%s/%s", dwDoMain, serviceCodeEnum.getServiceCode()), params);
}
... ... @@ -188,16 +190,16 @@ public class DwHelperUtil {
/**
* 分页查询
*
* @param callBack 回调方法,需要返回总页数
* @param dwDataApi dw获取数据接口
* @param api 申请的api
* @param paramVo 请求参数
* @param typeReference json字符串转实体引用类型
* @param callBack 回调方法,需要返回总页数
* @param dwDataApi dw获取数据接口
* @param serviceCodeEnum 服务编码
* @param paramVo 请求参数
* @param typeReference json字符串转实体引用类型
* @param <T>
*/
public static <T> void pageReader(Function<T, Integer> callBack,
String dwDataApi,
String api,
ServiceCodeEnum serviceCodeEnum,
DwParamVo paramVo,
TypeReference<T> typeReference) {
... ... @@ -213,9 +215,9 @@ public class DwHelperUtil {
paramVo.setToken(TokenController.getCurrentToken());
paramVo.setPageNumber(currentPageNumber);
String jsonStr = DwHelperUtil.doGet(dwDataApi, api, paramVo.toMap());
String jsonStr = DwHelperUtil.doGet(dwDataApi, serviceCodeEnum, paramVo.toMap());
if (StringUtils.isEmpty(jsonStr)) {
throw new RuntimeException(String.format("API %s 调用失败", dwDataApi + api));
throw new RuntimeException(String.format("API %s 调用失败", serviceCodeEnum.getServiceCode()));
}
// json解析为对象
... ... @@ -228,7 +230,7 @@ public class DwHelperUtil {
totalPageNum = calculatePage(total, paramVo.getPageSize());
}
log.info("========== 获取API:{} 第:{}页数据 ==========", dwDataApi + api, currentPageNumber);
log.info("========== 获取API:{} 第:{}页数据 ==========", serviceCodeEnum.getServiceCode(), currentPageNumber);
} while (currentPageNumber < totalPageNum);
}
... ... @@ -255,21 +257,22 @@ public class DwHelperUtil {
/**
* 流式读取
*
* @param dwDataApi dw数据请求接口
* @param api 平台申请的api
* @param paramMap 参数
* @param callback 业务回调
* @param batchSize 业务回调数据量大小,参考值 [500,5000]
* @param constructor 实体的构造函数
* @param dwDataApi dw数据请求接口
* @param serviceCodeEnum 服务编码
* @param paramMap 参数
* @param callback 业务回调
* @param batchSize 业务回调数据量大小,参考值 [500,5000]
* @param constructor 实体的构造函数
* @throws Exception
*/
public static <T> void streamReader(String dwDataApi, String api,
public static <T> void streamReader(String dwDataApi,
ServiceCodeEnum serviceCodeEnum,
Map<String, Object> paramMap,
Consumer<List<T>> callback,
int batchSize,
Supplier<T> constructor) throws Exception {
dwDataApi = dwDataApi + api;
dwDataApi = String.format("%s/%s", dwDataApi, serviceCodeEnum.getServiceCode());
Response response = null;
InputStream is = null;
GZIPInputStream gzipInputStream = null;
... ...
... ... @@ -2,9 +2,9 @@ package com.aukey.example.web;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.aukey.example.constant.DwApi;
import com.aukey.example.entity.CurrencySet;
import com.aukey.example.constant.ServiceCodeEnum;
import com.aukey.example.entity.FulfillmentCurrentInventory;
import com.aukey.example.entity.LogisticsStockIdMapping;
import com.aukey.example.util.DwHelperUtil;
import com.aukey.example.vo.DwParamVo;
import com.aukey.example.vo.DwResultVo;
... ... @@ -60,19 +60,19 @@ public class TestApiController {
DwParamVo paramVo = new DwParamVo(this.appId, TokenController.getCurrentToken());
// 添加查询条件
paramVo.setQueryCondition("WHERE currency_code = 'USD'");
paramVo.setQueryCondition("WHERE id = 1");
// 每页数据条数 dw服务端有限制 取值[1, 3000]
paramVo.setPageSize(3000);
paramVo.setDataStructure(DwParamVo.OBJ);
// 调用API
String result = DwHelperUtil.doGet(dwDataApi, DwApi.CURRENCY_SET_API, paramVo.toMap());
String result = DwHelperUtil.doGet(dwDataApi, ServiceCodeEnum.logistics_stock_id_mapping, paramVo.toMap());
if (StringUtils.isEmpty(result)) {
throw new RuntimeException(String.format("API %s 调用失败", DwApi.CURRENCY_SET_API));
throw new RuntimeException(String.format("API %s 调用失败", ServiceCodeEnum.logistics_stock_id_mapping.getServiceCode()));
}
// json解析为对象
DwResultVo<ObjVo<CurrencySet>> resultVo = JSON.parseObject(result, new TypeReference<DwResultVo<ObjVo<CurrencySet>>>() {
DwResultVo<ObjVo<LogisticsStockIdMapping>> resultVo = JSON.parseObject(result, new TypeReference<DwResultVo<ObjVo<LogisticsStockIdMapping>>>() {
});
log.info("");
... ... @@ -91,16 +91,16 @@ public class TestApiController {
DwParamVo paramVo = new DwParamVo(this.appId, TokenController.getCurrentToken());
// 添加查询条件
paramVo.setQueryCondition("WHERE currency_code = 'USD'");
paramVo.setQueryCondition("WHERE id < 1000");
// 每页数据条数 dw服务端有限制 取值[1, 3000]
paramVo.setPageSize(3000);
paramVo.setDataStructure(DwParamVo.SL);
// 调用API
String result = DwHelperUtil.doGet(dwDataApi, DwApi.CURRENCY_SET_API, paramVo.toMap());
String result = DwHelperUtil.doGet(dwDataApi, ServiceCodeEnum.logistics_stock_id_mapping, paramVo.toMap());
if (StringUtils.isEmpty(result)) {
throw new RuntimeException(String.format("API %s 调用失败", DwApi.CURRENCY_SET_API));
throw new RuntimeException(String.format("API %s 调用失败", ServiceCodeEnum.logistics_stock_id_mapping.getServiceCode()));
}
// json解析为对象
... ... @@ -127,17 +127,17 @@ public class TestApiController {
// 构造请求参数
DwParamVo paramVo = new DwParamVo(this.appId, TokenController.getCurrentToken());
// 每页数据条数 dw服务端有限制 取值[1, 3000]
paramVo.setPageSize(1000);
paramVo.setPageSize(100);
paramVo.setDataStructure(DwParamVo.OBJ);
// TODO 自定义查询条件
// json字符串转实体引用类型
TypeReference typeReference = new TypeReference<DwResultVo<ObjVo<CurrencySet>>>() {
TypeReference typeReference = new TypeReference<DwResultVo<ObjVo<LogisticsStockIdMapping>>>() {
};
// 回调函数
Function<DwResultVo<ObjVo<CurrencySet>>, Integer> callback = (DwResultVo<ObjVo<CurrencySet>> resultVo) -> {
List<CurrencySet> dataList = resultVo.getData().getData();
Function<DwResultVo<ObjVo<LogisticsStockIdMapping>>, Integer> callback = (DwResultVo<ObjVo<LogisticsStockIdMapping>> resultVo) -> {
List<LogisticsStockIdMapping> dataList = resultVo.getData().getData();
System.out.println(dataList.size());
// TODO 业务逻辑在这里实现
// ... more
... ... @@ -150,7 +150,7 @@ public class TestApiController {
// 使用分页API
DwHelperUtil.pageReader(callback,
dwDataApi,
DwApi.CURRENCY_SET_API,
ServiceCodeEnum.logistics_stock_id_mapping,
paramVo,
typeReference);
}
... ... @@ -167,7 +167,7 @@ public class TestApiController {
// 构造请求参数
DwParamVo paramVo = new DwParamVo(this.appId, TokenController.getCurrentToken());
// 每页数据条数 dw服务端有限制 取值[1, 3000]
paramVo.setPageSize(1000);
paramVo.setPageSize(100);
paramVo.setDataStructure(DwParamVo.SL);
// TODO 自定义查询条件
... ... @@ -190,7 +190,7 @@ public class TestApiController {
// 使用分页API
DwHelperUtil.pageReader(callback,
dwDataApi,
DwApi.CURRENCY_SET_API,
ServiceCodeEnum.logistics_stock_id_mapping,
paramVo,
typeReference);
}
... ... @@ -231,10 +231,10 @@ public class TestApiController {
// TODO 自定义查询条件
// 定义实体构造函数
Supplier<FulfillmentCurrentInventory> constructor = FulfillmentCurrentInventory::new;
Supplier<LogisticsStockIdMapping> constructor = LogisticsStockIdMapping::new;
// 流式读取回调函数
Consumer<List<FulfillmentCurrentInventory>> callBack = (List<FulfillmentCurrentInventory> list) -> {
Consumer<List<LogisticsStockIdMapping>> callBack = (List<LogisticsStockIdMapping> list) -> {
System.out.println(list.size());
// TODO 业务逻辑在这里实现
// ... more
... ... @@ -243,12 +243,12 @@ public class TestApiController {
// 指定回调函数数据大小,取值[1000, 3000]. 建议1000最为稳定
// 取值越大,数据读取占用的堆内存越高
int batchSize = 1000;
int batchSize = 100;
// 使用 StreamAPI
DwHelperUtil.streamReader(
dwDataApi,
DwApi.FULFILLMENT_CURRENT_INVENTORY,
ServiceCodeEnum.logistics_stock_id_mapping,
paramVo.toMap(),
callBack,
batchSize,
... ...