Просмотр исходного кода

维护购物车增减商品 逻辑 注释掉websocket

lutong 2 месяцев назад
Родитель
Сommit
56e5073c1d

+ 18 - 18
alien-dining/src/main/java/shop/alien/dining/controller/StoreOrderController.java

@@ -2,28 +2,28 @@ package shop.alien.dining.controller;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-import io.swagger.annotations.*;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
-import shop.alien.entity.result.R;
-import shop.alien.entity.store.StoreOrder;
-import shop.alien.entity.store.StoreOrderDetail;
-import shop.alien.entity.store.StoreTableLog;
-import shop.alien.entity.store.dto.*;
-import shop.alien.mapper.StoreOrderDetailMapper;
-import shop.alien.mapper.StoreTableLogMapper;
-import shop.alien.mapper.StoreTableMapper;
-import shop.alien.mapper.StoreInfoMapper;
-import shop.alien.entity.store.StoreTable;
-import shop.alien.entity.store.StoreInfo;
-import shop.alien.dining.config.CartWebSocketProcess;
 import shop.alien.dining.service.CartService;
 import shop.alien.dining.service.SseService;
 import shop.alien.dining.service.StoreOrderService;
 import shop.alien.dining.util.TokenUtil;
+import shop.alien.entity.result.R;
+import shop.alien.entity.store.*;
+import shop.alien.entity.store.dto.AddCartItemDTO;
+import shop.alien.entity.store.dto.CartDTO;
+import shop.alien.entity.store.dto.ChangeTableDTO;
+import shop.alien.entity.store.dto.CreateOrderDTO;
+import shop.alien.mapper.StoreInfoMapper;
+import shop.alien.mapper.StoreOrderDetailMapper;
+import shop.alien.mapper.StoreTableLogMapper;
+import shop.alien.mapper.StoreTableMapper;
 
 import javax.validation.Valid;
 import java.util.List;
@@ -78,7 +78,7 @@ public class StoreOrderController {
             // 推送购物车更新消息(SSE)
             sseService.pushCartUpdate(dto.getTableId(), cart);
             // 推送购物车更新消息(WebSocket)
-            CartWebSocketProcess.pushCartUpdate(dto.getTableId(), cart);
+//            CartWebSocketProcess.pushCartUpdate(dto.getTableId(), cart);
             return R.data(cart);
         } catch (Exception e) {
             log.error("添加商品到购物车失败: {}", e.getMessage(), e);
@@ -101,7 +101,7 @@ public class StoreOrderController {
             // 推送购物车更新消息(SSE)
             sseService.pushCartUpdate(tableId, cart);
             // 推送购物车更新消息(WebSocket)
-            CartWebSocketProcess.pushCartUpdate(tableId, cart);
+//            CartWebSocketProcess.pushCartUpdate(tableId, cart);
             return R.data(cart);
         } catch (Exception e) {
             log.error("更新购物车商品数量失败: {}", e.getMessage(), e);
@@ -123,7 +123,7 @@ public class StoreOrderController {
             // 推送购物车更新消息(SSE)
             sseService.pushCartUpdate(tableId, cart);
             // 推送购物车更新消息(WebSocket)
-            CartWebSocketProcess.pushCartUpdate(tableId, cart);
+//            CartWebSocketProcess.pushCartUpdate(tableId, cart);
             return R.data(cart);
         } catch (Exception e) {
             log.error("删除购物车商品失败: {}", e.getMessage(), e);
@@ -148,7 +148,7 @@ public class StoreOrderController {
             // 推送购物车更新消息(SSE)
             sseService.pushCartUpdate(tableId, cart);
             // 推送购物车更新消息(WebSocket)
-            CartWebSocketProcess.pushCartUpdate(tableId, cart);
+//            CartWebSocketProcess.pushCartUpdate(tableId, cart);
             return R.data(cart);
         } catch (Exception e) {
             log.error("设置用餐人数失败: {}", e.getMessage(), e);
@@ -173,7 +173,7 @@ public class StoreOrderController {
             // 推送购物车更新消息(SSE)
             sseService.pushCartUpdate(tableId, cart);
             // 推送购物车更新消息(WebSocket)
-            CartWebSocketProcess.pushCartUpdate(tableId, cart);
+//            CartWebSocketProcess.pushCartUpdate(tableId, cart);
             return R.data(cart);
         } catch (Exception e) {
             log.error("更新餐具数量失败: {}", e.getMessage(), e);

+ 21 - 23
alien-dining/src/main/java/shop/alien/dining/service/impl/CartServiceImpl.java

@@ -191,30 +191,25 @@ public class CartServiceImpl implements CartService {
                 .orElse(null);
 
         if (existingItem != null) {
-            // 更新数量
-            existingItem.setQuantity(existingItem.getQuantity() + dto.getQuantity());
-            existingItem.setSubtotalAmount(existingItem.getUnitPrice()
-                    .multiply(BigDecimal.valueOf(existingItem.getQuantity())));
-            if (StringUtils.hasText(dto.getRemark())) {
-                existingItem.setRemark(dto.getRemark());
-            }
-        } 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);
+            // 商品已存在,不允许重复添加
+            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);
+
         // 重新计算总金额和总数量
         BigDecimal totalAmount = items.stream()
                 .map(CartItemDTO::getSubtotalAmount)
@@ -234,7 +229,10 @@ public class CartServiceImpl implements CartService {
     @Override
     public CartDTO updateItemQuantity(Integer tableId, Integer cuisineId, Integer quantity) {
         log.info("更新购物车商品数量, tableId={}, cuisineId={}, quantity={}", tableId, cuisineId, quantity);
-        if (quantity <= 0) {
+        
+        // 如果数量为0或小于0,删除该商品
+        if (quantity == null || quantity <= 0) {
+            log.info("商品数量为0或小于0,删除商品, tableId={}, cuisineId={}", tableId, cuisineId);
             return removeItem(tableId, cuisineId);
         }