Explorar el Código

feat: 京东物流接口对接;

hanchaolong hace 1 semana
padre
commit
7f990b3fbc

+ 9 - 0
jd-logistics-modules/jd-logistics-system/src/main/java/com/ruoyi/logistics/constant/SysConfigConstants.java

@@ -0,0 +1,9 @@
+package com.ruoyi.logistics.constant;
+
+public class SysConfigConstants {
+
+    /**
+     * 京东客户编码
+     */
+    public static final String JD_LOGISTICS_CUSTOMERCODE = "jd.logistics.customercode";
+}

+ 22 - 0
jd-logistics-modules/jd-logistics-system/src/main/java/com/ruoyi/logistics/domain/BizWaybillOrder.java

@@ -152,6 +152,10 @@ public class BizWaybillOrder extends BaseEntity
     @Excel(name = "用户ID")
     private Long userId;
 
+    private String productCode;
+
+    private String addedService;
+
     public void setWaybillId(Long waybillId) 
     {
         this.waybillId = waybillId;
@@ -482,6 +486,22 @@ public class BizWaybillOrder extends BaseEntity
         return userId;
     }
 
+    public String getProductCode() {
+        return productCode;
+    }
+
+    public void setProductCode(String productCode) {
+        this.productCode = productCode;
+    }
+
+    public String getAddedService() {
+        return addedService;
+    }
+
+    public void setAddedService(String addedService) {
+        this.addedService = addedService;
+    }
+
     @Override
     public String toString() {
         return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
@@ -523,6 +543,8 @@ public class BizWaybillOrder extends BaseEntity
             .append("updateBy", getUpdateBy())
             .append("deptId", getDeptId())
             .append("userId", getUserId())
+            .append("productCode", getProductCode())
+            .append("addedService", getAddedService())
             .toString();
     }
 }

+ 60 - 15
jd-logistics-modules/jd-logistics-system/src/main/java/com/ruoyi/logistics/service/impl/JDLogisticsService.java

@@ -6,20 +6,26 @@ import com.lop.open.api.sdk.LopException;
 import com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCheckPreCreateOrderV1.CommonCreateOrderRequest;
 import com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCheckPreCreateOrderV1.CommonProductInfo;
 import com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCheckPreCreateOrderV1.Contact;
+import com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.CommonCargoInfo;
 import com.lop.open.api.sdk.plugin.LopPlugin;
 import com.lop.open.api.sdk.plugin.factory.OAuth2PluginFactory;
 import com.lop.open.api.sdk.request.ECAP.EcapV1OrdersCreateLopRequest;
 import com.lop.open.api.sdk.request.ECAP.EcapV1OrdersPrecheckLopRequest;
 import com.lop.open.api.sdk.response.ECAP.EcapV1OrdersCreateLopResponse;
 import com.lop.open.api.sdk.response.ECAP.EcapV1OrdersPrecheckLopResponse;
+import com.ruoyi.common.core.utils.DateUtils;
 import com.ruoyi.logistics.config.JDExpressConfig;
+import com.ruoyi.logistics.constant.SysConfigConstants;
 import com.ruoyi.logistics.domain.BizWaybillOrder;
-import com.ruoyi.logistics.domain.LogisticsOrder;
 import com.ruoyi.logistics.service.LogisticsService;
