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