|
@@ -1,23 +1,22 @@
|
|
|
package com.sd.wln.service.impl;
|
|
|
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+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.after.po.AfterSaleOrder;
|
|
|
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.after.AfterSaleOrderService;
|
|
|
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.service.WlnSalesReturnStockInService;
|
|
|
import com.sd.wln.util.WlnUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -53,27 +52,32 @@ public class WLnSalesReturnStockInServiceImpl implements WlnSalesReturnStockInSe
|
|
|
@Autowired
|
|
|
private OrderExchangeDetailService orderExchangeDetailService;
|
|
|
|
|
|
- @Autowired
|
|
|
- private AfterSaleOrderService afterSaleOrderService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private WlnReturnOrderService wlnReturnOrderService;
|
|
|
-
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
public void syncSalesReturnStockIn() {
|
|
|
- // 同步售后单明细
|
|
|
- wlnReturnOrderService.syncReturnOrder();
|
|
|
-
|
|
|
// 获取万里牛销售退货入库明细
|
|
|
- List<JSONObject> list = getWlnSalesReturnStockIn();
|
|
|
+ List<JSONObject> data = getWlnSalesReturnStockIn();
|
|
|
|
|
|
- List<AfterSaleOrder> afterSaleOrderList = afterSaleOrderService.list(q -> q.eq(AfterSaleOrder::getStatus, StatusConstant.NO));
|
|
|
+ List<JSONObject> wlnSalesReturnStockInList = data.stream()
|
|
|
+ .filter(item -> Objects.equals(item.getString("storage_code"), "T007"))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ if (ObjectUtil.isEmpty(wlnSalesReturnStockInList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
// 获取订单数据
|
|
|
- List<String> orderWlnCodeList = afterSaleOrderList.stream().map(AfterSaleOrder::getOrderWlnCode).collect(Collectors.toList());
|
|
|
- Map<String, OrderInfo> orderInfoMap = orderService.mapKEntity(OrderInfo::getWlnCode, q -> q.in(OrderInfo::getWlnCode, orderWlnCodeList));
|
|
|
- List<Long> orderIds = orderInfoMap.values().stream().map(BaseIdPo::getId).collect(Collectors.toList());
|
|
|
+ Map<Integer, List<String>> orderCodeMap = wlnSalesReturnStockInList.stream()
|
|
|
+ .map(item -> item.getString("tp_tid"))
|
|
|
+ .collect(Collectors.groupingBy(item -> item.startsWith("XD") ? 1 : 0));
|
|
|
+ List<String> orderCodeList = orderCodeMap.getOrDefault(0, Collections.emptyList());
|
|
|
+ List<String> orderWlnCodeList = orderCodeMap.getOrDefault(1, Collections.emptyList());
|
|
|
+
|
|
|
+ List<OrderInfo> orderInfoList = orderService.list(q -> q.eq(OrderInfo::getStatus, OrderStatusEnum.COMPLETION_PRODUCTION.getKey())
|
|
|
+ .and(i -> i.in(!orderCodeList.isEmpty(), OrderInfo::getCode, orderCodeList)
|
|
|
+ .or(!orderWlnCodeList.isEmpty(), o -> o.in(OrderInfo::getWlnCode, orderWlnCodeList))));
|
|
|
+
|
|
|
+ List<Long> orderIds = orderInfoList.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()
|
|
@@ -83,76 +87,75 @@ public class WLnSalesReturnStockInServiceImpl implements WlnSalesReturnStockInSe
|
|
|
Map<Long, BomSpecBo> bomSpecBoMap = skuSpecService.getBomSpecBoByIdList(bomSpecIdList);
|
|
|
|
|
|
// 获取sku信息
|
|
|
- List<String> skuSpecUidList = afterSaleOrderList.stream().map(AfterSaleOrder::getWlnSkuSpecId).collect(Collectors.toList());
|
|
|
- Map<String, SkuSpec> skuSpecMap = skuSpecService.mapKEntity(SkuSpec::getWlnUid, q -> q.in(SkuSpec::getWlnUid, skuSpecUidList));
|
|
|
+ List<String> skuSpecList = wlnSalesReturnStockInList.stream()
|
|
|
+ .map(item -> item.getJSONArray("details").toJavaList(JSONObject.class))
|
|
|
+ .flatMap(Collection::stream)
|
|
|
+ .map(item -> item.getString("sku_no"))
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ Map<String, SkuSpec> skuSpecMap = skuSpecService.mapKEntity(SkuSpec::getCode, q -> q.in(SkuSpec::getCode, skuSpecList));
|
|
|
|
|
|
- Map<String, List<AfterSaleOrder>> orderCodeMap = afterSaleOrderList.stream().collect(Collectors.groupingBy(AfterSaleOrder::getOrderCode));
|
|
|
- Map<String, List<AfterSaleOrder>> orderWlnCodeMap = afterSaleOrderList.stream().collect(Collectors.groupingBy(AfterSaleOrder::getOrderWlnCode));
|
|
|
- Map<String, List<AfterSaleOrder>> codeMap = afterSaleOrderList.stream().collect(Collectors.groupingBy(AfterSaleOrder::getCode));
|
|
|
+ // 订单单号可能会有多个
|
|
|
+ Map<String, List<OrderInfo>> codeOrderMap = orderInfoList.stream().collect(Collectors.groupingBy(OrderInfo::getCode));
|
|
|
+ Map<String, OrderInfo> wlnCodeOrderMap = orderInfoList.stream().collect(Collectors.toMap(OrderInfo::getWlnCode, Function.identity()));
|
|
|
|
|
|
List<OrderExchange> orderExchangeList = new ArrayList<>();
|
|
|
List<OrderExchangeDetail> orderExchangeDetailList = new ArrayList<>();
|
|
|
- List<Long> editAfterSaleOrderIdList = new ArrayList<>();
|
|
|
|
|
|
String thCode = CodeEnum.TH_CODE.getCode();
|
|
|
- for (JSONObject item : list) {
|
|
|
- if (!Objects.equals(item.getString("storage_code"), "T007")) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取万里牛售后单数据
|
|
|
- List<AfterSaleOrder> saleOrderList = new ArrayList<>();
|
|
|
- String fromExchangeNo = item.getString("from_exchange_no");
|
|
|
- if (StrUtil.isNotBlank(fromExchangeNo)) {
|
|
|
- saleOrderList = codeMap.getOrDefault(fromExchangeNo, Collections.emptyList());
|
|
|
- }
|
|
|
- // 订单号存在两种,XD和普通单号
|
|
|
+ for (JSONObject item : wlnSalesReturnStockInList) {
|
|
|
+ // 获取订单,订单号存在两种,XD和普通单号
|
|
|
String orderCode = item.getString("tp_tid");
|
|
|
- if (saleOrderList.isEmpty()) {
|
|
|
- saleOrderList = orderCodeMap.getOrDefault(orderCode, Collections.emptyList());
|
|
|
- }
|
|
|
- if (saleOrderList.isEmpty()) {
|
|
|
- saleOrderList = orderWlnCodeMap.getOrDefault(orderCode, Collections.emptyList());
|
|
|
+ List<OrderInfo> orderList = new ArrayList<>();
|
|
|
+ if (orderCode.startsWith("XD")) {
|
|
|
+ OrderInfo orderInfo = wlnCodeOrderMap.get(orderCode);
|
|
|
+ if (orderInfo != null) {
|
|
|
+ orderList.add(orderInfo);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ orderList = codeOrderMap.getOrDefault(orderCode, Collections.emptyList());
|
|
|
}
|
|
|
|
|
|
- // 没有万里牛售后单时,不生成售后订单
|
|
|
- if (saleOrderList.isEmpty()) {
|
|
|
+ if (orderList.isEmpty()) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- Map<String, AfterSaleOrder> afterSaleOrderMap = saleOrderList.stream().collect(Collectors.toMap(AfterSaleOrder::getSkuSpecCode, Function.identity()));
|
|
|
+ Map<Long, OrderInfo> orderInfoMap = orderList.stream().collect(Collectors.toMap(BaseIdPo::getId, Function.identity()));
|
|
|
+
|
|
|
+ // 获取订单明细
|
|
|
+ List<OrderSku> orderSkuList = orderList.stream()
|
|
|
+ .map(i -> orderSkuMap.getOrDefault(i.getId(), Collections.emptyList()))
|
|
|
+ .flatMap(Collection::stream)
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
|
// 入库单明细
|
|
|
List<JSONObject> detailsList = item.getJSONArray("details").toJavaList(JSONObject.class);
|
|
|
|
|
|
-
|
|
|
// 出库单明细
|
|
|
OrderExchange orderExchange = null;
|
|
|
|
|
|
for (JSONObject itemDetails : detailsList) {
|
|
|
- AfterSaleOrder afterSaleOrder = afterSaleOrderMap.get(itemDetails.getString("sku_no"));
|
|
|
- if (afterSaleOrder == null || Objects.equals(afterSaleOrder.getStatus(), StatusConstant.YES)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- OrderInfo orderInfo = orderInfoMap.get(afterSaleOrder.getOrderWlnCode());
|
|
|
- // 无理由订单不处理
|
|
|
- if (orderInfo == null || Objects.equals(orderInfo.getClassify(), OrderClassifyEnum.NO_REASON_ORDER.getKey())) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- SkuSpec skuSpec = skuSpecMap.get(afterSaleOrder.getWlnSkuSpecId());
|
|
|
+ // 获取sku信息
|
|
|
+ SkuSpec skuSpec = skuSpecMap.get(itemDetails.getString("sku_no"));
|
|
|
if (skuSpec == null || Objects.equals(skuSpec.getGiftTag(), StatusConstant.YES)) {
|
|
|
continue;
|
|
|
}
|
|
|
- OrderSku orderSku = orderSkuMap
|
|
|
- .getOrDefault(orderInfo.getId(),Collections.emptyList())
|
|
|
- .stream()
|
|
|
+
|
|
|
+ // 通过sku获取订单明细
|
|
|
+ OrderSku orderSku = orderSkuList.stream()
|
|
|
.filter(i -> Objects.equals(i.getSkuSpecId(), skuSpec.getId()))
|
|
|
- .findAny()
|
|
|
+ .findFirst()
|
|
|
.orElse(null);
|
|
|
if (orderSku == null) {
|
|
|
continue;
|
|
|
}
|
|
|
+
|
|
|
+ OrderInfo orderInfo = orderInfoMap.get(orderSku.getOrderId());
|
|
|
+ // 无理由订单不处理
|
|
|
+ if (orderInfo == null || Objects.equals(orderInfo.getClassify(), OrderClassifyEnum.NO_REASON_ORDER.getKey())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
// 包材成本只退可二次利用的包材
|
|
|
List<OrderSkuBom> orderSkuBomList = orderSkuBomMap.getOrDefault(orderSku.getId(), Collections.emptyList());
|
|
|
BigDecimal packagingMaterialCost = orderSkuBomList.stream()
|
|
@@ -175,8 +178,8 @@ public class WLnSalesReturnStockInServiceImpl implements WlnSalesReturnStockInSe
|
|
|
orderExchange.setWarehouseId(WarehouseConstant.FINISHED_PRODUCT);
|
|
|
orderExchange.setType(StatusConstant.YES);
|
|
|
orderExchange.setStatus(StatusConstant.NO);
|
|
|
- orderExchange.setReason(afterSaleOrder.getReason());
|
|
|
- orderExchange.setRemark(afterSaleOrder.getRemark());
|
|
|
+ orderExchange.setReason(item.getString("reason_name"));
|
|
|
+ orderExchange.setRemark(item.getString("sys_remark"));
|
|
|
orderExchange.setCompletionTime(item.getDate("modify_time"));
|
|
|
orderExchange.setCompletionTimestamp(item.getLong("modify_time"));
|
|
|
orderExchangeList.add(orderExchange);
|
|
@@ -186,7 +189,7 @@ public class WLnSalesReturnStockInServiceImpl implements WlnSalesReturnStockInSe
|
|
|
}
|
|
|
OrderExchangeDetail orderExchangeDetail = new OrderExchangeDetail();
|
|
|
orderExchangeDetail.setOrderExchangeId(orderExchange.getId());
|
|
|
- orderExchangeDetail.setQuantity(afterSaleOrder.getQuantity());
|
|
|
+ orderExchangeDetail.setQuantity(itemDetails.getBigDecimal("nums"));
|
|
|
orderExchangeDetail.setReturnStatus(StatusConstant.NO);
|
|
|
orderExchangeDetail.setExchangeStatus(StatusConstant.NO);
|
|
|
orderExchangeDetail.setOrderSkuId(orderSku.getId());
|
|
@@ -194,17 +197,9 @@ public class WLnSalesReturnStockInServiceImpl implements WlnSalesReturnStockInSe
|
|
|
orderExchangeDetail.setReturnAmount((orderSku.getUnitPrice()
|
|
|
.add(packagingMaterialCost)).multiply(orderExchangeDetail.getQuantity()));
|
|
|
orderExchangeDetailList.add(orderExchangeDetail);
|
|
|
-
|
|
|
- afterSaleOrder.setStatus(StatusConstant.YES);
|
|
|
- editAfterSaleOrderIdList.add(afterSaleOrder.getId());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (!editAfterSaleOrderIdList.isEmpty()) {
|
|
|
- // 更新万里牛售后单的状态
|
|
|
- afterSaleOrderService.update(q -> q.set(AfterSaleOrder::getStatus, StatusConstant.YES)
|
|
|
- .in(BaseIdPo::getId, editAfterSaleOrderIdList));
|
|
|
- }
|
|
|
// 新增售后订单
|
|
|
orderExchangeService.saveBatch(orderExchangeList);
|
|
|
orderExchangeDetailService.saveBatch(orderExchangeDetailList);
|
|
@@ -223,24 +218,34 @@ public class WLnSalesReturnStockInServiceImpl implements WlnSalesReturnStockInSe
|
|
|
if (orderExchange != null) {
|
|
|
startTime = orderExchange.getCompletionTimestamp() + 1;
|
|
|
} else {
|
|
|
- startTime = endTime - 1000 * 60 * 60 * 24 * 7;
|
|
|
+ startTime = endTime - 1000 * 60 * 60 * 24 * 60L;
|
|
|
}
|
|
|
|
|
|
+ //
|
|
|
+ startTime = DateUtil.parse("2023-12-01 00:00:00").getTime();
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
List<JSONObject> list = new ArrayList<>();
|
|
|
|
|
|
- int page = 1;
|
|
|
- int size;
|
|
|
- do {
|
|
|
- try {
|
|
|
- List<JSONObject> itemList = WlnUtil.getSalesReturnStockInList(page, 200, startTime, endTime);
|
|
|
- page++;
|
|
|
- size = itemList.size();
|
|
|
- list.addAll(itemList);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("销售退货入库单同步失败", e);
|
|
|
- return Collections.emptyList();
|
|
|
- }
|
|
|
- } while (size >= 200);
|
|
|
+ while (startTime < endTime) {
|
|
|
+
|
|
|
+ int page = 1;
|
|
|
+ int size;
|
|
|
+ do {
|
|
|
+ try {
|
|
|
+ List<JSONObject> itemList = WlnUtil.getSalesReturnStockInList(page, 200, startTime, endTime);
|
|
|
+ page++;
|
|
|
+ size = itemList.size();
|
|
|
+ list.addAll(itemList);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("销售退货入库单同步失败", e);
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ } while (size >= 200);
|
|
|
+
|
|
|
+ startTime = DateUtil.offsetDay(DateUtil.date(startTime), 7).getTime();
|
|
|
+ }
|
|
|
|
|
|
return list;
|
|
|
}
|