创建交易订单-单元测试-冲突合并

This commit is contained in:
ex_yang.li@ca-nio.com 2022-09-15 17:28:08 +08:00
parent f21ce954ec
commit 9cf470df94
4 changed files with 18 additions and 18 deletions

View File

@ -25,7 +25,7 @@ public class ProductSpuApiImpl implements ProductSpuApi {
private ProductSpuMapper productSpuMapper;
@Override
public List<SpuInfoRespDTO> getSpusByIds(Collection<Long> spuIds) {
public List<SpuInfoRespDTO> getSpuList(Collection<Long> spuIds) {
if (CollectionUtils.isAnyEmpty(spuIds)) {
return Collections.emptyList();
}

View File

@ -20,5 +20,5 @@ public interface TradeOrderItemConvert {
* @param items sku列表价格
* @return 订单项
*/
List<TradeOrderItemDO> convertList(List<PriceCalculateRespDTO.Item> items);
List<TradeOrderItemDO> convertList(List<PriceCalculateRespDTO.OrderItem> items);
}

View File

@ -30,7 +30,7 @@ import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO;
import cn.iocoder.yudao.module.trade.dal.mysql.order.TradeOrderMapper;
import cn.iocoder.yudao.module.trade.dal.mysql.orderitem.TradeOrderItemMapper;
import cn.iocoder.yudao.module.trade.enums.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderItemRefundStatusEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderRefundStatusEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderStatusEnum;
@ -82,12 +82,12 @@ public class TradeOrderServiceImpl implements TradeOrderService {
List<Item> items = createReqVO.getItems();
// 商品SKU检查 sku可售状态,库存
List<SkuInfoRespDTO> skuInfos = productSkuApi.getSkusByIds(CollectionUtils.convertSet(items, Item::getSkuId));
Map<Long, SkuInfoRespDTO> skuInfoMap = CollectionUtils.convertMap(skuInfos, SkuInfoRespDTO::getId);
List<ProductSkuRespDTO> skuInfos = productSkuApi.getSkuList(CollectionUtils.convertSet(items, Item::getSkuId));
Map<Long, ProductSkuRespDTO> skuInfoMap = CollectionUtils.convertMap(skuInfos, ProductSkuRespDTO::getId);
checkSaleableAndStockFromSpu(skuInfoMap, items);
// 商品SPU检查 sku可售状态,库存
List<SpuInfoRespDTO> spuInfos = productSpuApi.getSpusByIds(CollectionUtils.convertSet(skuInfos, SkuInfoRespDTO::getSpuId));
List<SpuInfoRespDTO> spuInfos = productSpuApi.getSpuList(CollectionUtils.convertSet(skuInfos, ProductSkuRespDTO::getSpuId));
checkSaleableFromSpu(spuInfos);
// 价格计算
@ -99,7 +99,7 @@ public class TradeOrderServiceImpl implements TradeOrderService {
tradeOrderMapper.insert(tradeOrderDO);
// 订单项信息记录
List<TradeOrderItemDO> tradeOrderItems = TradeOrderItemConvert.INSTANCE.convertList(priceResp.getItems());
List<TradeOrderItemDO> tradeOrderItems = TradeOrderItemConvert.INSTANCE.convertList(priceResp.getOrder().getItems());
//-填充订单项-SKU信息
fillItemsInfoFromSkuAndOrder(tradeOrderDO, tradeOrderItems, skuInfoMap);
tradeOrderItemMapper.insertBatch(tradeOrderItems);
@ -156,13 +156,13 @@ public class TradeOrderServiceImpl implements TradeOrderService {
}
private void fillItemsInfoFromSkuAndOrder(TradeOrderDO tradeOrderDO, List<TradeOrderItemDO> tradeOrderItems,
Map<Long, SkuInfoRespDTO> spuInfos) {
Map<Long, ProductSkuRespDTO> spuInfos) {
for (TradeOrderItemDO tradeOrderItem : tradeOrderItems) {
// 填充订单信息
tradeOrderItem.setOrderId(tradeOrderDO.getId());
tradeOrderItem.setUserId(tradeOrderDO.getUserId());
// 填充SKU信息
SkuInfoRespDTO skuInfoRespDTO = spuInfos.get(tradeOrderItem.getSkuId());
ProductSkuRespDTO skuInfoRespDTO = spuInfos.get(tradeOrderItem.getSkuId());
tradeOrderItem.setSpuId(skuInfoRespDTO.getSpuId());
tradeOrderItem.setPicUrl(skuInfoRespDTO.getPicUrl());
tradeOrderItem.setName(skuInfoRespDTO.getName());
@ -182,14 +182,14 @@ public class TradeOrderServiceImpl implements TradeOrderService {
}
}
private void checkSaleableAndStockFromSpu(Map<Long, SkuInfoRespDTO> skuInfoMap,
private void checkSaleableAndStockFromSpu(Map<Long, ProductSkuRespDTO> skuInfoMap,
List<Item> items) {
// sku 不存在
if (items.size() != skuInfoMap.size()) {
throw ServiceExceptionUtil.exception(ErrorCodeConstants.ORDER_SKU_NOT_FOUND);
}
for (Item item : items) {
SkuInfoRespDTO skuInfoDTO = skuInfoMap.get(item.getSkuId());
ProductSkuRespDTO skuInfoDTO = skuInfoMap.get(item.getSkuId());
// sku禁用
if (!Objects.equals(CommonStatusEnum.ENABLE.getStatus(), skuInfoDTO.getStatus())) {
throw ServiceExceptionUtil.exception(ErrorCodeConstants.ORDER_SKU_NOT_SALE);

View File

@ -6,7 +6,7 @@ import cn.iocoder.yudao.module.market.api.price.PriceApi;
import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateRespDTO;
import cn.iocoder.yudao.module.pay.api.order.PayOrderApi;
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
import cn.iocoder.yudao.module.product.api.sku.dto.SkuInfoRespDTO;
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
import cn.iocoder.yudao.module.product.api.spu.dto.SpuInfoRespDTO;
import cn.iocoder.yudao.module.trade.controller.app.order.vo.AppTradeOrderCreateReqVO;
@ -60,21 +60,21 @@ class TradeOrderServiceTest extends BaseDbUnitTest {
spuInfo.setId(1L);
spuInfo.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
when(productSpuApi.getSpusByIds(Collections.singleton(1L))).thenReturn(Lists.newArrayList(spuInfoRespDTO));
when(productSpuApi.getSpuList(Collections.singleton(1L))).thenReturn(Lists.newArrayList(spuInfoRespDTO));
// mock 商品SkU数据
SkuInfoRespDTO skuInfoRespDTO = randomPojo(SkuInfoRespDTO.class, skuInfo -> {
ProductSkuRespDTO skuInfoRespDTO = randomPojo(ProductSkuRespDTO.class, skuInfo -> {
skuInfo.setId(1L);
skuInfo.setStatus(CommonStatusEnum.ENABLE.getStatus());
skuInfo.setStock(randomInteger());
skuInfo.setSpuId(1L);
});
when(productSkuApi.getSkusByIds(Collections.singleton(1L))).thenReturn(Lists.newArrayList(skuInfoRespDTO));
when(productSkuApi.getSkuList(Collections.singleton(1L))).thenReturn(Lists.newArrayList(skuInfoRespDTO));
// mock 价格信息
PriceCalculateRespDTO calculateRespDTO = randomPojo(PriceCalculateRespDTO.class, priceCalculateRespDTO -> {
PriceCalculateRespDTO.Item item = priceCalculateRespDTO.getItems().get(0);
PriceCalculateRespDTO.OrderItem item = priceCalculateRespDTO.getOrder().getItems().get(0);
item.setSkuId(1L);
item.setCount(2);
priceCalculateRespDTO.setItems(Collections.singletonList(item));
priceCalculateRespDTO.getOrder().setItems(Collections.singletonList(item));
});
when(priceApi.calculatePrice(any())).thenReturn(calculateRespDTO);
//mock 支付订单信息
@ -103,7 +103,7 @@ class TradeOrderServiceTest extends BaseDbUnitTest {
assertEquals(skuInfoRespDTO.getId(), tradeOrderItemDO.getSkuId());
assertEquals(1L, tradeOrderItemDO.getUserId());
//价格
assertEquals(calculateRespDTO.getItems().get(0).getPresentPrice(), tradeOrderItemDO.getPresentPrice());
assertEquals(calculateRespDTO.getOrder().getItems().get(0).getPresentPrice(), tradeOrderItemDO.getPresentPrice());
}
}