Commit 1662d17fc97c824e5373c28809e5f6b075538ec5

Authored by jason
1 parent e69e6d88

快递客户端 review 修改

Showing 26 changed files with 315 additions and 323 deletions
yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java
@@ -51,9 +51,10 @@ public interface ErrorCodeConstants { @@ -51,9 +51,10 @@ public interface ErrorCodeConstants {
51 ErrorCode EXPRESS_CODE_DUPLICATE = new ErrorCode(1011003001, "已经存在该编码的快递公司"); 51 ErrorCode EXPRESS_CODE_DUPLICATE = new ErrorCode(1011003001, "已经存在该编码的快递公司");
52 ErrorCode EXPRESS_TEMPLATE_NOT_EXISTS = new ErrorCode(1011003002, "运费模板不存在"); 52 ErrorCode EXPRESS_TEMPLATE_NOT_EXISTS = new ErrorCode(1011003002, "运费模板不存在");
53 ErrorCode EXPRESS_TEMPLATE_NAME_DUPLICATE = new ErrorCode(1011003003, "已经存在该运费模板名"); 53 ErrorCode EXPRESS_TEMPLATE_NAME_DUPLICATE = new ErrorCode(1011003003, "已经存在该运费模板名");
54 - ErrorCode EXPRESS_API_QUERY_ERROR = new ErrorCode(1011003006, "快递查询接口异常");  
55 - ErrorCode EXPRESS_API_QUERY_FAILED = new ErrorCode(1011003007, "快递查询返回失败, 原因:{}");  
56 - ErrorCode PICK_UP_STORE_NOT_EXISTS = new ErrorCode(1011003008, "自提门店不存在"); 54 + ErrorCode EXPRESS_API_QUERY_ERROR = new ErrorCode(1011003004, "快递查询接口异常");
  55 + ErrorCode EXPRESS_API_QUERY_FAILED = new ErrorCode(1011003005, "快递查询返回失败, 原因:{}");
  56 + ErrorCode EXPRESS_CLIENT_NOT_PROVIDE = new ErrorCode(1011003006, "需要接入快递服务商,比如【快递100】");
  57 + ErrorCode PICK_UP_STORE_NOT_EXISTS = new ErrorCode(1011003007, "自提门店不存在");
57 58
58 // ========== Price 相关 1011004000 ============ 59 // ========== Price 相关 1011004000 ============
59 ErrorCode PRICE_CALCULATE_PAY_PRICE_ILLEGAL = new ErrorCode(1011004000, "支付价格计算异常,原因:价格小于等于 0"); 60 ErrorCode PRICE_CALCULATE_PAY_PRICE_ILLEGAL = new ErrorCode(1011004000, "支付价格计算异常,原因:价格小于等于 0");
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/config/ExpressClientConfig.java 0 → 100644
  1 +package cn.iocoder.yudao.module.trade.framework.delivery.config;
  2 +
  3 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
  4 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientFactory;
  5 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl.ExpressClientFactoryImpl;
  6 +import org.springframework.context.annotation.Bean;
  7 +import org.springframework.context.annotation.Configuration;
  8 +import org.springframework.web.client.RestTemplate;
  9 +
  10 +/**
  11 + * 快递客户端端配置类, 提供快递客户端工厂,默认的快递客户端实现
  12 + *
  13 + * @author jason
  14 + */
  15 +@Configuration(
  16 + proxyBeanMethods = false
  17 +)
  18 +public class ExpressClientConfig {
  19 +
  20 + @Bean
  21 + public ExpressClientFactory expressClientFactory(TradeExpressProperties tradeExpressProperties,
  22 + RestTemplate restTemplate) {
  23 + return new ExpressClientFactoryImpl(tradeExpressProperties, restTemplate);
  24 + }
  25 +
  26 + @Bean
  27 + public ExpressClient defaultExpressClient(ExpressClientFactory expressClientFactory) {
  28 + return expressClientFactory.getDefaultExpressClient();
  29 + }
  30 +
  31 +}
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/config/TradeExpressQueryProperties.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/config/TradeExpressProperties.java
1 package cn.iocoder.yudao.module.trade.framework.delivery.config; 1 package cn.iocoder.yudao.module.trade.framework.delivery.config;
2 2
3 -import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderEnum; 3 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientEnum;
4 import lombok.Data; 4 import lombok.Data;
5 import org.springframework.boot.context.properties.ConfigurationProperties; 5 import org.springframework.boot.context.properties.ConfigurationProperties;
6 import org.springframework.stereotype.Component; 6 import org.springframework.stereotype.Component;
@@ -9,27 +9,24 @@ import org.springframework.validation.annotation.Validated; @@ -9,27 +9,24 @@ import org.springframework.validation.annotation.Validated;
9 import javax.validation.Valid; 9 import javax.validation.Valid;
10 import javax.validation.constraints.NotEmpty; 10 import javax.validation.constraints.NotEmpty;
11 11
12 -// TODO @jason:TradeExpressProperties;更通用哈  
13 // TODO @芋艿:未来要不要放数据库中?考虑 saas 多租户时,不同租户使用不同的配置? 12 // TODO @芋艿:未来要不要放数据库中?考虑 saas 多租户时,不同租户使用不同的配置?
14 /** 13 /**
15 - * 交易快递查询的配置项 14 + * 交易运费快递的配置项
16 * 15 *
17 * @author jason 16 * @author jason
18 */ 17 */
19 @Component 18 @Component
20 -@ConfigurationProperties(prefix = "yudao.trade.express.query") 19 +@ConfigurationProperties(prefix = "yudao.trade.express")
21 @Data 20 @Data
22 @Validated 21 @Validated
23 -public class TradeExpressQueryProperties { 22 +public class TradeExpressProperties {
24 23
25 /** 24 /**
26 - * 快递查询服务商 25 + * 快递客户端
27 * 26 *
28 - * 如果未配置,默认使用快递鸟 27 + * 默认不提供,需要提醒用户配置一个快递服务商。
29 */ 28 */
30 - // TODO @jason:可以把 expressQueryProvider 改成 client 变量,更简洁一点;  
31 - private ExpressQueryProviderEnum expressQueryProvider; // TODO @jaosn:默认值可以通过属性直接赋值哈;  
32 - // TODO @jason:需要考虑下,用户只配置了其中一个; 29 + private ExpressClientEnum client = ExpressClientEnum.NOT_PROVIDE;
33 /** 30 /**
34 * 快递鸟配置 31 * 快递鸟配置
35 */ 32 */
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/ExpressQueryClient.java deleted
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core;  
2 -  
3 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;  
4 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;  
5 -  
6 -import java.util.List;  
7 -  
8 -// TODO @jason:可以改成 ExpressClient,未来可能还对接别的接口噢  
9 -/**  
10 - * 快递查询客户端  
11 - *  
12 - * @author jason  
13 - */  
14 -public interface ExpressQueryClient {  
15 -  
16 - /**  
17 - * 快递实时查询  
18 - *  
19 - * @param reqDTO 查询请求参数  
20 - */  
21 - // TODO @jason:可以改成 getExpressTrackList。返回字段可以参考 https://doc.youzanyun.com/detail/API/0/5 响应的 data  
22 - List<ExpressQueryRespDTO> realTimeQuery(ExpressQueryReqDTO reqDTO);  
23 -  
24 -}  
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/ExpressQueryProviderFactory.java deleted
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core;  
2 -  
3 -/**  
4 - * 快递服务商工厂,用于创建和缓存快递服务商服务  
5 - *  
6 - * @author jason  
7 - */  
8 -public interface ExpressQueryProviderFactory {  
9 -  
10 - /**  
11 - * 通过枚举获取快递查询服务商  
12 - *  
13 - * 如果不存在,就创建一个对应的快递查询服务商  
14 - *  
15 - * @param queryProviderEnum 快递服务商枚举  
16 - */  
17 - ExpressQueryProvider getOrCreateExpressQueryProvider(ExpressQueryProviderEnum queryProviderEnum);  
18 -  
19 -}  
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/ExpressQueryProvider.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/ExpressClient.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client;
2 2
3 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;  
4 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO; 3 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
  4 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO;
5 5
6 import java.util.List; 6 import java.util.List;
7 7
8 /** 8 /**
9 - * 快递查询服务商 9 + * 快递客户端接口
10 * 10 *
11 * @author jason 11 * @author jason
12 */ 12 */
13 -public interface ExpressQueryProvider { 13 +public interface ExpressClient {
14 14
15 /** 15 /**
16 * 快递实时查询 16 * 快递实时查询
17 * 17 *
18 * @param reqDTO 查询请求参数 18 * @param reqDTO 查询请求参数
19 */ 19 */
20 - List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO); 20 + // TODO @jason:返回字段可以参考 https://doc.youzanyun.com/detail/API/0/5 响应的 data
  21 + List<ExpressQueryRespDTO> getExpressTrackList(ExpressQueryReqDTO reqDTO);
21 22
22 } 23 }
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/ExpressQueryProviderEnum.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/ExpressClientEnum.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client;
2 2
3 import lombok.Getter; 3 import lombok.Getter;
4 4
5 /** 5 /**
6 - * 快递查询服务商枚举 6 + * 快递客户端枚举
7 * 7 *
8 * @author jason 8 * @author jason
9 */ 9 */
10 @Getter 10 @Getter
11 -  
12 -public enum ExpressQueryProviderEnum {  
13 - 11 +public enum ExpressClientEnum {
  12 + NOT_PROVIDE("not-provide","未提供"),
14 KD_NIAO("kd-niao", "快递鸟"), 13 KD_NIAO("kd-niao", "快递鸟"),
15 KD_100("kd-100", "快递100"); 14 KD_100("kd-100", "快递100");
16 15
@@ -24,10 +23,8 @@ public enum ExpressQueryProviderEnum { @@ -24,10 +23,8 @@ public enum ExpressQueryProviderEnum {
24 */ 23 */
25 private final String name; 24 private final String name;
26 25
27 - // TODO @jaosn:@AllArgsConstructor 可以替代哈  
28 - ExpressQueryProviderEnum(String code, String name) { 26 + ExpressClientEnum(String code, String name) {
29 this.code = code; 27 this.code = code;
30 this.name = name; 28 this.name = name;
31 } 29 }
32 -  
33 } 30 }
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/ExpressClientFactory.java 0 → 100644
  1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client;
  2 +
  3 +/**
  4 + * 快递客户端工厂接口,快递客户端工厂:用于创建和缓存快递客户端
  5 + *
  6 + * @author jason
  7 + */
  8 +public interface ExpressClientFactory {
  9 +
  10 + /**
  11 + * 获取默认的快递客户端
  12 + */
  13 + ExpressClient getDefaultExpressClient();
  14 +
  15 + /**
  16 + * 通过枚举获取快递客户端,如果不存在,就创建一个对应快递客户端
  17 + *
  18 + * @param clientEnum 快递客户端枚举
  19 + */
  20 + ExpressClient getOrCreateExpressClient(ExpressClientEnum clientEnum);
  21 +
  22 +
  23 +}
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/convert/ExpressQueryConvert.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/convert/ExpressQueryConvert.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.convert;  
2 -  
3 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;  
4 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;  
5 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100.Kd100ExpressQueryReqDTO;  
6 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100.Kd100ExpressQueryRespDTO;  
7 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao.KdNiaoExpressQueryReqDTO;  
8 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao.KdNiaoExpressQueryRespDTO; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.convert;
  2 +
  3 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
  4 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO;
  5 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryReqDTO;
  6 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryRespDTO;
  7 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryReqDTO;
  8 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryRespDTO;
9 import org.mapstruct.Mapper; 9 import org.mapstruct.Mapper;
10 import org.mapstruct.factory.Mappers; 10 import org.mapstruct.factory.Mappers;
11 11
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/dto/ExpressQueryReqDTO.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/dto/ExpressQueryReqDTO.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.dto; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto;
2 2
3 import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO; 3 import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
4 import lombok.Data; 4 import lombok.Data;
@@ -16,8 +16,7 @@ public class ExpressQueryReqDTO { @@ -16,8 +16,7 @@ public class ExpressQueryReqDTO {
16 * 16 *
17 * 对应 {@link DeliveryExpressDO#getCode()} 17 * 对应 {@link DeliveryExpressDO#getCode()}
18 */ 18 */
19 - // TODO @jaosn:要不改成 expressCode;项目里使用这个哈  
20 - private String expressCompanyCode; 19 + private String expressCode;
21 20
22 /** 21 /**
23 * 发货快递单号 22 * 发货快递单号
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/dto/ExpressQueryRespDTO.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/dto/ExpressQueryRespDTO.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.dto; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto;
2 2
3 import lombok.Data; 3 import lombok.Data;
4 4
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/dto/provider/kd100/Kd100ExpressQueryReqDTO.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/dto/kd100/Kd100ExpressQueryReqDTO.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100;
2 2
3 import com.fasterxml.jackson.annotation.JsonInclude; 3 import com.fasterxml.jackson.annotation.JsonInclude;
4 import com.fasterxml.jackson.annotation.JsonProperty; 4 import com.fasterxml.jackson.annotation.JsonProperty;
@@ -13,12 +13,11 @@ import lombok.Data; @@ -13,12 +13,11 @@ import lombok.Data;
13 @JsonInclude(JsonInclude.Include.NON_NULL) 13 @JsonInclude(JsonInclude.Include.NON_NULL)
14 public class Kd100ExpressQueryReqDTO { 14 public class Kd100ExpressQueryReqDTO {
15 15
16 - // TODO @jaosn:要不改成 expressCode;项目里使用这个哈  
17 /** 16 /**
18 * 快递公司编码 17 * 快递公司编码
19 */ 18 */
20 @JsonProperty("com") 19 @JsonProperty("com")
21 - private String expressCompanyCode; 20 + private String expressCode;
22 21
23 /** 22 /**
24 * 快递单号 23 * 快递单号
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/dto/provider/kd100/Kd100ExpressQueryRespDTO.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/dto/kd100/Kd100ExpressQueryRespDTO.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100;
2 2
3 import com.fasterxml.jackson.annotation.JsonProperty; 3 import com.fasterxml.jackson.annotation.JsonProperty;
4 import lombok.Data; 4 import lombok.Data;
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/dto/provider/kdniao/KdNiaoExpressQueryReqDTO.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/dto/kdniao/KdNiaoExpressQueryReqDTO.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao;
2 2
3 import com.fasterxml.jackson.annotation.JsonInclude; 3 import com.fasterxml.jackson.annotation.JsonInclude;
4 import com.fasterxml.jackson.annotation.JsonProperty; 4 import com.fasterxml.jackson.annotation.JsonProperty;
@@ -13,12 +13,11 @@ import lombok.Data; @@ -13,12 +13,11 @@ import lombok.Data;
13 @JsonInclude(JsonInclude.Include.NON_NULL) 13 @JsonInclude(JsonInclude.Include.NON_NULL)
14 public class KdNiaoExpressQueryReqDTO { 14 public class KdNiaoExpressQueryReqDTO {
15 15
16 - // TODO @jaosn:要不改成 expressCode;项目里使用这个哈  
17 /** 16 /**
18 * 快递公司编码 17 * 快递公司编码
19 */ 18 */
20 @JsonProperty("ShipperCode") 19 @JsonProperty("ShipperCode")
21 - private String expressCompanyCode; 20 + private String expressCode;
22 /** 21 /**
23 * 快递单号 22 * 快递单号
24 */ 23 */
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/dto/provider/kdniao/KdNiaoExpressQueryRespDTO.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/dto/kdniao/KdNiaoExpressQueryRespDTO.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao;
2 2
3 import com.fasterxml.jackson.annotation.JsonProperty; 3 import com.fasterxml.jackson.annotation.JsonProperty;
4 import lombok.Data; 4 import lombok.Data;
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/ExpressClientFactoryImpl.java 0 → 100644
  1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
  2 +
  3 +import cn.hutool.core.lang.Assert;
  4 +import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
  5 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientEnum;
  6 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
  7 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientFactory;
  8 +import org.springframework.web.client.RestTemplate;
  9 +
  10 +import java.util.Map;
  11 +import java.util.concurrent.ConcurrentHashMap;
  12 +
  13 +/**
  14 + * 快递客户端工厂实现类
  15 + *
  16 + * @author jason
  17 + */
  18 +public class ExpressClientFactoryImpl implements ExpressClientFactory {
  19 +
  20 + private final Map<ExpressClientEnum, ExpressClient> clientMap = new ConcurrentHashMap<>(8);
  21 +
  22 + private final TradeExpressProperties tradeExpressProperties;
  23 + private final RestTemplate restTemplate;
  24 +
  25 + public ExpressClientFactoryImpl(TradeExpressProperties tradeExpressProperties,
  26 + RestTemplate restTemplate) {
  27 + this.tradeExpressProperties = tradeExpressProperties;
  28 + this.restTemplate = restTemplate;
  29 + }
  30 +
  31 + @Override
  32 + public ExpressClient getDefaultExpressClient() {
  33 + ExpressClient defaultClient = getOrCreateExpressClient(tradeExpressProperties.getClient());
  34 + Assert.notNull("默认的快递客户端不能为空");
  35 + return defaultClient;
  36 + }
  37 +
  38 + @Override
  39 + public ExpressClient getOrCreateExpressClient(ExpressClientEnum clientEnum) {
  40 + return clientMap.computeIfAbsent(clientEnum,
  41 + client -> createExpressClient(client, tradeExpressProperties));
  42 + }
  43 +
  44 + private ExpressClient createExpressClient(ExpressClientEnum queryProviderEnum,
  45 + TradeExpressProperties tradeExpressProperties) {
  46 + switch (queryProviderEnum) {
  47 + case NOT_PROVIDE:
  48 + return new NoProvideExpressClient();
  49 + case KD_NIAO:
  50 + return new KdNiaoExpressClient(restTemplate, tradeExpressProperties.getKdNiao());
  51 + case KD_100:
  52 + return new Kd100ExpressClient(restTemplate, tradeExpressProperties.getKd100());
  53 + }
  54 + return null;
  55 + }
  56 +}
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/impl/Kd100ExpressQueryProvider.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/Kd100ExpressClient.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.impl; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
2 2
3 import cn.hutool.core.collection.CollUtil; 3 import cn.hutool.core.collection.CollUtil;
4 import cn.hutool.core.util.HexUtil; 4 import cn.hutool.core.util.HexUtil;
5 import cn.hutool.crypto.digest.DigestUtil; 5 import cn.hutool.crypto.digest.DigestUtil;
6 import cn.iocoder.yudao.framework.common.util.json.JsonUtils; 6 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
7 -import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;  
8 -import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProvider;  
9 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;  
10 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;  
11 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100.Kd100ExpressQueryReqDTO;  
12 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100.Kd100ExpressQueryRespDTO; 7 +import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
  8 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
  9 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
  10 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO;
  11 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryReqDTO;
  12 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryRespDTO;
  13 +import lombok.AllArgsConstructor;
13 import lombok.extern.slf4j.Slf4j; 14 import lombok.extern.slf4j.Slf4j;
14 import org.springframework.http.*; 15 import org.springframework.http.*;
15 import org.springframework.util.LinkedMultiValueMap; 16 import org.springframework.util.LinkedMultiValueMap;
@@ -23,48 +24,41 @@ import java.util.Objects; @@ -23,48 +24,41 @@ import java.util.Objects;
23 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; 24 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
24 import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_ERROR; 25 import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_ERROR;
25 import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_FAILED; 26 import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_FAILED;
26 -import static cn.iocoder.yudao.module.trade.framework.delivery.core.convert.ExpressQueryConvert.INSTANCE; 27 +import static cn.iocoder.yudao.module.trade.framework.delivery.core.client.convert.ExpressQueryConvert.INSTANCE;
27 28
28 -// TODO @jason:可以参考 KdNiaoExpressQueryProvider 建议改改哈  
29 /** 29 /**
30 - * 快递 100 服务商 30 + * 快递 100 客户端
31 * 31 *
32 * @author jason 32 * @author jason
33 */ 33 */
34 @Slf4j 34 @Slf4j
35 -public class Kd100ExpressQueryProvider implements ExpressQueryProvider { 35 +@AllArgsConstructor
  36 +public class Kd100ExpressClient implements ExpressClient {
36 37
37 private static final String REAL_TIME_QUERY_URL = "https://poll.kuaidi100.com/poll/query.do"; 38 private static final String REAL_TIME_QUERY_URL = "https://poll.kuaidi100.com/poll/query.do";
38 -  
39 private final RestTemplate restTemplate; 39 private final RestTemplate restTemplate;
40 - private final TradeExpressQueryProperties.Kd100Config config;  
41 -  
42 - public Kd100ExpressQueryProvider(RestTemplate restTemplate, TradeExpressQueryProperties.Kd100Config config) {  
43 - this.restTemplate = restTemplate;  
44 - this.config = config;  
45 - } 40 + private final TradeExpressProperties.Kd100Config config;
46 41
47 @Override 42 @Override
48 - public List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO) { 43 + public List<ExpressQueryRespDTO> getExpressTrackList(ExpressQueryReqDTO reqDTO) {
49 // 发起查询 44 // 发起查询
50 Kd100ExpressQueryReqDTO kd100ReqParam = INSTANCE.convert2(reqDTO); 45 Kd100ExpressQueryReqDTO kd100ReqParam = INSTANCE.convert2(reqDTO);
51 - kd100ReqParam.setExpressCompanyCode(kd100ReqParam.getExpressCompanyCode().toLowerCase()); // 快递公司编码需要转成小写  
52 - Kd100ExpressQueryRespDTO respDTO = sendExpressQueryReq(REAL_TIME_QUERY_URL, kd100ReqParam, 46 + kd100ReqParam.setExpressCode(kd100ReqParam.getExpressCode().toLowerCase()); // 快递公司编码需要转成小写
  47 + Kd100ExpressQueryRespDTO respDTO = requestExpressQuery(REAL_TIME_QUERY_URL, kd100ReqParam,
53 Kd100ExpressQueryRespDTO.class); 48 Kd100ExpressQueryRespDTO.class);
54 - log.debug("[realTimeQueryExpress][快递 100 接口 查询接口返回 {}]", respDTO); 49 + log.debug("[getExpressTrackList][快递 100 接口 查询接口返回 {}]", respDTO);
55 // 处理结果 50 // 处理结果
56 if (Objects.equals("false", respDTO.getResult())) { 51 if (Objects.equals("false", respDTO.getResult())) {
57 - log.error("[realTimeQueryExpress][快递 100 接口 返回失败 {}]", respDTO.getMessage()); 52 + log.error("[getExpressTrackList][快递 100 接口 返回失败 {}]", respDTO.getMessage());
58 throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getMessage()); 53 throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getMessage());
59 - // TODO @json:else 可以不用写哈; 54 + }
  55 + // TODO @jason:convertList2 如果空,应该返回 list 了; @芋艿 为了避免返回 null
  56 + if (CollUtil.isNotEmpty(respDTO.getTracks())) {
  57 + return INSTANCE.convertList2(respDTO.getTracks());
60 } else { 58 } else {
61 - // TODO @jason:convertList2 如果空,应该返回 list 了;  
62 - if (CollUtil.isNotEmpty(respDTO.getTracks())) {  
63 - return INSTANCE.convertList2(respDTO.getTracks());  
64 - } else {  
65 - return Collections.emptyList();  
66 - } 59 + return Collections.emptyList();
67 } 60 }
  61 +
68 } 62 }
69 63
70 /** 64 /**
@@ -76,8 +70,7 @@ public class Kd100ExpressQueryProvider implements ExpressQueryProvider { @@ -76,8 +70,7 @@ public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
76 * @param <Req> 每个请求的请求结构 Req DTO 70 * @param <Req> 每个请求的请求结构 Req DTO
77 * @param <Resp> 每个请求的响应结构 Resp DTO 71 * @param <Resp> 每个请求的响应结构 Resp DTO
78 */ 72 */
79 - // TODO @jason:可以改成 request,发起请求哈;  
80 - private <Req, Resp> Resp sendExpressQueryReq(String url, Req req, Class<Resp> respClass) { 73 + private <Req, Resp> Resp requestExpressQuery(String url, Req req, Class<Resp> respClass) {
81 // 请求头 74 // 请求头
82 HttpHeaders headers = new HttpHeaders(); 75 HttpHeaders headers = new HttpHeaders();
83 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); 76 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
@@ -92,23 +85,20 @@ public class Kd100ExpressQueryProvider implements ExpressQueryProvider { @@ -92,23 +85,20 @@ public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
92 log.debug("[sendExpressQueryReq][快递 100 接口的请求参数: {}]", requestBody); 85 log.debug("[sendExpressQueryReq][快递 100 接口的请求参数: {}]", requestBody);
93 // 发送请求 86 // 发送请求
94 HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers); 87 HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
95 - // TODO @jason:可以使用 restTemplate 的 post 方法哇? 88 + // TODO @jason:可以使用 restTemplate 的 post 方法哇 @芋艿 为了获取接口的原始返回。用exchange 便于查问题。
96 ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class); 89 ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
97 log.debug("[sendExpressQueryReq][快递 100 接口响应结果 {}]", responseEntity); 90 log.debug("[sendExpressQueryReq][快递 100 接口响应结果 {}]", responseEntity);
98 91
99 // 处理响应 92 // 处理响应
100 - // TODO @jason:if return 原则;if (!responseEntity.getStatusCode().is2xxSuccessful()) 抛出异常;接着处理成功的  
101 - if (responseEntity.getStatusCode().is2xxSuccessful()) {  
102 - String response = responseEntity.getBody();  
103 - return JsonUtils.parseObject(response, respClass);  
104 - } else { 93 + if (!responseEntity.getStatusCode().is2xxSuccessful()) {
105 throw exception(EXPRESS_API_QUERY_ERROR); 94 throw exception(EXPRESS_API_QUERY_ERROR);
106 } 95 }
  96 + return JsonUtils.parseObject(responseEntity.getBody(), respClass);
107 } 97 }
108 98
109 private String generateReqSign(String param, String key, String customer) { 99 private String generateReqSign(String param, String key, String customer) {
110 String plainText = String.format("%s%s%s", param, key, customer); 100 String plainText = String.format("%s%s%s", param, key, customer);
111 - // TODO @jason:DigestUtil.md5Hex(plainText); 101 + // TODO @芋艿。 这里需要转换成大写, 没有对应方法
112 return HexUtil.encodeHexStr(DigestUtil.md5(plainText), false); 102 return HexUtil.encodeHexStr(DigestUtil.md5(plainText), false);
113 } 103 }
114 104
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/impl/KdNiaoExpressQueryProvider.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/KdNiaoExpressClient.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.impl; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
2 2
3 import cn.hutool.core.codec.Base64; 3 import cn.hutool.core.codec.Base64;
4 import cn.hutool.core.collection.CollUtil; 4 import cn.hutool.core.collection.CollUtil;
5 import cn.hutool.core.net.URLEncodeUtil; 5 import cn.hutool.core.net.URLEncodeUtil;
6 import cn.hutool.crypto.digest.DigestUtil; 6 import cn.hutool.crypto.digest.DigestUtil;
7 import cn.iocoder.yudao.framework.common.util.json.JsonUtils; 7 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
8 -import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;  
9 -import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProvider;  
10 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;  
11 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;  
12 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao.KdNiaoExpressQueryReqDTO;  
13 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao.KdNiaoExpressQueryRespDTO; 8 +import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
  9 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
  10 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
  11 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO;
  12 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryReqDTO;
  13 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryRespDTO;
  14 +import lombok.AllArgsConstructor;
14 import lombok.extern.slf4j.Slf4j; 15 import lombok.extern.slf4j.Slf4j;
15 import org.springframework.http.*; 16 import org.springframework.http.*;
16 import org.springframework.util.LinkedMultiValueMap; 17 import org.springframework.util.LinkedMultiValueMap;
@@ -23,15 +24,16 @@ import java.util.List; @@ -23,15 +24,16 @@ import java.util.List;
23 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; 24 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
24 import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_FAILED; 25 import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_FAILED;
25 import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_ERROR; 26 import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_ERROR;
26 -import static cn.iocoder.yudao.module.trade.framework.delivery.core.convert.ExpressQueryConvert.INSTANCE; 27 +import static cn.iocoder.yudao.module.trade.framework.delivery.core.client.convert.ExpressQueryConvert.INSTANCE;
27 28
28 /** 29 /**
29 - * 快递鸟服务商 30 + * 快递鸟客户端
30 * 31 *
31 * @author jason 32 * @author jason
32 */ 33 */
33 @Slf4j 34 @Slf4j
34 -public class KdNiaoExpressQueryProvider implements ExpressQueryProvider { 35 +@AllArgsConstructor
  36 +public class KdNiaoExpressClient implements ExpressClient {
35 37
36 private static final String REAL_TIME_QUERY_URL = "https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx"; 38 private static final String REAL_TIME_QUERY_URL = "https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
37 39
@@ -39,15 +41,8 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider { @@ -39,15 +41,8 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
39 * 快递鸟即时查询免费版 RequestType 41 * 快递鸟即时查询免费版 RequestType
40 */ 42 */
41 private static final String REAL_TIME_FREE_REQ_TYPE = "1002"; 43 private static final String REAL_TIME_FREE_REQ_TYPE = "1002";
42 -  
43 private final RestTemplate restTemplate; 44 private final RestTemplate restTemplate;
44 - private final TradeExpressQueryProperties.KdNiaoConfig config;  
45 -  
46 - // TODO @jason:可以改成 lombok 哈  
47 - public KdNiaoExpressQueryProvider(RestTemplate restTemplate, TradeExpressQueryProperties.KdNiaoConfig config) {  
48 - this.restTemplate = restTemplate;  
49 - this.config = config;  
50 - } 45 + private final TradeExpressProperties.KdNiaoConfig config;
51 46
52 /** 47 /**
53 * 快递鸟即时查询免费版本 48 * 快递鸟即时查询免费版本
@@ -56,21 +51,20 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider { @@ -56,21 +51,20 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
56 * @param reqDTO 查询请求参数 51 * @param reqDTO 查询请求参数
57 */ 52 */
58 @Override 53 @Override
59 - public List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO) { 54 + public List<ExpressQueryRespDTO> getExpressTrackList(ExpressQueryReqDTO reqDTO) {
60 KdNiaoExpressQueryReqDTO kdNiaoReqData = INSTANCE.convert(reqDTO); 55 KdNiaoExpressQueryReqDTO kdNiaoReqData = INSTANCE.convert(reqDTO);
61 // 快递公司编码需要转成大写 56 // 快递公司编码需要转成大写
62 - kdNiaoReqData.setExpressCompanyCode(reqDTO.getExpressCompanyCode().toUpperCase());  
63 - KdNiaoExpressQueryRespDTO respDTO = sendKdNiaoApiRequest(REAL_TIME_QUERY_URL, REAL_TIME_FREE_REQ_TYPE, 57 + kdNiaoReqData.setExpressCode(reqDTO.getExpressCode().toUpperCase());
  58 + KdNiaoExpressQueryRespDTO respDTO = requestKdNiaoApi(REAL_TIME_QUERY_URL, REAL_TIME_FREE_REQ_TYPE,
64 kdNiaoReqData, KdNiaoExpressQueryRespDTO.class); 59 kdNiaoReqData, KdNiaoExpressQueryRespDTO.class);
65 - log.debug("[realTimeQueryExpress][快递鸟即时查询接口返回 {}]", respDTO);  
66 - if(!respDTO.getSuccess()){  
67 - throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getReason());  
68 - }else{  
69 - if (CollUtil.isNotEmpty(respDTO.getTracks())) {  
70 - return INSTANCE.convertList(respDTO.getTracks());  
71 - }else{  
72 - return Collections.emptyList();  
73 - } 60 + log.debug("[getExpressTrackList][快递鸟即时查询接口返回 {}]", respDTO);
  61 + if (respDTO == null || !respDTO.getSuccess()) {
  62 + throw exception(EXPRESS_API_QUERY_FAILED, respDTO == null ? "" : respDTO.getReason());
  63 + }
  64 + if (CollUtil.isNotEmpty(respDTO.getTracks())) {
  65 + return INSTANCE.convertList(respDTO.getTracks());
  66 + } else {
  67 + return Collections.emptyList();
74 } 68 }
75 } 69 }
76 70
@@ -83,8 +77,8 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider { @@ -83,8 +77,8 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
83 * @param <Req> 每个请求的请求结构 Req DTO 77 * @param <Req> 每个请求的请求结构 Req DTO
84 * @param <Resp> 每个请求的响应结构 Resp DTO 78 * @param <Resp> 每个请求的响应结构 Resp DTO
85 */ 79 */
86 - private <Req, Resp> Resp sendKdNiaoApiRequest(String url, String requestType, Req req,  
87 - Class<Resp> respClass){ 80 + private <Req, Resp> Resp requestKdNiaoApi(String url, String requestType, Req req,
  81 + Class<Resp> respClass){
88 // 请求头 82 // 请求头
89 HttpHeaders headers = new HttpHeaders(); 83 HttpHeaders headers = new HttpHeaders();
90 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); 84 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
@@ -97,19 +91,16 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider { @@ -97,19 +91,16 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
97 requestBody.add("EBusinessID", config.getBusinessId()); 91 requestBody.add("EBusinessID", config.getBusinessId());
98 requestBody.add("DataSign", dataSign); 92 requestBody.add("DataSign", dataSign);
99 requestBody.add("RequestType", requestType); 93 requestBody.add("RequestType", requestType);
100 - log.debug("[sendKdNiaoApiRequest][快递鸟接口 RequestType : {}, 的请求参数 {}]", requestType, requestBody);  
101 - 94 + log.debug("[requestKdNiaoApi][快递鸟接口 RequestType : {}, 的请求参数 {}]", requestType, requestBody);
102 // 发送请求 95 // 发送请求
103 HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers); 96 HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
104 ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class); 97 ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
105 log.debug("快递鸟接口 RequestType : {}, 的响应结果 {}", requestType, responseEntity); 98 log.debug("快递鸟接口 RequestType : {}, 的响应结果 {}", requestType, responseEntity);
106 // 处理响应 99 // 处理响应
107 - if (responseEntity.getStatusCode().is2xxSuccessful()) {  
108 - String response = responseEntity.getBody();  
109 - return JsonUtils.parseObject(response, respClass);  
110 - } else { 100 + if (!responseEntity.getStatusCode().is2xxSuccessful()) {
111 throw exception(EXPRESS_API_QUERY_ERROR); 101 throw exception(EXPRESS_API_QUERY_ERROR);
112 } 102 }
  103 + return JsonUtils.parseObject(responseEntity.getBody(), respClass);
113 } 104 }
114 105
115 /** 106 /**
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/NoProvideExpressClient.java 0 → 100644
  1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
  2 +
  3 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
  4 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
  5 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO;
  6 +
  7 +import java.util.List;
  8 +
  9 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  10 +import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_CLIENT_NOT_PROVIDE;
  11 +
  12 +/**
  13 + * 未实现的快递客户端,用来提醒用户需要接入快递服务商,
  14 + *
  15 + * @author jason
  16 + */
  17 +public class NoProvideExpressClient implements ExpressClient {
  18 + @Override
  19 + public List<ExpressQueryRespDTO> getExpressTrackList(ExpressQueryReqDTO reqDTO) {
  20 + throw exception(EXPRESS_CLIENT_NOT_PROVIDE);
  21 + }
  22 +}
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/impl/ExpressQueryClientImpl.java deleted
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;  
2 -  
3 -import cn.hutool.core.lang.Assert;  
4 -import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;  
5 -import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryClient;  
6 -import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProvider;  
7 -import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderEnum;  
8 -import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderFactory;  
9 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;  
10 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;  
11 -import lombok.extern.slf4j.Slf4j;  
12 -import org.springframework.stereotype.Component;  
13 -  
14 -import javax.annotation.PostConstruct;  
15 -import javax.annotation.Resource;  
16 -import java.util.List;  
17 -  
18 -import static cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderEnum.KD_NIAO;  
19 -  
20 -// TODO @jason:可以把整体包结构调整下;参考 sms client 的方式;  
21 -// + config  
22 -// + core  
23 -// client  
24 -// + dto  
25 -// + impl:里面可以放 kdniaoclient、kd100client  
26 -// ExpressClient  
27 -// ExpressClientFactory: 通过它直接获取默认和创建默认的 Client  
28 -// enums  
29 -/**  
30 - * 快递查询客户端实现  
31 - *  
32 - * @author jason  
33 - */  
34 -@Component  
35 -@Slf4j  
36 -public class ExpressQueryClientImpl implements ExpressQueryClient {  
37 -  
38 - @Resource  
39 - private ExpressQueryProviderFactory expressQueryProviderFactory;  
40 - @Resource  
41 - private TradeExpressQueryProperties tradeExpressQueryProperties;  
42 -  
43 - private ExpressQueryProvider expressQueryProvider;  
44 -  
45 - @PostConstruct  
46 - private void init() {  
47 - // 如果未设置,默认使用快递鸟  
48 - ExpressQueryProviderEnum queryProvider = tradeExpressQueryProperties.getExpressQueryProvider();  
49 - if (queryProvider == null) {  
50 - queryProvider = KD_NIAO;  
51 - }  
52 - // 创建客户端  
53 - expressQueryProvider = expressQueryProviderFactory.getOrCreateExpressQueryProvider(queryProvider);  
54 - if (expressQueryProvider == null) {  
55 - log.error("获取创建快递查询服务商{}失败,请检查相关配置", queryProvider);  
56 - }  
57 - Assert.notNull(expressQueryProvider, "快递查询服务商不能为空");  
58 - }  
59 -  
60 - @Override  
61 - public List<ExpressQueryRespDTO> realTimeQuery(ExpressQueryReqDTO reqDTO) {  
62 - return expressQueryProvider.realTimeQueryExpress(reqDTO);  
63 - }  
64 -  
65 -}  
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/impl/ExpressQueryProviderFactoryImpl.java deleted
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;  
2 -  
3 -import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;  
4 -import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProvider;  
5 -import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderEnum;  
6 -import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderFactory;  
7 -import org.springframework.stereotype.Component;  
8 -import org.springframework.web.client.RestTemplate;  
9 -  
10 -import javax.annotation.Resource;  
11 -import java.util.Map;  
12 -import java.util.concurrent.ConcurrentHashMap;  
13 -  
14 -/**  
15 - * // TODO @jason:注释不全  
16 - * @author jason  
17 - */  
18 -@Component  
19 -public class ExpressQueryProviderFactoryImpl implements ExpressQueryProviderFactory {  
20 -  
21 - private final Map<ExpressQueryProviderEnum, ExpressQueryProvider> providerMap = new ConcurrentHashMap<>(8);  
22 -  
23 - @Resource  
24 - private TradeExpressQueryProperties tradeExpressQueryProperties;  
25 - @Resource  
26 - private RestTemplate restTemplate;  
27 -  
28 - @Override  
29 - public ExpressQueryProvider getOrCreateExpressQueryProvider(ExpressQueryProviderEnum queryProviderEnum) {  
30 - return providerMap.computeIfAbsent(queryProviderEnum,  
31 - provider -> createExpressQueryProvider(provider, tradeExpressQueryProperties));  
32 - }  
33 -  
34 - private ExpressQueryProvider createExpressQueryProvider(ExpressQueryProviderEnum queryProviderEnum,  
35 - TradeExpressQueryProperties tradeExpressQueryProperties) {  
36 - // TODO @jason:是不是直接 return 就好啦,更简洁一点  
37 - ExpressQueryProvider result = null;  
38 - switch (queryProviderEnum) {  
39 - case KD_NIAO:  
40 - result = new KdNiaoExpressQueryProvider(restTemplate, tradeExpressQueryProperties.getKdNiao());  
41 - break;  
42 - case KD_100:  
43 - result = new Kd100ExpressQueryProvider(restTemplate, tradeExpressQueryProperties.getKd100());  
44 - break;  
45 - }  
46 - return result;  
47 - }  
48 -}  
yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/framework/delivery/core/impl/Kd100ExpressQueryProviderTest.java renamed to yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/Kd100ExpressClientTest.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.impl; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
2 2
3 import cn.iocoder.yudao.framework.common.exception.ServiceException; 3 import cn.iocoder.yudao.framework.common.exception.ServiceException;
4 -import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;  
5 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO; 4 +import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
  5 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
6 import org.junit.jupiter.api.BeforeEach; 6 import org.junit.jupiter.api.BeforeEach;
7 import org.junit.jupiter.api.Disabled; 7 import org.junit.jupiter.api.Disabled;
8 import org.junit.jupiter.api.Test; 8 import org.junit.jupiter.api.Test;
@@ -21,36 +21,36 @@ import static org.junit.jupiter.api.Assertions.assertThrows; @@ -21,36 +21,36 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
21 /** 21 /**
22 * @author jason 22 * @author jason
23 */ 23 */
24 -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = Kd100ExpressQueryProviderTest.Application.class)  
25 -@ActiveProfiles("trade-delivery-query") // 设置使用 trade-delivery-query 配置文件  
26 -public class Kd100ExpressQueryProviderTest { 24 +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = Kd100ExpressClientTest.Application.class)
  25 +@ActiveProfiles("unit-test") // 设置使用 trade-delivery-query 配置文件
  26 +public class Kd100ExpressClientTest {
27 @Resource 27 @Resource
28 private RestTemplateBuilder builder; 28 private RestTemplateBuilder builder;
29 @Resource 29 @Resource
30 - private TradeExpressQueryProperties expressQueryProperties; 30 + private TradeExpressProperties expressQueryProperties;
31 31
32 - private Kd100ExpressQueryProvider kd100ExpressQueryProvider; 32 + private Kd100ExpressClient kd100ExpressClient;
33 33
34 @BeforeEach 34 @BeforeEach
35 public void init(){ 35 public void init(){
36 - kd100ExpressQueryProvider = new Kd100ExpressQueryProvider(builder.build(),expressQueryProperties.getKd100()); 36 + kd100ExpressClient = new Kd100ExpressClient(builder.build(),expressQueryProperties.getKd100());
37 } 37 }
38 @Test 38 @Test
39 @Disabled("需要 授权 key. 暂时忽略") 39 @Disabled("需要 授权 key. 暂时忽略")
40 void testRealTimeQueryExpressFailed() { 40 void testRealTimeQueryExpressFailed() {
41 ServiceException t = assertThrows(ServiceException.class, () -> { 41 ServiceException t = assertThrows(ServiceException.class, () -> {
42 ExpressQueryReqDTO reqDTO = new ExpressQueryReqDTO(); 42 ExpressQueryReqDTO reqDTO = new ExpressQueryReqDTO();
43 - reqDTO.setExpressCompanyCode("yto"); 43 + reqDTO.setExpressCode("yto");
44 reqDTO.setLogisticsNo("YT9383342193097"); 44 reqDTO.setLogisticsNo("YT9383342193097");
45 - kd100ExpressQueryProvider.realTimeQueryExpress(reqDTO); 45 + kd100ExpressClient.getExpressTrackList(reqDTO);
46 }); 46 });
47 - assertEquals(1011003007, t.getCode()); 47 + assertEquals(1011003005, t.getCode());
48 } 48 }
49 49
50 @Import({ 50 @Import({
51 RestTemplateAutoConfiguration.class 51 RestTemplateAutoConfiguration.class
52 }) 52 })
53 - @EnableConfigurationProperties(TradeExpressQueryProperties.class) 53 + @EnableConfigurationProperties(TradeExpressProperties.class)
54 public static class Application { 54 public static class Application {
55 } 55 }
56 } 56 }
57 \ No newline at end of file 57 \ No newline at end of file
yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/framework/delivery/core/impl/KdNiaoExpressQueryProviderTest.java renamed to yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/KdNiaoExpressClientTest.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.impl; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
2 2
3 import cn.iocoder.yudao.framework.common.exception.ServiceException; 3 import cn.iocoder.yudao.framework.common.exception.ServiceException;
4 -import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;  
5 -import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO; 4 +import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
  5 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
6 import org.junit.jupiter.api.BeforeEach; 6 import org.junit.jupiter.api.BeforeEach;
7 import org.junit.jupiter.api.Disabled; 7 import org.junit.jupiter.api.Disabled;
8 import org.junit.jupiter.api.Test; 8 import org.junit.jupiter.api.Test;
@@ -21,35 +21,35 @@ import static org.junit.jupiter.api.Assertions.assertThrows; @@ -21,35 +21,35 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
21 /** 21 /**
22 * @author jason 22 * @author jason
23 */ 23 */
24 -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = KdNiaoExpressQueryProviderTest.Application.class)  
25 -@ActiveProfiles("trade-delivery-query") // 设置使用 trade-delivery-query 配置文件 TODO @jason:可以直接写到 application-unit-test.yaml 配置文件里  
26 -public class KdNiaoExpressQueryProviderTest { 24 +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = KdNiaoExpressClientTest.Application.class)
  25 +@ActiveProfiles("unit-test")
  26 +public class KdNiaoExpressClientTest {
27 @Resource 27 @Resource
28 private RestTemplateBuilder builder; 28 private RestTemplateBuilder builder;
29 @Resource 29 @Resource
30 - private TradeExpressQueryProperties expressQueryProperties; 30 + private TradeExpressProperties expressQueryProperties;
31 31
32 - private KdNiaoExpressQueryProvider kdNiaoExpressQueryProvider; 32 + private KdNiaoExpressClient kdNiaoExpressClient;
33 33
34 @BeforeEach 34 @BeforeEach
35 public void init(){ 35 public void init(){
36 - kdNiaoExpressQueryProvider = new KdNiaoExpressQueryProvider(builder.build(),expressQueryProperties.getKdNiao()); 36 + kdNiaoExpressClient = new KdNiaoExpressClient(builder.build(),expressQueryProperties.getKdNiao());
37 } 37 }
38 @Test 38 @Test
39 @Disabled("需要 授权 key. 暂时忽略") 39 @Disabled("需要 授权 key. 暂时忽略")
40 void testRealTimeQueryExpressFailed() { 40 void testRealTimeQueryExpressFailed() {
41 assertThrows(ServiceException.class,() ->{ 41 assertThrows(ServiceException.class,() ->{
42 ExpressQueryReqDTO reqDTO = new ExpressQueryReqDTO(); 42 ExpressQueryReqDTO reqDTO = new ExpressQueryReqDTO();
43 - reqDTO.setExpressCompanyCode("yy"); 43 + reqDTO.setExpressCode("yy");
44 reqDTO.setLogisticsNo("YT9383342193097"); 44 reqDTO.setLogisticsNo("YT9383342193097");
45 - kdNiaoExpressQueryProvider.realTimeQueryExpress(reqDTO); 45 + kdNiaoExpressClient.getExpressTrackList(reqDTO);
46 }); 46 });
47 } 47 }
48 48
49 @Import({ 49 @Import({
50 RestTemplateAutoConfiguration.class 50 RestTemplateAutoConfiguration.class
51 }) 51 })
52 - @EnableConfigurationProperties(TradeExpressQueryProperties.class) 52 + @EnableConfigurationProperties(TradeExpressProperties.class)
53 public static class Application { 53 public static class Application {
54 } 54 }
55 } 55 }
yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/NoProvideExpressClientTest.java 0 → 100644
  1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
  2 +
  3 +import cn.iocoder.yudao.framework.common.exception.ServiceException;
  4 +import cn.iocoder.yudao.module.trade.framework.delivery.config.ExpressClientConfig;
  5 +import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
  6 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
  7 +import org.junit.jupiter.api.Test;
  8 +import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
  9 +import org.springframework.boot.context.properties.EnableConfigurationProperties;
  10 +import org.springframework.boot.test.context.SpringBootTest;
  11 +import org.springframework.boot.web.client.RestTemplateBuilder;
  12 +import org.springframework.context.annotation.Bean;
  13 +import org.springframework.context.annotation.Import;
  14 +import org.springframework.test.context.ActiveProfiles;
  15 +import org.springframework.web.client.RestTemplate;
  16 +
  17 +import javax.annotation.Resource;
  18 +
  19 +import static org.junit.jupiter.api.Assertions.assertEquals;
  20 +import static org.junit.jupiter.api.Assertions.assertThrows;
  21 +
  22 +/**
  23 + * @author jason
  24 + */
  25 +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = NoProvideExpressClientTest.Application.class)
  26 +@ActiveProfiles("unit-test") // 设置使用 trade-delivery-query 配置文件
  27 +@Import({ExpressClientConfig.class})
  28 +public class NoProvideExpressClientTest {
  29 +
  30 + @Resource
  31 + private ExpressClient expressClient;
  32 +
  33 + @Test
  34 + void getExpressTrackList() {
  35 + ServiceException t = assertThrows(ServiceException.class, () -> {
  36 + expressClient.getExpressTrackList(null);
  37 + });
  38 + assertEquals(1011003006, t.getCode());
  39 + }
  40 +
  41 + @Import({
  42 + RestTemplateAutoConfiguration.class,
  43 + })
  44 + @EnableConfigurationProperties(TradeExpressProperties.class)
  45 + public static class Application {
  46 +
  47 + @Bean
  48 + private RestTemplate restTemplate(RestTemplateBuilder builder) {
  49 + return builder.build();
  50 + }
  51 + }
  52 +}
0 \ No newline at end of file 53 \ No newline at end of file
yudao-module-mall/yudao-module-trade-biz/src/test/resources/application-trade-delivery-query.yaml deleted
1 -spring:  
2 - main:  
3 - lazy-initialization: true # 开启懒加载,加快速度  
4 - banner-mode: off # 单元测试,禁用 Banner  
5 -  
6 ---- #################### 交易快递查询相关配置 ####################  
7 -  
8 -yudao:  
9 - trade:  
10 - express:  
11 - query:  
12 - express-query-provider: kd_niao  
13 - kd-niao:  
14 - api-key: xxx  
15 - business-id: xxxxxxxx  
16 - kd100:  
17 - customer: xxxx  
18 - key: xxxxx  
yudao-module-mall/yudao-module-trade-biz/src/test/resources/application-unit-test.yaml
@@ -51,3 +51,11 @@ yudao: @@ -51,3 +51,11 @@ yudao:
51 order: 51 order:
52 app-id: 1 52 app-id: 1
53 merchant-order-id: 1 53 merchant-order-id: 1
  54 + express:
  55 + kd-niao:
  56 + api-key: xxxx
  57 + business-id: xxxxx
  58 + kd100:
  59 + customer: xxxxx
  60 + key: xxxxx
  61 + client: not_provide
54 \ No newline at end of file 62 \ No newline at end of file