fix(库存管理): 修复商品和柜机格口库存同步问题

修复了商品和柜机格口库存不同步的问题,确保在订单创建和退货审批时,商品库存和柜机格口库存能够正确扣减和增加。具体修改包括:
1. 在订单创建时,同时扣减商品和柜机格口的库存。
2. 在退货审批时,同时增加商品和柜机格口的库存。
3. 更新SQL查询以包含柜机格口的库存信息。
This commit is contained in:
dzq 2025-04-18 16:27:04 +08:00
parent 00bb295cb0
commit 65b6dbd1d9
3 changed files with 23 additions and 3 deletions

View File

@ -2,6 +2,8 @@ package com.agileboot.domain.shop.approval;
import com.agileboot.common.constant.PayApiConstants; import com.agileboot.common.constant.PayApiConstants;
import com.agileboot.common.core.page.PageDTO; import com.agileboot.common.core.page.PageDTO;
import com.agileboot.domain.cabinet.cell.model.CabinetCellModel;
import com.agileboot.domain.cabinet.cell.model.CabinetCellModelFactory;
import com.agileboot.domain.common.command.BulkOperationCommand; import com.agileboot.domain.common.command.BulkOperationCommand;
import com.agileboot.domain.qywx.accessToken.AccessTokenApplicationService; import com.agileboot.domain.qywx.accessToken.AccessTokenApplicationService;
import com.agileboot.domain.qywx.accessToken.db.QyAccessTokenEntity; import com.agileboot.domain.qywx.accessToken.db.QyAccessTokenEntity;
@ -59,6 +61,7 @@ public class ReturnApprovalApplicationService {
private final AuthCorpInfoApplicationService authCorpInfoApplicationService; private final AuthCorpInfoApplicationService authCorpInfoApplicationService;
private final AccessTokenApplicationService accessTokenApplicationService; private final AccessTokenApplicationService accessTokenApplicationService;
private final QyUserService qyUserService; private final QyUserService qyUserService;
private final CabinetCellModelFactory cabinetCellModelFactory;
/** /**
* 获取退货审批列表 * 获取退货审批列表
@ -167,6 +170,11 @@ public class ReturnApprovalApplicationService {
GoodsModel goodsModel = goodsModelFactory.loadById(orderGoodsModel.getGoodsId()); GoodsModel goodsModel = goodsModelFactory.loadById(orderGoodsModel.getGoodsId());
goodsModel.setStock(goodsModel.getStock() + orderGoodsModel.getQuantity()); goodsModel.setStock(goodsModel.getStock() + orderGoodsModel.getQuantity());
goodsModel.updateById(); goodsModel.updateById();
// 更新格口库存
CabinetCellModel cabinetCellModel = cabinetCellModelFactory.loadById(orderGoodsModel.getCellId());
cabinetCellModel.setStock(cabinetCellModel.getStock() + orderGoodsModel.getQuantity());
cabinetCellModel.updateById();
} }
/** /**

View File

@ -63,7 +63,10 @@ public interface ShopGoodsMapper extends BaseMapper<ShopGoodsEntity> {
* 查询商品及其关联的柜机格口信息 * 查询商品及其关联的柜机格口信息
* @return 商品列表 * @return 商品列表
*/ */
@Select("SELECT g.*, sc.cabinet_id, sc.cabinet_name, cc.cell_id " + @Select("SELECT g.goods_id, g.goods_name, g.category_id, g.price, " +
"g.status, g.cover_img, g.goods_detail, " +
"g.creator_id, g.create_time, g.updater_id, g.update_time, g.remark, g.deleted, " +
"sc.cabinet_id, sc.cabinet_name, cc.stock, cc.cell_id " +
"FROM shop_goods g " + "FROM shop_goods g " +
"LEFT JOIN cabinet_cell cc ON g.goods_id = cc.goods_id " + "LEFT JOIN cabinet_cell cc ON g.goods_id = cc.goods_id " +
"LEFT JOIN smart_cabinet sc ON cc.cabinet_id = sc.cabinet_id " + "LEFT JOIN smart_cabinet sc ON cc.cabinet_id = sc.cabinet_id " +

View File

@ -197,7 +197,7 @@ public class OrderApplicationService {
goodsModel.insert(); goodsModel.insert();
// 扣减库存 // 扣减库存
deductGoodsStock(goodsModel.getGoodsId(), goodsModel.getQuantity()); deductGoodsStock(goodsModel.getGoodsId(), goodsModel.getQuantity(), goodsModel.getCellId());
totalAmount = totalAmount.add(goodsModel.getTotalAmount()); totalAmount = totalAmount.add(goodsModel.getTotalAmount());
} }
@ -208,11 +208,20 @@ public class OrderApplicationService {
orderModel.updateById(); orderModel.updateById();
} }
private void deductGoodsStock(Long goodsId, Integer quantity) { 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); ShopGoodsEntity goods = goodsService.getById(goodsId);
if (goods == null || goods.getStock() < quantity) { if (goods == null || goods.getStock() < quantity) {
throw new ApiException(ErrorCode.FAILED, "商品库存不足"); throw new ApiException(ErrorCode.FAILED, "商品库存不足");
} }
// 扣减库存
cabinetCellEntity.setStock(cabinetCellEntity.getStock() - quantity);
cabinetCellService.updateById(cabinetCellEntity);
goods.setStock(goods.getStock() - quantity); goods.setStock(goods.getStock() - quantity);
goodsService.updateById(goods); goodsService.updateById(goods);
} }