自动采购任务

20230922-ljl-fixBug
wangchaoxu 1 year ago
parent 48974cd1c2
commit 300cd84a8b

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -33,4 +33,6 @@ public interface AfterSaleService {
ApiResult rsyncAfterSaleByAftersaleIds(String authShopId, String aftersaleIds, boolean fromDyCloud);
boolean processDoudianAftersaleMsg(Long shopId, String msgId);
List<AfterSale> getAfterSalesByPid(Long shopId, String pid);
}

@ -128,4 +128,5 @@ public interface OpOrderService {
ApiResult startAnalysisRelateProductDsOrder(DsRelateProductAnalysisOpOrderQueue queueMsg);
OpOrder getOpOrderByOrderId(String orderId);
}

@ -82,6 +82,7 @@ public interface PurchaseOrderService {
DsAutoPurchaseOrderQueue lockDsAutoPurchaseQueue();
boolean deleteDsAutoPurchaseOrderQueue(Long queueId);
List<PurchaseOrderItem> getNormalPurchaseItemsByShopIdAndOrderId(Long shopId, String orderId);
}

@ -475,6 +475,13 @@ public class AfterSaleServiceImpl implements AfterSaleService {
return isSuccess;
}
public List<AfterSale> getAfterSalesByPid(Long shopId, String pid) {
if (shopId == null || StringUtils.isEmpty(pid)) {
return new ArrayList<>();
}
return afterSaleMapper.selectListByShopIdAndPid(shopId, pid);
}
}

