购物车不操作库存/耗材

This commit is contained in:
2024-07-02 11:40:41 +08:00
parent 47838fe10d
commit ac3ef8d8d3

View File

@@ -110,12 +110,175 @@ public class CartService {
* @param jsonObject 商品信息 * @param jsonObject 商品信息
*/ */
// @Transactional(rollbackFor = Exception.class) // @Transactional(rollbackFor = Exception.class)
// 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));
// if (tbProduct == null) {
// JSONObject jsonObject1 = new JSONObject();
// jsonObject1.put("status", "fail");
// jsonObject1.put("msg", "该商品不存在");
// jsonObject1.put("data", new ArrayList<>());
// PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true);
// log.error("该商品不存在 productId:{}", productId);
// throw new MsgException("该商品不存在");
// }
//
// String skuId = jsonObject.getString("skuId");
// JSONArray jsonArray = new JSONArray();
// BigDecimal amount = BigDecimal.ZERO;
// TbProductSkuWithBLOBs tbProductSkuWithBLOBs = productSkuMapper.selectByPrimaryKey(Integer.valueOf(skuId));
//
//
//
//
// JSONObject objectMsg=new JSONObject();
// objectMsg.put("skuId",tbProductSkuWithBLOBs.getId());
// objectMsg.put("shopId",Integer.valueOf(shopId));
//
//
// producer.con_msg(objectMsg.toString());
//
//
// if (Integer.valueOf(tbProduct.getIsPauseSale()).equals(1)) {
// JSONObject jsonObject1 = new JSONObject();
// jsonObject1.put("status", "fail");
// jsonObject1.put("msg", "该商品已售罄");
// jsonObject1.put("data", new ArrayList<>());
// PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true);
// log.error("该商品已售罄 productId:{}", productId);
// throw new MsgException("该商品已售罄");
// }
//
// String skuNum;
//
// // 1:共享库存 0:独立库存
// if (Integer.valueOf(tbProduct.getIsDistribute()).equals(1)) {
// boolean exist = redisUtil.exists(RedisCst.PRODUCT + shopId + ":product" + productId);
// if (!exist) {
// redisUtil.saveMessage(RedisCst.PRODUCT + shopId + ":product" + productId, tbProduct.getStockNumber() + "");
// }
// skuNum = redisUtil.getMessage(RedisCst.PRODUCT + shopId + ":product" + productId);
//
// if (!skuNum.equals(tbProduct.getStockNumber() + "")) {
// skuNum = tbProduct.getStockNumber() + "";
// redisUtil.saveMessage(RedisCst.PRODUCT + shopId + ":product" + productId, skuNum);
// }
// } else {
// boolean exist = redisUtil.exists(RedisCst.PRODUCT + shopId + ":" + skuId);
// if (!exist) {
// Double stock = tbProductSkuWithBLOBs.getStockNumber();
// redisUtil.saveMessage(RedisCst.PRODUCT + shopId + ":" + skuId, Math.round(stock) + "");
// }
// skuNum = redisUtil.getMessage(RedisCst.PRODUCT + shopId + ":" + skuId);
//
// if (!skuNum.equals(Math.round(tbProductSkuWithBLOBs.getStockNumber()) + "")) {
// skuNum = Math.round(tbProductSkuWithBLOBs.getStockNumber()) + "";
// redisUtil.saveMessage(RedisCst.PRODUCT + shopId + ":product" + productId, skuNum);
// }
// }
//
// Integer buyNum = jsonObject.getInteger("num");
// if (tbProduct.getIsStock() == 1) {
// boolean flag = false;
//
// String id = skuId;
// if (Integer.valueOf(tbProduct.getIsDistribute()).equals(1)) {
// if (tbProduct.getStockNumber() < 1 && buyNum > 0) {
// flag = true;
// id = productId;
// }
// } else {
// if (tbProductSkuWithBLOBs.getIsPauseSale().equals(1)) {
// flag = true;
// } else if (Integer.valueOf(skuNum) < 1 && buyNum > 0) {
// flag = true;
// }
// }
//
// if (flag) {
// JSONObject jsonObject1 = new JSONObject();
// jsonObject1.put("status", "fail");
// jsonObject1.put("msg", "该商品库存已售罄");
// jsonObject1.put("data", new ArrayList<>());
// PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true);
// log.error("该商品库存已售罄 skuId:{}", id);
// throw new MsgException("该商品库存已售罄");
// }
// }
//
// 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())));
//
// updateProductStock(tbProduct, tbProductSkuWithBLOBs, buyNum);
// }
// } else {
// boolean flag = true;
// 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);
// updateProductStock(tbProduct, tbProductSkuWithBLOBs, 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);
// } else {
// cashierCartMapper.deleteByPrimaryKey(cashierCart.getId());
// continue;
// }
// flag = false;
// }
// 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())));
// updateProductStock(tbProduct, tbProductSkuWithBLOBs, buyNum);
// }
// }
// } else {
// if (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())));
// updateProductStock(tbProduct, tbProductSkuWithBLOBs, buyNum);
// }
// }
// redisUtil.saveMessage(RedisCst.TABLE_CART.concat(tableId).concat("-").concat(shopId), jsonArray.toJSONString());
// JSONObject jsonObject1 = new JSONObject();
// jsonObject1.put("status", "success");
// jsonObject1.put("msg", "成功");
// jsonObject1.put("type", jsonObject.getString("type"));
// 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());
// }
// }
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));
if (tbProduct == null) { if (tbProduct == null) {
@@ -127,89 +290,35 @@ public class CartService {
log.error("该商品不存在 productId:{}", productId); log.error("该商品不存在 productId:{}", productId);
throw new MsgException("该商品不存在"); throw new MsgException("该商品不存在");
} }
String skuId = jsonObject.getString("skuId"); String skuId = jsonObject.getString("skuId");
JSONArray jsonArray = new JSONArray();
BigDecimal amount = BigDecimal.ZERO;
TbProductSkuWithBLOBs tbProductSkuWithBLOBs = productSkuMapper.selectByPrimaryKey(Integer.valueOf(skuId));
JSONObject objectMsg=new JSONObject();
objectMsg.put("skuId",tbProductSkuWithBLOBs.getId());
objectMsg.put("shopId",Integer.valueOf(shopId));
producer.con_msg(objectMsg.toString());
if (Integer.valueOf(tbProduct.getIsPauseSale()).equals(1)) {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("status", "fail");
jsonObject1.put("msg", "该商品已售罄");
jsonObject1.put("data", new ArrayList<>());
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true);
log.error("该商品已售罄 productId:{}", productId);
throw new MsgException("该商品已售罄");
}
String skuNum;
// 1:共享库存 0:独立库存
if (Integer.valueOf(tbProduct.getIsDistribute()).equals(1)) {
boolean exist = redisUtil.exists(RedisCst.PRODUCT + shopId + ":product" + productId);
if (!exist) {
redisUtil.saveMessage(RedisCst.PRODUCT + shopId + ":product" + productId, tbProduct.getStockNumber() + "");
}
skuNum = redisUtil.getMessage(RedisCst.PRODUCT + shopId + ":product" + productId);
if (!skuNum.equals(tbProduct.getStockNumber() + "")) {
skuNum = tbProduct.getStockNumber() + "";
redisUtil.saveMessage(RedisCst.PRODUCT + shopId + ":product" + productId, skuNum);
}
} else {
boolean exist = redisUtil.exists(RedisCst.PRODUCT + shopId + ":" + skuId);
if (!exist) {
Double stock = tbProductSkuWithBLOBs.getStockNumber();
redisUtil.saveMessage(RedisCst.PRODUCT + shopId + ":" + skuId, Math.round(stock) + "");
}
skuNum = redisUtil.getMessage(RedisCst.PRODUCT + shopId + ":" + skuId);
if (!skuNum.equals(Math.round(tbProductSkuWithBLOBs.getStockNumber()) + "")) {
skuNum = Math.round(tbProductSkuWithBLOBs.getStockNumber()) + "";
redisUtil.saveMessage(RedisCst.PRODUCT + shopId + ":product" + productId, skuNum);
}
}
Integer buyNum = jsonObject.getInteger("num");
if (tbProduct.getIsStock() == 1) { if (tbProduct.getIsStock() == 1) {
boolean flag = false; // 1:共享库存 0:独立库存
String id = skuId;
if (Integer.valueOf(tbProduct.getIsDistribute()).equals(1)) { if (Integer.valueOf(tbProduct.getIsDistribute()).equals(1)) {
if (tbProduct.getStockNumber() < 1 && buyNum > 0) { if (tbProduct.getIsPauseSale().equals(1)) {//是否售罄
flag = true; JSONObject jsonObject1 = new JSONObject();
id = productId; jsonObject1.put("status", "fail");
jsonObject1.put("msg", "该商品已售罄");
jsonObject1.put("data", new ArrayList<>());
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true);
log.error("该商品已售罄 productId:{}", productId);
throw new MsgException("该商品已售罄");
} }
} else { } else {
if (tbProductSkuWithBLOBs.getIsPauseSale().equals(1)) { TbProductSkuWithBLOBs tbProductSkuWithBLOBs = productSkuMapper.selectByPrimaryKey(Integer.valueOf(skuId));
flag = true; if(tbProductSkuWithBLOBs.getIsPauseSale().equals(1)){//是否售罄
} else if (Integer.valueOf(skuNum) < 1 && buyNum > 0) { JSONObject jsonObject1 = new JSONObject();
flag = true; jsonObject1.put("status", "fail");
jsonObject1.put("msg", "该商品已售罄");
jsonObject1.put("data", new ArrayList<>());
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true);
log.error("该商品已售罄 productId:{}", productId);
throw new MsgException("该商品已售罄");
} }
} }
if (flag) {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("status", "fail");
jsonObject1.put("msg", "该商品库存已售罄");
jsonObject1.put("data", new ArrayList<>());
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(jsonObject1.toString(), key, jsonObject.getString("userId"), true);
log.error("该商品库存已售罄 skuId:{}", id);
throw new MsgException("该商品库存已售罄");
}
} }
JSONArray jsonArray = new JSONArray();
BigDecimal amount = BigDecimal.ZERO;
Integer buyNum = jsonObject.getInteger("num");
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))));
@@ -219,8 +328,6 @@ public class CartService {
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())));
updateProductStock(tbProduct, tbProductSkuWithBLOBs, buyNum);
} }
} else { } else {
boolean flag = true; boolean flag = true;
@@ -230,7 +337,6 @@ public class CartService {
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);
updateProductStock(tbProduct, tbProductSkuWithBLOBs, 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());
@@ -249,7 +355,6 @@ public class CartService {
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())));
updateProductStock(tbProduct, tbProductSkuWithBLOBs, buyNum);
} }
} }
} else { } else {
@@ -258,7 +363,6 @@ public class CartService {
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())));
updateProductStock(tbProduct, tbProductSkuWithBLOBs, buyNum);
} }
} }
redisUtil.saveMessage(RedisCst.TABLE_CART.concat(tableId).concat("-").concat(shopId), jsonArray.toJSONString()); redisUtil.saveMessage(RedisCst.TABLE_CART.concat(tableId).concat("-").concat(shopId), jsonArray.toJSONString());
@@ -384,10 +488,10 @@ public class CartService {
//修改耗材数据 //修改耗材数据
JSONObject jsonObject=new JSONObject(); // JSONObject jsonObject=new JSONObject();
jsonObject.put("cartId",cashierCart.getId()); // jsonObject.put("cartId",cashierCart.getId());
jsonObject.put("type","create"); // jsonObject.put("type","create");
producer.cons(jsonObject.toString()); // producer.cons(jsonObject.toString());
return cashierCart; return cashierCart;
@@ -679,19 +783,19 @@ public class CartService {
try { try {
String shopId = jsonObject.getString("shopId"); String shopId = jsonObject.getString("shopId");
List<String> skuIds=new ArrayList<>(); // List<String> skuIds=new ArrayList<>();
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() || array.size() < 1) { // if (Objects.isNull(array) || array.isEmpty() || array.size() < 1) {
for (int i = 0; i < array.size(); i++) { // for (int i = 0; i < array.size(); i++) {
TbCashierCart cashierCart = JSONUtil.parseJSONStr2T(array.get(i).toString(), TbCashierCart.class); // TbCashierCart cashierCart = JSONUtil.parseJSONStr2T(array.get(i).toString(), TbCashierCart.class);
redisUtil.secAdd(RedisCst.PRODUCT + shopId + ":" + jsonObject.getString("skuId"), cashierCart.getNumber().toString()); // redisUtil.secAdd(RedisCst.PRODUCT + shopId + ":" + jsonObject.getString("skuId"), cashierCart.getNumber().toString());
productSkuMapper.updateAddStockById(jsonObject.getString("skuId"), cashierCart.getNumber()); // productSkuMapper.updateAddStockById(jsonObject.getString("skuId"), cashierCart.getNumber());
skuIds.add(cashierCart.getSkuId()); // skuIds.add(cashierCart.getSkuId());
} // }
} // }
} // }
cashierCartMapper.updateStatusByTableId(jsonObject.getString("tableId"), "closed"); cashierCartMapper.updateStatusByTableId(jsonObject.getString("tableId"), "closed");
redisUtil.saveMessage(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId").concat("-").concat(shopId)), new JSONArray().toJSONString()); redisUtil.saveMessage(RedisCst.TABLE_CART.concat(jsonObject.getString("tableId").concat("-").concat(shopId)), new JSONArray().toJSONString());
JSONObject jsonObject1 = new JSONObject(); JSONObject jsonObject1 = new JSONObject();
@@ -700,16 +804,12 @@ public class CartService {
jsonObject1.put("type", "clearCart"); jsonObject1.put("type", "clearCart");
jsonObject1.put("amount", BigDecimal.ZERO); jsonObject1.put("amount", BigDecimal.ZERO);
jsonObject1.put("data", new ArrayList<>()); jsonObject1.put("data", new ArrayList<>());
// //修改耗材数据
// JSONObject jsonObject2=new JSONObject();
//修改耗材数据 // jsonObject2.put("type","delete");
JSONObject jsonObject2=new JSONObject(); // jsonObject2.put("skuIds",skuIds);
jsonObject2.put("type","delete"); // jsonObject2.put("shopId",shopId);
jsonObject2.put("skuIds",skuIds); // producer.cons(jsonObject2.toString());
jsonObject2.put("shopId",shopId);
producer.cons(jsonObject2.toString());
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.info("长链接错误 clearCart{}", e.getMessage()); log.info("长链接错误 clearCart{}", e.getMessage());