xiaoyong931011
2022-09-23 55ed00804fcd0e8fe9026ae6464bfb69d3eb0d15
src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallOrderInfoServiceImpl.java
@@ -86,7 +86,7 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createOrder(AddOrderDto addOrderDto) {
        synchronized(this){
//        synchronized(this){
        MallMember member = LoginUserUtil.getLoginUser();
        String takeUniqueCode = addOrderDto.getTakeUniqueCode();
        if(StrUtil.isEmpty(takeUniqueCode)){
@@ -240,7 +240,7 @@
        agentProducer.sendOrderCancelDelayMsg(orderInfo.getId(), 15 * 60 * 1000L);
        return orderInfo.getId();
        }
//        }
    }
@@ -260,23 +260,34 @@
        orderInfo.setStatus(OrderStatusEnum.CANCEL.getValue());
        orderInfo.setCancelType(MallOrderInfo.CANCEL_BY_SELF);
        this.baseMapper.updateById(orderInfo);
        List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectListByOrderId(id);
        List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectListByOrderId(orderInfo.getId());
        if(CollUtil.isNotEmpty(mallOrderItemList)){
            for(MallOrderItem mallOrderItem : mallOrderItemList){
                Long goodsId = mallOrderItem.getGoodsId();
                Long skuId = mallOrderItem.getSkuId();
                Integer cnt = mallOrderItem.getCnt();
                MallGoods mallGoods = mallGoodsMapper.selectById(goodsId);
                mallGoods.setVolume(mallGoods.getVolume() - cnt);
                mallGoods.setStock(mallGoods.getStock() + cnt);
                mallGoodsMapper.updateById(mallGoods);
                MallGoodsSku sku = mallGoodsSkuMapper.selectSkuInfoById(mallOrderItem.getSkuId());
                if (sku == null) {
                    throw new FebsException("购买商品或sku不存在");
                }
                MallGoodsSku mallGoodsSku = mallGoodsSkuMapper.selectById(skuId);
                mallGoodsSku.setSkuVolume(mallGoodsSku.getSkuVolume() - cnt);
                mallGoodsSku.setStock(mallGoodsSku.getStock() + cnt);
                mallGoodsSkuMapper.updateById(mallGoodsSku);
                if (sku.getStock() < mallOrderItem.getCnt()) {
                    throw new FebsException(sku.getSkuName() + "库存不足");
                }
                MallGoods mallGoods = mallGoodsMapper.selectById(sku.getGoodsId());
                Integer goodsResult = mallGoodsMapper.updateStockAndVolumeByGoodsId(mallGoods.getId(), mallOrderItem.getCnt());
                if(1 != goodsResult){
                    throw new FebsException(mallGoods.getGoodsName() + "库存不足");
                }
                Integer skuResult = mallGoodsSkuMapper.updateStockAndVolumeBySkuId(sku.getId(),mallOrderItem.getCnt());
                if(1 != skuResult){
                    throw new FebsException(sku.getSkuName() + "库存不足");
                }
            }
        }
    }
    /**
@@ -478,11 +489,18 @@
    @Override
    public List<OrderListVo> findOrderList(OrderListDto orderListDto) {
        log.info("订单查询");
        MallMember member = LoginUserUtil.getLoginUser();
        IPage<MallOrderInfo> page = new Page<>(orderListDto.getPageNum(), orderListDto.getPageSize());
        orderListDto.setMemberId(member.getId());
        IPage<MallOrderInfo> mallOrderInfos = this.baseMapper.selectApiOrderListInPage(page, orderListDto);
//        IPage<MallOrderInfo> mallOrderInfos = this.baseMapper.selectApiOrderListInPage(page, orderListDto);
        IPage<MallOrderInfo> mallOrderInfos = this.baseMapper.selectNewApiOrderListInPage(page, orderListDto);
        if (CollUtil.isNotEmpty(mallOrderInfos.getRecords())) {
            mallOrderInfos.getRecords().forEach(item -> {
                item.setItems(mallOrderItemMapper.selectListByOrderId(item.getId()));
            });
        }
        return MallOrderInfoConversion.INSTANCE.entitysToVos(mallOrderInfos.getRecords());
    }
@@ -682,6 +700,31 @@
            orderInfo.setStatus(OrderStatusEnum.CANCEL.getValue());
            orderInfo.setCancelType(MallOrderInfo.CANCEL_OVERTIME_NO_PAY);
            this.baseMapper.updateById(orderInfo);
            List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectListByOrderId(orderInfo.getId());
            if(CollUtil.isNotEmpty(mallOrderItemList)){
                for(MallOrderItem mallOrderItem : mallOrderItemList){
                    MallGoodsSku sku = mallGoodsSkuMapper.selectSkuInfoById(mallOrderItem.getSkuId());
                    if (sku == null) {
                        throw new FebsException("购买商品或sku不存在");
                    }
                    if (sku.getStock() < mallOrderItem.getCnt()) {
                        throw new FebsException(sku.getSkuName() + "库存不足");
                    }
                    MallGoods mallGoods = mallGoodsMapper.selectById(sku.getGoodsId());
                    Integer goodsResult = mallGoodsMapper.updateStockAndVolumeByGoodsId(mallGoods.getId(), mallOrderItem.getCnt());
                    if(1 != goodsResult){
                        throw new FebsException(mallGoods.getGoodsName() + "库存不足");
                    }
                    Integer skuResult = mallGoodsSkuMapper.updateStockAndVolumeBySkuId(sku.getId(),mallOrderItem.getCnt());
                    if(1 != skuResult){
                        throw new FebsException(sku.getSkuName() + "库存不足");
                    }
                }
            }
        }
    }