+import com.ruoyi.system.service.ISysConfigService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
+import java.util.List;
+
 @Slf4j
 @Service
 public class JDLogisticsService  implements LogisticsService {
@@ -27,28 +33,40 @@ public class JDLogisticsService  implements LogisticsService {
     @Autowired
     private JDExpressConfig jdExpressConfig;
 
+    @Autowired
+    private ISysConfigService sysConfigService;
+
     @Override
     public JSONObject precheckOrder(BizWaybillOrder bizWaybillOrder) {
-        log.info("调用京东开放平台订单预校验接口");
+        log.info("京东开放平台订单预校验接口,入参:{}", bizWaybillOrder);
         JSONObject result = new JSONObject();
         try {
             //设置接口域名(有的对接方案同时支持生产和沙箱环境,有的仅支持生产,具体以对接方案中的【API文档-请求地址】为准),生产域名:https://api.jdl.com 预发环境域名:https://uat-api.jdl.com
             //DefaultDomainApiClient对象全局只需要创建一次
             DefaultDomainApiClient client = new DefaultDomainApiClient(jdExpressConfig.getServerUrl(),500,15000);
 
-            //入参对象(请记得更换为自己要使用的接口入参对象)
+            //入参对象
             EcapV1OrdersPrecheckLopRequest request = new EcapV1OrdersPrecheckLopRequest();
 
-            //设置入参(请记得更换为自己要使用的接口入参)
+            //发货人
             Contact senderContact  = new Contact();
-            senderContact.setFullAddress("河北省廊坊市广阳区万庄镇中心小学");
+            senderContact.setName(bizWaybillOrder.getSenderName());
+            senderContact.setMobile(bizWaybillOrder.getSenderPhone());
+            senderContact.setFullAddress(bizWaybillOrder.getSenderProvince() + bizWaybillOrder.getSenderCity() + bizWaybillOrder.getSenderCounty() + bizWaybillOrder.getSenderAddress());
+
+            //收货人
             Contact receiverContact = new Contact();
-            receiverContact.setFullAddress("河北省廊坊市广阳区万庄镇中心小学");
+            receiverContact.setName(bizWaybillOrder.getReceiverName());
+            receiverContact.setMobile(bizWaybillOrder.getReceiverPhone());
+            receiverContact.setFullAddress(bizWaybillOrder.getReceiverProvince() + bizWaybillOrder.getReceiverCity() + bizWaybillOrder.getReceiverCounty() + bizWaybillOrder.getReceiverAddress());
+
             CommonCreateOrderRequest requestDTO = new  CommonCreateOrderRequest();
             requestDTO.setOrderOrigin(1);
-            requestDTO.setCustomerCode("010K****20");
+            requestDTO.setCustomerCode(sysConfigService.selectConfigByKey(SysConfigConstants.JD_LOGISTICS_CUSTOMERCODE));
+
             CommonProductInfo productInfo = new CommonProductInfo();
-            productInfo.setProductCode("ed-m-0001");
+            productInfo.setProductCode(bizWaybillOrder.getProductCode());
+
             requestDTO.setProductsReq(productInfo);
             requestDTO.setReceiverContact(receiverContact);
             requestDTO.setSenderContact(senderContact);
@@ -59,7 +77,9 @@ public class JDLogisticsService  implements LogisticsService {
             LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(jdExpressConfig.getAppKey(), jdExpressConfig.getAppSecret(), jdExpressConfig.getAccessToken());
             request.addLopPlugin(lopPlugin);
 
+            log.info("京东开放平台订单预校验接口,请求参数:{}", request);
             EcapV1OrdersPrecheckLopResponse response = client.execute(request);
+            log.info("京东开放平台订单预校验接口,响应结果:{}", response);
         } catch (LopException e) {
             e.printStackTrace();
         } catch (Exception e) {
@@ -70,25 +90,48 @@ public class JDLogisticsService  implements LogisticsService {
 
     @Override
     public JSONObject createOrder(BizWaybillOrder bizWaybillOrder) {
-        log.info("调用京东开放平台订单预校验接口");
+        log.info("京东开放平台下单接口,入参:{}", bizWaybillOrder);
         JSONObject result = new JSONObject();
         try {
             //设置接口域名(有的对接方案同时支持生产和沙箱环境,有的仅支持生产,具体以对接方案中的【API文档-请求地址】为准),生产域名:https://api.jdl.com 预发环境域名:https://uat-api.jdl.com
             //DefaultDomainApiClient对象全局只需要创建一次
             DefaultDomainApiClient client = new DefaultDomainApiClient(jdExpressConfig.getServerUrl(),500,15000);
 
-            //入参对象(请记得更换为自己要使用的接口入参对象)
+            //入参对象
             EcapV1OrdersCreateLopRequest request = new EcapV1OrdersCreateLopRequest();
-            //设置入参(请记得更换为自己要使用的接口入参)
+
+            //发货人
             com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.Contact senderContact  = new com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.Contact();
-            senderContact.setFullAddress("河北省廊坊市广阳区万庄镇中心小学");
+            senderContact.setFullAddress(bizWaybillOrder.getSenderProvince() + bizWaybillOrder.getSenderCity() + bizWaybillOrder.getSenderCounty() + bizWaybillOrder.getSenderAddress());
+
+            //收货人
             com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.Contact receiverContact = new com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.Contact();
-            receiverContact.setFullAddress("河北省廊坊市广阳区万庄镇中心小学");
+            receiverContact.setFullAddress(bizWaybillOrder.getReceiverProvince() + bizWaybillOrder.getReceiverCity() + bizWaybillOrder.getReceiverCounty() + bizWaybillOrder.getReceiverAddress());
+
+            //订单信息
             com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.CommonCreateOrderRequest requestDTO = new  com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.CommonCreateOrderRequest();
+            requestDTO.setOrderId(bizWaybillOrder.getWaybillNo());
             requestDTO.setOrderOrigin(1);
-            requestDTO.setCustomerCode("010K****20");
+            requestDTO.setCustomerCode(sysConfigService.selectConfigByKey(SysConfigConstants.JD_LOGISTICS_CUSTOMERCODE));
+            requestDTO.setSettleType(3);
+            requestDTO.setPickupStartTime(bizWaybillOrder.getPickupTime().getTime());
+            requestDTO.setPickupEndTime(DateUtils.addHours(bizWaybillOrder.getPickupTime(), 1).getTime());
+            requestDTO.setPickupType(1);
+
+            //产品信息
             com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.CommonProductInfo productInfo = new com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.CommonProductInfo();
-            productInfo.setProductCode("ed-m-0001");
+            productInfo.setProductCode(bizWaybillOrder.getProductCode());
+
+            //货物信息
+            List<CommonCargoInfo> cargoes = new ArrayList<>();
+            CommonCargoInfo cargoInfo = new CommonCargoInfo();
+            cargoInfo.setName(bizWaybillOrder.getGoodsName());
+            cargoInfo.setQuantity(bizWaybillOrder.getGoodsQty().intValue());
+            cargoInfo.setWeight(bizWaybillOrder.getGoodsWeight());
+            cargoInfo.setVolume(bizWaybillOrder.getGoodsVolume());
+            cargoes.add(cargoInfo);
+
+            requestDTO.setCargoes(cargoes);
             requestDTO.setProductsReq(productInfo);
             requestDTO.setReceiverContact(receiverContact);
             requestDTO.setSenderContact(senderContact);
@@ -99,7 +142,9 @@ public class JDLogisticsService  implements LogisticsService {
             LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(jdExpressConfig.getAppKey(), jdExpressConfig.getAppSecret(), jdExpressConfig.getAccessToken());
             request.addLopPlugin(lopPlugin);
 
+            log.info("京东开放平台下单接口,请求参数:{}", request);
             EcapV1OrdersCreateLopResponse response = client.execute(request);
+            log.info("京东开放平台下单接口,响应结果:{}", response);
         } catch (LopException e) {
             e.printStackTrace();
         } catch (Exception e) {