|
|
@@ -670,7 +670,7 @@ public class CartServiceImpl implements CartService {
|
|
|
* 餐具的特殊ID(用于标识餐具项)
|
|
|
*/
|
|
|
private static final Integer TABLEWARE_CUISINE_ID = -1;
|
|
|
- private static final String TABLEWARE_NAME = "餐具";
|
|
|
+ private static final String TABLEWARE_NAME = "餐具费";
|
|
|
|
|
|
/**
|
|
|
* 获取餐具单价(从 store_info 表获取)
|
|
|
@@ -719,39 +719,47 @@ public class CartServiceImpl implements CartService {
|
|
|
}
|
|
|
BigDecimal tablewareUnitPrice = getTablewareUnitPrice(storeId);
|
|
|
|
|
|
- // 查找是否已存在餐具项
|
|
|
- CartItemDTO tablewareItem = items.stream()
|
|
|
- .filter(item -> TABLEWARE_CUISINE_ID.equals(item.getCuisineId()))
|
|
|
- .findFirst()
|
|
|
- .orElse(null);
|
|
|
+ // 商铺未设置餐具费时,不往购物车加餐具;若已有餐具项则移除
|
|
|
+ if (tablewareUnitPrice == null || tablewareUnitPrice.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ log.info("门店未设置餐具费, storeId={},设置就餐人数时不添加餐具", storeId);
|
|
|
+ CartItemDTO existing = items.stream()
|
|
|
+ .filter(item -> TABLEWARE_CUISINE_ID.equals(item.getCuisineId()))
|
|
|
+ .findFirst()
|
|
|
+ .orElse(null);
|
|
|
+ if (existing != null) {
|
|
|
+ items.remove(existing);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 查找是否已存在餐具项
|
|
|
+ CartItemDTO tablewareItem = items.stream()
|
|
|
+ .filter(item -> TABLEWARE_CUISINE_ID.equals(item.getCuisineId()))
|
|
|
+ .findFirst()
|
|
|
+ .orElse(null);
|
|
|
|
|
|
- // 获取当前用户信息
|
|
|
- Integer userId = TokenUtil.getCurrentUserId();
|
|
|
- String userPhone = TokenUtil.getCurrentUserPhone();
|
|
|
+ Integer userId = TokenUtil.getCurrentUserId();
|
|
|
+ String userPhone = TokenUtil.getCurrentUserPhone();
|
|
|
|
|
|
- if (tablewareItem != null) {
|
|
|
- // 下单后只能增不能减:已有已下单数量时,用餐人数不能少于已下单数量
|
|
|
- Integer lockedQuantity = tablewareItem.getLockedQuantity();
|
|
|
- if (lockedQuantity != null && lockedQuantity > 0 && dinerCount < lockedQuantity) {
|
|
|
- throw new RuntimeException("餐具数量不能少于已下单数量(" + lockedQuantity + ")");
|
|
|
+ if (tablewareItem != null) {
|
|
|
+ Integer lockedQuantity = tablewareItem.getLockedQuantity();
|
|
|
+ if (lockedQuantity != null && lockedQuantity > 0 && dinerCount < lockedQuantity) {
|
|
|
+ throw new RuntimeException("餐具数量不能少于已下单数量(" + lockedQuantity + ")");
|
|
|
+ }
|
|
|
+ tablewareItem.setQuantity(dinerCount);
|
|
|
+ tablewareItem.setUnitPrice(tablewareUnitPrice);
|
|
|
+ tablewareItem.setSubtotalAmount(tablewareUnitPrice.multiply(BigDecimal.valueOf(dinerCount)));
|
|
|
+ } else {
|
|
|
+ CartItemDTO newTablewareItem = new CartItemDTO();
|
|
|
+ newTablewareItem.setCuisineId(TABLEWARE_CUISINE_ID);
|
|
|
+ newTablewareItem.setCuisineName(TABLEWARE_NAME);
|
|
|
+ newTablewareItem.setCuisineType(0);
|
|
|
+ newTablewareItem.setCuisineImage("");
|
|
|
+ newTablewareItem.setUnitPrice(tablewareUnitPrice);
|
|
|
+ newTablewareItem.setQuantity(dinerCount);
|
|
|
+ newTablewareItem.setSubtotalAmount(tablewareUnitPrice.multiply(BigDecimal.valueOf(dinerCount)));
|
|
|
+ newTablewareItem.setAddUserId(userId);
|
|
|
+ newTablewareItem.setAddUserPhone(userPhone);
|
|
|
+ items.add(newTablewareItem);
|
|
|
}
|
|
|
- // 更新餐具数量和单价
|
|
|
- tablewareItem.setQuantity(dinerCount);
|
|
|
- tablewareItem.setUnitPrice(tablewareUnitPrice);
|
|
|
- tablewareItem.setSubtotalAmount(tablewareUnitPrice.multiply(BigDecimal.valueOf(dinerCount)));
|
|
|
- } else {
|
|
|
- // 添加餐具项
|
|
|
- CartItemDTO newTablewareItem = new CartItemDTO();
|
|
|
- newTablewareItem.setCuisineId(TABLEWARE_CUISINE_ID);
|
|
|
- newTablewareItem.setCuisineName(TABLEWARE_NAME);
|
|
|
- newTablewareItem.setCuisineType(0); // 0表示餐具
|
|
|
- newTablewareItem.setCuisineImage("");
|
|
|
- newTablewareItem.setUnitPrice(tablewareUnitPrice);
|
|
|
- newTablewareItem.setQuantity(dinerCount);
|
|
|
- newTablewareItem.setSubtotalAmount(tablewareUnitPrice.multiply(BigDecimal.valueOf(dinerCount)));
|
|
|
- newTablewareItem.setAddUserId(userId);
|
|
|
- newTablewareItem.setAddUserPhone(userPhone);
|
|
|
- items.add(newTablewareItem);
|
|
|
}
|
|
|
|
|
|
// 重新计算总金额和总数量
|
|
|
@@ -790,7 +798,6 @@ public class CartServiceImpl implements CartService {
|
|
|
// 获取门店ID和餐具单价
|
|
|
Integer storeId = cart.getStoreId();
|
|
|
if (storeId == null) {
|
|
|
- // 如果购物车中没有门店ID,从桌号获取
|
|
|
StoreTable table = storeTableMapper.selectById(tableId);
|
|
|
if (table != null) {
|
|
|
storeId = table.getStoreId();
|
|
|
@@ -798,6 +805,28 @@ public class CartServiceImpl implements CartService {
|
|
|
}
|
|
|
BigDecimal tablewareUnitPrice = getTablewareUnitPrice(storeId);
|
|
|
|
|
|
+ // 商铺未设置餐具费(单价为0或未配置)时,不往购物车加餐具;若已有餐具项则移除
|
|
|
+ if (tablewareUnitPrice == null || tablewareUnitPrice.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ log.info("门店未设置餐具费, storeId={},不添加餐具到购物车", storeId);
|
|
|
+ CartItemDTO existing = items.stream()
|
|
|
+ .filter(item -> TABLEWARE_CUISINE_ID.equals(item.getCuisineId()))
|
|
|
+ .findFirst()
|
|
|
+ .orElse(null);
|
|
|
+ if (existing != null) {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ return cart;
|
|
|
+ }
|
|
|
+
|
|
|
// 查找餐具项
|
|
|
CartItemDTO tablewareItem = items.stream()
|
|
|
.filter(item -> TABLEWARE_CUISINE_ID.equals(item.getCuisineId()))
|