Преглед изворни кода

预约服务 桌号管理 编辑 增加桌号去重效验

qinxuyang пре 2 месеци
родитељ
комит
668205c609

+ 21 - 20
alien-store/src/main/java/shop/alien/store/controller/StoreBookingTableController.java

@@ -7,6 +7,7 @@ import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.*;
 import shop.alien.entity.result.R;
 import shop.alien.entity.store.StoreBookingTable;
+import shop.alien.entity.store.dto.StoreBookingTableBatchDTO;
 import shop.alien.entity.store.dto.StoreBookingTableDTO;
 import shop.alien.store.service.StoreBookingTableService;
 
@@ -80,9 +81,9 @@ public class StoreBookingTableController {
     }
 
     @ApiOperationSupport(order = 3)
-    @ApiOperation("新增预订服务桌号")
+    @ApiOperation("新增预订服务桌号(支持批量添加)")
     @PostMapping("/add")
-    public R<StoreBookingTable> addTable(@RequestBody StoreBookingTableDTO dto) {
+    public R<String> addTable(@RequestBody StoreBookingTableBatchDTO dto) {
         log.info("StoreBookingTableController.addTable?dto={}", dto);
         
         // 参数验证
@@ -92,33 +93,33 @@ public class StoreBookingTableController {
         if (dto.getCategoryId() == null) {
             return R.fail("分类ID不能为空");
         }
-        if (!StringUtils.hasText(dto.getTableNumber())) {
-            return R.fail("桌号不能为空");
-        }
-        if (dto.getSeatingCapacity() == null || dto.getSeatingCapacity() <= 0) {
-            return R.fail("座位数必须大于0");
+        if (dto.getTables() == null || dto.getTables().isEmpty()) {
+            return R.fail("桌号列表不能为空");
         }
         
         try {
-            StoreBookingTable table = new StoreBookingTable();
-            table.setStoreId(dto.getStoreId());
-            table.setCategoryId(dto.getCategoryId());
-            table.setTableNumber(dto.getTableNumber());
-            table.setSeatingCapacity(dto.getSeatingCapacity());
+            // 转换为 StoreBookingTable 列表
+            List<StoreBookingTable> tables = dto.getTables().stream()
+                    .map(item -> {
+                        StoreBookingTable table = new StoreBookingTable();
+                        table.setTableNumber(item.getTableNumber());
+                        table.setSeatingCapacity(item.getSeatingCapacity());
+                        return table;
+                    })
+                    .collect(java.util.stream.Collectors.toList());
             
-            boolean result = storeBookingTableService.addTable(table);
+            boolean result = storeBookingTableService.batchAddTables(dto.getStoreId(), dto.getCategoryId(), tables);
             if (result) {
-                // 返回保存后的完整对象
-                StoreBookingTable savedTable = storeBookingTableService.getById(table.getId());
-                return R.data(savedTable, "新增成功");
+                return R.success("新增成功");
             } else {
                 return R.fail("新增失败");
             }
         } catch (Exception e) {
             log.error("新增预订服务桌号失败", e);
             // 如果是桌号已存在的错误,直接返回友好提示
-            if (e.getMessage() != null && e.getMessage().contains("此桌号已存在")) {
-                return R.fail("此桌号已存在");
+            if (e.getMessage() != null && (e.getMessage().contains("该桌号已存在不能添加") || 
+                e.getMessage().contains("以下桌号已存在不能添加"))) {
+                return R.fail(e.getMessage());
             }
             return R.fail("新增失败:" + e.getMessage());
         }
@@ -156,8 +157,8 @@ public class StoreBookingTableController {
         } catch (Exception e) {
             log.error("更新预订服务桌号失败", e);
             // 如果是桌号已存在的错误,直接返回友好提示
-            if (e.getMessage() != null && e.getMessage().contains("此桌号已存在")) {
-                return R.fail("此桌号已存在");
+            if (e.getMessage() != null && e.getMessage().contains("该桌号已存在不能编辑")) {
+                return R.fail("该桌号已存在不能编辑");
             }
             return R.fail("更新失败:" + e.getMessage());
         }

+ 10 - 0
alien-store/src/main/java/shop/alien/store/service/StoreBookingTableService.java

@@ -31,6 +31,16 @@ public interface StoreBookingTableService extends IService<StoreBookingTable> {
     boolean addTable(StoreBookingTable table);
 
     /**
+     * 批量新增预订服务桌号
+     *
+     * @param storeId   门店ID
+     * @param categoryId 分类ID
+     * @param tables    桌号列表(每个包含桌号和座位数)
+     * @return boolean
+     */
+    boolean batchAddTables(Integer storeId, Integer categoryId, List<StoreBookingTable> tables);
+
+    /**
      * 更新预订服务桌号
      *
      * @param table 桌号对象

+ 90 - 15
alien-store/src/main/java/shop/alien/store/service/impl/StoreBookingTableServiceImpl.java

@@ -15,6 +15,7 @@ import shop.alien.store.service.StoreBookingTableService;
 import shop.alien.util.common.JwtUtil;
 
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * 预订服务桌号表 服务实现类
@@ -75,17 +76,16 @@ public class StoreBookingTableServiceImpl extends ServiceImpl<StoreBookingTableM
         String tableNumber = table.getTableNumber().trim();
         table.setTableNumber(tableNumber);
         
-        // 检查当前门店下该分类是否已存在相同的桌号
+        // 检查当前门店下是否已存在相同的桌号(不限制分类)
         LambdaQueryWrapper<StoreBookingTable> checkWrapper = new LambdaQueryWrapper<>();
         checkWrapper.eq(StoreBookingTable::getStoreId, table.getStoreId())
-                .eq(StoreBookingTable::getCategoryId, table.getCategoryId())
                 .eq(StoreBookingTable::getTableNumber, tableNumber);
         // @TableLogic 会自动过滤已删除的记录(delete_flag=0)
         StoreBookingTable existingTable = this.getOne(checkWrapper);
         if (existingTable != null) {
-            log.warn("新增预订服务桌号失败:当前分类下桌号已存在,storeId={}, categoryId={}, tableNumber={}", 
-                    table.getStoreId(), table.getCategoryId(), tableNumber);
-            throw new RuntimeException("此桌号已存在");
+            log.warn("新增预订服务桌号失败:当前门店下桌号已存在,storeId={}, tableNumber={}", 
+                    table.getStoreId(), tableNumber);
+            throw new RuntimeException("该桌号已存在不能添加");
         }
         
         table.setCreatedUserId(userId);
@@ -94,6 +94,86 @@ public class StoreBookingTableServiceImpl extends ServiceImpl<StoreBookingTableM
     }
 
     @Override
+    public boolean batchAddTables(Integer storeId, Integer categoryId, List<StoreBookingTable> tables) {
+        log.info("StoreBookingTableServiceImpl.batchAddTables?storeId={}, categoryId={}, tables.size={}", 
+                storeId, categoryId, tables != null ? tables.size() : 0);
+        
+        // 从JWT获取当前登录用户ID
+        Integer userId = getCurrentUserId();
+        
+        // 参数验证
+        if (storeId == null) {
+            log.warn("批量新增预订服务桌号失败:门店ID不能为空");
+            throw new RuntimeException("门店ID不能为空");
+        }
+        if (categoryId == null) {
+            log.warn("批量新增预订服务桌号失败:分类ID不能为空");
+            throw new RuntimeException("分类ID不能为空");
+        }
+        if (tables == null || tables.isEmpty()) {
+            log.warn("批量新增预订服务桌号失败:桌号列表不能为空");
+            throw new RuntimeException("桌号列表不能为空");
+        }
+        
+        // 验证并处理每个桌号
+        for (StoreBookingTable table : tables) {
+            if (!StringUtils.hasText(table.getTableNumber())) {
+                log.warn("批量新增预订服务桌号失败:桌号不能为空");
+                throw new RuntimeException("桌号不能为空");
+            }
+            if (table.getSeatingCapacity() == null || table.getSeatingCapacity() <= 0) {
+                log.warn("批量新增预订服务桌号失败:座位数必须大于0");
+                throw new RuntimeException("座位数必须大于0");
+            }
+            
+            // 处理桌号:去除前后空格
+            String tableNumber = table.getTableNumber().trim();
+            table.setTableNumber(tableNumber);
+            table.setStoreId(storeId);
+            table.setCategoryId(categoryId);
+            table.setCreatedUserId(userId);
+        }
+        
+        // 检查是否有重复的桌号(在本次批量添加的列表中)
+        long distinctCount = tables.stream()
+                .map(StoreBookingTable::getTableNumber)
+                .distinct()
+                .count();
+        if (distinctCount != tables.size()) {
+            log.warn("批量新增预订服务桌号失败:本次批量添加的桌号列表中存在重复");
+            throw new RuntimeException("本次批量添加的桌号列表中存在重复");
+        }
+        
+        // 检查当前门店下是否已存在相同的桌号(不限制分类)
+        List<String> tableNumbers = tables.stream()
+                .map(StoreBookingTable::getTableNumber)
+                .collect(Collectors.toList());
+        
+        LambdaQueryWrapper<StoreBookingTable> checkWrapper = new LambdaQueryWrapper<>();
+        checkWrapper.eq(StoreBookingTable::getStoreId, storeId)
+                .in(StoreBookingTable::getTableNumber, tableNumbers);
+        // @TableLogic 会自动过滤已删除的记录(delete_flag=0)
+        List<StoreBookingTable> existingTables = this.list(checkWrapper);
+        
+        if (!existingTables.isEmpty()) {
+            List<String> existingNumbers = existingTables.stream()
+                    .map(StoreBookingTable::getTableNumber)
+                    .distinct()
+                    .collect(Collectors.toList());
+            log.warn("批量新增预订服务桌号失败:当前门店下桌号已存在,storeId={}, existingNumbers={}", 
+                    storeId, existingNumbers);
+            if (existingNumbers.size() == 1) {
+                throw new RuntimeException("该桌号已存在不能添加");
+            } else {
+                throw new RuntimeException("以下桌号已存在不能添加:" + String.join("、", existingNumbers));
+            }
+        }
+        
+        // 批量保存
+        return this.saveBatch(tables);
+    }
+
+    @Override
     public boolean updateTable(StoreBookingTable table) {
         log.info("StoreBookingTableServiceImpl.updateTable?table={}", table);
         
@@ -118,23 +198,18 @@ public class StoreBookingTableServiceImpl extends ServiceImpl<StoreBookingTableM
             String tableNumber = table.getTableNumber().trim();
             table.setTableNumber(tableNumber);
             
-            // 如果桌号有变化,检查当前门店下该分类是否已存在相同的桌号
-            if (!tableNumber.equals(existingTable.getTableNumber()) || 
-                (table.getCategoryId() != null && !table.getCategoryId().equals(existingTable.getCategoryId()))) {
-                // 确定要检查的分类ID(如果传入了新的分类ID,使用新的;否则使用原有的)
-                Integer checkCategoryId = table.getCategoryId() != null ? table.getCategoryId() : existingTable.getCategoryId();
-                
+            // 如果桌号有变化,检查当前门店下是否已存在相同的桌号(不限制分类,排除当前记录)
+            if (!tableNumber.equals(existingTable.getTableNumber())) {
                 LambdaQueryWrapper<StoreBookingTable> wrapper = new LambdaQueryWrapper<>();
                 wrapper.eq(StoreBookingTable::getStoreId, existingTable.getStoreId())
-                        .eq(StoreBookingTable::getCategoryId, checkCategoryId)
                         .eq(StoreBookingTable::getTableNumber, tableNumber)
                         .ne(StoreBookingTable::getId, table.getId());
                 // @TableLogic 会自动过滤已删除的记录(delete_flag=0)
                 StoreBookingTable duplicateTable = this.getOne(wrapper);
                 if (duplicateTable != null) {
-                    log.warn("更新预订服务桌号失败:当前分类下桌号已存在,storeId={}, categoryId={}, tableNumber={}, id={}", 
-                            existingTable.getStoreId(), checkCategoryId, tableNumber, table.getId());
-                    throw new RuntimeException("此桌号已存在");
+                    log.warn("更新预订服务桌号失败:当前门店下桌号已存在,storeId={}, tableNumber={}, id={}", 
+                            existingTable.getStoreId(), tableNumber, table.getId());
+                    throw new RuntimeException("该桌号已存在不能编辑");
                 }
             }
         }