瀏覽代碼

万里牛售后订单同步

fgd 1 年之前
父節點
當前提交
6d9caa40bd

+ 12 - 0
sd-wln/src/main/java/com/sd/wln/controller/OrderHandleController.java

@@ -3,6 +3,7 @@ package com.sd.wln.controller;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.exception.ServiceException;
 import com.sd.wln.entity.ResynchronizationDto;
 import com.sd.wln.entity.ResynchronizationDto;
 import com.sd.wln.service.OrderHandleService;
 import com.sd.wln.service.OrderHandleService;
+import com.sd.wln.service.WlnReturnOrderService;
 import com.sd.wln.service.WlnSkuService;
 import com.sd.wln.service.WlnSkuService;
 import com.sd.wln.service.WlnStatementOfAccount;
 import com.sd.wln.service.WlnStatementOfAccount;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -24,6 +25,9 @@ public class OrderHandleController {
     @Autowired
     @Autowired
     private WlnSkuService wlnSkuService;
     private WlnSkuService wlnSkuService;
 
 
+    @Autowired
+    private WlnReturnOrderService wlnReturnOrderService;
+
     /**
     /**
      * 重新同步订单
      * 重新同步订单
      */
      */
@@ -65,4 +69,12 @@ public class OrderHandleController {
         }
         }
     }
     }
 
 
+    /**
+     * 同步万里牛售后单数据
+     */
+    @PostMapping("/resynchronizationReturnOrder")
+    public void resynchronizationReturnOrder() {
+        wlnReturnOrderService.syncReturnOrder();
+    }
+
 }
 }

+ 44 - 0
sd-wln/src/main/java/com/sd/wln/entity/ReturnParam.java

@@ -0,0 +1,44 @@
+package com.sd.wln.entity;
+
+import com.alibaba.fastjson2.JSONObject;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * 查询售后订单参数实体类
+ */
+@Getter
+@Setter
+public class ReturnParam extends Signature {
+
+    /**
+     * 万里牛系统售后单(SH开头)
+     */
+    private String bill_code;
+
+    /**
+     * 开始时间,仅支持近3个月内售后单查询
+     */
+    private Long start_time;
+
+    /**
+     * 结束时间,仅支持与开始时间间隔7天,默认与查询开始时间间隔7天
+     */
+    private Long end_time;
+
+    /**
+     * 时间类型 1:创建时间 2:处理时间 3:完成时间 6:退款完成时间 7:系统级别修改时间 默认为1
+     */
+    private Integer time_type;
+
+    /**
+     * 仓库编码
+     */
+    private String storage_code;
+
+    /**
+     * 扩展字段 为JSON格式
+     * {"status":1}
+     */
+    private JSONObject ext;
+}

+ 12 - 0
sd-wln/src/main/java/com/sd/wln/scheduled/WlnSyncTask.java

@@ -1,6 +1,7 @@
 package com.sd.wln.scheduled;
 package com.sd.wln.scheduled;
 
 
 import com.sd.wln.service.WlnOrderService;
 import com.sd.wln.service.WlnOrderService;
+import com.sd.wln.service.WlnReturnOrderService;
 import com.sd.wln.service.WlnSkuService;
 import com.sd.wln.service.WlnSkuService;
 import com.sd.wln.service.WlnStatementOfAccount;
 import com.sd.wln.service.WlnStatementOfAccount;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -24,6 +25,9 @@ public class WlnSyncTask {
     @Autowired
     @Autowired
     private WlnStatementOfAccount wlnStatementOfAccount;
     private WlnStatementOfAccount wlnStatementOfAccount;
 
 
+    @Autowired
+    private WlnReturnOrderService wlnReturnOrderService;
+
     /**
     /**
      * 每天凌晨1点同步一次sku信息
      * 每天凌晨1点同步一次sku信息
      */
      */
@@ -55,4 +59,12 @@ public class WlnSyncTask {
         wlnStatementOfAccount.createStatementOfAccount();
         wlnStatementOfAccount.createStatementOfAccount();
     }
     }
 
 
