下单扣取库存

This commit is contained in:
2024-07-03 10:57:40 +08:00
parent e31f3c831a
commit 8da3d036b1
5 changed files with 60 additions and 1 deletions

View File

@@ -73,6 +73,8 @@ public class OrderService {
private static ConcurrentHashMap<String, HashSet<Integer>> codeMap = new ConcurrentHashMap<>();
private static ConcurrentHashMap<String, HashSet<String>> userMap = new ConcurrentHashMap<>();
@Autowired
private ProductService productService;
public OrderService(WechatUtil wechatUtil, TbUserShopMsgMapper tbUserShopMsgMapper, TbShopOpenIdMapper shopOpenIdMapper) {
this.wechatUtil = wechatUtil;
@@ -491,8 +493,27 @@ public class OrderService {
}
boolean flag = true;
for (TbCashierCart cashierCart : list) {
// 已经添加的商品,修改数量
if (StringUtils.isNotEmpty(cashierCart.getOrderId())) {
flag = false;
String message = redisUtil.getMessage(RedisCst.ORDER_PRODUCT_NUM + cashierCart.getId());
if (message != null) {
int lastNum = Integer.parseInt(message);
// 数量减少, 返还库存
if (lastNum > cashierCart.getNumber()) {
productService.incrStock(cashierCart.getProductId(), cashierCart.getSkuId(), lastNum - cashierCart.getNumber());
} else {
productService.decrStock(cashierCart.getProductId(), cashierCart.getSkuId(), cashierCart.getNumber() - lastNum);
}
redisUtil.saveMessage(RedisCst.ORDER_PRODUCT_NUM + cashierCart.getId(), cashierCart.getNumber().toString(), 24 * 60*60*1000);
}
// 首次添加的商品
}else {
redisUtil.saveMessage(RedisCst.ORDER_PRODUCT_NUM + cashierCart.getId(), cashierCart.getNumber().toString(), 24 * 60*60*1000);
// 修改库存
productService.decrStock(cashierCart.getProductId(), cashierCart.getSkuId(), cashierCart.getNumber());
}
cashierCart.setOrderId(orderId + "");
cashierCart.setUpdatedAt(System.currentTimeMillis());

View File

@@ -4,6 +4,7 @@ import cn.hutool.core.util.ObjectUtil;
import com.chaozhanggui.system.cashierservice.dao.*;
import com.chaozhanggui.system.cashierservice.entity.*;
import com.chaozhanggui.system.cashierservice.entity.vo.ShopCategoryVo;
import com.chaozhanggui.system.cashierservice.exception.MsgException;
import com.chaozhanggui.system.cashierservice.sign.CodeEnum;
import com.chaozhanggui.system.cashierservice.sign.Result;
import com.chaozhanggui.system.cashierservice.util.DateUtils;
@@ -122,4 +123,26 @@ public class ProductService {
PageInfo pageInfo=new PageInfo(tbProductWithBLOBs);
return Result.success(CodeEnum.SUCCESS,pageInfo);
}
public void incrStock(String productId, String skuId, int addNum) {
TbProductWithBLOBs product = tbProductMapper.selectByPrimaryKey(Integer.valueOf(productId));
if (product.getIsDistribute() == 1) {
tbProductMapper.incrStock(productId, addNum);
}else {
tbProductSkuMapper.incrStock(skuId, addNum);
}
}
public void decrStock(String productId, String skuId, int decrNum) {
TbProductWithBLOBs product = tbProductMapper.selectByPrimaryKey(Integer.valueOf(productId));
if (product.getIsDistribute() == 1) {
if (tbProductMapper.decrStock(productId, decrNum) < 1) {
throw new MsgException("库存不足,下单失败");
}
}else {
if (tbProductSkuMapper.decrStock(String.valueOf(skuId), decrNum) < 1) {
throw new MsgException("库存不足,下单失败");
}
}
}
}