@ -1481,6 +1481,7 @@ public class OpOrderServiceImpl implements OpOrderService {
return true;
}
@Override
public OpOrder getOpOrderByOrderId(String orderId) {
if (ObjectUtil.isEmpty(orderId)) {
return null;

@ -1899,14 +1899,17 @@ public class PurchaseOrderServiceImpl implements PurchaseOrderService {
return null;
}
@Override
public boolean deleteDsAutoPurchaseOrderQueue(Long queueId) {
int affRow = dsAutoPurchaseOrderQueueMapper.deleteByPrimaryKey(queueId);
return affRow > 0 ? true : false;
}
@Override
public List<PurchaseOrderItem> getNormalPurchaseItemsByShopIdAndOrderId(Long shopId, String orderId) {
return purchaseOrderItemMapper.getNormalPurchaseItemsByShopIdAndOrderId(shopId, orderId);
}
}

@ -2,6 +2,7 @@ package com.ms.api.task;
import com.ms.api.common.TaskBaseService;
import com.ms.api.service.PurchaseOrderService;
import com.ms.api.util.PurchaseOrderUtil;
import com.ms.dal.entity.DsAutoPurchaseOrderQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -18,6 +19,9 @@ import java.util.concurrent.Executor;
@Slf4j
public class DsAutoPurchaseTaskService extends TaskBaseService {
@Autowired
private PurchaseOrderUtil purchaseOrderUtil;
@Autowired
private PurchaseOrderService purchaseOrderService;
@ -66,8 +70,20 @@ public class DsAutoPurchaseTaskService extends TaskBaseService {
if (params == null) {
return null;
}
// purchaseOrderService.doDsAutoPurchase();
return true;
DsAutoPurchaseOrderQueue queue = (DsAutoPurchaseOrderQueue) params;
boolean isSuccess = false;
try {
isSuccess = purchaseOrderUtil.doDsAutoPurchase(queue.getShopId(), queue.getOrderId());
} catch (Exception e) {
log.error("DsAutoPurchaseTask Exception: ", e);
}
if (isSuccess) {
return queue.getDsAutoPurchaseOrderQueueId();
} else {
return null;
}
}
@Override

@ -1,36 +1,56 @@
package com.ms.api.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.JSONObject;
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.SessionConst;
import com.ms.api.dto.order.OriginalAndCustomConsigneeInfoRequestDTO;
import com.ms.api.dto.order.OriginalAndCustomConsigneeInfoResponseDTO;
import com.ms.api.dto.order.PurchaseSettingDTO;
import com.ms.api.dto.order.SearchDsOrderFilterDTO;
import com.ms.api.dto.order.SearchDsOrderListRequestDTO;
import com.ms.api.service.PurchaseOrderService;
import com.ms.api.dto.dsapi.request.GetPurchaseOrderItemsAndSourceItemsRequestDTO;
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.SecurityTool;
import com.ms.dal.bo.OpOrderChildBO;
import com.ms.dal.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.stream.Collectors;
@Component
@Slf4j
public class PurchaseOrderUtil {
@Autowired
private PurchaseOrderService purchaseOrderService;
@Autowired
private OpOrderService opOrderService;
@Autowired
private AfterSaleService afterSaleService;
@Autowired
private OpOrderDsService opOrderDsService;
@Autowired
private OrderPrintService orderPrintService;
@Autowired
private ProductToDsItemService productToDsItemService;
@Autowired
private DsApiService dsApiService;
private OriginalAndCustomConsigneeInfoResponseDTO getOriginalAndCustomConsigneeInfo(OriginalAndCustomConsigneeInfoRequestDTO requestDTO) {
return new OriginalAndCustomConsigneeInfoResponseDTO();
}
@ -247,4 +267,117 @@ public class PurchaseOrderUtil {
public boolean savePurchaseSetting(Long shopId, PurchaseSettingDTO purchaseSettingDTO) {
return purchaseOrderService.savePurchaseSetting(shopId, purchaseSettingDTO);
}
public boolean doDsAutoPurchase(Long shopId, String orderId) {
log.info(String.format("start method doDsAutoPurchase shopId: %s, orderId: %s", shopId, orderId));
try {
if (shopId == null || StringUtils.isEmpty(orderId)) {
log.error(String.format("参数错误shopId: %s, orderId: %s", shopId, orderId));
throw new MSException("参数错误");
}
OpOrder orderInfo = opOrderService.getOpOrderByOrderId(orderId);
if (orderInfo == null) {
log.error(String.format("订单不存在shopId: %s, orderId: %s", shopId, orderId));
throw new MSException("订单不存在");
}
DsPurchaseSettingDTO dsAutoPurchaseSetting = purchaseOrderService.getDsPurchaseSettingByShopId(shopId);
OpOrderDs opOrderDs = opOrderDsService.getByOrderId(orderId);
ApiResult checkRes = purchaseOrderService.checkAllowDsAutoPurchase(shopId, orderId, dsAutoPurchaseSetting, orderInfo.getOrderStatus(), orderInfo.getGmtPayTime(), ObjectUtil.isNotNull(opOrderDs));
if (!checkRes.isSuccess()) {
log.error(String.format("不符合自动采购条件shopId: %s, orderId: %s", shopId, orderId));
throw new MSException(StringUtils.isEmpty(checkRes.getMsg()) ? "不符合自动采购条件" : checkRes.getMsg());
}
List<String> orderIds = new ArrayList<>();
orderIds.add(orderId);
Map<String, List<OpOrderChildBO>> orderIdAndOpOrderChildsMap = orderPrintService.getOpOrderChildList(orderIds, ""); // todo tag这次不需要实现以后注意类型
List<OpOrderChildBO> opOrderChilds = orderIdAndOpOrderChildsMap.get(orderId);
Set<String> productIdSet = opOrderChilds.stream().map(OpOrderChildBO::getProductId).collect(Collectors.toSet());
List<String> productIds = new ArrayList<>(productIdSet);
// purchaseOrderService.getProductIdAndProductToDsItemMapByProductIds(productIds, PurchaseOrderConst.PURCHASE_PLATFORM1688_D_S);
Map<String, ProductToDsItem> productIdAndProductToDsItemMap = productToDsItemService.getProductIdAndProductToDsItemMapByProductIds(productIdSet, PurchaseOrderConst.PURCHASE_PLATFORM1688_D_S);
if (productIdAndProductToDsItemMap.isEmpty()) {
log.error(String.format("不存在分销关系shopId: %s, orderId: %s", shopId, orderId));
throw new MSException("不存在分销关系");
}
List<AfterSale> afterSaleList = afterSaleService.getAfterSalesByPid(orderInfo.getShopId(), orderInfo.getOrderId());
if (!afterSaleList.isEmpty()) {
log.error(String.format("订单已发起售后shopId: %s, orderId: %s", shopId, orderId));
throw new MSException("订单已发起售后");
}
GetPurchaseOrderItemsAndSourceItemsRequestDTO requestParams = new GetPurchaseOrderItemsAndSourceItemsRequestDTO();
List<String> shopIds = new ArrayList<>();
shopIds.add(SecurityTool.encodeByAES(String.valueOf(orderInfo.getShopId())));
requestParams.setPlatform("dd");
requestParams.setShopIds(shopIds);
requestParams.setPlatformItemIds(productIds);
requestParams.setPlatformOrderIds(null);
GetPurchaseOrderItemsAndSourceItemsResponseDTO dsRet = dsApiService.getPurchaseOrderItemsAndSourceItems(requestParams);
if (!dsRet.isSuccess()) {
log.error(String.format("请求失败接口getPurchaseOrderItemsAndSourceItemsshopId: %s, orderId: %sreason%s", shopId, orderId, dsRet.getReason()));
throw new MSException(dsRet.getReason());
}
List<PurchaseOrderItem> purchaseOrderItems = purchaseOrderService.getNormalPurchaseItemsByShopIdAndOrderId(orderInfo.getShopId(), orderId);
Map<Long, PurchaseOrderItem> skuIdAndPurchaseOrderItemMap = CommonTool.convertListToMap(purchaseOrderItems, PurchaseOrderItem::getSkuId);
Map<String, GetPurchaseOrderItemsAndSourceItemsResponseDTO.SourceItemInfo> platformItemIdAndSourceItemInfoMap = dsRet.getPlatformItemIdAndSourceItemInfoMap();
if (platformItemIdAndSourceItemInfoMap == null) {
platformItemIdAndSourceItemInfoMap = new HashMap<>();
}
Map map = getPlatformSkuIdAndMatchSourceSkuInfoMap(opOrderChilds, platformItemIdAndSourceItemInfoMap, productIdAndProductToDsItemMap);
//todo:
return true;
} catch (Exception e) {
return false;
}
}
private Map getPlatformSkuIdAndMatchSourceSkuInfoMap(List<OpOrderChildBO> opOrderChilds, Map<String, GetPurchaseOrderItemsAndSourceItemsResponseDTO.SourceItemInfo> platformItemIdAndSourceItemInfoMap, Map<String, ProductToDsItem> productIdAndProductToDsItemMap) {
Map platformSkuIdAndMatchSourceSkuInfoMap = 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) {
JSONObject obj = new JSONObject();
obj.put("reason", String.format("%s商品未代销", platfromItemId));
obj.put("platfromItemId", platfromItemId);
obj.put("platfromSkuId", platfromSkuId);
platformSkuIdAndMatchFailSkuInfoMap.put(platfromSkuId, obj);
continue;
}
GetPurchaseOrderItemsAndSourceItemsResponseDTO.SourceItemInfo sourceItemInfo = platformItemIdAndSourceItemInfoMap.get(platfromItemId);
// todo: rebuildSourceItemProductInfo 各种特判,看起来没用, 先跳过
// todo: DsOrderTool.getMatchSourceSkuInfo
// todo...
}
Map retMap = new HashMap();
retMap.put("platformSkuIdAndMatchSourceSkuInfoMap", platformSkuIdAndMatchSourceSkuInfoMap);
retMap.put("platformSkuIdAndMatchFailSkuInfoMap", platformSkuIdAndMatchFailSkuInfoMap);
return retMap;
}
}

@ -30,4 +30,6 @@ public interface AfterSaleMapper {
List<AfterSale> selectById(@Param("afterSaleId") List<Long> afterSaleId);
AfterSale getByOrderIdAndNotInStatusList(@Param("orderId") String orderId, @Param("notInStatusList") List<Integer> notInStatusList);
List<AfterSale> selectListByShopIdAndPid(Long shopId, String pid);
}

@ -41,4 +41,6 @@ public interface PurchaseOrderItemMapper {
List<PurchaseOrderItem> getSkuIdsByOrderId(@Param("orderId") String orderId);
List<PurchaseOrderItem> getRelatePurchaseOrderItemsByPurchaseOrderId(@Param("purchaseOrderId") Long purchaseOrderId);
List<PurchaseOrderItem> getNormalPurchaseItemsByShopIdAndOrderId(@Param("shopId") Long shopId, @Param("orderId") String orderId);
}

@ -58,7 +58,7 @@
select
<include refid="Base_Column_List" />
from after_sale
where after_sale_id = #{afterSaleId,jdbcType=BIGINT}
where after_sale_id = #{afterSaleId,jdbcType=BIGINT}
</select>
<select id="selectLastAfterSaleId" resultType="java.lang.Long">
select max(after_sale_id)
@ -88,9 +88,16 @@
#{item}
</foreach>
</select>
<select id="selectListByShopIdAndPid" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from after_sale
where shop_id = #{shopId,jdbcType=BIGINT} and pid=#{pid,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from after_sale
where after_sale_id = #{afterSaleId,jdbcType=BIGINT}
where after_sale_id = #{afterSaleId,jdbcType=BIGINT}
</delete>
<insert id="insert" keyColumn="after_sale_id" keyProperty="afterSaleId" parameterType="com.ms.dal.entity.AfterSale" useGeneratedKeys="true">
insert into after_sale
@ -290,11 +297,11 @@
gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
</if>
</set>
where after_sale_id = #{afterSaleId,jdbcType=BIGINT}
where after_sale_id = #{afterSaleId,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.ms.dal.entity.AfterSale">
update after_sale
set
set
shop_id = #{shopId,jdbcType=BIGINT},
order_id = #{orderId,jdbcType=VARCHAR},
pid = #{pId,jdbcType=VARCHAR},
@ -327,6 +334,6 @@
gmt_update_time = #{gmtUpdateTime,jdbcType=TIMESTAMP},
gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},
gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}
where after_sale_id = #{afterSaleId,jdbcType=BIGINT}
where after_sale_id = #{afterSaleId,jdbcType=BIGINT}
</update>
</mapper>

