wuchen 2 месяцев назад
Родитель
Сommit
136f718ce6

+ 214 - 37
alien-store/src/main/java/shop/alien/store/service/impl/StorePlatformMenuServiceImpl.java

@@ -224,55 +224,99 @@ public class StorePlatformMenuServiceImpl extends ServiceImpl<StorePlatformMenuM
                 || StringUtils.hasText(storePlatformMenu.getStatus());
 
         if (isUpdate) {
-            // 更新操作:只允许更新本级菜单的名称、路径、状态
+            // 更新操作:更新菜单的名称、路径、状态
             log.info("开始更新菜单信息,菜单ID={}", menuId);
 
-            // 只更新允许的字段:菜单名称、路径、状态
+            // 保存原始状态,用于判断是否需要级联更新
+            String originalStatus = currentMenu.getStatus();
+            String newStatus = storePlatformMenu.getStatus();
+            boolean statusChanged = StringUtils.hasText(newStatus) && !newStatus.equals(originalStatus);
+            
+            // 保存原始名称,用于判断是否需要校验重名
+            String originalMenuName = currentMenu.getMenuName();
+            String newMenuName = storePlatformMenu.getMenuName();
+            boolean nameChanged = StringUtils.hasText(newMenuName) && !newMenuName.equals(originalMenuName);
+
+            // 1. 如果菜单名称有变化,校验菜单名称是否重复(同一父菜单下不能有重名)
+            if (nameChanged) {
+                LambdaQueryWrapper<StorePlatformMenu> nameCheck = new LambdaQueryWrapper<>();
+                nameCheck.eq(StorePlatformMenu::getMenuName, newMenuName)
+                        .eq(StorePlatformMenu::getParentId, currentMenu.getParentId())
+                        .eq(StorePlatformMenu::getDelFlag, "0")
+                        .ne(StorePlatformMenu::getMenuId, menuId); // 排除当前菜单
+                StorePlatformMenu duplicateMenu = storePlatformMenuMapper.selectOne(nameCheck);
+                if (duplicateMenu != null) {
+                    log.warn("更新菜单失败:菜单名称已存在,菜单名称={}, parentId={}",
+                            newMenuName, currentMenu.getParentId());
+                    return R.fail("该菜单名称在同级菜单中已存在,请更换其他名称");
+                }
+            }
+
+            // 2. 更新菜单名称和路径(如果 provided)
+            boolean needUpdateNameOrPath = false;
             if (StringUtils.hasText(storePlatformMenu.getMenuName())) {
                 currentMenu.setMenuName(storePlatformMenu.getMenuName());
+                needUpdateNameOrPath = true;
             }
             if (StringUtils.hasText(storePlatformMenu.getPath())) {
                 currentMenu.setPath(storePlatformMenu.getPath());
-            }
-            if (StringUtils.hasText(storePlatformMenu.getStatus())) {
-                currentMenu.setStatus(storePlatformMenu.getStatus());
+                needUpdateNameOrPath = true;
             }
 
-            currentMenu.setUpdatedTime(new Date());
-            currentMenu.setUpdateBy("admin");
-//            // 切换状态:0启用,1禁用
-//            String newStatus = "0".equals(currentMenu.getStatus()) ? "1" : "0";
-//            currentMenu.setStatus(newStatus);
-//            // 如果状态为空,默认设置为"0"(开启)
-//            if (!StringUtils.hasText(currentMenu.getStatus())) {
-//                currentMenu.setStatus("0");
-//            }
-
-            // 4. 校验菜单名称是否重复(同一父菜单下不能有重名)
-            LambdaQueryWrapper<StorePlatformMenu> nameCheck = new LambdaQueryWrapper<>();
-            nameCheck.eq(StorePlatformMenu::getMenuName, currentMenu.getMenuName())
-                    .eq(StorePlatformMenu::getParentId, currentMenu.getParentId())
-                    .eq(StorePlatformMenu::getDelFlag, "0");
-            StorePlatformMenu duplicateMenu = storePlatformMenuMapper.selectOne(nameCheck);
-            if (duplicateMenu.getMenuId().equals(menuId)){
-                editUpdateMenuStatus(menuId,currentMenu.getStatus(),currentMenu.getLevel());
-                return R.data(currentMenu);
-            }
-            if (duplicateMenu != null) {
-                log.warn("更新菜单失败:菜单名称已存在,菜单名称={}, parentId={}",
-                        currentMenu.getMenuName(), currentMenu.getParentId());
-                return R.fail("该菜单名称在同级菜单中已存在,请更换其他名称");
+            // 3. 如果只更新名称或路径,直接更新
+            if (needUpdateNameOrPath && !statusChanged) {
+                currentMenu.setUpdatedTime(new Date());
+                currentMenu.setUpdateBy("admin");
+                boolean result = this.updateById(currentMenu);
+                if (result) {
+                    log.info("更新菜单信息成功,菜单ID={}, 菜单名称={}, 路径={}",
+                            menuId, currentMenu.getMenuName(), currentMenu.getPath());
+                    // 重新查询更新后的菜单
+                    currentMenu = storePlatformMenuMapper.selectById(menuId);
+                } else {
+                    log.warn("更新菜单信息失败,菜单ID={}", menuId);
+                    return R.fail("更新菜单信息失败");
+                }
             }
-
-            boolean result = this.updateById(currentMenu);
-            if (result) {
-                log.info("更新菜单信息成功,菜单ID={}, 菜单名称={}, 路径={}, 状态={}",
-                        menuId, currentMenu.getMenuName(), currentMenu.getPath(), currentMenu.getStatus());
+            // 4. 如果状态有变化,调用editUpdateMenuStatus进行级联更新
+            else if (statusChanged) {
+                // 如果同时更新了名称或路径,先更新名称和路径
+                if (needUpdateNameOrPath) {
+                    currentMenu.setUpdatedTime(new Date());
+                    currentMenu.setUpdateBy("admin");
+                    // 注意:这里不更新状态,状态由editUpdateMenuStatus处理
+                    String tempStatus = currentMenu.getStatus();
+                    currentMenu.setStatus(originalStatus); // 临时恢复原状态
+                    boolean result = this.updateById(currentMenu);
+                    if (!result) {
+                        log.warn("更新菜单名称或路径失败,菜单ID={}", menuId);
+                        return R.fail("更新菜单信息失败");
+                    }
+                    currentMenu.setStatus(tempStatus); // 恢复新状态
+                }
+                
+                // 调用editUpdateMenuStatus进行级联状态更新
+                Integer level = currentMenu.getLevel();
+                if (level == null) {
+                    log.warn("更新菜单状态失败:层级信息为空,菜单ID={}", menuId);
+                    return R.fail("层级信息不能为空");
+                }
+                
+                // 调用editUpdateMenuStatus方法,传入新的状态值
+                R<StorePlatformMenu> statusUpdateResult = editUpdateMenuStatusWithNewStatus(menuId, newStatus, level);
+                if (!statusUpdateResult.isSuccess()) {
+                    log.warn("更新菜单状态失败:{}", statusUpdateResult.getMsg());
+                    return statusUpdateResult;
+                }
+                
                 // 重新查询更新后的菜单
                 currentMenu = storePlatformMenuMapper.selectById(menuId);
-            } else {
-                log.warn("更新菜单信息失败,菜单ID={}", menuId);
-                return R.fail("更新菜单信息失败");
+                log.info("更新菜单信息成功(包含级联状态更新),菜单ID={}, 菜单名称={}, 路径={}, 状态={}",
+                        menuId, currentMenu.getMenuName(), currentMenu.getPath(), currentMenu.getStatus());
+            }
+            // 5. 如果只更新状态且状态没有变化,或者没有需要更新的内容
+            else {
+                log.info("没有需要更新的内容,菜单ID={}", menuId);
             }
         }
 
@@ -568,6 +612,139 @@ public class StorePlatformMenuServiceImpl extends ServiceImpl<StorePlatformMenuM
         return R.data(updatedMenu);
     }
 
+    /**
+     * 根据指定的新状态值更新菜单状态(级联更新子菜单)
+     * 用于编辑页面直接设置状态值
+     * 
+     * @param menuId 菜单ID
+     * @param newStatus 新的状态值("0"开启,"1"关闭)
+     * @param level 菜单层级
+     * @return 更新结果
+     */
+    private R<StorePlatformMenu> editUpdateMenuStatusWithNewStatus(Long menuId, String newStatus, Integer level) {
+        // 参数校验:菜单ID不能为空
+        if (menuId == null) {
+            log.warn("更新菜单状态失败:菜单ID为空");
+            return R.fail("菜单ID不能为空");
+        }
+
+        // 参数校验:状态值必须有效
+        if (!"0".equals(newStatus) && !"1".equals(newStatus)) {
+            log.warn("更新菜单状态失败:状态值无效,菜单ID={}, 状态值={}", menuId, newStatus);
+            return R.fail("状态值无效,应为0(开启)或1(关闭)");
+        }
+
+        // 查询当前菜单信息
+        StorePlatformMenu currentMenu = storePlatformMenuMapper.selectById(menuId);
+        if (currentMenu == null) {
+            log.warn("更新菜单状态失败:菜单不存在,菜单ID={}", menuId);
+            return R.fail("菜单不存在");
+        }
+
+        // 检查菜单是否已删除
+        if (!"0".equals(currentMenu.getDelFlag())) {
+            log.warn("更新菜单状态失败:菜单已被删除,菜单ID={}, del_flag={}", menuId, currentMenu.getDelFlag());
+            return R.fail("菜单已被删除");
+        }
+
+        int totalUpdateCount = 0;
+
+        // 根据层级进行级联更新状态
+        if (level == 1) {
+            // 关闭/开启一级菜单:关闭/开启本级和所有子级(二级和三级)
+            log.info("更新一级菜单状态,同时更新所有子级菜单状态,菜单ID={}, 层级={}, 新状态={}", menuId, level, newStatus);
+            
+            // 1. 查找所有子菜单ID(包括二级和三级)
+            List<Long> allChildrenIds = findAllChildrenMenuIds(menuId);
+            
+            // 2. 构建要更新状态的菜单ID列表(包括本级和所有子级)
+            List<Long> menuIdsToUpdate = new ArrayList<>();
+            menuIdsToUpdate.add(menuId);
+            menuIdsToUpdate.addAll(allChildrenIds);
+            
+            // 3. 批量更新状态
+            if (!CollectionUtils.isEmpty(menuIdsToUpdate)) {
+                LambdaUpdateWrapper<StorePlatformMenu> wrapper = new LambdaUpdateWrapper<>();
+                wrapper.in(StorePlatformMenu::getMenuId, menuIdsToUpdate)
+                       .eq(StorePlatformMenu::getDelFlag, "0")
+                       .set(StorePlatformMenu::getStatus, newStatus);
+                int count = storePlatformMenuMapper.update(null, wrapper);
+                totalUpdateCount = count;
+                log.info("更新一级菜单及其所有子菜单状态成功,菜单ID={}, 子菜单数量={}, 总更新记录数={}", 
+                        menuId, allChildrenIds.size(), totalUpdateCount);
+            } else {
+                // 只更新本级菜单状态
+                LambdaUpdateWrapper<StorePlatformMenu> wrapper = new LambdaUpdateWrapper<>();
+                wrapper.eq(StorePlatformMenu::getMenuId, menuId)
+                       .eq(StorePlatformMenu::getDelFlag, "0")
+                       .set(StorePlatformMenu::getStatus, newStatus);
+                int count = storePlatformMenuMapper.update(null, wrapper);
+                totalUpdateCount = count;
+                log.info("更新一级菜单状态成功(无子菜单),菜单ID={}, 更新记录数={}", menuId, totalUpdateCount);
+            }
+
+        } else if (level == 2) {
+            // 关闭/开启二级菜单:关闭/开启本级和所有子级(三级)
+            log.info("更新二级菜单状态,同时更新所有子级菜单状态,菜单ID={}, 层级={}, 新状态={}", menuId, level, newStatus);
+            
+            // 1. 查找所有子菜单ID(三级菜单)
+            List<Long> allChildrenIds = findAllChildrenMenuIds(menuId);
+            
+            // 2. 构建要更新状态的菜单ID列表(包括本级和所有子级)
+            List<Long> menuIdsToUpdate = new ArrayList<>();
+            menuIdsToUpdate.add(menuId);
+            menuIdsToUpdate.addAll(allChildrenIds);
+            
+            // 3. 批量更新状态
+            if (!CollectionUtils.isEmpty(menuIdsToUpdate)) {
+                LambdaUpdateWrapper<StorePlatformMenu> wrapper = new LambdaUpdateWrapper<>();
+                wrapper.in(StorePlatformMenu::getMenuId, menuIdsToUpdate)
+                       .eq(StorePlatformMenu::getDelFlag, "0")
+                       .set(StorePlatformMenu::getStatus, newStatus);
+                int count = storePlatformMenuMapper.update(null, wrapper);
+                totalUpdateCount = count;
+                log.info("更新二级菜单及其所有子菜单状态成功,菜单ID={}, 子菜单数量={}, 总更新记录数={}", 
+                        menuId, allChildrenIds.size(), totalUpdateCount);
+            } else {
+                // 只更新本级菜单状态
+                LambdaUpdateWrapper<StorePlatformMenu> wrapper = new LambdaUpdateWrapper<>();
+                wrapper.eq(StorePlatformMenu::getMenuId, menuId)
+                       .eq(StorePlatformMenu::getDelFlag, "0")
+                       .set(StorePlatformMenu::getStatus, newStatus);
+                int count = storePlatformMenuMapper.update(null, wrapper);
+                totalUpdateCount = count;
+                log.info("更新二级菜单状态成功(无子菜单),菜单ID={}, 更新记录数={}", menuId, totalUpdateCount);
+            }
+
+        } else if (level == 3) {
+            // 关闭/开启三级菜单:只更新本级
+            log.info("更新三级菜单状态,只更新当前菜单,菜单ID={}, 层级={}, 新状态={}", menuId, level, newStatus);
+            
+            LambdaUpdateWrapper<StorePlatformMenu> wrapper = new LambdaUpdateWrapper<>();
+            wrapper.eq(StorePlatformMenu::getMenuId, menuId)
+                   .eq(StorePlatformMenu::getDelFlag, "0")
+                   .set(StorePlatformMenu::getStatus, newStatus);
+            int count = storePlatformMenuMapper.update(null, wrapper);
+            totalUpdateCount = count;
+            
+            if (totalUpdateCount > 0) {
+                log.info("更新三级菜单状态成功,菜单ID={}, 层级={}, 更新记录数={}", menuId, level, totalUpdateCount);
+            } else {
+                log.warn("更新三级菜单状态未找到记录或已删除,菜单ID={}, 层级={}, del_flag={}", 
+                        menuId, level, currentMenu.getDelFlag());
+                return R.fail("操作失败:菜单不存在或已被删除");
+            }
+
+        } else {
+            log.warn("更新菜单状态失败:层级值不正确,菜单ID={}, 层级={}", menuId, level);
+            return R.fail("层级值不正确,应为1、2或3");
+        }
+
+        // 重新查询更新后的菜单信息
+        StorePlatformMenu updatedMenu = storePlatformMenuMapper.selectById(menuId);
+        return R.data(updatedMenu);
+    }
+
     @Override
     public R<StorePlatformMenu> addByMenu(StorePlatformMenu store) {
         log.info("开始新增菜单,菜单名称={}, 一级分类名称={}, 二级分类名称={}",