+    /**
+     * 同步万里牛售后单数据
+     */
+    @Scheduled(cron = "0 0 0/1 * * ?")
+    public void syncReturnOrder() {
+        wlnReturnOrderService.syncReturnOrder();
+    }
+
 }
 }

+ 9 - 0
sd-wln/src/main/java/com/sd/wln/service/WlnReturnOrderService.java

@@ -0,0 +1,9 @@
+package com.sd.wln.service;
+
+public interface WlnReturnOrderService {
+
+    /**
+     * 同步万里牛售后单数据
+     */
+    void syncReturnOrder();
+}

+ 199 - 0
sd-wln/src/main/java/com/sd/wln/service/impl/WlnReturnOrderServiceImpl.java

@@ -0,0 +1,199 @@
+package com.sd.wln.service.impl;
+
+import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson2.JSONObject;
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
+import com.ruoyi.common.constant.StatusConstant;
+import com.ruoyi.common.core.domain.BaseIdPo;
+import com.sd.business.entity.bom.bo.BomSpecBo;
+import com.sd.business.entity.bom.constant.BomClassifyConstant;
+import com.sd.business.entity.order.enums.OrderClassifyEnum;
+import com.sd.business.entity.order.enums.OrderStatusEnum;
+import com.sd.business.entity.order.po.*;
+import com.sd.business.entity.sku.po.SkuSpec;
+import com.sd.business.entity.warehouse.constant.WarehouseConstant;
+import com.sd.business.service.order.*;
+import com.sd.business.service.sku.SkuSpecService;
+import com.sd.business.util.CodeEnum;
+import com.sd.wln.service.WlnReturnOrderService;
+import com.sd.wln.util.WlnUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.math.BigDecimal;
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Slf4j
+@Service
+public class WlnReturnOrderServiceImpl implements WlnReturnOrderService {
+
+    @Autowired
+    private OrderService orderService;
+
+    @Autowired
+    private OrderSkuService orderSkuService;
+
+    @Autowired
+    private OrderSkuBomService orderSkuBomService;
+
+    @Autowired
+    private SkuSpecService skuSpecService;
+
+    @Autowired
+    private OrderExchangeService orderExchangeService;
+
+    @Autowired
+    private OrderExchangeDetailService orderExchangeDetailService;
+
+
+    @Transactional(rollbackFor = Exception.class)
+    @Override
+    public void syncReturnOrder() {
+
+        List<JSONObject> list = getWlnReturnOrder();
+        // 只获取退货类型的订单
+        List<JSONObject> wlnReturnOrder = list.stream()
+                .filter(item -> Objects.equals(item.getInteger("type"), StatusConstant.NO))
+                .collect(Collectors.toList());
+
+        if (ObjectUtil.isEmpty(wlnReturnOrder)) {
+            return;
+        }
+        // 获取订单信息
+        List<String> wlnOrderCodeList = wlnReturnOrder.stream()
+                .map(item -> item.getString("trade_code"))
+                .filter(ObjectUtil::isNotNull)
+                .collect(Collectors.toList());
+        Map<String, OrderInfo> orderMap = orderService.mapKEntity(OrderInfo::getWlnCode, q -> q.
+                in(OrderInfo::getWlnCode, wlnOrderCodeList)
+                .eq(OrderInfo::getStatus, OrderStatusEnum.COMPLETION_PRODUCTION.getKey()));
+        List<Long> orderIds = orderMap.values().stream().map(BaseIdPo::getId).collect(Collectors.toList());
+        Map<Long, List<OrderSku>> orderSkuMap = orderSkuService.mapKGroup(OrderSku::getOrderId, q -> q.in(OrderSku::getOrderId, orderIds));
+        Map<Long, List<OrderSkuBom>> orderSkuBomMap = orderSkuBomService.mapKGroup(OrderSkuBom::getOrderSkuId, q -> q.in(OrderSkuBom::getOrderId, orderIds));
+        List<Long> bomSpecIdList = orderSkuBomMap.values().stream()
+                .flatMap(item -> item.stream().map(OrderSkuBom::getBomSpecId))
+                .distinct()
+                .collect(Collectors.toList());
+        Map<Long, BomSpecBo> bomSpecBoMap = skuSpecService.getBomSpecBoByIdList(bomSpecIdList);
+
+        // 获取sku信息
+        List<String> wlnSkuSpecUidList = wlnReturnOrder.stream()
+                .flatMap(item -> item.getJSONArray("items").toJavaList(JSONObject.class).stream())
+                .map(item -> item.getString("sys_spec_uid"))
+                .filter(ObjectUtil::isNotNull)
+                .distinct()
+                .collect(Collectors.toList());
+        Map<String, SkuSpec> skuSpecMap = skuSpecService.mapKEntity(SkuSpec::getWlnUid, q -> q.in(SkuSpec::getWlnUid, wlnSkuSpecUidList));
+
+        List<OrderExchange> orderExchangeList = new ArrayList<>();
+        List<OrderExchangeDetail> orderExchangeDetailList = new ArrayList<>();
+
+        for (JSONObject item : wlnReturnOrder) {
+            OrderInfo orderInfo = orderMap.get(item.getString("trade_code"));
+            if (orderInfo == null || Objects.equals(orderInfo.getClassify(), OrderClassifyEnum.NO_REASON_ORDER.getKey())) {
+                continue;
+            }
+
+            // 出库单明细
+            List<JSONObject> detailsList = item.getJSONArray("items").toJavaList(JSONObject.class);
+            OrderExchange orderExchange = new OrderExchange();
+            orderExchange.setId(IdWorker.getId());
+            orderExchange.setCode(CodeEnum.TH_CODE.getCode());
+            orderExchange.setOrderInfoId(orderInfo.getId());
+            orderExchange.setWarehouseId(WarehouseConstant.FINISHED_PRODUCT);
+            orderExchange.setType(StatusConstant.YES);
+            orderExchange.setStatus(StatusConstant.NO);
+            orderExchange.setReason(item.getString("reason"));
+            orderExchange.setRemark(item.getString("describe"));
+            orderExchange.setCompletionTime(item.getDate("end_time"));
+            orderExchange.setCompletionTimestamp(item.getLong("end_time"));
+            orderExchangeList.add(orderExchange);
+            for (JSONObject itemDetails : detailsList) {
+                // 获取订单sku id
+                SkuSpec skuSpec = skuSpecMap.get(itemDetails.getString("sys_spec_uid"));
+                if (skuSpec == null || Objects.equals(skuSpec.getGiftTag(), StatusConstant.YES)) {
+                    continue;
+                }
+                OrderSku orderSku = orderSkuMap
+                        .getOrDefault(orderInfo.getId(),Collections.emptyList())
+                        .stream()
+                        .filter(i -> Objects.equals(i.getSkuSpecId(), skuSpec.getId()))
+                        .findAny()
+                        .orElse(null);
+                if (orderSku == null) {
+                    continue;
+                }
+                // 包材成本只退可二次利用的包材
+                List<OrderSkuBom> orderSkuBomList = orderSkuBomMap.getOrDefault(orderSku.getId(), Collections.emptyList());
+                BigDecimal packagingMaterialCost = orderSkuBomList.stream()
+                        .map(b -> {
+                            BomSpecBo bomSpecBo = bomSpecBoMap.get(b.getBomSpecId());
+                            if (bomSpecBo == null
+                                    || !(Objects.equals(bomSpecBo.getClassifyId(), BomClassifyConstant.MESH_BAG)
+                                    || Objects.equals(bomSpecBo.getClassifyId(), BomClassifyConstant.SUSPENDERS))) {
+                                return BigDecimal.ZERO;
+                            }
+                            return b.getUnitPrice().multiply(b.getQuantity());
+                        })
+                        .reduce(BigDecimal.ZERO, BigDecimal::add);
+
+
+                OrderExchangeDetail orderExchangeDetail = new OrderExchangeDetail();
+                orderExchangeDetail.setOrderExchangeId(orderExchange.getId());
+                orderExchangeDetail.setQuantity(itemDetails.getBigDecimal("size"));
+                orderExchangeDetail.setReturnStatus(StatusConstant.NO);
+                orderExchangeDetail.setExchangeStatus(StatusConstant.NO);
+                orderExchangeDetail.setOrderSkuId(orderSku.getId());
+                orderExchangeDetail.setCheckPassesQuantity(BigDecimal.ZERO);
+                orderExchangeDetail.setReturnAmount((orderSku.getUnitPrice()
+                        .add(packagingMaterialCost)).multiply(orderExchangeDetail.getQuantity()));
+                orderExchangeDetailList.add(orderExchangeDetail);
+            }
+        }
+
+        orderExchangeService.saveBatch(orderExchangeList);
+        orderExchangeDetailService.saveBatch(orderExchangeDetailList);
+
+    }
+
+
+
+    /**
+     * 查询近万里牛售后单
+     */
+    private List<JSONObject> getWlnReturnOrder() {
+        OrderExchange orderExchange = orderExchangeService.getOne(q -> q
+                .isNotNull(OrderExchange::getCompletionTimestamp)
+                .orderByDesc(OrderExchange::getCompletionTimestamp));
+
+        long startTime;
+        long endTime = System.currentTimeMillis();
+        if (orderExchange != null) {
+            startTime = orderExchange.getCompletionTimestamp() + 1;
+        } else {
+            startTime = endTime - 1000 * 60 * 60 * 24 * 7;
+        }
+
+        List<JSONObject> list = new ArrayList<>();
+
+        int page = 1;
+        int size;
+        do {
+            try {
+                List<JSONObject> itemList = WlnUtil.getReturnOrderList(page, 200, startTime, endTime);
+                page++;
+                size = itemList.size();
+                list.addAll(itemList);
+            } catch (Exception e) {
+                log.error("售后单同步失败", e);
+                return Collections.emptyList();
+            }
+        } while (size >= 200);
+
+        return list;
+    }
+
+}

