|
|
|
@ -4,19 +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.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.ms.api.bo.*;
|
|
|
|
|
import com.ms.api.bo.CheckNeedDsAutoPurchaseOrderResultBO;
|
|
|
|
|
import com.ms.api.bo.MatchFailSkuInfoBO;
|
|
|
|
|
import com.ms.api.bo.MatchSourceSkuInfoResultBO;
|
|
|
|
|
import com.ms.api.bo.OrderItemSkuInfoBO;
|
|
|
|
|
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.request.*;
|
|
|
|
|
import com.ms.api.dto.dsapi.response.BatchCreateDistributionOrdersResponseDTO;
|
|
|
|
|
import com.ms.api.dto.dsapi.response.DsErrorRetDTO;
|
|
|
|
|
import com.ms.api.dto.dsapi.response.DsSuccessRetDTO;
|
|
|
|
|
import com.ms.api.dto.dsapi.response.GetPurchaseOrderItemsAndSourceItemsResponseDTO;
|
|
|
|
|
import com.ms.api.dto.dsorder.PurchaseOrderDTO;
|
|
|
|
|
import com.ms.api.dto.order.*;
|
|
|
|
|
import com.ms.api.service.*;
|
|
|
|
|
import com.ms.api.tool.CommonTool;
|
|
|
|
@ -62,6 +67,9 @@ public class PurchaseOrderUtil {
|
|
|
|
|
@Autowired
|
|
|
|
|
private OpOrderSensitiveService opOrderSensitiveService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private DistributionOrderService distributionOrderService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private DsApiService dsApiService;
|
|
|
|
|
|
|
|
|
@ -370,14 +378,32 @@ public class PurchaseOrderUtil {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<Long, Long> skuIdAndDsAutoPurchaseLogIdMap = autoPurchaseBeforeAddDsAutoPurchaseLog(orderInfo.getShopId(), orderInfo.getOrderId(), platformOrder.getItems());
|
|
|
|
|
|
|
|
|
|
List<PlatformOrderDTO> platformOrders = new ArrayList<>();
|
|
|
|
|
platformOrders.add(platformOrder);
|
|
|
|
|
BatchCreateDistributionOrdersRequestDTO params = new BatchCreateDistributionOrdersRequestDTO();
|
|
|
|
|
params.setPlatformOrders(platformOrders);
|
|
|
|
|
params.setIsAutoPay(dsAutoPurchaseSetting.getIsAutoPay());
|
|
|
|
|
params.setRepurchase(false);
|
|
|
|
|
params.setActionSource(DsOrderConst.ACTION_SOURCE_PLATFORM_AUTO_PURCHASE);
|
|
|
|
|
params.setPurchaseSource(DsOrderConst.PURCHASE_SOURCE_PLATFORM_AUTO);
|
|
|
|
|
BatchCreateDistributionOrdersResponseDTO batchDsRet = distributionOrderService.batchCreateDistributionOrders(shopId, params);
|
|
|
|
|
log.info("batchDsRet: " + JSON.toJSONString(batchDsRet));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// todo:
|
|
|
|
|
|
|
|
|
|
autoPurchaseAfterUpdateDsAutoPurchaseLog(orderInfo.getOrderId(), platformOrder.getItems(), skuIdAndDsAutoPurchaseLogIdMap, batchDsRet);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
DsAutoPurchaseLog logData = new DsAutoPurchaseLog();
|
|
|
|
|
logData.setShopId(shopId);
|
|
|
|
|
logData.setOrderId(orderId);
|
|
|
|
|
logData.setStatus(StatusConst.fail);
|
|
|
|
|
logData.setReason(e.getMessage());
|
|
|
|
|
logData.setGmtCreate(new Date());
|
|
|
|
|
logData.setGmtModified(new Date());
|
|
|
|
|
purchaseOrderService.addDsAutoPurchaseLog(logData);
|
|
|
|
|
log.error("doDsAutoPurchase exception: ", e);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -649,6 +675,8 @@ public class PurchaseOrderUtil {
|
|
|
|
|
|
|
|
|
|
GetPurchaseOrderItemsAndSourceItemsResponseDTO.SourceItemInfo sourceItemInfo = platformItemIdAndSourceItemInfoMap.get(platfromItemId);
|
|
|
|
|
// todo: rebuildSourceItemProductInfo 各种特判,看起来没用, 先跳过
|
|
|
|
|
GetPurchaseOrderItemsAndSourceItemsResponseDTO.ProductInfo productInfo = rebuildSourceItemProductInfo(opOrderChild.getShopId(), sourceItemInfo.getProductInfo());
|
|
|
|
|
sourceItemInfo.setProductInfo(productInfo);
|
|
|
|
|
|
|
|
|
|
OrderItemSkuInfoBO orderItemInfo = new OrderItemSkuInfoBO();
|
|
|
|
|
orderItemInfo.setSkuSubName(opOrderChild.getSkuSubName());
|
|
|
|
@ -685,6 +713,20 @@ public class PurchaseOrderUtil {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private GetPurchaseOrderItemsAndSourceItemsResponseDTO.ProductInfo rebuildSourceItemProductInfo(Long shopId, GetPurchaseOrderItemsAndSourceItemsResponseDTO.ProductInfo sourceItemProductInfo) {
|
|
|
|
|
if (sourceItemProductInfo == null) {
|
|
|
|
|
return sourceItemProductInfo;
|
|
|
|
|
}
|
|
|
|
|
// isBoutiquefenxiao 相关,用不到, 跳过
|
|
|
|
|
sourceItemProductInfo.setIsJxhyOffer(0);
|
|
|
|
|
if (sourceItemProductInfo.getSkus() != null) {
|
|
|
|
|
for (GetPurchaseOrderItemsAndSourceItemsResponseDTO.Sku sku : sourceItemProductInfo.getSkus()) {
|
|
|
|
|
sku.setIsJxhyOffer(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return sourceItemProductInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void autoPurchaseBeforeAddSkuMatchErrorPurchaseLog(Long shopId, String orderId, Map<Long, MatchFailSkuInfoBO> platformSkuIdAndMatchFailSkuInfoMap) {
|
|
|
|
|
List<DsAutoPurchaseLog> logDatas = new ArrayList<>();
|
|
|
|
|
for (MatchFailSkuInfoBO matchFailSkuInfo : platformSkuIdAndMatchFailSkuInfoMap.values()) {
|
|
|
|
@ -704,4 +746,62 @@ public class PurchaseOrderUtil {
|
|
|
|
|
purchaseOrderService.addDsAutoPurchaseLog(logDatas);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<Long, Long> autoPurchaseBeforeAddDsAutoPurchaseLog(Long shopId, String orderId, List<PlatformOrderItemDTO> items) {
|
|
|
|
|
Map<Long, Long> skuIdAndDsAutoPurchaseLogIdMap = new HashMap<>();
|
|
|
|
|
for (PlatformOrderItemDTO item : items) {
|
|
|
|
|
DsAutoPurchaseLog dsAutoPurchaseLog = new DsAutoPurchaseLog();
|
|
|
|
|
dsAutoPurchaseLog.setShopId(shopId);
|
|
|
|
|
dsAutoPurchaseLog.setOrderId(orderId);
|
|
|
|
|
dsAutoPurchaseLog.setProductId(Long.valueOf(item.getPlatformItemId()));
|
|
|
|
|
dsAutoPurchaseLog.setSkuId(item.getPlatformSkuId());
|
|
|
|
|
dsAutoPurchaseLog.setSourceItemId(item.getSourceItemId());
|
|
|
|
|
dsAutoPurchaseLog.setSourceSkuId(item.getSourceSkuId().toString());
|
|
|
|
|
dsAutoPurchaseLog.setStatus(StatusConst.wait);
|
|
|
|
|
dsAutoPurchaseLog.setGmtCreate(new Date());
|
|
|
|
|
dsAutoPurchaseLog.setGmtModified(new Date());
|
|
|
|
|
boolean isSuccess = purchaseOrderService.addDsAutoPurchaseLog(dsAutoPurchaseLog);
|
|
|
|
|
if (isSuccess) {
|
|
|
|
|
skuIdAndDsAutoPurchaseLogIdMap.put(item.getPlatformSkuId(), dsAutoPurchaseLog.getDsAutoPurchaseLogId());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return skuIdAndDsAutoPurchaseLogIdMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void autoPurchaseAfterUpdateDsAutoPurchaseLog(String orderId,
|
|
|
|
|
List<PlatformOrderItemDTO> items,
|
|
|
|
|
Map<Long, Long> skuIdAndDsAutoPurchaseLogIdMap,
|
|
|
|
|
BatchCreateDistributionOrdersResponseDTO dsRet) {
|
|
|
|
|
List<PurchaseOrderDTO> relatePurchaseOrderInfos = new ArrayList<>();
|
|
|
|
|
for (DsSuccessRetDTO successRet : dsRet.getSuccessList()) {
|
|
|
|
|
relatePurchaseOrderInfos.addAll(successRet.getRelatePurchaseOrderInfos());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, PurchaseOrderDTO> platformOrderIdAndRelatePurchaseOrderInfoMap = CommonTool.convertListToMap(relatePurchaseOrderInfos, PurchaseOrderDTO::getPlatformOrderId);
|
|
|
|
|
Map<String, String> platformOrderIdAndReasonMap = dsRet.getErrorList().stream().collect(Collectors.toMap(DsErrorRetDTO::getPlatformOrderId, DsErrorRetDTO::getReason, (value1, value2) -> value1));
|
|
|
|
|
|
|
|
|
|
for (PlatformOrderItemDTO platformItemInfo : items) {
|
|
|
|
|
String platformOrderId = orderId;
|
|
|
|
|
PurchaseOrderDTO relatePurchaseOrderInfo = platformOrderIdAndRelatePurchaseOrderInfoMap.get(platformOrderId);
|
|
|
|
|
String relatePurchaseOrderSn = relatePurchaseOrderInfo.getPurchaseOrderSn();
|
|
|
|
|
|
|
|
|
|
String status = dsRet.getResult();
|
|
|
|
|
String reason = dsRet.getReason();
|
|
|
|
|
if (platformOrderIdAndReasonMap.containsKey(platformOrderId)) {
|
|
|
|
|
status = StatusConst.fail;
|
|
|
|
|
reason = platformOrderIdAndReasonMap.get(platformOrderId);
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.isEmpty(relatePurchaseOrderSn) && status != StatusConst.fail) {
|
|
|
|
|
status = StatusConst.fail;
|
|
|
|
|
reason = "采购失败";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DsAutoPurchaseLog updateData = new DsAutoPurchaseLog();
|
|
|
|
|
updateData.setDsAutoPurchaseLogId(skuIdAndDsAutoPurchaseLogIdMap.get(platformItemInfo.getPlatformSkuId()));
|
|
|
|
|
updateData.setPurchaseOrderSn(relatePurchaseOrderSn);
|
|
|
|
|
updateData.setStatus(status);
|
|
|
|
|
updateData.setReason(reason);
|
|
|
|
|
purchaseOrderService.updateDsAutoPurchaseLog(updateData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|