소스 검색

价目表 套餐引用单品 增加单品下架效验判断逻辑

qinxuyang 2 달 전
부모
커밋
d28757348c
1개의 변경된 파일60개의 추가작업 그리고 3개의 파일을 삭제
  1. 60 3
      alien-store/src/main/java/shop/alien/store/controller/StoreCuisineController.java

+ 60 - 3
alien-store/src/main/java/shop/alien/store/controller/StoreCuisineController.java

@@ -13,6 +13,7 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.web.bind.annotation.*;
 import shop.alien.entity.result.R;
 import shop.alien.entity.store.StoreCuisine;
+import shop.alien.entity.store.StoreCuisineCombo;
 import shop.alien.entity.store.StoreInfo;
 import shop.alien.entity.store.StorePrice;
 import shop.alien.entity.store.dto.CuisineComboDto;
@@ -21,6 +22,7 @@ import shop.alien.entity.store.dto.TablewareFeeDto;
 import shop.alien.entity.store.vo.PriceListVo;
 import shop.alien.mapper.StoreCuisineMapper;
 import shop.alien.store.annotation.TrackEvent;
+import shop.alien.store.service.StoreCuisineComboService;
 import shop.alien.store.service.StoreCuisineService;
 import shop.alien.store.service.StoreInfoService;
 import shop.alien.store.service.StorePriceService;
@@ -59,6 +61,8 @@ public class StoreCuisineController {
 
     private final StoreCuisineMapper storeCuisineMapper;
 
+    private final StoreCuisineComboService storeCuisineComboService;
+
     @ApiOperation("新增美食套餐或单品")
     @ApiOperationSupport(order = 1)
     @PostMapping("/addCuisineCombo")
@@ -376,13 +380,66 @@ public class StoreCuisineController {
     @PostMapping("/changeShelfStatus")
     public R<String> changeShelfStatus(@RequestParam("id") Integer id, @RequestParam("shelfStatus") Integer shelfStatus) {
         log.info("StoreCuisineController.changeShelfStatus?id={},shelfStatus={}", id, shelfStatus);
+        
+        // 参数验证
+        if (id == null) {
+            return R.fail("美食价目ID不能为空");
+        }
         if (shelfStatus == null || (shelfStatus != 1 && shelfStatus != 2)) {
             return R.fail("上下架状态不合法(只能为1或2)");
         }
-        if (storeCuisineService.changeShelfStatus(id, shelfStatus)) {
-            return R.success("操作成功");
+        
+        try {
+            // 查询当前美食信息
+            StoreCuisine cuisine = storeCuisineService.getById(id);
+            if (cuisine == null) {
+                return R.fail("美食价目不存在");
+            }
+            
+            // 如果是下架操作(shelfStatus == 2),且是单品(cuisineType == 1),需要检查是否被套餐引用
+            if (shelfStatus == 2 && cuisine.getCuisineType() != null && cuisine.getCuisineType() == 1) {
+                // 查询是否有套餐引用了该单品
+                LambdaQueryWrapper<StoreCuisineCombo> comboQueryWrapper = new LambdaQueryWrapper<>();
+                comboQueryWrapper.eq(StoreCuisineCombo::getSid, id);
+                List<StoreCuisineCombo> comboList = storeCuisineComboService.list(comboQueryWrapper);
+                
+                if (comboList != null && !comboList.isEmpty()) {
+                    // 收集所有引用该单品的套餐ID
+                    List<Integer> comboIds = comboList.stream()
+                            .map(StoreCuisineCombo::getCid)
+                            .distinct()
+                            .collect(java.util.stream.Collectors.toList());
+                    
+                    // 查询这些套餐的上架状态
+                    List<StoreCuisine> combos = new ArrayList<>(storeCuisineService.listByIds(comboIds));
+                    List<String> onShelfComboNames = new ArrayList<>();
+                    
+                    for (StoreCuisine combo : combos) {
+                        // 检查套餐是否还在上架状态(shelfStatus == 1)
+                        if (combo.getShelfStatus() != null && combo.getShelfStatus() == 1) {
+                            onShelfComboNames.add(combo.getName());
+                        }
+                    }
+                    
+                    // 如果有套餐还在上架,不能下架单品
+                    if (!onShelfComboNames.isEmpty()) {
+                        String comboNames = String.join("、", onShelfComboNames);
+                        log.warn("下架单品失败:单品ID={}被以下套餐引用且套餐还在上架:{}", id, comboNames);
+                        return R.fail("该单品被套餐引用,无法下架。请先下架以下套餐:" + comboNames);
+                    }
+                }
+            }
+            
+            // 执行上下架操作
+            if (storeCuisineService.changeShelfStatus(id, shelfStatus)) {
+                return R.success("操作成功");
+            }
+            return R.fail("操作失败");
+            
+        } catch (Exception e) {
+            log.error("上下架操作异常,ID: {}, shelfStatus: {}", id, shelfStatus, e);
+            return R.fail("操作失败:" + e.getMessage());
         }
-        return R.fail("操作失败");
     }
 
     @ApiOperation("分页查询美食价目/通用价目")