|
|
@@ -134,6 +134,7 @@ public class CartServiceImpl implements CartService {
|
|
|
item.setCuisineImage(cartItem.getCuisineImage());
|
|
|
item.setUnitPrice(cartItem.getUnitPrice());
|
|
|
item.setQuantity(cartItem.getQuantity());
|
|
|
+ item.setLockedQuantity(cartItem.getLockedQuantity());
|
|
|
item.setSubtotalAmount(cartItem.getSubtotalAmount());
|
|
|
item.setAddUserId(cartItem.getAddUserId());
|
|
|
item.setAddUserPhone(cartItem.getAddUserPhone());
|
|
|
@@ -191,24 +192,38 @@ public class CartServiceImpl implements CartService {
|
|
|
.orElse(null);
|
|
|
|
|
|
if (existingItem != null) {
|
|
|
- // 商品已存在,不允许重复添加
|
|
|
- throw new RuntimeException("已添加过本商品");
|
|
|
- }
|
|
|
-
|
|
|
- // 添加新商品
|
|
|
- CartItemDTO newItem = new CartItemDTO();
|
|
|
- newItem.setCuisineId(cuisine.getId());
|
|
|
- newItem.setCuisineName(cuisine.getName());
|
|
|
- newItem.setCuisineType(cuisine.getCuisineType());
|
|
|
- newItem.setCuisineImage(cuisine.getImages());
|
|
|
- newItem.setUnitPrice(cuisine.getTotalPrice());
|
|
|
- newItem.setQuantity(dto.getQuantity());
|
|
|
- newItem.setSubtotalAmount(cuisine.getTotalPrice()
|
|
|
- .multiply(BigDecimal.valueOf(dto.getQuantity())));
|
|
|
- newItem.setAddUserId(userId);
|
|
|
- newItem.setAddUserPhone(userPhone);
|
|
|
- newItem.setRemark(dto.getRemark());
|
|
|
- items.add(newItem);
|
|
|
+ // 商品已存在
|
|
|
+ Integer lockedQuantity = existingItem.getLockedQuantity();
|
|
|
+ if (lockedQuantity != null && lockedQuantity > 0) {
|
|
|
+ // 如果商品有锁定数量(已下单),将新数量叠加到当前数量和锁定数量上
|
|
|
+ Integer newQuantity = existingItem.getQuantity() + dto.getQuantity();
|
|
|
+ Integer newLockedQuantity = lockedQuantity + dto.getQuantity();
|
|
|
+ existingItem.setQuantity(newQuantity);
|
|
|
+ existingItem.setLockedQuantity(newLockedQuantity);
|
|
|
+ existingItem.setSubtotalAmount(existingItem.getUnitPrice()
|
|
|
+ .multiply(BigDecimal.valueOf(newQuantity)));
|
|
|
+ log.info("商品已存在且有锁定数量,叠加数量, cuisineId={}, oldQuantity={}, newQuantity={}, oldLockedQuantity={}, newLockedQuantity={}",
|
|
|
+ dto.getCuisineId(), existingItem.getQuantity() - dto.getQuantity(), newQuantity, lockedQuantity, newLockedQuantity);
|
|
|
+ } else {
|
|
|
+ // 商品已存在但没有锁定数量,不允许重复添加
|
|
|
+ throw new RuntimeException("已添加过本商品");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 添加新商品
|
|
|
+ CartItemDTO newItem = new CartItemDTO();
|
|
|
+ newItem.setCuisineId(cuisine.getId());
|
|
|
+ newItem.setCuisineName(cuisine.getName());
|
|
|
+ newItem.setCuisineType(cuisine.getCuisineType());
|
|
|
+ newItem.setCuisineImage(cuisine.getImages());
|
|
|
+ newItem.setUnitPrice(cuisine.getTotalPrice());
|
|
|
+ newItem.setQuantity(dto.getQuantity());
|
|
|
+ newItem.setSubtotalAmount(cuisine.getTotalPrice()
|
|
|
+ .multiply(BigDecimal.valueOf(dto.getQuantity())));
|
|
|
+ newItem.setAddUserId(userId);
|
|
|
+ newItem.setAddUserPhone(userPhone);
|
|
|
+ newItem.setRemark(dto.getRemark());
|
|
|
+ items.add(newItem);
|
|
|
+ }
|
|
|
|
|
|
// 重新计算总金额和总数量
|
|
|
BigDecimal totalAmount = items.stream()
|
|
|
@@ -244,6 +259,14 @@ public class CartServiceImpl implements CartService {
|
|
|
.orElse(null);
|
|
|
|
|
|
if (item != null) {
|
|
|
+ // 检查锁定数量:不允许将数量减少到小于锁定数量
|
|
|
+ Integer lockedQuantity = item.getLockedQuantity();
|
|
|
+ if (lockedQuantity != null && lockedQuantity > 0) {
|
|
|
+ if (quantity < lockedQuantity) {
|
|
|
+ throw new RuntimeException("商品数量不能少于锁定数量(" + lockedQuantity + "),该数量已下单");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
item.setQuantity(quantity);
|
|
|
item.setSubtotalAmount(item.getUnitPrice()
|
|
|
.multiply(BigDecimal.valueOf(quantity)));
|
|
|
@@ -269,7 +292,21 @@ public class CartServiceImpl implements CartService {
|
|
|
log.info("删除购物车商品, tableId={}, cuisineId={}", tableId, cuisineId);
|
|
|
CartDTO cart = getCart(tableId);
|
|
|
List<CartItemDTO> items = cart.getItems();
|
|
|
- items.removeIf(item -> item.getCuisineId().equals(cuisineId));
|
|
|
+
|
|
|
+ // 检查是否有锁定数量,如果有则不允许删除
|
|
|
+ CartItemDTO item = items.stream()
|
|
|
+ .filter(i -> i.getCuisineId().equals(cuisineId))
|
|
|
+ .findFirst()
|
|
|
+ .orElse(null);
|
|
|
+
|
|
|
+ if (item != null) {
|
|
|
+ Integer lockedQuantity = item.getLockedQuantity();
|
|
|
+ if (lockedQuantity != null && lockedQuantity > 0) {
|
|
|
+ throw new RuntimeException("商品已下单,锁定数量为 " + lockedQuantity + ",不允许删除");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ items.removeIf(i -> i.getCuisineId().equals(cuisineId));
|
|
|
|
|
|
// 重新计算总金额和总数量
|
|
|
BigDecimal totalAmount = items.stream()
|
|
|
@@ -640,6 +677,50 @@ public class CartServiceImpl implements CartService {
|
|
|
return cart;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public CartDTO lockCartItems(Integer tableId) {
|
|
|
+ log.info("锁定购物车商品数量, tableId={}", tableId);
|
|
|
+
|
|
|
+ // 获取购物车
|
|
|
+ CartDTO cart = getCart(tableId);
|
|
|
+ List<CartItemDTO> items = cart.getItems();
|
|
|
+
|
|
|
+ if (items == null || items.isEmpty()) {
|
|
|
+ log.warn("购物车为空,无需锁定, tableId={}", tableId);
|
|
|
+ return cart;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 遍历所有商品,将当前数量设置为锁定数量
|
|
|
+ boolean hasChanges = false;
|
|
|
+ for (CartItemDTO item : items) {
|
|
|
+ Integer currentQuantity = item.getQuantity();
|
|
|
+ Integer lockedQuantity = item.getLockedQuantity();
|
|
|
+
|
|
|
+ if (currentQuantity != null && currentQuantity > 0) {
|
|
|
+ if (lockedQuantity == null || lockedQuantity == 0) {
|
|
|
+ // 如果还没有锁定数量,将当前数量设置为锁定数量
|
|
|
+ item.setLockedQuantity(currentQuantity);
|
|
|
+ hasChanges = true;
|
|
|
+ log.info("锁定商品数量, cuisineId={}, quantity={}", item.getCuisineId(), currentQuantity);
|
|
|
+ } else if (currentQuantity > lockedQuantity) {
|
|
|
+ // 如果已有锁定数量,且当前数量大于锁定数量(再次下单的情况),将新增数量累加到锁定数量
|
|
|
+ Integer newLockedQuantity = lockedQuantity + (currentQuantity - lockedQuantity);
|
|
|
+ item.setLockedQuantity(newLockedQuantity);
|
|
|
+ hasChanges = true;
|
|
|
+ log.info("更新锁定商品数量, cuisineId={}, oldLockedQuantity={}, newLockedQuantity={}",
|
|
|
+ item.getCuisineId(), lockedQuantity, newLockedQuantity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有变化,保存购物车
|
|
|
+ if (hasChanges) {
|
|
|
+ saveCart(cart);
|
|
|
+ }
|
|
|
+
|
|
|
+ return cart;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 保存购物车到Redis和数据库(双写策略)
|
|
|
*/
|
|
|
@@ -693,6 +774,7 @@ public class CartServiceImpl implements CartService {
|
|
|
storeCart.setCuisineImage(item.getCuisineImage());
|
|
|
storeCart.setUnitPrice(item.getUnitPrice());
|
|
|
storeCart.setQuantity(item.getQuantity());
|
|
|
+ storeCart.setLockedQuantity(item.getLockedQuantity());
|
|
|
storeCart.setSubtotalAmount(item.getSubtotalAmount());
|
|
|
storeCart.setAddUserId(item.getAddUserId());
|
|
|
storeCart.setAddUserPhone(item.getAddUserPhone());
|