|
@@ -149,7 +149,9 @@ public class CartServiceImpl implements CartService {
|
|
|
it.setOriginalUnitPrice(p);
|
|
it.setOriginalUnitPrice(p);
|
|
|
it.setCurrentUnitPrice(p);
|
|
it.setCurrentUnitPrice(p);
|
|
|
it.setHasActiveDiscount(Boolean.FALSE);
|
|
it.setHasActiveDiscount(Boolean.FALSE);
|
|
|
- BigDecimal newSub = p.multiply(BigDecimal.valueOf(qty)).setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
|
|
+ BigDecimal origSub = p.multiply(BigDecimal.valueOf(qty)).setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
+ BigDecimal newSub = origSub;
|
|
|
|
|
+ it.setOriginalSubtotalAmount(origSub);
|
|
|
if (!Objects.equals(it.getUnitPrice(), p) || !Objects.equals(it.getSubtotalAmount(), newSub)) {
|
|
if (!Objects.equals(it.getUnitPrice(), p) || !Objects.equals(it.getSubtotalAmount(), newSub)) {
|
|
|
changed = true;
|
|
changed = true;
|
|
|
}
|
|
}
|
|
@@ -162,8 +164,11 @@ public class CartServiceImpl implements CartService {
|
|
|
it.setOriginalUnitPrice(list);
|
|
it.setOriginalUnitPrice(list);
|
|
|
it.setCurrentUnitPrice(sale);
|
|
it.setCurrentUnitPrice(sale);
|
|
|
it.setHasActiveDiscount(list.compareTo(sale) != 0);
|
|
it.setHasActiveDiscount(list.compareTo(sale) != 0);
|
|
|
|
|
+ BigDecimal origSub = list.multiply(BigDecimal.valueOf(qty)).setScale(2, RoundingMode.HALF_UP);
|
|
|
BigDecimal newSub = sale.multiply(BigDecimal.valueOf(qty)).setScale(2, RoundingMode.HALF_UP);
|
|
BigDecimal newSub = sale.multiply(BigDecimal.valueOf(qty)).setScale(2, RoundingMode.HALF_UP);
|
|
|
- if (!Objects.equals(it.getUnitPrice(), sale) || !Objects.equals(it.getSubtotalAmount(), newSub)) {
|
|
|
|
|
|
|
+ it.setOriginalSubtotalAmount(origSub);
|
|
|
|
|
+ if (!Objects.equals(it.getUnitPrice(), sale) || !Objects.equals(it.getSubtotalAmount(), newSub)
|
|
|
|
|
+ || !Objects.equals(it.getOriginalSubtotalAmount(), origSub)) {
|
|
|
changed = true;
|
|
changed = true;
|
|
|
}
|
|
}
|
|
|
it.setUnitPrice(sale);
|
|
it.setUnitPrice(sale);
|
|
@@ -303,17 +308,7 @@ public class CartServiceImpl implements CartService {
|
|
|
items.add(newItem);
|
|
items.add(newItem);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 重新计算总金额和总数量
|
|
|
|
|
- BigDecimal totalAmount = items.stream()
|
|
|
|
|
- .map(CartItemDTO::getSubtotalAmount)
|
|
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
- Integer totalQuantity = items.stream()
|
|
|
|
|
- .mapToInt(CartItemDTO::getQuantity)
|
|
|
|
|
- .sum();
|
|
|
|
|
- cart.setTotalAmount(totalAmount);
|
|
|
|
|
- cart.setTotalQuantity(totalQuantity);
|
|
|
|
|
-
|
|
|
|
|
- // 保存到Redis和数据库(双写策略)
|
|
|
|
|
|
|
+ applyRealtimeMenuPricing(cart);
|
|
|
saveCart(cart);
|
|
saveCart(cart);
|
|
|
|
|
|
|
|
return cart;
|
|
return cart;
|
|
@@ -350,16 +345,7 @@ public class CartServiceImpl implements CartService {
|
|
|
item.setSubtotalAmount(item.getUnitPrice()
|
|
item.setSubtotalAmount(item.getUnitPrice()
|
|
|
.multiply(BigDecimal.valueOf(quantity)));
|
|
.multiply(BigDecimal.valueOf(quantity)));
|
|
|
|
|
|
|
|
- // 重新计算总金额和总数量
|
|
|
|
|
- BigDecimal totalAmount = items.stream()
|
|
|
|
|
- .map(CartItemDTO::getSubtotalAmount)
|
|
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
- Integer totalQuantity = items.stream()
|
|
|
|
|
- .mapToInt(CartItemDTO::getQuantity)
|
|
|
|
|
- .sum();
|
|
|
|
|
- cart.setTotalAmount(totalAmount);
|
|
|
|
|
- cart.setTotalQuantity(totalQuantity);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ applyRealtimeMenuPricing(cart);
|
|
|
saveCart(cart);
|
|
saveCart(cart);
|
|
|
} else {
|
|
} else {
|
|
|
// 商品不存在,自动添加
|
|
// 商品不存在,自动添加
|
|
@@ -392,17 +378,7 @@ public class CartServiceImpl implements CartService {
|
|
|
newItem.setAddUserPhone(userPhone);
|
|
newItem.setAddUserPhone(userPhone);
|
|
|
items.add(newItem);
|
|
items.add(newItem);
|
|
|
|
|
|
|
|
- // 重新计算总金额和总数量
|
|
|
|
|
- BigDecimal totalAmount = items.stream()
|
|
|
|
|
- .map(CartItemDTO::getSubtotalAmount)
|
|
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
- Integer totalQuantity = items.stream()
|
|
|
|
|
- .mapToInt(CartItemDTO::getQuantity)
|
|
|
|
|
- .sum();
|
|
|
|
|
- cart.setTotalAmount(totalAmount);
|
|
|
|
|
- cart.setTotalQuantity(totalQuantity);
|
|
|
|
|
-
|
|
|
|
|
- // 保存到Redis和数据库(双写策略)
|
|
|
|
|
|
|
+ applyRealtimeMenuPricing(cart);
|
|
|
saveCart(cart);
|
|
saveCart(cart);
|
|
|
log.info("商品已自动添加到购物车, tableId={}, cuisineId={}, quantity={}", tableId, cuisineId, quantity);
|
|
log.info("商品已自动添加到购物车, tableId={}, cuisineId={}, quantity={}", tableId, cuisineId, quantity);
|
|
|
}
|
|
}
|
|
@@ -431,17 +407,14 @@ public class CartServiceImpl implements CartService {
|
|
|
|
|
|
|
|
items.removeIf(i -> i.getCuisineId().equals(cuisineId));
|
|
items.removeIf(i -> i.getCuisineId().equals(cuisineId));
|
|
|
|
|
|
|
|
- // 重新计算总金额和总数量
|
|
|
|
|
- BigDecimal totalAmount = items.stream()
|
|
|
|
|
- .map(CartItemDTO::getSubtotalAmount)
|
|
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
- Integer totalQuantity = items.stream()
|
|
|
|
|
- .mapToInt(CartItemDTO::getQuantity)
|
|
|
|
|
- .sum();
|
|
|
|
|
- cart.setTotalAmount(totalAmount);
|
|
|
|
|
- cart.setTotalQuantity(totalQuantity);
|
|
|
|
|
-
|
|
|
|
|
- saveCart(cart);
|
|
|
|
|
|
|
+ if (items.isEmpty()) {
|
|
|
|
|
+ cart.setTotalAmount(BigDecimal.ZERO);
|
|
|
|
|
+ cart.setTotalQuantity(0);
|
|
|
|
|
+ saveCart(cart);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ applyRealtimeMenuPricing(cart);
|
|
|
|
|
+ saveCart(cart);
|
|
|
|
|
+ }
|
|
|
return cart;
|
|
return cart;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -515,15 +488,12 @@ public class CartServiceImpl implements CartService {
|
|
|
if (hasChanges) {
|
|
if (hasChanges) {
|
|
|
// 1. 更新购物车(删除未下单商品,已下单商品数量已恢复)
|
|
// 1. 更新购物车(删除未下单商品,已下单商品数量已恢复)
|
|
|
cart.setItems(orderedItems);
|
|
cart.setItems(orderedItems);
|
|
|
- // 重新计算总金额和总数量(只计算保留的商品,数量已恢复为已下单数量)
|
|
|
|
|
- BigDecimal totalAmount = orderedItems.stream()
|
|
|
|
|
- .map(CartItemDTO::getSubtotalAmount)
|
|
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
- Integer totalQuantity = orderedItems.stream()
|
|
|
|
|
- .mapToInt(CartItemDTO::getQuantity)
|
|
|
|
|
- .sum();
|
|
|
|
|
- cart.setTotalAmount(totalAmount);
|
|
|
|
|
- cart.setTotalQuantity(totalQuantity);
|
|
|
|
|
|
|
+ if (!orderedItems.isEmpty()) {
|
|
|
|
|
+ applyRealtimeMenuPricing(cart);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ cart.setTotalAmount(BigDecimal.ZERO);
|
|
|
|
|
+ cart.setTotalQuantity(0);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
// 更新Redis(保留已下单的商品,数量已恢复)
|
|
// 更新Redis(保留已下单的商品,数量已恢复)
|
|
|
if (orderedItems.isEmpty()) {
|
|
if (orderedItems.isEmpty()) {
|
|
@@ -566,8 +536,8 @@ public class CartServiceImpl implements CartService {
|
|
|
// 4. 更新桌号表的购物车统计
|
|
// 4. 更新桌号表的购物车统计
|
|
|
StoreTable table = storeTableMapper.selectById(tableId);
|
|
StoreTable table = storeTableMapper.selectById(tableId);
|
|
|
if (table != null) {
|
|
if (table != null) {
|
|
|
- table.setCartItemCount(totalQuantity);
|
|
|
|
|
- table.setCartTotalAmount(totalAmount);
|
|
|
|
|
|
|
+ table.setCartItemCount(cart.getTotalQuantity());
|
|
|
|
|
+ table.setCartTotalAmount(cart.getTotalAmount());
|
|
|
storeTableMapper.updateById(table);
|
|
storeTableMapper.updateById(table);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -622,17 +592,7 @@ public class CartServiceImpl implements CartService {
|
|
|
toCart.setTableNumber(toTable.getTableNumber());
|
|
toCart.setTableNumber(toTable.getTableNumber());
|
|
|
toCart.setStoreId(toTable.getStoreId());
|
|
toCart.setStoreId(toTable.getStoreId());
|
|
|
|
|
|
|
|
- // 重新计算总金额和总数量
|
|
|
|
|
- BigDecimal totalAmount = mergedItems.stream()
|
|
|
|
|
- .map(CartItemDTO::getSubtotalAmount)
|
|
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
- Integer totalQuantity = mergedItems.stream()
|
|
|
|
|
- .mapToInt(CartItemDTO::getQuantity)
|
|
|
|
|
- .sum();
|
|
|
|
|
- toCart.setTotalAmount(totalAmount);
|
|
|
|
|
- toCart.setTotalQuantity(totalQuantity);
|
|
|
|
|
-
|
|
|
|
|
- // 保存目标购物车
|
|
|
|
|
|
|
+ applyRealtimeMenuPricing(toCart);
|
|
|
saveCart(toCart);
|
|
saveCart(toCart);
|
|
|
|
|
|
|
|
// 清空原购物车
|
|
// 清空原购物车
|
|
@@ -836,17 +796,7 @@ public class CartServiceImpl implements CartService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 重新计算总金额和总数量
|
|
|
|
|
- BigDecimal totalAmount = items.stream()
|
|
|
|
|
- .map(CartItemDTO::getSubtotalAmount)
|
|
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
- Integer totalQuantity = items.stream()
|
|
|
|
|
- .mapToInt(CartItemDTO::getQuantity)
|
|
|
|
|
- .sum();
|
|
|
|
|
- cart.setTotalAmount(totalAmount);
|
|
|
|
|
- cart.setTotalQuantity(totalQuantity);
|
|
|
|
|
-
|
|
|
|
|
- // 保存到Redis和数据库(双写策略)
|
|
|
|
|
|
|
+ applyRealtimeMenuPricing(cart);
|
|
|
saveCart(cart);
|
|
saveCart(cart);
|
|
|
|
|
|
|
|
return cart;
|
|
return cart;
|
|
@@ -888,15 +838,14 @@ public class CartServiceImpl implements CartService {
|
|
|
.orElse(null);
|
|
.orElse(null);
|
|
|
if (existing != null) {
|
|
if (existing != null) {
|
|
|
items.remove(existing);
|
|
items.remove(existing);
|
|
|
- BigDecimal totalAmount = items.stream()
|
|
|
|
|
- .map(CartItemDTO::getSubtotalAmount)
|
|
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
- Integer totalQuantity = items.stream()
|
|
|
|
|
- .mapToInt(CartItemDTO::getQuantity)
|
|
|
|
|
- .sum();
|
|
|
|
|
- cart.setTotalAmount(totalAmount);
|
|
|
|
|
- cart.setTotalQuantity(totalQuantity);
|
|
|
|
|
- saveCart(cart);
|
|
|
|
|
|
|
+ if (items.isEmpty()) {
|
|
|
|
|
+ cart.setTotalAmount(BigDecimal.ZERO);
|
|
|
|
|
+ cart.setTotalQuantity(0);
|
|
|
|
|
+ saveCart(cart);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ applyRealtimeMenuPricing(cart);
|
|
|
|
|
+ saveCart(cart);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
return cart;
|
|
return cart;
|
|
|
}
|
|
}
|
|
@@ -935,17 +884,7 @@ public class CartServiceImpl implements CartService {
|
|
|
tablewareItem.setQuantity(quantity);
|
|
tablewareItem.setQuantity(quantity);
|
|
|
tablewareItem.setSubtotalAmount(tablewareUnitPrice.multiply(BigDecimal.valueOf(quantity)));
|
|
tablewareItem.setSubtotalAmount(tablewareUnitPrice.multiply(BigDecimal.valueOf(quantity)));
|
|
|
|
|
|
|
|
- // 重新计算总金额和总数量
|
|
|
|
|
- BigDecimal totalAmount = items.stream()
|
|
|
|
|
- .map(CartItemDTO::getSubtotalAmount)
|
|
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
- Integer totalQuantity = items.stream()
|
|
|
|
|
- .mapToInt(CartItemDTO::getQuantity)
|
|
|
|
|
- .sum();
|
|
|
|
|
- cart.setTotalAmount(totalAmount);
|
|
|
|
|
- cart.setTotalQuantity(totalQuantity);
|
|
|
|
|
-
|
|
|
|
|
- // 保存到Redis和数据库(双写策略)
|
|
|
|
|
|
|
+ applyRealtimeMenuPricing(cart);
|
|
|
saveCart(cart);
|
|
saveCart(cart);
|
|
|
|
|
|
|
|
return cart;
|
|
return cart;
|