下单扣取库存

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

View File

@ -5,6 +5,7 @@ import com.chaozhanggui.system.cashierservice.entity.TbProductWithBLOBs;
import com.chaozhanggui.system.cashierservice.entity.po.ProConsSkuInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;
import java.util.List;
@ -41,4 +42,10 @@ public interface TbProductMapper {
List<ProConsSkuInfo> selectBySkuId(Integer skuId);
@Update("update tb_product set stock_number=stock_number+#{num} WHERE id=#{id}")
int incrStock(String id, int num);
@Update("update tb_product set stock_number=stock_number-#{num} WHERE id=#{id} and stock_number-#{num} > 0")
int decrStock(String id, int num);
}

View File

@ -5,6 +5,7 @@ import com.chaozhanggui.system.cashierservice.entity.TbProductSkuWithBLOBs;
import com.chaozhanggui.system.cashierservice.entity.po.ProductSkuPo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;
import java.util.List;
@ -39,4 +40,10 @@ public interface TbProductSkuMapper {
void batchStockNum(@Param("list")List<ProductSkuPo> list);
}
@Update("update tb_product_sku set stock_number=stock_number+#{num} WHERE id=#{id}")
int incrStock(String skuId, int addNum);
@Update("update tb_product set stock_number=stock_number-#{num} WHERE id=#{id} and stock_number-#{num} > 0")
int decrStock(String id, int num);
}

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("库存不足,下单失败");
}
}
}
}

View File

@ -18,4 +18,5 @@ public class RedisCst {
public static final String OUT_NUMBER="ORDER:NUMBER:";
public static final String ORDER_MESSAGE="ORDER:MESSAGE:";
public static final String ORDER_PRODUCT_NUM = "ORDER_NUM:";
}