@ -61,12 +61,12 @@
select
<include refid="Base_Column_List" />
from purchase_order_item
where purchase_order_item_id = #{purchaseOrderItemId,jdbcType=BIGINT}
where purchase_order_item_id = #{purchaseOrderItemId,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from purchase_order_item
where purchase_order_item_id = #{purchaseOrderItemId,jdbcType=BIGINT}
where purchase_order_item_id = #{purchaseOrderItemId,jdbcType=BIGINT}
</delete>
<insert id="insert" keyColumn="purchase_order_item_id" keyProperty="purchaseOrderItemId" parameterType="com.ms.dal.entity.PurchaseOrderItem" useGeneratedKeys="true">
insert into purchase_order_item
@ -159,11 +159,11 @@
gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
</if>
</set>
where purchase_order_item_id = #{purchaseOrderItemId,jdbcType=BIGINT}
where purchase_order_item_id = #{purchaseOrderItemId,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.ms.dal.entity.PurchaseOrderItem">
update purchase_order_item
set
set
purchase_order_id = #{purchaseOrderId,jdbcType=INTEGER},
order_id = #{orderId,jdbcType=VARCHAR},
shop_id = #{shopId,jdbcType=BIGINT},
@ -177,7 +177,7 @@
real_purchase_url = #{realPurchaseUrl,jdbcType=VARCHAR},
gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},
gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}
where purchase_order_item_id = #{purchaseOrderItemId,jdbcType=BIGINT}
where purchase_order_item_id = #{purchaseOrderItemId,jdbcType=BIGINT}
</update>
<update id="updateStatusByIds">
update purchase_order_item
@ -227,4 +227,12 @@
where purchase_order_id = #{purchaseOrderId,jdbcType=BIGINT}
and status = 'normal'
</select>
<select id="getNormalPurchaseItemsByShopIdAndOrderId" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from purchase_order_item
where shop_id = #{shopId,jdbcType=BIGINT}
and order_id = #{orderId,jdbcType=VARCHAR}
and status = 'normal'
</select>
</mapper>

Loading…
Cancel
Save