创建订单扣除库存
This commit is contained in:
@@ -122,7 +122,6 @@ public class CartService {
|
||||
|
||||
/**
|
||||
* 加入购物车
|
||||
*
|
||||
* @param jsonObject 商品信息
|
||||
*/
|
||||
// @Transactional(rollbackFor = Exception.class)
|
||||
@@ -289,16 +288,14 @@ public class CartService {
|
||||
// log.error("长链接错误 createCart{}", e.getMessage());
|
||||
// }
|
||||
// }
|
||||
|
||||
public void createCart(JSONObject jsonObject) {
|
||||
try {
|
||||
|
||||
String tableId = jsonObject.getString("tableId");
|
||||
String shopId = jsonObject.getString("shopId");
|
||||
String productId = jsonObject.getString("productId");
|
||||
String key = tableId + "-" + shopId;
|
||||
|
||||
TbProduct tbProduct = productMapper.selectById(Integer.valueOf(productId));
|
||||
TbProductSkuWithBLOBs tbProductSkuWithBLOBs = null;
|
||||
if (tbProduct == null) {
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("status", "fail");
|
||||
@@ -322,8 +319,8 @@ public class CartService {
|
||||
throw new MsgException("该商品已售罄");
|
||||
}
|
||||
} else {
|
||||
tbProductSkuWithBLOBs = productSkuMapper.selectByPrimaryKey(Integer.valueOf(skuId));
|
||||
if (tbProductSkuWithBLOBs.getIsPauseSale().equals(1)) {//是否售罄
|
||||
TbProductSkuWithBLOBs tbProductSkuWithBLOBs = productSkuMapper.selectByPrimaryKey(Integer.valueOf(skuId));
|
||||
if(tbProductSkuWithBLOBs.getIsPauseSale().equals(1)){//是否售罄
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("status", "fail");
|
||||
jsonObject1.put("msg", "该商品已售罄");
|
||||
@@ -340,13 +337,10 @@ public class CartService {
|
||||
|
||||
if (redisUtil.exists(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId").concat("-").concat(shopId)))) {
|
||||
JSONArray array = JSON.parseArray(redisUtil.getMessage(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId").concat("-").concat(shopId))));
|
||||
// 未创建购物车,新增购物车
|
||||
if (Objects.isNull(array) || array.isEmpty()) {
|
||||
if (buyNum > 0) {
|
||||
|
||||
TbCashierCart cashierCart = addCart(jsonObject.getString("productId"), skuId,
|
||||
jsonObject.getInteger("userId"), buyNum, tableId, shopId);
|
||||
|
||||
jsonArray.add(cashierCart);
|
||||
amount = amount.add(new BigDecimal(cashierCart.getNumber()).multiply(cashierCart.getSalePrice().add(cashierCart.getPackFee())));
|
||||
}
|
||||
@@ -355,13 +349,10 @@ public class CartService {
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JSONObject object = array.getJSONObject(i);
|
||||
TbCashierCart cashierCart = JSONUtil.parseJSONStr2T(object.toJSONString(), TbCashierCart.class);
|
||||
|
||||
// 已添加购物车商品,加数量
|
||||
if (cashierCart.getSkuId().equals(skuId)) {
|
||||
cashierCart.setTotalNumber(cashierCart.getTotalNumber() + buyNum);
|
||||
cashierCart.setNumber(cashierCart.getNumber() + buyNum);
|
||||
if (cashierCart.getNumber() > 0) {
|
||||
|
||||
cashierCart.setTotalAmount(new BigDecimal(cashierCart.getTotalNumber()).multiply(cashierCart.getSalePrice().add(cashierCart.getPackFee())));
|
||||
cashierCart.setUpdatedAt(Instant.now().toEpochMilli());
|
||||
cashierCartMapper.updateByPrimaryKeySelective(cashierCart);
|
||||
@@ -374,16 +365,13 @@ public class CartService {
|
||||
jsonArray.add(cashierCart);
|
||||
amount = amount.add(new BigDecimal(cashierCart.getTotalNumber()).multiply(cashierCart.getSalePrice().add(cashierCart.getPackFee())));
|
||||
}
|
||||
// 已有购物车中不存在,新增
|
||||
if (flag && buyNum > 0) {
|
||||
|
||||
TbCashierCart cashierCart = addCart(jsonObject.getString("productId"), skuId,
|
||||
jsonObject.getInteger("userId"), buyNum, tableId, jsonObject.getString("shopId"));
|
||||
jsonArray.add(cashierCart);
|
||||
amount = amount.add(new BigDecimal(cashierCart.getTotalNumber()).multiply(cashierCart.getSalePrice().add(cashierCart.getPackFee())));
|
||||
}
|
||||
}
|
||||
// 未创建购物车新增
|
||||
} else {
|
||||
if (buyNum > 0) {
|
||||
TbCashierCart cashierCart = addCart(jsonObject.getString("productId"), skuId,
|
||||
@@ -400,12 +388,10 @@ public class CartService {
|
||||
jsonObject1.put("data", jsonArray);
|
||||
jsonObject1.put("amount", amount);
|
||||
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), jsonObject.getString("tableId").concat("-").concat(shopId), "", false);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("长链接错误 createCart{}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改库存并根据警告线发送消息
|
||||
*
|
||||
@@ -566,7 +552,18 @@ public class CartService {
|
||||
TbProduct tbProduct1 = tbProductMapper.selectById(Integer.valueOf(tbProduct.getProductId()));
|
||||
log.info("开始修改库存,商品id:{},商品名:{}", tbProduct1.getId(), tbProduct1.getName());
|
||||
// 修改库存
|
||||
productService.updateStock(tbProduct.getProductId(), tbProduct.getId(), cashierCart.getNumber(), tbProduct1.getIsDistribute() == 1);
|
||||
try {
|
||||
productService.updateStock(tbProduct.getProductId(), tbProduct.getId(), cashierCart.getNumber(), tbProduct1.getIsDistribute() == 1);
|
||||
}catch (Exception e) {
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("status", "fail");
|
||||
jsonObject1.put("msg", "商品库存不足" + tbProduct1.getName());
|
||||
jsonObject1.put("data", new ArrayList<>());
|
||||
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true);
|
||||
|
||||
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true);
|
||||
return;
|
||||
}
|
||||
|
||||
totalAmount = totalAmount.add(cashierCart.getTotalAmount());
|
||||
packAMount = packAMount.add(cashierCart.getPackFee());
|
||||
|
||||
Reference in New Issue
Block a user