快递客户端 review 修改

This commit is contained in:
jason 2023-06-15 14:59:31 +08:00
parent e69e6d880b
commit 1662d17fc9
28 changed files with 351 additions and 359 deletions

View File

@ -51,9 +51,10 @@ public interface ErrorCodeConstants {
ErrorCode EXPRESS_CODE_DUPLICATE = new ErrorCode(1011003001, "已经存在该编码的快递公司");
ErrorCode EXPRESS_TEMPLATE_NOT_EXISTS = new ErrorCode(1011003002, "运费模板不存在");
ErrorCode EXPRESS_TEMPLATE_NAME_DUPLICATE = new ErrorCode(1011003003, "已经存在该运费模板名");
ErrorCode EXPRESS_API_QUERY_ERROR = new ErrorCode(1011003006, "快递查询接口异常");
ErrorCode EXPRESS_API_QUERY_FAILED = new ErrorCode(1011003007, "快递查询返回失败, 原因:{}");
ErrorCode PICK_UP_STORE_NOT_EXISTS = new ErrorCode(1011003008, "自提门店不存在");
ErrorCode EXPRESS_API_QUERY_ERROR = new ErrorCode(1011003004, "快递查询接口异常");
ErrorCode EXPRESS_API_QUERY_FAILED = new ErrorCode(1011003005, "快递查询返回失败, 原因:{}");
ErrorCode EXPRESS_CLIENT_NOT_PROVIDE = new ErrorCode(1011003006, "需要接入快递服务商,比如【快递100】");
ErrorCode PICK_UP_STORE_NOT_EXISTS = new ErrorCode(1011003007, "自提门店不存在");
// ========== Price 相关 1011004000 ============
ErrorCode PRICE_CALCULATE_PAY_PRICE_ILLEGAL = new ErrorCode(1011004000, "支付价格计算异常,原因:价格小于等于 0");

View File

@ -0,0 +1,31 @@
package cn.iocoder.yudao.module.trade.framework.delivery.config;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientFactory;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl.ExpressClientFactoryImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* 快递客户端端配置类, 提供快递客户端工厂默认的快递客户端实现
*
* @author jason
*/
@Configuration(
proxyBeanMethods = false
)
public class ExpressClientConfig {
@Bean
public ExpressClientFactory expressClientFactory(TradeExpressProperties tradeExpressProperties,
RestTemplate restTemplate) {
return new ExpressClientFactoryImpl(tradeExpressProperties, restTemplate);
}
@Bean
public ExpressClient defaultExpressClient(ExpressClientFactory expressClientFactory) {
return expressClientFactory.getDefaultExpressClient();
}
}

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.module.trade.framework.delivery.config;
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderEnum;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientEnum;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ -9,27 +9,24 @@ import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
// TODO @jasonTradeExpressProperties更通用哈
// TODO @芋艿未来要不要放数据库中考虑 saas 多租户时不同租户使用不同的配置
/**
* 交易快递查询的配置项
* 交易运费快递的配置项
*
* @author jason
*/
@Component
@ConfigurationProperties(prefix = "yudao.trade.express.query")
@ConfigurationProperties(prefix = "yudao.trade.express")
@Data
@Validated
public class TradeExpressQueryProperties {
public class TradeExpressProperties {
/**
* 快递查询服务商
* 快递客户端
*
* 如果未配置默认使用快递鸟
* 默认不提供需要提醒用户配置一个快递服务商
*/
// TODO @jason可以把 expressQueryProvider 改成 client 变量更简洁一点
private ExpressQueryProviderEnum expressQueryProvider; // TODO @jaosn默认值可以通过属性直接赋值哈
// TODO @jason需要考虑下用户只配置了其中一个
private ExpressClientEnum client = ExpressClientEnum.NOT_PROVIDE;
/**
* 快递鸟配置
*/

View File

@ -1,24 +0,0 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;
import java.util.List;
// TODO @jason可以改成 ExpressClient未来可能还对接别的接口噢
/**
* 快递查询客户端
*
* @author jason
*/
public interface ExpressQueryClient {
/**
* 快递实时查询
*
* @param reqDTO 查询请求参数
*/
// TODO @jason可以改成 getExpressTrackList返回字段可以参考 https://doc.youzanyun.com/detail/API/0/5 响应的 data
List<ExpressQueryRespDTO> realTimeQuery(ExpressQueryReqDTO reqDTO);
}

View File

@ -1,22 +0,0 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;
import java.util.List;
/**
* 快递查询服务商
*
* @author jason
*/
public interface ExpressQueryProvider {
/**
* 快递实时查询
*
* @param reqDTO 查询请求参数
*/
List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO);
}

View File

@ -1,19 +0,0 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core;
/**
* 快递服务商工厂用于创建和缓存快递服务商服务
*
* @author jason
*/
public interface ExpressQueryProviderFactory {
/**
* 通过枚举获取快递查询服务商
*
* 如果不存在就创建一个对应的快递查询服务商
*
* @param queryProviderEnum 快递服务商枚举
*/
ExpressQueryProvider getOrCreateExpressQueryProvider(ExpressQueryProviderEnum queryProviderEnum);
}

View File

@ -0,0 +1,23 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.client;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO;
import java.util.List;
/**
* 快递客户端接口
*
* @author jason
*/
public interface ExpressClient {
/**
* 快递实时查询
*
* @param reqDTO 查询请求参数
*/
// TODO @jason返回字段可以参考 https://doc.youzanyun.com/detail/API/0/5 响应的 data
List<ExpressQueryRespDTO> getExpressTrackList(ExpressQueryReqDTO reqDTO);
}

View File

@ -1,16 +1,15 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core;
package cn.iocoder.yudao.module.trade.framework.delivery.core.client;
import lombok.Getter;
/**
* 快递查询服务商枚举
* 快递客户端枚举
*
* @author jason
*/
@Getter
public enum ExpressQueryProviderEnum {
public enum ExpressClientEnum {
NOT_PROVIDE("not-provide","未提供"),
KD_NIAO("kd-niao", "快递鸟"),
KD_100("kd-100", "快递100");
@ -24,10 +23,8 @@ public enum ExpressQueryProviderEnum {
*/
private final String name;
// TODO @jaosn@AllArgsConstructor 可以替代哈
ExpressQueryProviderEnum(String code, String name) {
ExpressClientEnum(String code, String name) {
this.code = code;
this.name = name;
}
}

View File

@ -0,0 +1,23 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.client;
/**
* 快递客户端工厂接口快递客户端工厂用于创建和缓存快递客户端
*
* @author jason
*/
public interface ExpressClientFactory {
/**
* 获取默认的快递客户端
*/
ExpressClient getDefaultExpressClient();
/**
* 通过枚举获取快递客户端如果不存在就创建一个对应快递客户端
*
* @param clientEnum 快递客户端枚举
*/
ExpressClient getOrCreateExpressClient(ExpressClientEnum clientEnum);
}

View File

@ -0,0 +1,27 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.convert;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryRespDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryRespDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface ExpressQueryConvert {
ExpressQueryConvert INSTANCE = Mappers.getMapper(ExpressQueryConvert.class);
List<ExpressQueryRespDTO> convertList(List<KdNiaoExpressQueryRespDTO.ExpressTrack> expressTrackList);
List<ExpressQueryRespDTO> convertList2(List<Kd100ExpressQueryRespDTO.ExpressTrack> expressTrackList);
KdNiaoExpressQueryReqDTO convert(ExpressQueryReqDTO dto);
Kd100ExpressQueryReqDTO convert2(ExpressQueryReqDTO dto);
}

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.dto;
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
import lombok.Data;
@ -16,8 +16,7 @@ public class ExpressQueryReqDTO {
*
* 对应 {@link DeliveryExpressDO#getCode()}
*/
// TODO @jaosn要不改成 expressCode项目里使用这个哈
private String expressCompanyCode;
private String expressCode;
/**
* 发货快递单号

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.dto;
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100;
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
@ -13,12 +13,11 @@ import lombok.Data;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Kd100ExpressQueryReqDTO {
// TODO @jaosn要不改成 expressCode项目里使用这个哈
/**
* 快递公司编码
*/
@JsonProperty("com")
private String expressCompanyCode;
private String expressCode;
/**
* 快递单号

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100;
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao;
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
@ -13,12 +13,11 @@ import lombok.Data;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class KdNiaoExpressQueryReqDTO {
// TODO @jaosn要不改成 expressCode项目里使用这个哈
/**
* 快递公司编码
*/
@JsonProperty("ShipperCode")
private String expressCompanyCode;
private String expressCode;
/**
* 快递单号
*/

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao;
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

View File

@ -0,0 +1,56 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientEnum;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientFactory;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 快递客户端工厂实现类
*
* @author jason
*/
public class ExpressClientFactoryImpl implements ExpressClientFactory {
private final Map<ExpressClientEnum, ExpressClient> clientMap = new ConcurrentHashMap<>(8);
private final TradeExpressProperties tradeExpressProperties;
private final RestTemplate restTemplate;
public ExpressClientFactoryImpl(TradeExpressProperties tradeExpressProperties,
RestTemplate restTemplate) {
this.tradeExpressProperties = tradeExpressProperties;
this.restTemplate = restTemplate;
}
@Override
public ExpressClient getDefaultExpressClient() {
ExpressClient defaultClient = getOrCreateExpressClient(tradeExpressProperties.getClient());
Assert.notNull("默认的快递客户端不能为空");
return defaultClient;
}
@Override
public ExpressClient getOrCreateExpressClient(ExpressClientEnum clientEnum) {
return clientMap.computeIfAbsent(clientEnum,
client -> createExpressClient(client, tradeExpressProperties));
}
private ExpressClient createExpressClient(ExpressClientEnum queryProviderEnum,
TradeExpressProperties tradeExpressProperties) {
switch (queryProviderEnum) {
case NOT_PROVIDE:
return new NoProvideExpressClient();
case KD_NIAO:
return new KdNiaoExpressClient(restTemplate, tradeExpressProperties.getKdNiao());
case KD_100:
return new Kd100ExpressClient(restTemplate, tradeExpressProperties.getKd100());
}
return null;
}
}

View File

@ -1,15 +1,16 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProvider;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100.Kd100ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100.Kd100ExpressQueryRespDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryRespDTO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
@ -23,48 +24,41 @@ import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_ERROR;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_FAILED;
import static cn.iocoder.yudao.module.trade.framework.delivery.core.convert.ExpressQueryConvert.INSTANCE;
import static cn.iocoder.yudao.module.trade.framework.delivery.core.client.convert.ExpressQueryConvert.INSTANCE;
// TODO @jason可以参考 KdNiaoExpressQueryProvider 建议改改哈
/**
* 快递 100 服务商
* 快递 100 客户端
*
* @author jason
*/
@Slf4j
public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
@AllArgsConstructor
public class Kd100ExpressClient implements ExpressClient {
private static final String REAL_TIME_QUERY_URL = "https://poll.kuaidi100.com/poll/query.do";
private final RestTemplate restTemplate;
private final TradeExpressQueryProperties.Kd100Config config;
public Kd100ExpressQueryProvider(RestTemplate restTemplate, TradeExpressQueryProperties.Kd100Config config) {
this.restTemplate = restTemplate;
this.config = config;
}
private final TradeExpressProperties.Kd100Config config;
@Override
public List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO) {
public List<ExpressQueryRespDTO> getExpressTrackList(ExpressQueryReqDTO reqDTO) {
// 发起查询
Kd100ExpressQueryReqDTO kd100ReqParam = INSTANCE.convert2(reqDTO);
kd100ReqParam.setExpressCompanyCode(kd100ReqParam.getExpressCompanyCode().toLowerCase()); // 快递公司编码需要转成小写
Kd100ExpressQueryRespDTO respDTO = sendExpressQueryReq(REAL_TIME_QUERY_URL, kd100ReqParam,
kd100ReqParam.setExpressCode(kd100ReqParam.getExpressCode().toLowerCase()); // 快递公司编码需要转成小写
Kd100ExpressQueryRespDTO respDTO = requestExpressQuery(REAL_TIME_QUERY_URL, kd100ReqParam,
Kd100ExpressQueryRespDTO.class);
log.debug("[realTimeQueryExpress][快递 100 接口 查询接口返回 {}]", respDTO);
log.debug("[getExpressTrackList][快递 100 接口 查询接口返回 {}]", respDTO);
// 处理结果
if (Objects.equals("false", respDTO.getResult())) {
log.error("[realTimeQueryExpress][快递 100 接口 返回失败 {}]", respDTO.getMessage());
log.error("[getExpressTrackList][快递 100 接口 返回失败 {}]", respDTO.getMessage());
throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getMessage());
// TODO @jsonelse 可以不用写哈
} else {
// TODO @jasonconvertList2 如果空应该返回 list
if (CollUtil.isNotEmpty(respDTO.getTracks())) {
return INSTANCE.convertList2(respDTO.getTracks());
} else {
return Collections.emptyList();
}
}
// TODO @jasonconvertList2 如果空应该返回 list @芋艿 为了避免返回 null
if (CollUtil.isNotEmpty(respDTO.getTracks())) {
return INSTANCE.convertList2(respDTO.getTracks());
} else {
return Collections.emptyList();
}
}
/**
@ -76,8 +70,7 @@ public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
* @param <Req> 每个请求的请求结构 Req DTO
* @param <Resp> 每个请求的响应结构 Resp DTO
*/
// TODO @jason可以改成 request发起请求哈
private <Req, Resp> Resp sendExpressQueryReq(String url, Req req, Class<Resp> respClass) {
private <Req, Resp> Resp requestExpressQuery(String url, Req req, Class<Resp> respClass) {
// 请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
@ -92,23 +85,20 @@ public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
log.debug("[sendExpressQueryReq][快递 100 接口的请求参数: {}]", requestBody);
// 发送请求
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
// TODO @jason可以使用 restTemplate post 方法哇
// TODO @jason可以使用 restTemplate post 方法哇 @芋艿 为了获取接口的原始返回用exchange 便于查问题
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
log.debug("[sendExpressQueryReq][快递 100 接口响应结果 {}]", responseEntity);
// 处理响应
// TODO @jasonif return 原则if (!responseEntity.getStatusCode().is2xxSuccessful()) 抛出异常接着处理成功的
if (responseEntity.getStatusCode().is2xxSuccessful()) {
String response = responseEntity.getBody();
return JsonUtils.parseObject(response, respClass);
} else {
if (!responseEntity.getStatusCode().is2xxSuccessful()) {
throw exception(EXPRESS_API_QUERY_ERROR);
}
return JsonUtils.parseObject(responseEntity.getBody(), respClass);
}
private String generateReqSign(String param, String key, String customer) {
String plainText = String.format("%s%s%s", param, key, customer);
// TODO @jasonDigestUtil.md5Hex(plainText);
// TODO @芋艿 这里需要转换成大写 没有对应方法
return HexUtil.encodeHexStr(DigestUtil.md5(plainText), false);
}

View File

@ -1,16 +1,17 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.net.URLEncodeUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProvider;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao.KdNiaoExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao.KdNiaoExpressQueryRespDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryRespDTO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
@ -23,15 +24,16 @@ import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_FAILED;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_ERROR;
import static cn.iocoder.yudao.module.trade.framework.delivery.core.convert.ExpressQueryConvert.INSTANCE;
import static cn.iocoder.yudao.module.trade.framework.delivery.core.client.convert.ExpressQueryConvert.INSTANCE;
/**
* 快递鸟服务商
* 快递鸟客户端
*
* @author jason
*/
@Slf4j
public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
@AllArgsConstructor
public class KdNiaoExpressClient implements ExpressClient {
private static final String REAL_TIME_QUERY_URL = "https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
@ -39,15 +41,8 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
* 快递鸟即时查询免费版 RequestType
*/
private static final String REAL_TIME_FREE_REQ_TYPE = "1002";
private final RestTemplate restTemplate;
private final TradeExpressQueryProperties.KdNiaoConfig config;
// TODO @jason可以改成 lombok
public KdNiaoExpressQueryProvider(RestTemplate restTemplate, TradeExpressQueryProperties.KdNiaoConfig config) {
this.restTemplate = restTemplate;
this.config = config;
}
private final TradeExpressProperties.KdNiaoConfig config;
/**
* 快递鸟即时查询免费版本
@ -56,21 +51,20 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
* @param reqDTO 查询请求参数
*/
@Override
public List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO) {
public List<ExpressQueryRespDTO> getExpressTrackList(ExpressQueryReqDTO reqDTO) {
KdNiaoExpressQueryReqDTO kdNiaoReqData = INSTANCE.convert(reqDTO);
// 快递公司编码需要转成大写
kdNiaoReqData.setExpressCompanyCode(reqDTO.getExpressCompanyCode().toUpperCase());
KdNiaoExpressQueryRespDTO respDTO = sendKdNiaoApiRequest(REAL_TIME_QUERY_URL, REAL_TIME_FREE_REQ_TYPE,
kdNiaoReqData.setExpressCode(reqDTO.getExpressCode().toUpperCase());
KdNiaoExpressQueryRespDTO respDTO = requestKdNiaoApi(REAL_TIME_QUERY_URL, REAL_TIME_FREE_REQ_TYPE,
kdNiaoReqData, KdNiaoExpressQueryRespDTO.class);
log.debug("[realTimeQueryExpress][快递鸟即时查询接口返回 {}]", respDTO);
if(!respDTO.getSuccess()){
throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getReason());
}else{
if (CollUtil.isNotEmpty(respDTO.getTracks())) {
return INSTANCE.convertList(respDTO.getTracks());
}else{
return Collections.emptyList();
}
log.debug("[getExpressTrackList][快递鸟即时查询接口返回 {}]", respDTO);
if (respDTO == null || !respDTO.getSuccess()) {
throw exception(EXPRESS_API_QUERY_FAILED, respDTO == null ? "" : respDTO.getReason());
}
if (CollUtil.isNotEmpty(respDTO.getTracks())) {
return INSTANCE.convertList(respDTO.getTracks());
} else {
return Collections.emptyList();
}
}
@ -83,8 +77,8 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
* @param <Req> 每个请求的请求结构 Req DTO
* @param <Resp> 每个请求的响应结构 Resp DTO
*/
private <Req, Resp> Resp sendKdNiaoApiRequest(String url, String requestType, Req req,
Class<Resp> respClass){
private <Req, Resp> Resp requestKdNiaoApi(String url, String requestType, Req req,
Class<Resp> respClass){
// 请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
@ -97,19 +91,16 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
requestBody.add("EBusinessID", config.getBusinessId());
requestBody.add("DataSign", dataSign);
requestBody.add("RequestType", requestType);
log.debug("[sendKdNiaoApiRequest][快递鸟接口 RequestType : {}, 的请求参数 {}]", requestType, requestBody);
log.debug("[requestKdNiaoApi][快递鸟接口 RequestType : {}, 的请求参数 {}]", requestType, requestBody);
// 发送请求
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
log.debug("快递鸟接口 RequestType : {}, 的响应结果 {}", requestType, responseEntity);
// 处理响应
if (responseEntity.getStatusCode().is2xxSuccessful()) {
String response = responseEntity.getBody();
return JsonUtils.parseObject(response, respClass);
} else {
if (!responseEntity.getStatusCode().is2xxSuccessful()) {
throw exception(EXPRESS_API_QUERY_ERROR);
}
return JsonUtils.parseObject(responseEntity.getBody(), respClass);
}
/**

View File

@ -0,0 +1,22 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_CLIENT_NOT_PROVIDE;
/**
* 未实现的快递客户端用来提醒用户需要接入快递服务商
*
* @author jason
*/
public class NoProvideExpressClient implements ExpressClient {
@Override
public List<ExpressQueryRespDTO> getExpressTrackList(ExpressQueryReqDTO reqDTO) {
throw exception(EXPRESS_CLIENT_NOT_PROVIDE);
}
}

View File

@ -1,27 +0,0 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.convert;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100.Kd100ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100.Kd100ExpressQueryRespDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao.KdNiaoExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao.KdNiaoExpressQueryRespDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface ExpressQueryConvert {
ExpressQueryConvert INSTANCE = Mappers.getMapper(ExpressQueryConvert.class);
List<ExpressQueryRespDTO> convertList(List<KdNiaoExpressQueryRespDTO.ExpressTrack> expressTrackList);
List<ExpressQueryRespDTO> convertList2(List<Kd100ExpressQueryRespDTO.ExpressTrack> expressTrackList);
KdNiaoExpressQueryReqDTO convert(ExpressQueryReqDTO dto);
Kd100ExpressQueryReqDTO convert2(ExpressQueryReqDTO dto);
}

View File

@ -1,65 +0,0 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;
import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryClient;
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProvider;
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderEnum;
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderFactory;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import static cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderEnum.KD_NIAO;
// TODO @jason可以把整体包结构调整下参考 sms client 的方式
// + config
// + core
// client
// + dto
// + impl里面可以放 kdniaoclientkd100client
// ExpressClient
// ExpressClientFactory: 通过它直接获取默认和创建默认的 Client
// enums
/**
* 快递查询客户端实现
*
* @author jason
*/
@Component
@Slf4j
public class ExpressQueryClientImpl implements ExpressQueryClient {
@Resource
private ExpressQueryProviderFactory expressQueryProviderFactory;
@Resource
private TradeExpressQueryProperties tradeExpressQueryProperties;
private ExpressQueryProvider expressQueryProvider;
@PostConstruct
private void init() {
// 如果未设置默认使用快递鸟
ExpressQueryProviderEnum queryProvider = tradeExpressQueryProperties.getExpressQueryProvider();
if (queryProvider == null) {
queryProvider = KD_NIAO;
}
// 创建客户端
expressQueryProvider = expressQueryProviderFactory.getOrCreateExpressQueryProvider(queryProvider);
if (expressQueryProvider == null) {
log.error("获取创建快递查询服务商{}失败,请检查相关配置", queryProvider);
}
Assert.notNull(expressQueryProvider, "快递查询服务商不能为空");
}
@Override
public List<ExpressQueryRespDTO> realTimeQuery(ExpressQueryReqDTO reqDTO) {
return expressQueryProvider.realTimeQueryExpress(reqDTO);
}
}

View File

@ -1,48 +0,0 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProvider;
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderEnum;
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* // TODO @jason注释不全
* @author jason
*/
@Component
public class ExpressQueryProviderFactoryImpl implements ExpressQueryProviderFactory {
private final Map<ExpressQueryProviderEnum, ExpressQueryProvider> providerMap = new ConcurrentHashMap<>(8);
@Resource
private TradeExpressQueryProperties tradeExpressQueryProperties;
@Resource
private RestTemplate restTemplate;
@Override
public ExpressQueryProvider getOrCreateExpressQueryProvider(ExpressQueryProviderEnum queryProviderEnum) {
return providerMap.computeIfAbsent(queryProviderEnum,
provider -> createExpressQueryProvider(provider, tradeExpressQueryProperties));
}
private ExpressQueryProvider createExpressQueryProvider(ExpressQueryProviderEnum queryProviderEnum,
TradeExpressQueryProperties tradeExpressQueryProperties) {
// TODO @jason是不是直接 return 就好啦更简洁一点
ExpressQueryProvider result = null;
switch (queryProviderEnum) {
case KD_NIAO:
result = new KdNiaoExpressQueryProvider(restTemplate, tradeExpressQueryProperties.getKdNiao());
break;
case KD_100:
result = new Kd100ExpressQueryProvider(restTemplate, tradeExpressQueryProperties.getKd100());
break;
}
return result;
}
}

View File

@ -1,8 +1,8 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@ -21,36 +21,36 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* @author jason
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = Kd100ExpressQueryProviderTest.Application.class)
@ActiveProfiles("trade-delivery-query") // 设置使用 trade-delivery-query 配置文件
public class Kd100ExpressQueryProviderTest {
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = Kd100ExpressClientTest.Application.class)
@ActiveProfiles("unit-test") // 设置使用 trade-delivery-query 配置文件
public class Kd100ExpressClientTest {
@Resource
private RestTemplateBuilder builder;
@Resource
private TradeExpressQueryProperties expressQueryProperties;
private TradeExpressProperties expressQueryProperties;
private Kd100ExpressQueryProvider kd100ExpressQueryProvider;
private Kd100ExpressClient kd100ExpressClient;
@BeforeEach
public void init(){
kd100ExpressQueryProvider = new Kd100ExpressQueryProvider(builder.build(),expressQueryProperties.getKd100());
kd100ExpressClient = new Kd100ExpressClient(builder.build(),expressQueryProperties.getKd100());
}
@Test
@Disabled("需要 授权 key. 暂时忽略")
void testRealTimeQueryExpressFailed() {
ServiceException t = assertThrows(ServiceException.class, () -> {
ExpressQueryReqDTO reqDTO = new ExpressQueryReqDTO();
reqDTO.setExpressCompanyCode("yto");
reqDTO.setExpressCode("yto");
reqDTO.setLogisticsNo("YT9383342193097");
kd100ExpressQueryProvider.realTimeQueryExpress(reqDTO);
kd100ExpressClient.getExpressTrackList(reqDTO);
});
assertEquals(1011003007, t.getCode());
assertEquals(1011003005, t.getCode());
}
@Import({
RestTemplateAutoConfiguration.class
})
@EnableConfigurationProperties(TradeExpressQueryProperties.class)
@EnableConfigurationProperties(TradeExpressProperties.class)
public static class Application {
}
}

View File

@ -1,8 +1,8 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@ -21,35 +21,35 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* @author jason
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = KdNiaoExpressQueryProviderTest.Application.class)
@ActiveProfiles("trade-delivery-query") // 设置使用 trade-delivery-query 配置文件 TODO @jason可以直接写到 application-unit-test.yaml 配置文件里
public class KdNiaoExpressQueryProviderTest {
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = KdNiaoExpressClientTest.Application.class)
@ActiveProfiles("unit-test")
public class KdNiaoExpressClientTest {
@Resource
private RestTemplateBuilder builder;
@Resource
private TradeExpressQueryProperties expressQueryProperties;
private TradeExpressProperties expressQueryProperties;
private KdNiaoExpressQueryProvider kdNiaoExpressQueryProvider;
private KdNiaoExpressClient kdNiaoExpressClient;
@BeforeEach
public void init(){
kdNiaoExpressQueryProvider = new KdNiaoExpressQueryProvider(builder.build(),expressQueryProperties.getKdNiao());
kdNiaoExpressClient = new KdNiaoExpressClient(builder.build(),expressQueryProperties.getKdNiao());
}
@Test
@Disabled("需要 授权 key. 暂时忽略")
void testRealTimeQueryExpressFailed() {
assertThrows(ServiceException.class,() ->{
ExpressQueryReqDTO reqDTO = new ExpressQueryReqDTO();
reqDTO.setExpressCompanyCode("yy");
reqDTO.setExpressCode("yy");
reqDTO.setLogisticsNo("YT9383342193097");
kdNiaoExpressQueryProvider.realTimeQueryExpress(reqDTO);
kdNiaoExpressClient.getExpressTrackList(reqDTO);
});
}
@Import({
RestTemplateAutoConfiguration.class
})
@EnableConfigurationProperties(TradeExpressQueryProperties.class)
@EnableConfigurationProperties(TradeExpressProperties.class)
public static class Application {
}
}

View File

@ -0,0 +1,52 @@
package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.trade.framework.delivery.config.ExpressClientConfig;
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* @author jason
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = NoProvideExpressClientTest.Application.class)
@ActiveProfiles("unit-test") // 设置使用 trade-delivery-query 配置文件
@Import({ExpressClientConfig.class})
public class NoProvideExpressClientTest {
@Resource
private ExpressClient expressClient;
@Test
void getExpressTrackList() {
ServiceException t = assertThrows(ServiceException.class, () -> {
expressClient.getExpressTrackList(null);
});
assertEquals(1011003006, t.getCode());
}
@Import({
RestTemplateAutoConfiguration.class,
})
@EnableConfigurationProperties(TradeExpressProperties.class)
public static class Application {
@Bean
private RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}
}

View File

@ -1,18 +0,0 @@
spring:
main:
lazy-initialization: true # 开启懒加载,加快速度
banner-mode: off # 单元测试,禁用 Banner
--- #################### 交易快递查询相关配置 ####################
yudao:
trade:
express:
query:
express-query-provider: kd_niao
kd-niao:
api-key: xxx
business-id: xxxxxxxx
kd100:
customer: xxxx
key: xxxxx

View File

@ -51,3 +51,11 @@ yudao:
order:
app-id: 1
merchant-order-id: 1
express:
kd-niao:
api-key: xxxx
business-id: xxxxx
kd100:
customer: xxxxx
key: xxxxx
client: not_provide