Parcourir la source

调整参数错误

yfh il y a 4 jours
Parent
commit
defdb04160

+ 57 - 27
fs-service/src/main/java/com/fs/erp/service/impl/WdtErpOrderServiceImpl.java

@@ -25,13 +25,14 @@ import com.fs.his.service.IFsStoreOrderItemService;
 import com.fs.his.service.IFsStoreOrderService;
 import com.fs.his.service.IFsStoreProductService;
 import com.fs.his.utils.ConfigUtil;
-import com.fs.his.vo.FsStoreOrderItemVO;
+import com.fs.hisStore.config.FsErpConfig;
 import com.fs.hisStore.domain.FsStoreOrderItemScrm;
 import com.fs.hisStore.domain.FsStoreOrderScrm;
 import com.fs.hisStore.domain.FsStoreProductScrm;
 import com.fs.hisStore.service.IFsStoreOrderItemScrmService;
 import com.fs.hisStore.service.IFsStoreOrderScrmService;
 import com.fs.hisStore.service.IFsStoreProductScrmService;
+import com.fs.hisStore.vo.FsStoreOrderItemVO;
 import com.hc.openapi.tool.util.StringUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
@@ -287,21 +288,44 @@ public class WdtErpOrderServiceImpl implements IErpOrderService {
         //测试环境sid、appkey、密钥请到旺店通开放平台-自助对接-申请测试环境内查看,测试环境url=https://sandbox.wangdian.cn/openapi2/
         //调用正式环境时请将sid、appkey、appsecret切换为实际参数,参数在旺店通开放平台-自助对接-应用管理内应用状态为已上线的应用中查看,调用正式环境注意切换正式环境url=https://api.wangdian.cn/openapi2/
 
-        FsSysConfig erpconfig = configUtil.getSysConfig();
+        FsErpConfig erpconfig = configUtil.getErpConfig();
         ErpWdtBusinessRequestParams erpWdtBusinessRequestParams = new ErpWdtBusinessRequestParams();
         erpWdtBusinessRequestParams.setShopNo(erpconfig.getErpWdShopCode());
         erpWdtBusinessRequestParams.setSwitchMode(0);
         ErpWdtTrade erpWdtTrade = new ErpWdtTrade();
+        FsStoreOrderScrm fsStoreOrder = fsStoreOrderScrmService.selectFsStoreOrderByOrderCode(order.getPlatform_code());
+        BigDecimal couponPrice = fsStoreOrder.getCouponPrice();
+
+        // 平台状态
+        erpWdtTrade.setTradeStatus(TradeStatus.PAID_WAITING_FOR_SHIPMENT.getValue());
 
         // 订单id
         erpWdtTrade.setTid(order.getPlatform_code());
-        // 平台状态
-        erpWdtTrade.setTradeStatus(TradeStatus.REFUNDED.getValue());
 
-        // 支付状态
-        erpWdtTrade.setPayStatus(PaymentStatus.FULLY_PAID.getValue());
-        // 发货条件
-        erpWdtTrade.setDeliveryTerm(DeliveryTerm.PAYMENT_BEFORE_DELIVERY.getValue());
+
+        // 如果是货到付款
+        if("2".equals(fsStoreOrder.getPayType())){
+            // 支付状态
+            erpWdtTrade.setPayStatus(PaymentStatus.PARTIALLY_PAID.getValue());
+            // 发货条件
+            erpWdtTrade.setDeliveryTerm(DeliveryTerm.CASH_ON_DELIVERY.getValue());
+            // 货到付款金额 = 物流代收金额-优惠金额
+            erpWdtTrade.setCodAmount(fsStoreOrder.getPayDelivery().subtract(couponPrice));
+
+        } else if("3".equals(fsStoreOrder.getPayType())){
+            // 支付状态
+            erpWdtTrade.setPayStatus(PaymentStatus.UNPAID.getValue());
+            // 发货条件
+            erpWdtTrade.setDeliveryTerm(DeliveryTerm.CASH_ON_DELIVERY.getValue());
+            // 货到付款金额 = 物流代收金额-优惠金额
+            erpWdtTrade.setCodAmount(fsStoreOrder.getPayDelivery().subtract(couponPrice));
+        } else { // 如果是线上付款
+            // 支付状态
+            erpWdtTrade.setPayStatus(PaymentStatus.FULLY_PAID.getValue());
+            // 发货条件
+            erpWdtTrade.setDeliveryTerm(DeliveryTerm.PAYMENT_BEFORE_DELIVERY.getValue());
+        }
+
         // 下单时间
         erpWdtTrade.setTradeTime(order.getDeal_datetime());
         // 支付时间
@@ -330,41 +354,43 @@ public class WdtErpOrderServiceImpl implements IErpOrderService {
         erpWdtTrade.setSellerMemo(order.getSeller_memo());
         erpWdtTrade.setWarehouseNo(order.getWarehouse_code());
 
-        // 已付金额
-        FsStoreOrderScrm fsStoreOrder = fsStoreOrderScrmService.selectFsStoreOrderByOrderCode(order.getPlatform_code());
         // 运费金额
         erpWdtTrade.setPostAmount(fsStoreOrder.getFreightPrice());
+        // 已付金额
         if(ObjectUtils.isNotNull(fsStoreOrder)){
             erpWdtTrade.setPaid(fsStoreOrder.getPayMoney());
         }
 
-        List<FsStoreOrderItemScrm> fsStoreOrderItemVOS = fsStoreOrderScrmService.selectFsStoreOrderItemList(String.valueOf(fsStoreOrder.getId()));
+        List<FsStoreOrderItemVO> fsStoreOrderItemVOS = fsStoreOrderItemScrmService.selectFsStoreOrderItemListByOrderId(fsStoreOrder.getId());
         List<ErpWdtOrder> erpWdtOrderList = new ArrayList<>();
+        // 商品总价
+        BigDecimal totalPrice = fsStoreOrder.getTotalPrice();
 
+        // 总折扣金额 = 优惠券金额 + 折扣金额
+        BigDecimal totalDiscountPrice = BigDecimal.ZERO;
+        // 折扣金额
+        BigDecimal discountPrice = totalPrice.subtract(fsStoreOrder.getPayPrice());
+        totalDiscountPrice = couponPrice.add(discountPrice);
 
-        // 优惠券金额
-        BigDecimal couponPrice = fsStoreOrder.getCouponPrice();
-        if (fsStoreOrder.getCouponPrice().compareTo(BigDecimal.ZERO) == 0
-                && fsStoreOrder.getTotalPrice().compareTo(fsStoreOrder.getPayMoney()) > 0) {
-            couponPrice = fsStoreOrder.getTotalPrice().subtract(fsStoreOrder.getPayMoney());
+        // 防止除零错误
+        if (totalPrice.compareTo(BigDecimal.ZERO) == 0) {
+            totalPrice = BigDecimal.ONE;
         }
-        // 商品总价
-        BigDecimal totalPrice = fsStoreOrder.getTotalPrice();
 
-        for (FsStoreOrderItemScrm fsStoreOrderItem : fsStoreOrderItemVOS) {
+        for (FsStoreOrderItemVO fsStoreOrderItem : fsStoreOrderItemVOS) {
             ErpWdtOrder erpWdtOrder = new ErpWdtOrder();
             //平台订单货品表主键
             erpWdtOrder.setOid(String.format("%s%s",fsStoreOrderItem.getOrderCode(),fsStoreOrderItem.getItemId()));
             erpWdtOrder.setNum(BigDecimal.valueOf(fsStoreOrderItem.getNum()));
             FsStoreProductScrm fsStoreProduct = fsStoreProductScrmService.selectFsStoreProductById(fsStoreOrderItem.getProductId());
             Asserts.check(ObjectUtils.isNotNull(fsStoreProduct),"该产品不存在! 产品id: {} ",fsStoreOrderItem.getProductId());
+
             // 单价
-            erpWdtOrder.setPrice(fsStoreProduct.getPrice());
+//            erpWdtOrder.setPrice(fsStoreProduct.getPrice());
             // 状态
-
-            erpWdtOrder.setStatus(TradeStatus.REFUNDED.getValue());
+            erpWdtOrder.setStatus(TradeStatus.SHIPPED.getValue());
             // 退款状态
-            erpWdtOrder.setRefundStatus(RefundStatus.REFUND_SUCCESSFUL.getValue());
+            erpWdtOrder.setRefundStatus(RefundStatus.NO_REFUND.getValue());
 
             // 平台货品ID
             erpWdtOrder.setGoodsId(String.valueOf(fsStoreProduct.getProductId()));
@@ -372,6 +398,8 @@ public class WdtErpOrderServiceImpl implements IErpOrderService {
             erpWdtOrder.setSpecId(jsonObject.getString("sku"));
             erpWdtOrder.setGoodsNo(jsonObject.getString("barCode"));
             erpWdtOrder.setSpecNo(jsonObject.getString("sku"));
+            //由于商品改价 用item里面的单价
+            erpWdtOrder.setPrice(jsonObject.getBigDecimal("price"));
             // 货品名称
             erpWdtOrder.setGoodsName(fsStoreProduct.getProductName());
             // 调整
@@ -382,7 +410,7 @@ public class WdtErpOrderServiceImpl implements IErpOrderService {
             // 分摊比例
             BigDecimal price = fsStoreProduct.getPrice().multiply(BigDecimal.valueOf(fsStoreOrderItem.getNum()));
             BigDecimal divide = price.divide(totalPrice, RoundingMode.HALF_UP);
-            erpWdtOrder.setShareDiscount(divide.multiply(couponPrice));
+            erpWdtOrder.setShareDiscount(divide.multiply(totalDiscountPrice));
 
             erpWdtOrderList.add(erpWdtOrder);
         }
@@ -392,7 +420,7 @@ public class WdtErpOrderServiceImpl implements IErpOrderService {
         long size = erpWdtOrderList.size();
         if(size > 1) {
             ErpWdtOrder erpWdtOrder = erpWdtOrderList.get(erpWdtOrderList.size() - 1);
-            erpWdtOrder.setShareDiscount(couponPrice.subtract(erpWdtOrderList.stream()
+            erpWdtOrder.setShareDiscount(totalDiscountPrice.subtract(erpWdtOrderList.stream()
                     .limit(size - 1L)
                     .map(item -> Optional.ofNullable(item.getShareDiscount()).orElse(BigDecimal.ZERO))
                     .reduce(BigDecimal.ZERO, BigDecimal::add)));
@@ -411,15 +439,17 @@ public class WdtErpOrderServiceImpl implements IErpOrderService {
             String response = client.execute("trade_push.php", map);
             ParserConfig config = new ParserConfig();
             config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
-            log.info("Erp推送传参: {}", map);
             ErpWdtApiResponse erpWdtApiResponse = JSON.parseObject(response, ErpWdtApiResponse.class,config);
             if(ObjectUtil.equal(0,erpWdtApiResponse.getCode())){
                 log.info("订单推送成功: {}", response);
                 ErpOrderResponse erpOrderResponse = new ErpOrderResponse();
                 erpOrderResponse.setCode(order.getPlatform_code());
+                erpOrderResponse.setSuccess(true);
+                erpOrderResponse.setRequestRawData(JSON.toJSONString(map));
+                erpOrderResponse.setResponseRawData(response);
                 return erpOrderResponse;
             } else {
-                // throw new RuntimeException(String.format("订单推送失败,原因: %s",erpWdtApiResponse.getMessage()));
+                throw new RuntimeException(String.format("订单推送失败,原因: %s",erpWdtApiResponse.getMessage()));
             }
         } catch (IOException e) {
             e.printStackTrace();