+ 26 - 4
sd-wln/src/main/java/com/sd/wln/util/WlnUtil.java

@@ -6,10 +6,7 @@ import com.alibaba.fastjson2.JSONObject;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.MapUtil;
 import com.ruoyi.common.utils.MapUtil;
 import com.sd.wln.constants.WlnConstant;
 import com.sd.wln.constants.WlnConstant;
-import com.sd.wln.entity.GoodsSpecParam;
-import com.sd.wln.entity.SkuClassifyParam;
-import com.sd.wln.entity.StockParam;
-import com.sd.wln.entity.TradesParam;
+import com.sd.wln.entity.*;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntity;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.methods.HttpPost;
@@ -210,4 +207,29 @@ public class WlnUtil {
         return json.getJSONArray("data").toJavaList(JSONObject.class);
         return json.getJSONArray("data").toJavaList(JSONObject.class);
     }
     }
 
 
+    /**
+     * 获取订单
+     */
+    public static List<JSONObject> getReturnOrderList(Integer page, Integer limit, Long startTime, Long endTime) throws Exception {
+        ReturnParam param = new ReturnParam();
+        param.setPage(page);
+        param.setLimit(limit);
+        param.setStart_time(startTime);
+        param.setEnd_time(endTime);
+        param.setTime_type(2);
+        param.setStorage_code("T007");
+        JSONObject ext = new JSONObject();
+        ext.put("status", 1);
+        param.setExt(ext);
+        param.generateSign(MapUtil.createLinkString(MapUtil.beanToMap(param)));
+
+        String result = send(PREFIX + "erp/open/return/order/list", MapUtil.beanToMap(param));
+        JSONObject json = JSONObject.parseObject(result);
+        Integer code = json.getInteger("code");
+        if (code != 0) {
+            throw new ServiceException(result);
+        }
+        return json.getJSONArray("data").toJavaList(JSONObject.class);
+    }
+
 }
 }