|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|