diff --git a/ms-biz/src/main/java/com/ms/api/bo/CheckNeedDsAutoPurchaseOrderResultBO.java b/ms-biz/src/main/java/com/ms/api/bo/CheckNeedDsAutoPurchaseOrderResultBO.java new file mode 100644 index 00000000..cbd58105 --- /dev/null +++ b/ms-biz/src/main/java/com/ms/api/bo/CheckNeedDsAutoPurchaseOrderResultBO.java @@ -0,0 +1,9 @@ +package com.ms.api.bo; + +import lombok.Data; + +@Data +public class CheckNeedDsAutoPurchaseOrderResultBO { + private Boolean isNeedAutoPurchase; + private String forbiddenAutoPurchaseReason; +} diff --git a/ms-biz/src/main/java/com/ms/api/bo/MatchSourceSkuInfoBO.java b/ms-biz/src/main/java/com/ms/api/bo/MatchFailSkuInfoBO.java similarity index 83% rename from ms-biz/src/main/java/com/ms/api/bo/MatchSourceSkuInfoBO.java rename to ms-biz/src/main/java/com/ms/api/bo/MatchFailSkuInfoBO.java index 3503ca5a..206b90a5 100644 --- a/ms-biz/src/main/java/com/ms/api/bo/MatchSourceSkuInfoBO.java +++ b/ms-biz/src/main/java/com/ms/api/bo/MatchFailSkuInfoBO.java @@ -3,7 +3,7 @@ package com.ms.api.bo; import lombok.Data; @Data -public class MatchSourceSkuInfoBO { +public class MatchFailSkuInfoBO { private String reason; private String platfromItemId; private Long platfromSkuId; diff --git a/ms-biz/src/main/java/com/ms/api/bo/MatchSourceSkuInfoResultBO.java b/ms-biz/src/main/java/com/ms/api/bo/MatchSourceSkuInfoResultBO.java index 89db2632..5817ef0d 100644 --- a/ms-biz/src/main/java/com/ms/api/bo/MatchSourceSkuInfoResultBO.java +++ b/ms-biz/src/main/java/com/ms/api/bo/MatchSourceSkuInfoResultBO.java @@ -3,11 +3,10 @@ package com.ms.api.bo; import com.ms.api.dto.dsapi.response.GetPurchaseOrderItemsAndSourceItemsResponseDTO; import lombok.Data; -import java.util.HashMap; import java.util.Map; @Data public class MatchSourceSkuInfoResultBO { private Map platformSkuIdAndMatchSourceSkuInfoMap; - private Map platformSkuIdAndMatchFailSkuInfoMap; + private Map platformSkuIdAndMatchFailSkuInfoMap; } diff --git a/ms-biz/src/main/java/com/ms/api/service/PurchaseOrderService.java b/ms-biz/src/main/java/com/ms/api/service/PurchaseOrderService.java index 4351bfd1..f880b789 100644 --- a/ms-biz/src/main/java/com/ms/api/service/PurchaseOrderService.java +++ b/ms-biz/src/main/java/com/ms/api/service/PurchaseOrderService.java @@ -85,4 +85,6 @@ public interface PurchaseOrderService { boolean deleteDsAutoPurchaseOrderQueue(Long queueId); List getNormalPurchaseItemsByShopIdAndOrderId(Long shopId, String orderId); + + boolean addDsAutoPurchaseLog(List data); } diff --git a/ms-biz/src/main/java/com/ms/api/service/impl/PurchaseOrderServiceImpl.java b/ms-biz/src/main/java/com/ms/api/service/impl/PurchaseOrderServiceImpl.java index 3a40195e..e48c1298 100644 --- a/ms-biz/src/main/java/com/ms/api/service/impl/PurchaseOrderServiceImpl.java +++ b/ms-biz/src/main/java/com/ms/api/service/impl/PurchaseOrderServiceImpl.java @@ -13,6 +13,7 @@ import com.doudian.open.api.order_BatchSearchIndex.data.PlainToEncryptIndexListI import com.doudian.open.api.order_BatchSearchIndex.param.OrderBatchSearchIndexParam; import com.doudian.open.api.order_BatchSearchIndex.param.PlainTextListItem; import com.ms.api.common.ApiResult; +import com.ms.api.common.MSException; import com.ms.api.common.Pager; import com.ms.api.consts.*; import com.ms.api.dto.dsorder.AppendVenderRemarkTplItemDTO; @@ -1910,6 +1911,21 @@ public class PurchaseOrderServiceImpl implements PurchaseOrderService { return purchaseOrderItemMapper.getNormalPurchaseItemsByShopIdAndOrderId(shopId, orderId); } + @Override + @Transactional(rollbackFor = Exception.class) + public boolean addDsAutoPurchaseLog(List data) { + if (data == null || data.size() == 0) { + return false; + } + for (DsAutoPurchaseLog log : data) { + int affRow = dsAutoPurchaseLogMapper.insertSelective(log); + if (affRow == 0) { + throw new MSException("DsAutoPurchaseLog批量插入失败"); + } + } + return true; + } + } diff --git a/ms-biz/src/main/java/com/ms/api/tool/DsOrderTool.java b/ms-biz/src/main/java/com/ms/api/tool/DsOrderTool.java index aa6d0774..52c024a6 100644 --- a/ms-biz/src/main/java/com/ms/api/tool/DsOrderTool.java +++ b/ms-biz/src/main/java/com/ms/api/tool/DsOrderTool.java @@ -162,4 +162,20 @@ public class DsOrderTool { return manualRelateSkuInfo != null ? manualRelateSkuInfo : matchSkuInfo; } + public static int getRealSourceSkuPurchaseQuantity(Long platformSkuId, Integer platformItemTotal, GetPurchaseOrderItemsAndSourceItemsResponseDTO.Sku matchSourceSkuInfo, Map platformSkuIdAndSourceSkuInfoMap) { + if (platformSkuIdAndSourceSkuInfoMap == null || platformSkuIdAndSourceSkuInfoMap.isEmpty() || matchSourceSkuInfo == null) { + return platformItemTotal; + } + + int purchaseQuantity = platformItemTotal; + + GetPurchaseOrderItemsAndSourceItemsResponseDTO.SourceItemSkuRelation manualSourceSkuInfo = platformSkuIdAndSourceSkuInfoMap.get(platformSkuId); + boolean isSourceItemNoSkuFlag = (matchSourceSkuInfo.getSourceItemId() == matchSourceSkuInfo.getSourceSkuId()); + if (manualSourceSkuInfo != null && ((manualSourceSkuInfo.getSourceSkuId() == matchSourceSkuInfo.getSourceSkuId()) || (isSourceItemNoSkuFlag && (matchSourceSkuInfo.getSourceItemId() == manualSourceSkuInfo.getSourceItemId())))) { + purchaseQuantity = (manualSourceSkuInfo.getSourceNum() != null && manualSourceSkuInfo.getSourceNum() != 0) ? (platformItemTotal * manualSourceSkuInfo.getSourceNum()) : platformItemTotal; + } + + return purchaseQuantity; + } + } diff --git a/ms-biz/src/main/java/com/ms/api/util/PurchaseOrderUtil.java b/ms-biz/src/main/java/com/ms/api/util/PurchaseOrderUtil.java index edd14980..45c7256f 100644 --- a/ms-biz/src/main/java/com/ms/api/util/PurchaseOrderUtil.java +++ b/ms-biz/src/main/java/com/ms/api/util/PurchaseOrderUtil.java @@ -4,20 +4,24 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.lang.Validator; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; -import com.ms.api.bo.MatchSourceSkuInfoBO; -import com.ms.api.bo.MatchSourceSkuInfoResultBO; -import com.ms.api.bo.OrderItemSkuInfoBO; +import com.alibaba.fastjson.JSONObject; +import com.ms.api.bo.*; import com.ms.api.common.ApiResult; import com.ms.api.common.MSException; import com.ms.api.consts.DsOrderConst; import com.ms.api.consts.OrderConst; import com.ms.api.consts.PurchaseOrderConst; +import com.ms.api.consts.StatusConst; +import com.ms.api.dto.dsapi.request.ConsigneeInfoDTO; import com.ms.api.dto.dsapi.request.GetPurchaseOrderItemsAndSourceItemsRequestDTO; +import com.ms.api.dto.dsapi.request.PlatformOrderDTO; +import com.ms.api.dto.dsapi.request.PlatformOrderItemDTO; import com.ms.api.dto.dsapi.response.GetPurchaseOrderItemsAndSourceItemsResponseDTO; import com.ms.api.dto.order.*; import com.ms.api.service.*; import com.ms.api.tool.CommonTool; import com.ms.api.tool.DsOrderTool; +import com.ms.api.tool.OrderTool; import com.ms.api.tool.SecurityTool; import com.ms.dal.bo.OpOrderChildBO; import com.ms.dal.entity.*; @@ -26,6 +30,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; @@ -45,12 +50,18 @@ public class PurchaseOrderUtil { @Autowired private OpOrderDsService opOrderDsService; + @Autowired + private OpOrderExtService opOrderExtService; + @Autowired private OrderPrintService orderPrintService; @Autowired private ProductToDsItemService productToDsItemService; + @Autowired + private OpOrderSensitiveService opOrderSensitiveService; + @Autowired private DsApiService dsApiService; @@ -339,10 +350,30 @@ public class PurchaseOrderUtil { MatchSourceSkuInfoResultBO matchRes = getPlatformSkuIdAndMatchSourceSkuInfoMap(opOrderChilds, platformItemIdAndSourceItemInfoMap, productIdAndProductToDsItemMap); Map platformSkuIdAndMatchSourceSkuInfoMap = matchRes.getPlatformSkuIdAndMatchSourceSkuInfoMap(); - Map platformSkuIdAndMatchFailSkuInfoMap = matchRes.getPlatformSkuIdAndMatchFailSkuInfoMap(); + Map platformSkuIdAndMatchFailSkuInfoMap = matchRes.getPlatformSkuIdAndMatchFailSkuInfoMap(); + + autoPurchaseBeforeAddSkuMatchErrorPurchaseLog(orderInfo.getShopId(), orderInfo.getOrderId(), platformSkuIdAndMatchFailSkuInfoMap); + if (platformSkuIdAndMatchSourceSkuInfoMap.isEmpty()) { + log.error(String.format("没有需要自动下单的分销商品,shopId: %s, orderId: %s", shopId, orderId)); + return false; + } + + CheckNeedDsAutoPurchaseOrderResultBO checkResult = checkNeedDsAutoPurchaseOrder(orderInfo, opOrderChilds, platformSkuIdAndMatchSourceSkuInfoMap, dsAutoPurchaseSetting, skuIdAndPurchaseOrderItemMap, platformItemIdAndSourceItemInfoMap); + if (checkResult.getIsNeedAutoPurchase() != true) { + log.error(String.format("不符合自动下单的条件,shopId: %s, orderId: %s, reason: %s", shopId, orderId, checkResult.getForbiddenAutoPurchaseReason())); + throw new MSException(StringUtils.isEmpty(checkResult.getForbiddenAutoPurchaseReason()) ? "不符合自动下单的条件" : checkResult.getForbiddenAutoPurchaseReason()); + } + + PlatformOrderDTO platformOrder = buildDsAutoPurchasePlatformOrder(orderInfo, opOrderChilds, platformSkuIdAndMatchSourceSkuInfoMap, skuIdAndPurchaseOrderItemMap, platformItemIdAndSourceItemInfoMap, dsAutoPurchaseSetting); + if (platformOrder == null) { + log.info(String.format("buildDsAutoPurchasePlatformOrder -> 没有需要自动下单的商品%s", orderId)); + return false; + } + + -//todo: +// todo: return true; @@ -352,17 +383,263 @@ public class PurchaseOrderUtil { } + private PlatformOrderDTO buildDsAutoPurchasePlatformOrder(OpOrder orderInfo, + List opOrderChilds, + Map platformSkuIdAndMatchSourceSkuInfoMap, + Map skuIdAndPurchaseOrderItemMap, + Map platformItemIdAndSourceItemInfoMap, + DsPurchaseSettingDTO dsAutoPurchaseSetting) { + String orderId = orderInfo.getOrderId(); + Long shopId = orderInfo.getShopId(); + String platformOrderId = orderId; + + Set filterAutoPurchaseProductIds = dsAutoPurchaseSetting.getFilterAutoPurchaseProducts().values().stream().map(FilterAutoPurchaseProductDTO::getProductId).collect(Collectors.toSet()); + Set allowAutoPurchaseProductIds = dsAutoPurchaseSetting.getAllowAutoPurchaseProducts().values().stream().map(AllowAutoPurchaseProductDTO::getProductId).collect(Collectors.toSet()); + + List needPurchaseItems = new ArrayList<>(); + List dsAutoPurchaseLogs = new ArrayList<>(); + for (OpOrderChildBO opOrderChild : opOrderChilds) { + String platformItemId = opOrderChild.getProductId(); + Long platformSkuId = opOrderChild.getSkuId(); + + if (skuIdAndPurchaseOrderItemMap.containsKey(platformSkuId)) { + continue; + } + + if (dsAutoPurchaseSetting.getIsUseAllProduct() == 1 && filterAutoPurchaseProductIds.contains(Long.valueOf(platformItemId))) { + DsAutoPurchaseLog dsAutoPurchaseLog = new DsAutoPurchaseLog(); + dsAutoPurchaseLog.setShopId(shopId); + dsAutoPurchaseLog.setOrderId(orderId); + dsAutoPurchaseLog.setProductId(Long.valueOf(platformItemId)); + dsAutoPurchaseLog.setSkuId(platformSkuId); + dsAutoPurchaseLog.setSourceItemId(null); + dsAutoPurchaseLog.setSourceSkuId(null); + dsAutoPurchaseLog.setStatus(StatusConst.fail); + dsAutoPurchaseLog.setReason(String.format("%s商品设置了过滤自动采购", platformItemId)); + dsAutoPurchaseLog.setGmtCreate(new Date()); + dsAutoPurchaseLog.setGmtModified(new Date()); + dsAutoPurchaseLogs.add(dsAutoPurchaseLog); + continue; + } + + if (dsAutoPurchaseSetting.getIsUsePartProduct() == 1 && !allowAutoPurchaseProductIds.contains(Long.valueOf(platformItemId))) { + DsAutoPurchaseLog dsAutoPurchaseLog = new DsAutoPurchaseLog(); + dsAutoPurchaseLog.setShopId(shopId); + dsAutoPurchaseLog.setOrderId(orderId); + dsAutoPurchaseLog.setProductId(Long.valueOf(platformItemId)); + dsAutoPurchaseLog.setSkuId(platformSkuId); + dsAutoPurchaseLog.setSourceItemId(null); + dsAutoPurchaseLog.setSourceSkuId(null); + dsAutoPurchaseLog.setStatus(StatusConst.fail); + dsAutoPurchaseLog.setReason(String.format("%s商品不在允许自动采购商品中", platformItemId)); + dsAutoPurchaseLog.setGmtCreate(new Date()); + dsAutoPurchaseLog.setGmtModified(new Date()); + dsAutoPurchaseLogs.add(dsAutoPurchaseLog); + continue; + } + + GetPurchaseOrderItemsAndSourceItemsResponseDTO.Sku matchSourceSkuInfo = platformSkuIdAndMatchSourceSkuInfoMap.get(Long.valueOf(platformItemId)); + if (matchSourceSkuInfo == null) { + continue; + } + + GetPurchaseOrderItemsAndSourceItemsResponseDTO.SourceItemInfo sourceItemInfo = platformItemIdAndSourceItemInfoMap.get(platformItemId); + GetPurchaseOrderItemsAndSourceItemsResponseDTO.ProductInfo sourceProductInfo = sourceItemInfo.getProductInfo(); + + int purchaseQuantity = DsOrderTool.getRealSourceSkuPurchaseQuantity(platformSkuId, opOrderChild.getComboNum(), matchSourceSkuInfo, sourceProductInfo.getPlatformSkuIdAndSourceSkuInfoMap()); + if (purchaseQuantity > matchSourceSkuInfo.getStock()) { + DsAutoPurchaseLog dsAutoPurchaseLog = new DsAutoPurchaseLog(); + dsAutoPurchaseLog.setShopId(shopId); + dsAutoPurchaseLog.setOrderId(orderId); + dsAutoPurchaseLog.setProductId(Long.valueOf(platformItemId)); + dsAutoPurchaseLog.setSkuId(platformSkuId); + dsAutoPurchaseLog.setSourceItemId(matchSourceSkuInfo.getSourceItemId().toString()); + dsAutoPurchaseLog.setSourceSkuId(matchSourceSkuInfo.getSourceSkuId().toString()); + dsAutoPurchaseLog.setStatus(StatusConst.fail); + dsAutoPurchaseLog.setReason(String.format("购买数量超过商品的库存,当前库存为%s", matchSourceSkuInfo.getStock())); + dsAutoPurchaseLog.setGmtCreate(new Date()); + dsAutoPurchaseLog.setGmtModified(new Date()); + dsAutoPurchaseLogs.add(dsAutoPurchaseLog); + continue; + } + + PlatformOrderItemDTO platformOrderItem = new PlatformOrderItemDTO(); + platformOrderItem.setSourceItemId(matchSourceSkuInfo.getSourceItemId().toString()); + platformOrderItem.setSpecId(matchSourceSkuInfo.getSpecId()); + platformOrderItem.setSourceSkuId(matchSourceSkuInfo.getSourceSkuId()); + platformOrderItem.setItemTotal(purchaseQuantity); + platformOrderItem.setPlatformItemId(platformItemId); + platformOrderItem.setPlatformSkuId(platformSkuId); + platformOrderItem.setPlatformOrderId(platformOrderId); + platformOrderItem.setInstanceCode(matchSourceSkuInfo.getInstanceCode()); + platformOrderItem.setIsJxhyOffer(matchSourceSkuInfo.getIsJxhyOffer() == 1); + platformOrderItem.setJxhyPrice(matchSourceSkuInfo.getJxhyPrice()); + needPurchaseItems.add(platformOrderItem); + } + + if (needPurchaseItems.isEmpty() && dsAutoPurchaseLogs.isEmpty()) { + DsAutoPurchaseLog dsAutoPurchaseLog = new DsAutoPurchaseLog(); + dsAutoPurchaseLog.setShopId(shopId); + dsAutoPurchaseLog.setOrderId(orderId); + dsAutoPurchaseLog.setStatus(StatusConst.fail); + dsAutoPurchaseLog.setReason("没有需要自动下单的商品"); + dsAutoPurchaseLog.setGmtCreate(new Date()); + dsAutoPurchaseLog.setGmtModified(new Date()); + dsAutoPurchaseLogs.add(dsAutoPurchaseLog); + } + + purchaseOrderService.addDsAutoPurchaseLog(dsAutoPurchaseLogs); + + if (needPurchaseItems.isEmpty()) { + return null; + } + +// List shopIds = new ArrayList<>(); +// shopIds.add(shopId.toString()); + List orderIds = new ArrayList<>(); + orderIds.add(orderId); + +// Map orderIdAndCustomAddressMap = orderPrintService.getOrderReceiveAddress(shopIds, orderIds); + List opOrderSensitiveList = opOrderSensitiveService.getListByOrderIds(orderIds); + OpOrderSensitive opOrderSensitive = null; + if (!opOrderSensitiveList.isEmpty()) { + opOrderSensitive = opOrderSensitiveList.get(0); + } + +// JSONObject customAddress = OrderTool.formatOpOrderCustomAddress(orderIdAndCustomAddressMap.get(orderId), true); + JSONObject consignee = OrderTool.formatOpOrderConsignee(orderInfo, opOrderSensitive, true); + ConsigneeInfoDTO consigneeInfo = consignee.toJavaObject(ConsigneeInfoDTO.class); + + PlatformOrderDTO ret = new PlatformOrderDTO(); + ret.setPlatformOrderId(platformOrderId); + ret.setShopId(shopId.toString()); + ret.setBuyerRemark(orderInfo.getBuyerWords()); + ret.setConsigneeInfo(consigneeInfo); + ret.setItems(needPurchaseItems); + return ret; + } + + private CheckNeedDsAutoPurchaseOrderResultBO checkNeedDsAutoPurchaseOrder(OpOrder orderInfo, + List opOrderChilds, + Map platformSkuIdAndMatchSourceSkuInfoMap, + DsPurchaseSettingDTO dsAutoPurchaseSetting, + Map skuIdAndPurchaseOrderItemMap, + Map platformItemIdAndSourceItemInfoMap) { + CheckNeedDsAutoPurchaseOrderResultBO checkResult = new CheckNeedDsAutoPurchaseOrderResultBO(); + + String orderId = orderInfo.getOrderId(); + + if (dsAutoPurchaseSetting.getIsAutoPurchase() == 0) { + checkResult.setIsNeedAutoPurchase(false); + checkResult.setForbiddenAutoPurchaseReason("未开启自动下单"); + return checkResult; + } + + Set filterAutoPurchaseProductIds = dsAutoPurchaseSetting.getFilterAutoPurchaseProducts().values().stream().map(FilterAutoPurchaseProductDTO::getProductId).collect(Collectors.toSet()); + Set allowAutoPurchaseProductIds = dsAutoPurchaseSetting.getAllowAutoPurchaseProducts().values().stream().map(AllowAutoPurchaseProductDTO::getProductId).collect(Collectors.toSet()); + + OpOrderExt opOrderExt = opOrderExtService.getByOrderId(orderId); + + double orderTotalAmount = CommonTool.correctCentToYuan(orderInfo.getOrderTotalAmount()); + double freightPrice = CommonTool.correctCentToYuan(orderInfo.getPostAmount()); + double orderPayment = CommonTool.numFormat(orderTotalAmount + freightPrice, 2); + double forecastPurchaseOrderPayment = 0; + + boolean isNeedAutoPurchase = false; + List forbiddenAutoPurchaseReasons = new ArrayList<>(); + + if (opOrderExt.getIsIsolation() == 1 && dsAutoPurchaseSetting.getIsForbidIsolationPurchase() == 1) { + checkResult.setIsNeedAutoPurchase(false); + checkResult.setForbiddenAutoPurchaseReason("您设置隔离订单禁止自动采购"); + return checkResult; + } + + for (OpOrderChildBO opOrderChild : opOrderChilds) { + String productId = opOrderChild.getProductId(); + Long skuId = opOrderChild.getSkuId(); + + if (skuIdAndPurchaseOrderItemMap.containsKey(skuId)) { + continue; + } + + GetPurchaseOrderItemsAndSourceItemsResponseDTO.Sku matchSourceSkuInfo = platformSkuIdAndMatchSourceSkuInfoMap.get(skuId); + if (matchSourceSkuInfo != null) { + double price = matchSourceSkuInfo.getConsignPrice() != null ? matchSourceSkuInfo.getConsignPrice() : (matchSourceSkuInfo.getPrice() != null ? matchSourceSkuInfo.getPrice() : 0); + if (price <= 0) { + isNeedAutoPurchase = false; + forbiddenAutoPurchaseReasons.add(String.format("商品:%s,sku:%s 获取采购价格失败", productId, skuId)); + break; + } + GetPurchaseOrderItemsAndSourceItemsResponseDTO.SourceItemInfo sourceItemInfo = platformItemIdAndSourceItemInfoMap.get(productId); + GetPurchaseOrderItemsAndSourceItemsResponseDTO.ProductInfo sourceProductInfo = sourceItemInfo.getProductInfo(); + + int purchaseQty = DsOrderTool.getRealSourceSkuPurchaseQuantity(skuId, opOrderChild.getComboNum(), matchSourceSkuInfo, sourceProductInfo.getPlatformSkuIdAndSourceSkuInfoMap()); + forecastPurchaseOrderPayment += purchaseQty * price; + } + + if (dsAutoPurchaseSetting.getIsUseAllProduct() == 1 && !filterAutoPurchaseProductIds.contains(Long.valueOf(productId))) { + isNeedAutoPurchase = true; + break; + } + + if (dsAutoPurchaseSetting.getIsUsePartProduct() == 1 && allowAutoPurchaseProductIds.contains(Long.valueOf(productId))) { + isNeedAutoPurchase = true; + break; + } + + if (dsAutoPurchaseSetting.getIsUsePartProduct() == 1) { + forbiddenAutoPurchaseReasons.add(String.format("%s商品不在允许自动采购商品中", productId)); + } + + if (dsAutoPurchaseSetting.getIsUseAllProduct() == 1) { + forbiddenAutoPurchaseReasons.add(String.format("%s商品设置了过滤自动采购", productId)); + } + } + + if (!isNeedAutoPurchase) { + checkResult.setIsNeedAutoPurchase(isNeedAutoPurchase); + checkResult.setForbiddenAutoPurchaseReason(String.join(";", forbiddenAutoPurchaseReasons)); + return checkResult; + } + + String forbiddenKeywordsStr = StringUtils.isEmpty(dsAutoPurchaseSetting.getForbiddenKeywords()) ? "" : dsAutoPurchaseSetting.getForbiddenKeywords(); + List forbiddenKeywords = CommonTool.convertValToArray(forbiddenKeywordsStr); + if (dsAutoPurchaseSetting.getIsUseForbiddenKeywords() == 1 && !forbiddenKeywords.isEmpty()) { + String checkForbiddenAutoPurchaseText = (StringUtils.isEmpty(orderInfo.getBuyerWords()) ? "" : orderInfo.getBuyerWords()) + + (StringUtils.isEmpty(orderInfo.getSellerWords()) ? "" : orderInfo.getSellerWords()) + + (StringUtils.isEmpty(opOrderExt.getShopNote()) ? "" : opOrderExt.getShopNote()); + for (String forbiddenKeyword : forbiddenKeywords) { + if (checkForbiddenAutoPurchaseText.indexOf(forbiddenKeyword) != -1) { + isNeedAutoPurchase = false; + forbiddenAutoPurchaseReasons.add(String.format("备注中含有过滤关键词【%s】", forbiddenKeyword)); + break; + } + } + } + + BigDecimal profit = new BigDecimal(CommonTool.correctCentToYuan(orderPayment - forecastPurchaseOrderPayment)); + + if (dsAutoPurchaseSetting.getIsUseForbiddenPurchaseProfit() == 1 && profit.compareTo(dsAutoPurchaseSetting.getForbiddenPurchaseProfit()) < 0) { + forbiddenAutoPurchaseReasons.add(String.format("利润低于%s", dsAutoPurchaseSetting.getForbiddenPurchaseProfit().toString())); + isNeedAutoPurchase = false; + } + + checkResult.setIsNeedAutoPurchase(isNeedAutoPurchase); + checkResult.setForbiddenAutoPurchaseReason(String.join(";", forbiddenAutoPurchaseReasons)); + return checkResult; + } + private MatchSourceSkuInfoResultBO getPlatformSkuIdAndMatchSourceSkuInfoMap(List opOrderChilds, Map platformItemIdAndSourceItemInfoMap, Map productIdAndProductToDsItemMap) { Map platformSkuIdAndMatchSourceSkuInfoMap = new HashMap<>(); - Map platformSkuIdAndMatchFailSkuInfoMap = new HashMap<>(); + Map platformSkuIdAndMatchFailSkuInfoMap = new HashMap<>(); for (OpOrderChildBO opOrderChild : opOrderChilds) { String platfromItemId = opOrderChild.getProductId(); Long platfromSkuId = opOrderChild.getSkuId(); if (!productIdAndProductToDsItemMap.containsKey(platfromItemId) || productIdAndProductToDsItemMap.get(platfromItemId) == null) { - MatchSourceSkuInfoBO obj = new MatchSourceSkuInfoBO(); + MatchFailSkuInfoBO obj = new MatchFailSkuInfoBO(); obj.setReason(String.format("%s商品未代销", platfromItemId)); obj.setPlatfromItemId(platfromItemId); obj.setPlatfromSkuId(platfromSkuId); @@ -380,7 +657,7 @@ public class PurchaseOrderUtil { GetPurchaseOrderItemsAndSourceItemsResponseDTO.Sku matchSourceSkuInfo = DsOrderTool.getMatchSourceSkuInfo(orderItemInfo, sourceItemInfo.getProductInfo().getPlatformSkuIdAndSourceSkuIdMap(), sourceItemInfo.getProductInfo()); if (!sourceItemInfo.getProductApiRet().isSuccess()) { - MatchSourceSkuInfoBO obj = new MatchSourceSkuInfoBO(); + MatchFailSkuInfoBO obj = new MatchFailSkuInfoBO(); obj.setReason(sourceItemInfo.getProductApiRet().getReason()); obj.setPlatfromItemId(platfromItemId); obj.setPlatfromSkuId(platfromSkuId); @@ -390,7 +667,7 @@ public class PurchaseOrderUtil { } if (matchSourceSkuInfo == null) { - MatchSourceSkuInfoBO obj = new MatchSourceSkuInfoBO(); + MatchFailSkuInfoBO obj = new MatchFailSkuInfoBO(); obj.setReason("sku规格匹配失败"); obj.setPlatfromItemId(platfromItemId); obj.setPlatfromSkuId(platfromSkuId); @@ -407,4 +684,24 @@ public class PurchaseOrderUtil { ret.setPlatformSkuIdAndMatchFailSkuInfoMap(platformSkuIdAndMatchFailSkuInfoMap); return ret; } + + private void autoPurchaseBeforeAddSkuMatchErrorPurchaseLog(Long shopId, String orderId, Map platformSkuIdAndMatchFailSkuInfoMap) { + List logDatas = new ArrayList<>(); + for (MatchFailSkuInfoBO matchFailSkuInfo : platformSkuIdAndMatchFailSkuInfoMap.values()) { + DsAutoPurchaseLog obj = new DsAutoPurchaseLog(); + obj.setShopId(shopId); + obj.setOrderId(orderId); + obj.setProductId(Long.valueOf(matchFailSkuInfo.getPlatfromItemId())); + obj.setSkuId(matchFailSkuInfo.getPlatfromSkuId()); + obj.setSourceItemId(matchFailSkuInfo.getSourceItemId() != null ? matchFailSkuInfo.getSourceItemId().toString() : null); + obj.setStatus(StatusConst.fail); + obj.setReason(matchFailSkuInfo.getReason()); + obj.setGmtCreate(new Date()); + obj.setGmtModified(new Date()); + logDatas.add(obj); + } + if (logDatas.size() != 0) { + purchaseOrderService.addDsAutoPurchaseLog(logDatas); + } + } }