feat(订单): 新增根据订单ID查询商品明细功能并调整库存扣减逻辑
新增`selectByOrderId`方法用于根据订单ID查询商品明细,并将库存扣减逻辑从订单创建时移至支付成功后执行,以避免未支付时库存被占用的问题。
This commit is contained in:
parent
365d7dfc11
commit
911a52f554
|
@ -217,12 +217,17 @@ public class OrderApplicationService {
|
|||
// 计算商品金额并验证库存
|
||||
goodsModel.calculateTotal();
|
||||
goodsModel.validateQuantity();
|
||||
CabinetCellEntity cabinetCellEntity = cabinetCellService.getById(goodsModel.getCellId());
|
||||
if (cabinetCellEntity == null || cabinetCellEntity.getStock() < goodsModel.getQuantity()) {
|
||||
throw new ApiException(ErrorCode.FAILED, "柜子库存不足");
|
||||
}
|
||||
|
||||
// 保存订单商品
|
||||
goodsModel.insert();
|
||||
|
||||
// 扣减库存
|
||||
deductGoodsStock(goodsModel.getGoodsId(), goodsModel.getQuantity(), goodsModel.getCellId());
|
||||
// 改为收到支付成功后扣减库存
|
||||
// deductGoodsStock(goodsModel.getGoodsId(), goodsModel.getQuantity(), goodsModel.getCellId());
|
||||
|
||||
totalAmount = totalAmount.add(goodsModel.getTotalAmount());
|
||||
}
|
||||
|
@ -233,24 +238,6 @@ public class OrderApplicationService {
|
|||
orderModel.updateById();
|
||||
}
|
||||
|
||||
private void deductGoodsStock(Long goodsId, Integer quantity, Long cellId) {
|
||||
CabinetCellEntity cabinetCellEntity = cabinetCellService.getById(cellId);
|
||||
if (cabinetCellEntity == null || cabinetCellEntity.getStock() < quantity) {
|
||||
throw new ApiException(ErrorCode.FAILED, "柜子库存不足");
|
||||
}
|
||||
|
||||
ShopGoodsEntity goods = goodsService.getById(goodsId);
|
||||
if (goods == null || goods.getStock() < quantity) {
|
||||
throw new ApiException(ErrorCode.FAILED, "商品库存不足");
|
||||
}
|
||||
|
||||
// 扣减库存
|
||||
cabinetCellEntity.setStock(cabinetCellEntity.getStock() - quantity);
|
||||
cabinetCellService.updateById(cabinetCellEntity);
|
||||
goods.setStock(goods.getStock() - quantity);
|
||||
goodsService.updateById(goods);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void cancelOrder(Long orderId) {
|
||||
OrderModel orderModel = orderModelFactory.loadById(orderId);
|
||||
|
|
|
@ -5,6 +5,8 @@ import org.apache.ibatis.annotations.Select;
|
|||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 订单商品明细表 Mapper 接口
|
||||
|
@ -18,4 +20,6 @@ public interface ShopOrderGoodsMapper extends BaseMapper<ShopOrderGoodsEntity> {
|
|||
@Select("SELECT * FROM shop_order_goods WHERE order_id = #{orderId} AND goods_id = #{goodsId} LIMIT 1")
|
||||
ShopOrderGoodsEntity selectByOrderIdAndGoodsId(@Param("orderId") Long orderId, @Param("goodsId") Long goodsId);
|
||||
|
||||
@Select("SELECT * FROM shop_order_goods WHERE order_id = #{orderId} AND deleted = 0")
|
||||
List<ShopOrderGoodsEntity> selectByOrderId(@Param("orderId") Long orderId);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@ package com.agileboot.domain.shop.order.db;
|
|||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 订单商品明细表 服务类
|
||||
|
@ -12,4 +14,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
|||
*/
|
||||
public interface ShopOrderGoodsService extends IService<ShopOrderGoodsEntity> {
|
||||
ShopOrderGoodsEntity getByOrderIdAndGoodsId(Long orderId, Long goodsId);
|
||||
|
||||
List<ShopOrderGoodsEntity> selectByOrderId(Long orderId);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package com.agileboot.domain.shop.order.db;
|
|||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 订单商品明细表 服务实现类
|
||||
|
@ -19,4 +21,9 @@ public class ShopOrderGoodsServiceImpl extends ServiceImpl<ShopOrderGoodsMapper,
|
|||
return baseMapper.selectByOrderIdAndGoodsId(orderId, goodsId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ShopOrderGoodsEntity> selectByOrderId(Long orderId) {
|
||||
return baseMapper.selectByOrderId(orderId);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,6 +5,10 @@ import cn.hutool.core.date.DateUtil;
|
|||
import com.agileboot.common.config.AgileBootConfig;
|
||||
import com.agileboot.common.exception.ApiException;
|
||||
import com.agileboot.common.exception.error.ErrorCode;
|
||||
import com.agileboot.domain.cabinet.cell.db.CabinetCellEntity;
|
||||
import com.agileboot.domain.cabinet.cell.db.CabinetCellService;
|
||||
import com.agileboot.domain.shop.goods.db.ShopGoodsEntity;
|
||||
import com.agileboot.domain.shop.goods.db.ShopGoodsService;
|
||||
import com.agileboot.domain.shop.order.db.ShopOrderEntity;
|
||||
import com.agileboot.domain.shop.order.db.ShopOrderGoodsService;
|
||||
import com.agileboot.domain.shop.order.db.ShopOrderService;
|
||||
|
@ -21,18 +25,23 @@ public class OrderModel extends ShopOrderEntity {
|
|||
|
||||
private ShopOrderService orderService;
|
||||
private ShopOrderGoodsService orderGoodsService;
|
||||
private CabinetCellService cabinetCellService;
|
||||
private ShopGoodsService goodsService;
|
||||
|
||||
public OrderModel(ShopOrderEntity entity, ShopOrderService orderService,
|
||||
ShopOrderGoodsService orderGoodsService) {
|
||||
this(orderService, orderGoodsService);
|
||||
ShopOrderGoodsService orderGoodsService, CabinetCellService cabinetCellService, ShopGoodsService goodsService) {
|
||||
this(orderService, orderGoodsService, cabinetCellService, goodsService);
|
||||
if (entity != null) {
|
||||
BeanUtil.copyProperties(entity, this);
|
||||
}
|
||||
}
|
||||
|
||||
public OrderModel(ShopOrderService orderService, ShopOrderGoodsService orderGoodsService) {
|
||||
public OrderModel(ShopOrderService orderService, ShopOrderGoodsService orderGoodsService,
|
||||
CabinetCellService cabinetCellService, ShopGoodsService goodsService) {
|
||||
this.orderService = orderService;
|
||||
this.orderGoodsService = orderGoodsService;
|
||||
this.cabinetCellService = cabinetCellService;
|
||||
this.goodsService = goodsService;
|
||||
}
|
||||
|
||||
public void calculateTotalAmount(List<Long> goodsIds) {
|
||||
|
@ -85,6 +94,11 @@ public class OrderModel extends ShopOrderEntity {
|
|||
log.error("支付时间转换失败", e);
|
||||
}
|
||||
this.updateById();
|
||||
|
||||
orderGoodsService.selectByOrderId(this.getOrderId()).forEach(orderGoods -> {
|
||||
// 扣减库存
|
||||
deductGoodsStock(orderGoods.getGoodsId(), orderGoods.getQuantity(), orderGoods.getCellId());
|
||||
});
|
||||
}
|
||||
|
||||
public void handleRefundSuccess() {
|
||||
|
@ -92,4 +106,22 @@ public class OrderModel extends ShopOrderEntity {
|
|||
this.setPayStatus(4);
|
||||
this.updateById();
|
||||
}
|
||||
|
||||
private void deductGoodsStock(Long goodsId, Integer quantity, Long cellId) {
|
||||
CabinetCellEntity cabinetCellEntity = cabinetCellService.getById(cellId);
|
||||
if (cabinetCellEntity == null || cabinetCellEntity.getStock() < quantity) {
|
||||
throw new ApiException(ErrorCode.FAILED, "柜子库存不足");
|
||||
}
|
||||
|
||||
ShopGoodsEntity goods = goodsService.getById(goodsId);
|
||||
if (goods == null || goods.getStock() < quantity) {
|
||||
throw new ApiException(ErrorCode.FAILED, "商品库存不足");
|
||||
}
|
||||
|
||||
// 扣减库存
|
||||
cabinetCellEntity.setStock(cabinetCellEntity.getStock() - quantity);
|
||||
cabinetCellService.updateById(cabinetCellEntity);
|
||||
goods.setStock(goods.getStock() - quantity);
|
||||
goodsService.updateById(goods);
|
||||
}
|
||||
}
|
|
@ -2,6 +2,8 @@ package com.agileboot.domain.shop.order.model;
|
|||
|
||||
import com.agileboot.common.exception.ApiException;
|
||||
import com.agileboot.common.exception.error.ErrorCode;
|
||||
import com.agileboot.domain.cabinet.cell.db.CabinetCellService;
|
||||
import com.agileboot.domain.shop.goods.db.ShopGoodsService;
|
||||
import com.agileboot.domain.shop.order.db.ShopOrderEntity;
|
||||
import com.agileboot.domain.shop.order.db.ShopOrderGoodsService;
|
||||
import com.agileboot.domain.shop.order.db.ShopOrderService;
|
||||
|
@ -14,13 +16,15 @@ public class OrderModelFactory {
|
|||
|
||||
private final ShopOrderService orderService;
|
||||
private final ShopOrderGoodsService orderGoodsService;
|
||||
private final CabinetCellService cabinetCellService;
|
||||
private final ShopGoodsService goodsService;
|
||||
|
||||
public OrderModel create(ShopOrderEntity entity) {
|
||||
return new OrderModel(entity, orderService, orderGoodsService);
|
||||
return new OrderModel(entity, orderService, orderGoodsService, cabinetCellService, goodsService);
|
||||
}
|
||||
|
||||
public OrderModel create() {
|
||||
return new OrderModel(orderService, orderGoodsService);
|
||||
return new OrderModel(orderService, orderGoodsService, cabinetCellService, goodsService);
|
||||
}
|
||||
|
||||
public OrderModel loadById(Long orderId) {
|
||||
|
@ -28,6 +32,6 @@ public class OrderModelFactory {
|
|||
if (entity == null) {
|
||||
throw new ApiException(ErrorCode.Business.COMMON_OBJECT_NOT_FOUND, orderId, "订单");
|
||||
}
|
||||
return new OrderModel(entity, orderService, orderGoodsService);
|
||||
return new OrderModel(entity, orderService, orderGoodsService, cabinetCellService, goodsService);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue