|
|
@@ -0,0 +1,1001 @@
|
|
|
+package shop.alien.store.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import shop.alien.entity.result.R;
|
|
|
+import shop.alien.entity.store.StorePlatformMenu;
|
|
|
+import shop.alien.entity.store.vo.StorePlatformMenuVo;
|
|
|
+import shop.alien.mapper.StorePlatformMenuMapper;
|
|
|
+import shop.alien.store.service.StorePlatformMenuService;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 商家菜单管理 业务层实现
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Transactional
|
|
|
+public class StorePlatformMenuServiceImpl extends ServiceImpl<StorePlatformMenuMapper, StorePlatformMenu> implements StorePlatformMenuService {
|
|
|
+
|
|
|
+ private final StorePlatformMenuMapper storePlatformMenuMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IPage<StorePlatformMenuVo> getMenuPage(int page, int size, String menuName) {
|
|
|
+ log.info("分页查询菜单列表,页码={}, 页大小={}, 菜单名称={}", page, size, menuName);
|
|
|
+
|
|
|
+ // 1. 查询所有未删除的菜单(支持模糊查询)
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(StorePlatformMenu::getDelFlag, "0");
|
|
|
+
|
|
|
+ // 支持菜单名称模糊查询
|
|
|
+ if (StringUtils.hasText(menuName)) {
|
|
|
+ queryWrapper.like(StorePlatformMenu::getMenuName, menuName);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按显示顺序和创建时间排序
|
|
|
+ queryWrapper.orderByAsc(StorePlatformMenu::getMenuSort);
|
|
|
+ queryWrapper.orderByDesc(StorePlatformMenu::getCreatedTime);
|
|
|
+
|
|
|
+ // 查询所有符合条件的菜单
|
|
|
+ List<StorePlatformMenu> allMenus = storePlatformMenuMapper.selectList(queryWrapper);
|
|
|
+
|
|
|
+ // 转换为VO对象
|
|
|
+ List<StorePlatformMenuVo> allMenuVos = allMenus.stream()
|
|
|
+ .map(this::convertToVo)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 2. 构建树形结构(根据parent_id构建)
|
|
|
+ List<StorePlatformMenuVo> treeMenus = buildMenuTreeByParentId(allMenuVos);
|
|
|
+
|
|
|
+ // 3. 对一级菜单(parent_id = 0 或 null)进行分页
|
|
|
+ List<StorePlatformMenuVo> level1Menus = treeMenus.stream()
|
|
|
+ .filter(menu -> menu.getParentId() == null || menu.getParentId() == 0)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 计算分页
|
|
|
+ int total = level1Menus.size();
|
|
|
+ int start = (page - 1) * size;
|
|
|
+ int end = Math.min(start + size, total);
|
|
|
+
|
|
|
+ List<StorePlatformMenuVo> pageMenus = start < total ? level1Menus.subList(start, end) : new ArrayList<>();
|
|
|
+
|
|
|
+ // 4. 构建分页结果
|
|
|
+ IPage<StorePlatformMenuVo> pageResult = new Page<>(page, size, total);
|
|
|
+ pageResult.setRecords(pageMenus);
|
|
|
+
|
|
|
+ log.info("分页查询菜单列表完成,总记录数={}, 当前页记录数={}", total, pageMenus.size());
|
|
|
+
|
|
|
+ return pageResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将StorePlatformMenu转换为StorePlatformMenuVo
|
|
|
+ */
|
|
|
+ private StorePlatformMenuVo convertToVo(StorePlatformMenu menu) {
|
|
|
+ StorePlatformMenuVo vo = new StorePlatformMenuVo();
|
|
|
+ vo.setMenuId(menu.getMenuId());
|
|
|
+ vo.setMenuName(menu.getMenuName());
|
|
|
+ vo.setParentId(menu.getParentId());
|
|
|
+ vo.setMenuType(menu.getMenuType());
|
|
|
+ vo.setMenuSort(menu.getMenuSort());
|
|
|
+ vo.setLevel(menu.getLevel());
|
|
|
+ vo.setPath(menu.getPath());
|
|
|
+ vo.setComponent(menu.getComponent());
|
|
|
+ vo.setPerms(menu.getPerms());
|
|
|
+ vo.setIcon(menu.getIcon());
|
|
|
+ vo.setStatus(menu.getStatus());
|
|
|
+ vo.setVisible(menu.getVisible());
|
|
|
+ vo.setIsFrame(menu.getIsFrame());
|
|
|
+ vo.setIsCache(menu.getIsCache());
|
|
|
+ vo.setDelFlag(menu.getDelFlag());
|
|
|
+ vo.setCreateBy(menu.getCreateBy());
|
|
|
+ vo.setCreatedTime(menu.getCreatedTime());
|
|
|
+ vo.setUpdateBy(menu.getUpdateBy());
|
|
|
+ vo.setUpdatedTime(menu.getUpdatedTime());
|
|
|
+ vo.setRemark(menu.getRemark());
|
|
|
+ vo.setChildren(new ArrayList<StorePlatformMenu>());
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据parent_id构建菜单树形结构
|
|
|
+ * parent_id = 0 或 null 表示一级菜单
|
|
|
+ * 一级菜单包含二级菜单,二级菜单包含三级菜单
|
|
|
+ * 同时设置父菜单名称用于回显
|
|
|
+ */
|
|
|
+ private List<StorePlatformMenuVo> buildMenuTreeByParentId(List<StorePlatformMenuVo> allMenus) {
|
|
|
+ // 创建菜单ID到菜单对象的映射,便于快速查找
|
|
|
+ Map<Long, StorePlatformMenuVo> menuMap = allMenus.stream()
|
|
|
+ .collect(Collectors.toMap(StorePlatformMenuVo::getMenuId, menu -> menu));
|
|
|
+
|
|
|
+ // 获取一级菜单(parent_id = 0 或 null)
|
|
|
+ List<StorePlatformMenuVo> rootMenus = allMenus.stream()
|
|
|
+ .filter(menu -> menu.getParentId() == null || menu.getParentId() == 0)
|
|
|
+ .sorted((a, b) -> {
|
|
|
+ if (a.getMenuSort() != null && b.getMenuSort() != null) {
|
|
|
+ return a.getMenuSort().compareTo(b.getMenuSort());
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 递归构建树形结构
|
|
|
+ for (StorePlatformMenuVo rootMenu : rootMenus) {
|
|
|
+ buildChildrenRecursive(rootMenu, allMenus, menuMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ return rootMenus;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归构建子菜单树
|
|
|
+ *
|
|
|
+ * @param parentMenu 父菜单
|
|
|
+ * @param allMenus 所有菜单列表
|
|
|
+ * @param menuMap 菜单ID到菜单对象的映射
|
|
|
+ */
|
|
|
+ private void buildChildrenRecursive(StorePlatformMenuVo parentMenu,
|
|
|
+ List<StorePlatformMenuVo> allMenus,
|
|
|
+ Map<Long, StorePlatformMenuVo> menuMap) {
|
|
|
+ // 查找当前菜单的所有子菜单(parent_id = 当前菜单的menu_id)
|
|
|
+ List<StorePlatformMenuVo> children = allMenus.stream()
|
|
|
+ .filter(menu -> parentMenu.getMenuId().equals(menu.getParentId()))
|
|
|
+ .sorted((a, b) -> {
|
|
|
+ if (a.getMenuSort() != null && b.getMenuSort() != null) {
|
|
|
+ return a.getMenuSort().compareTo(b.getMenuSort());
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(children)) {
|
|
|
+ parentMenu.setChildren(new ArrayList<StorePlatformMenu>());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置父菜单名称用于回显
|
|
|
+ for (StorePlatformMenuVo childMenu : children) {
|
|
|
+ // 设置一级父菜单名称
|
|
|
+ if (parentMenu.getParentId() == null || parentMenu.getParentId() == 0) {
|
|
|
+ // 如果父菜单是一级菜单,子菜单设置一级父菜单名称
|
|
|
+ childMenu.setFirstLevelMenuName(parentMenu.getMenuName());
|
|
|
+ } else {
|
|
|
+ // 如果父菜单是二级菜单,子菜单设置二级父菜单名称
|
|
|
+ childMenu.setSecondLevelMenuName(parentMenu.getMenuName());
|
|
|
+ // 同时查找一级父菜单名称
|
|
|
+ StorePlatformMenuVo grandParentMenu = menuMap.get(parentMenu.getParentId());
|
|
|
+ if (grandParentMenu != null) {
|
|
|
+ childMenu.setFirstLevelMenuName(grandParentMenu.getMenuName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 递归构建子菜单的子菜单
|
|
|
+ buildChildrenRecursive(childMenu, allMenus, menuMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转换为 List<StorePlatformMenu> 类型
|
|
|
+ List<StorePlatformMenu> menuChildren = new ArrayList<StorePlatformMenu>();
|
|
|
+ menuChildren.addAll(children);
|
|
|
+ parentMenu.setChildren(menuChildren);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑菜单 可以更新
|
|
|
+ * @param storePlatformMenu
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R<StorePlatformMenu> getByMenuId(StorePlatformMenu storePlatformMenu) {
|
|
|
+ Long menuId = storePlatformMenu.getMenuId();
|
|
|
+ log.info("开始查询/编辑菜单,菜单ID={}", menuId);
|
|
|
+
|
|
|
+ // 参数校验:菜单ID不能为空
|
|
|
+ if (menuId == null) {
|
|
|
+ log.warn("查询/编辑菜单失败:ID为空");
|
|
|
+ return R.fail("菜单ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询当前菜单信息
|
|
|
+ StorePlatformMenu currentMenu = storePlatformMenuMapper.selectById(menuId);
|
|
|
+ if (currentMenu == null) {
|
|
|
+ log.warn("查询失败:菜单不存在,菜单ID={}", menuId);
|
|
|
+ return R.fail("菜单不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断是否有更新操作(如果传入了菜单名称、路径或状态,则认为是更新操作)
|
|
|
+ boolean isUpdate = StringUtils.hasText(storePlatformMenu.getMenuName())
|
|
|
+ || StringUtils.hasText(storePlatformMenu.getPath())
|
|
|
+ || StringUtils.hasText(storePlatformMenu.getStatus());
|
|
|
+
|
|
|
+ if (isUpdate) {
|
|
|
+ // 更新操作:只允许更新本级菜单的名称、路径、状态
|
|
|
+ log.info("开始更新菜单信息,菜单ID={}", menuId);
|
|
|
+
|
|
|
+ // 只更新允许的字段:菜单名称、路径、状态
|
|
|
+ if (StringUtils.hasText(storePlatformMenu.getMenuName())) {
|
|
|
+ currentMenu.setMenuName(storePlatformMenu.getMenuName());
|
|
|
+ }
|
|
|
+ if (StringUtils.hasText(storePlatformMenu.getPath())) {
|
|
|
+ currentMenu.setPath(storePlatformMenu.getPath());
|
|
|
+ }
|
|
|
+ if (StringUtils.hasText(storePlatformMenu.getStatus())) {
|
|
|
+ currentMenu.setStatus(storePlatformMenu.getStatus());
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 != null) {
|
|
|
+ log.warn("更新菜单失败:菜单名称已存在,菜单名称={}, parentId={}",
|
|
|
+ currentMenu.getMenuName(), currentMenu.getParentId());
|
|
|
+ return R.fail("该菜单名称在同级菜单中已存在,请更换其他名称");
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean result = this.updateById(currentMenu);
|
|
|
+ if (result) {
|
|
|
+ log.info("更新菜单信息成功,菜单ID={}, 菜单名称={}, 路径={}, 状态={}",
|
|
|
+ menuId, currentMenu.getMenuName(), currentMenu.getPath(), currentMenu.getStatus());
|
|
|
+ // 重新查询更新后的菜单
|
|
|
+ currentMenu = storePlatformMenuMapper.selectById(menuId);
|
|
|
+ } else {
|
|
|
+ log.warn("更新菜单信息失败,菜单ID={}", menuId);
|
|
|
+ return R.fail("更新菜单信息失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询上级菜单信息并设置到返回对象中
|
|
|
+ Integer level = currentMenu.getLevel();
|
|
|
+ if (level != null) {
|
|
|
+ // 如果是二级菜单,查询一级菜单名称
|
|
|
+ if (level == 2 && currentMenu.getParentId() != null && currentMenu.getParentId() != 0) {
|
|
|
+ StorePlatformMenu firstLevelMenu = storePlatformMenuMapper.selectById(currentMenu.getParentId());
|
|
|
+ if (firstLevelMenu != null) {
|
|
|
+ currentMenu.setFirstLevelMenuName(firstLevelMenu.getMenuName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 如果是三级菜单,查询一级和二级菜单名称
|
|
|
+ else if (level == 3 && currentMenu.getParentId() != null && currentMenu.getParentId() != 0) {
|
|
|
+ // 先查询二级菜单
|
|
|
+ StorePlatformMenu secondLevelMenu = storePlatformMenuMapper.selectById(currentMenu.getParentId());
|
|
|
+ if (secondLevelMenu != null) {
|
|
|
+ currentMenu.setSecondLevelMenuName(secondLevelMenu.getMenuName());
|
|
|
+ // 再查询一级菜单
|
|
|
+ if (secondLevelMenu.getParentId() != null && secondLevelMenu.getParentId() != 0) {
|
|
|
+ StorePlatformMenu firstLevelMenu = storePlatformMenuMapper.selectById(secondLevelMenu.getParentId());
|
|
|
+ if (firstLevelMenu != null) {
|
|
|
+ currentMenu.setFirstLevelMenuName(firstLevelMenu.getMenuName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return R.data(currentMenu);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<StorePlatformMenu> deleteMenuDetails(StorePlatformMenu storePlatformMenu) {
|
|
|
+ Long menuId = storePlatformMenu.getMenuId();
|
|
|
+
|
|
|
+ log.info("开始删除菜单,菜单ID={}", menuId);
|
|
|
+
|
|
|
+ // 参数校验:菜单ID不能为空
|
|
|
+ if (menuId == null) {
|
|
|
+ log.warn("删除菜单失败:菜单ID为空");
|
|
|
+ return R.fail("菜单ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询当前菜单信息
|
|
|
+ StorePlatformMenu currentMenu = storePlatformMenuMapper.selectById(menuId);
|
|
|
+ if (currentMenu == null) {
|
|
|
+ log.warn("删除菜单失败:菜单不存在,菜单ID={}", menuId);
|
|
|
+ return R.fail("菜单不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer level = currentMenu.getLevel();
|
|
|
+ if (level == null) {
|
|
|
+ log.warn("删除菜单失败:层级信息为空,菜单ID={}", menuId);
|
|
|
+ return R.fail("层级信息不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ int totalUpdateCount = 0;
|
|
|
+
|
|
|
+ // 检查当前菜单是否已删除
|
|
|
+ if (!"0".equals(currentMenu.getDelFlag())) {
|
|
|
+ log.warn("删除菜单失败:菜单已被删除,菜单ID={}, 层级={}, del_flag={}", menuId, level, currentMenu.getDelFlag());
|
|
|
+ return R.fail("菜单已被删除");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据层级进行级联删除
|
|
|
+ if (level == 1) {
|
|
|
+ // 删除层级1的菜单:同时删除层级2和3的菜单
|
|
|
+ log.info("删除一级菜单,同时删除层级2和3的菜单,菜单ID={}, 层级={}", menuId, level);
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level1Query = new LambdaQueryWrapper<>();
|
|
|
+ level1Query
|
|
|
+ .eq(StorePlatformMenu::getLevel, 1)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0");
|
|
|
+ List<StorePlatformMenu> levelMenus = storePlatformMenuMapper.selectList(level1Query);
|
|
|
+ List<Long> levelMenuIds = levelMenus.stream()
|
|
|
+ .map(StorePlatformMenu::getMenuId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (!CollectionUtils.isEmpty(levelMenus)){
|
|
|
+ LambdaUpdateWrapper<StorePlatformMenu> wrapper2 = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper2.eq(StorePlatformMenu::getLevel, 1)
|
|
|
+ .in(StorePlatformMenu::getMenuId, levelMenuIds)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .set(StorePlatformMenu::getDelFlag, "2");
|
|
|
+ int count = storePlatformMenuMapper.update(null, wrapper2);
|
|
|
+ totalUpdateCount += count;
|
|
|
+ log.info("删除一级菜单本身,菜单ID={}, 更新记录数={}", menuId, count);
|
|
|
+ }
|
|
|
+ // 1. 先查询所有层级2的子菜单
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level2Query = new LambdaQueryWrapper<>();
|
|
|
+ level2Query
|
|
|
+ .eq(StorePlatformMenu::getLevel, 2)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0");
|
|
|
+ List<StorePlatformMenu> level2Menus = storePlatformMenuMapper.selectList(level2Query);
|
|
|
+ List<Long> level2MenuIds = level2Menus.stream()
|
|
|
+ .map(StorePlatformMenu::getMenuId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ log.info("查询到层级2子菜单数量:{}", level2Menus != null ? level2Menus.size() : 0);
|
|
|
+
|
|
|
+
|
|
|
+ // 3. 删除所有层级2子菜单
|
|
|
+ if (!CollectionUtils.isEmpty(level2Menus)) {
|
|
|
+ LambdaUpdateWrapper<StorePlatformMenu> wrapper2 = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper2.eq(StorePlatformMenu::getLevel, 2)
|
|
|
+ .in(StorePlatformMenu::getMenuId, level2MenuIds)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .set(StorePlatformMenu::getDelFlag, "2");
|
|
|
+ int count2 = storePlatformMenuMapper.update(null, wrapper2);
|
|
|
+ totalUpdateCount += count2;
|
|
|
+ log.info("删除层级2子菜单,parent_id={}, 更新记录数={}", menuId, count2);
|
|
|
+ } else {
|
|
|
+ log.info("没有层级2子菜单需要删除,菜单ID={}", menuId);
|
|
|
+ }
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level3Query = new LambdaQueryWrapper<>();
|
|
|
+ level3Query
|
|
|
+ .eq(StorePlatformMenu::getLevel, 3)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0");
|
|
|
+ List<StorePlatformMenu> level3Menus = storePlatformMenuMapper.selectList(level3Query);
|
|
|
+ List<Long> level3MenuIds = level3Menus.stream()
|
|
|
+ .map(StorePlatformMenu::getMenuId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ // 4. 删除所有层级3子菜单(parent_id在层级2菜单ID列表中)
|
|
|
+ if (!CollectionUtils.isEmpty(level3Menus)) {
|
|
|
+ LambdaUpdateWrapper<StorePlatformMenu> wrapper3 = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper3.eq(StorePlatformMenu::getLevel, 3)
|
|
|
+ .in(StorePlatformMenu::getMenuId, level3MenuIds)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .set(StorePlatformMenu::getDelFlag, "2");
|
|
|
+ int count3 = storePlatformMenuMapper.update(null, wrapper3);
|
|
|
+ totalUpdateCount += count3;
|
|
|
+ log.info("删除层级3子菜单, 更新记录数={}", level3MenuIds, count3);
|
|
|
+ } else {
|
|
|
+ log.info("没有层级3子菜单需要删除,菜单ID={}", menuId);
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("删除一级菜单成功,菜单ID={}, 层级={}, 总更新记录数={}", menuId, level, totalUpdateCount);
|
|
|
+
|
|
|
+ } else if (level == 2) {
|
|
|
+ // 删除层级2的菜单:同时删除层级3的菜单
|
|
|
+ log.info("删除二级菜单,同时删除层级3的菜单,菜单ID={}, 层级={}", menuId, level);
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level2Query = new LambdaQueryWrapper<>();
|
|
|
+ level2Query
|
|
|
+ .eq(StorePlatformMenu::getLevel, 2)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0");
|
|
|
+ List<StorePlatformMenu> level2Menus = storePlatformMenuMapper.selectList(level2Query);
|
|
|
+ List<Long> level2MenuIds = level2Menus.stream()
|
|
|
+ .map(StorePlatformMenu::getMenuId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ // 3. 删除所有层级2子菜单
|
|
|
+ if (!CollectionUtils.isEmpty(level2Menus)) {
|
|
|
+ LambdaUpdateWrapper<StorePlatformMenu> wrapper2 = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper2.eq(StorePlatformMenu::getLevel, 2)
|
|
|
+ .in(StorePlatformMenu::getMenuId, level2MenuIds)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .set(StorePlatformMenu::getDelFlag, "2");
|
|
|
+ int count2 = storePlatformMenuMapper.update(null, wrapper2);
|
|
|
+ totalUpdateCount += count2;
|
|
|
+ log.info("删除层级2子菜单,菜单ID={}, 更新记录数={}", menuId, count2);
|
|
|
+ } else {
|
|
|
+ log.info("没有层级2子菜单需要删除,菜单ID={}", menuId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 先查询所有层级3的子菜单
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level3Query = new LambdaQueryWrapper<>();
|
|
|
+ level3Query
|
|
|
+ .eq(StorePlatformMenu::getLevel, 3)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0");
|
|
|
+ List<StorePlatformMenu> level3Menus = storePlatformMenuMapper.selectList(level3Query);
|
|
|
+ List<Long> level3MenuIds = level3Menus.stream()
|
|
|
+ .map(StorePlatformMenu::getMenuId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ log.info("查询到层级3子菜单数量:{}", level3Menus != null ? level3Menus.size() : 0);
|
|
|
+
|
|
|
+ // 3. 删除所有层级3子菜单
|
|
|
+ if (!CollectionUtils.isEmpty(level3Menus)) {
|
|
|
+ LambdaUpdateWrapper<StorePlatformMenu> wrapper2 = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper2.eq(StorePlatformMenu::getLevel, 3)
|
|
|
+ .in(StorePlatformMenu::getMenuId, level3MenuIds)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .set(StorePlatformMenu::getDelFlag, "2");
|
|
|
+ int count2 = storePlatformMenuMapper.update(null, wrapper2);
|
|
|
+ totalUpdateCount += count2;
|
|
|
+ log.info("删除层级3子菜单,菜单ID={}, 更新记录数={}", menuId, totalUpdateCount);
|
|
|
+ } else {
|
|
|
+ log.info("没有层级3子菜单需要删除,菜单ID={}", menuId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } else if (level == 3) {
|
|
|
+ // 删除层级3的菜单:只删除当前菜单
|
|
|
+ log.info("删除三级菜单,只删除当前菜单,菜单ID={}, 层级={}", menuId, level);
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level3Query = new LambdaQueryWrapper<>();
|
|
|
+ level3Query
|
|
|
+ .eq(StorePlatformMenu::getLevel, 3)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0");
|
|
|
+ List<StorePlatformMenu> level3Menus = storePlatformMenuMapper.selectList(level3Query);
|
|
|
+ List<Long> level3MenuIds = level3Menus.stream()
|
|
|
+ .map(StorePlatformMenu::getMenuId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ log.info("查询到层级3子菜单数量:{}", level3Menus != null ? level3Menus.size() : 0);
|
|
|
+
|
|
|
+ // 3. 删除所有层级3子菜单
|
|
|
+ if (!CollectionUtils.isEmpty(level3Menus)) {
|
|
|
+ LambdaUpdateWrapper<StorePlatformMenu> wrapper2 = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper2.eq(StorePlatformMenu::getLevel, 3)
|
|
|
+ .in(StorePlatformMenu::getMenuId, level3MenuIds)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .set(StorePlatformMenu::getDelFlag, "2");
|
|
|
+ int count2 = storePlatformMenuMapper.update(null, wrapper2);
|
|
|
+ totalUpdateCount += count2;
|
|
|
+ log.info("删除层级3子菜单,parent_id={}, 更新记录数={}", menuId, count2);
|
|
|
+ } else {
|
|
|
+ log.info("没有层级3子菜单需要删除,菜单ID={}", menuId);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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");
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.data(currentMenu);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<StorePlatformMenu> editUpdateMenuStatus(Long menuId, String status, Integer level) {
|
|
|
+ // 参数校验:菜单ID不能为空
|
|
|
+ if (menuId == null) {
|
|
|
+ log.warn("更新菜单状态失败:菜单ID为空");
|
|
|
+ return R.fail("菜单ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询当前菜单信息
|
|
|
+ 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 ;
|
|
|
+
|
|
|
+ // 动态切换状态:如果数据库中是"1"则改为"0",如果是"0"则改为"1"
|
|
|
+ String currentStatus = currentMenu.getStatus();
|
|
|
+ String newStatus;
|
|
|
+ if ("1".equals(currentStatus)) {
|
|
|
+ newStatus = "0"; // 当前是禁用(1),切换为启用(0)
|
|
|
+ } else if ("0".equals(currentStatus)) {
|
|
|
+ newStatus = "1"; // 当前是启用(0),切换为禁用(1)
|
|
|
+ } else {
|
|
|
+ // 如果状态值异常,默认为启用(0)
|
|
|
+ log.warn("菜单状态值异常,菜单ID={}, 当前状态={},默认设置为启用(0)", menuId, currentStatus);
|
|
|
+ newStatus = "0";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 根据层级进行级联删除
|
|
|
+ if (level == 1) {
|
|
|
+ // 删除层级1的菜单:同时删除层级2和3的菜单
|
|
|
+ log.info("启用禁用一级菜单,同时启用禁用层级2和3的菜单,菜单ID={}, 层级={}", menuId, level);
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level1Query = new LambdaQueryWrapper<>();
|
|
|
+ level1Query
|
|
|
+ .eq(StorePlatformMenu::getLevel, 1)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .eq(StorePlatformMenu::getStatus, currentStatus);
|
|
|
+ List<StorePlatformMenu> levelMenus = storePlatformMenuMapper.selectList(level1Query);
|
|
|
+ List<Long> levelMenuIds = levelMenus.stream()
|
|
|
+ .map(StorePlatformMenu::getMenuId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (!CollectionUtils.isEmpty(levelMenus)){
|
|
|
+ LambdaUpdateWrapper<StorePlatformMenu> wrapper2 = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper2.eq(StorePlatformMenu::getLevel, 1)
|
|
|
+ .in(StorePlatformMenu::getMenuId, levelMenuIds)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .set(StorePlatformMenu::getStatus, newStatus);
|
|
|
+ int count = storePlatformMenuMapper.update(null, wrapper2);
|
|
|
+ totalUpdateCount += count;
|
|
|
+ log.info("启用禁用一级菜单本身,菜单ID={}, 更新记录数={}", menuId, count);
|
|
|
+ }
|
|
|
+ // 1. 先查询所有层级2的子菜单
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level2Query = new LambdaQueryWrapper<>();
|
|
|
+ level2Query
|
|
|
+ .eq(StorePlatformMenu::getLevel, 2)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .eq(StorePlatformMenu::getStatus, currentStatus);
|
|
|
+ List<StorePlatformMenu> level2Menus = storePlatformMenuMapper.selectList(level2Query);
|
|
|
+ List<Long> level2MenuIds = level2Menus.stream()
|
|
|
+ .map(StorePlatformMenu::getMenuId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ log.info("查询到层级2子菜单数量:{}", level2Menus != null ? level2Menus.size() : 0);
|
|
|
+
|
|
|
+
|
|
|
+ // 3. 删除所有层级2子菜单
|
|
|
+ if (!CollectionUtils.isEmpty(level2Menus)) {
|
|
|
+ LambdaUpdateWrapper<StorePlatformMenu> wrapper2 = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper2.eq(StorePlatformMenu::getLevel, 2)
|
|
|
+ .in(StorePlatformMenu::getMenuId, level2MenuIds)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .set(StorePlatformMenu::getStatus, newStatus);
|
|
|
+ int count2 = storePlatformMenuMapper.update(null, wrapper2);
|
|
|
+ totalUpdateCount += count2;
|
|
|
+ log.info("启用禁用层级2子菜单,菜单Id={}, 更新记录数={}", menuId, totalUpdateCount);
|
|
|
+ } else {
|
|
|
+ log.info("没有层级2子菜单需要启用禁用,菜单ID={}", menuId);
|
|
|
+ }
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level3Query = new LambdaQueryWrapper<>();
|
|
|
+ level3Query
|
|
|
+ .eq(StorePlatformMenu::getLevel, 3)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .eq(StorePlatformMenu::getStatus, currentStatus);
|
|
|
+ List<StorePlatformMenu> level3Menus = storePlatformMenuMapper.selectList(level3Query);
|
|
|
+ List<Long> level3MenuIds = level3Menus.stream()
|
|
|
+ .map(StorePlatformMenu::getMenuId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ // 4. 删除所有层级3子菜单(parent_id在层级2菜单ID列表中)
|
|
|
+ if (!CollectionUtils.isEmpty(level3Menus)) {
|
|
|
+ LambdaUpdateWrapper<StorePlatformMenu> wrapper3 = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper3.eq(StorePlatformMenu::getLevel, 3)
|
|
|
+ .in(StorePlatformMenu::getMenuId, level3MenuIds)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .set(StorePlatformMenu::getStatus, newStatus);
|
|
|
+ int count3 = storePlatformMenuMapper.update(null, wrapper3);
|
|
|
+ totalUpdateCount += count3;
|
|
|
+ log.info("启用禁用层级3子菜单, 更新记录数={}", level3MenuIds, count3);
|
|
|
+ } else {
|
|
|
+ log.info("没有层级3子菜单需要操作,菜单ID={}", menuId);
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("操作一级菜单成功,菜单ID={}, 层级={}, 总更新记录数={}", menuId, level, totalUpdateCount);
|
|
|
+
|
|
|
+ } else if (level == 2) {
|
|
|
+ // 删除层级2的菜单:同时删除层级3的菜单
|
|
|
+ log.info("启用禁用二级菜单,同时启用禁用层级3的菜单,菜单ID={}, 层级={}", menuId, level);
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level2Query = new LambdaQueryWrapper<>();
|
|
|
+ level2Query
|
|
|
+ .eq(StorePlatformMenu::getLevel, 2)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .eq(StorePlatformMenu::getStatus, currentStatus);
|
|
|
+ List<StorePlatformMenu> level2Menus = storePlatformMenuMapper.selectList(level2Query);
|
|
|
+ List<Long> level2MenuIds = level2Menus.stream()
|
|
|
+ .map(StorePlatformMenu::getMenuId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ // 3. 删除所有层级2子菜单
|
|
|
+ if (!CollectionUtils.isEmpty(level2Menus)) {
|
|
|
+ LambdaUpdateWrapper<StorePlatformMenu> wrapper2 = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper2.eq(StorePlatformMenu::getLevel, 2)
|
|
|
+ .in(StorePlatformMenu::getMenuId, level2MenuIds)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .set(StorePlatformMenu::getStatus, newStatus);
|
|
|
+ int count2 = storePlatformMenuMapper.update(null, wrapper2);
|
|
|
+ totalUpdateCount += count2;
|
|
|
+ log.info("启用禁用层级2子菜单,菜单ID={}, 更新记录数={}", menuId, count2);
|
|
|
+ } else {
|
|
|
+ log.info("没有层级2子菜单需要操作,菜单ID={}", menuId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 先查询所有层级3的子菜单
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level3Query = new LambdaQueryWrapper<>();
|
|
|
+ level3Query.eq(StorePlatformMenu::getMenuId, menuId)
|
|
|
+ .eq(StorePlatformMenu::getLevel, 3)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .eq(StorePlatformMenu::getStatus, currentStatus);
|
|
|
+ List<StorePlatformMenu> level3Menus = storePlatformMenuMapper.selectList(level3Query);
|
|
|
+ List<Long> level3MenuIds = level3Menus.stream()
|
|
|
+ .map(StorePlatformMenu::getMenuId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ log.info("查询到层级3子菜单数量:{}", level3Menus != null ? level3Menus.size() : 0);
|
|
|
+
|
|
|
+ // 3. 删除所有层级3子菜单
|
|
|
+ if (!CollectionUtils.isEmpty(level3Menus)) {
|
|
|
+ LambdaUpdateWrapper<StorePlatformMenu> wrapper2 = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper2.eq(StorePlatformMenu::getLevel, 3)
|
|
|
+ .in(StorePlatformMenu::getMenuId, level3MenuIds)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .set(StorePlatformMenu::getStatus, newStatus);
|
|
|
+ int count2 = storePlatformMenuMapper.update(null, wrapper2);
|
|
|
+ totalUpdateCount += count2;
|
|
|
+ log.info("启用禁用层级3子菜单,菜单ID={}, 更新记录数={}", menuId, totalUpdateCount);
|
|
|
+ } else {
|
|
|
+ log.info("没有层级3子菜单需要操作,菜单ID={}", menuId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } else if (level == 3) {
|
|
|
+ // 删除层级3的菜单:只删除当前菜单
|
|
|
+ log.info("删除三级菜单,只删除当前菜单,菜单ID={}, 层级={}", menuId, level);
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level3Query = new LambdaQueryWrapper<>();
|
|
|
+ level3Query.eq(StorePlatformMenu::getMenuId, menuId)
|
|
|
+ .eq(StorePlatformMenu::getLevel, 3)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .eq(StorePlatformMenu::getStatus, currentStatus);
|
|
|
+ List<StorePlatformMenu> level3Menus = storePlatformMenuMapper.selectList(level3Query);
|
|
|
+ List<Long> level3MenuIds = level3Menus.stream()
|
|
|
+ .map(StorePlatformMenu::getMenuId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ log.info("查询到层级3子菜单数量:{}", level3Menus != null ? level3Menus.size() : 0);
|
|
|
+
|
|
|
+ // 3. 删除所有层级3子菜单
|
|
|
+ if (!CollectionUtils.isEmpty(level3Menus)) {
|
|
|
+ LambdaUpdateWrapper<StorePlatformMenu> wrapper2 = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper2.eq(StorePlatformMenu::getLevel, 3)
|
|
|
+ .in(StorePlatformMenu::getMenuId, level3MenuIds)
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .set(StorePlatformMenu::getStatus, newStatus);
|
|
|
+ int count2 = storePlatformMenuMapper.update(null, wrapper2);
|
|
|
+ totalUpdateCount += count2;
|
|
|
+ log.info("删除层级3子菜单,parent_id={}, 更新记录数={}", menuId, count2);
|
|
|
+ } else {
|
|
|
+ log.info("没有层级3子菜单需要删除,菜单ID={}", menuId);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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("开始新增菜单,菜单名称={}, 一级分类名称={}, 二级分类名称={}",
|
|
|
+ store != null ? store.getMenuName() : null,
|
|
|
+ store != null ? store.getFirstLevelMenuName() : null,
|
|
|
+ store != null ? store.getSecondLevelMenuName() : null);
|
|
|
+
|
|
|
+ // 1. 参数校验
|
|
|
+ if (store == null) {
|
|
|
+ log.warn("新增菜单失败:菜单信息为空");
|
|
|
+ return R.fail("菜单信息不能为空");
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(store.getMenuName())) {
|
|
|
+ log.warn("新增菜单失败:菜单名称为空");
|
|
|
+ return R.fail("菜单名称不能为空");
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(store.getPath())) {
|
|
|
+ log.warn("新增菜单失败:路径为空");
|
|
|
+ return R.fail("路径不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 根据一级、二级分类名称确定 parentId 和 level
|
|
|
+ Long parentId = determineParentIdByMenuNames(store);
|
|
|
+ if (parentId == null && (StringUtils.hasText(store.getFirstLevelMenuName()) ||
|
|
|
+ StringUtils.hasText(store.getSecondLevelMenuName()))) {
|
|
|
+ log.warn("新增菜单失败:无法确定父菜单,一级分类名称={}, 二级分类名称={}",
|
|
|
+ store.getFirstLevelMenuName(), store.getSecondLevelMenuName());
|
|
|
+ return R.fail("无法确定父菜单,请检查一级和二级分类名称是否正确");
|
|
|
+ }
|
|
|
+
|
|
|
+ store.setParentId(parentId != null ? parentId : 0L);
|
|
|
+
|
|
|
+ // 3. 根据 parentId 计算 level
|
|
|
+ Integer level = calculateLevelByParentId(store.getParentId());
|
|
|
+ if (level == null) {
|
|
|
+ log.warn("新增菜单失败:无法确定菜单层级,parentId={}", store.getParentId());
|
|
|
+ return R.fail("无法确定菜单层级");
|
|
|
+ }
|
|
|
+ store.setLevel(level);
|
|
|
+
|
|
|
+ // 4. 校验菜单名称是否重复(同一父菜单下不能有重名)
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> nameCheck = new LambdaQueryWrapper<>();
|
|
|
+ nameCheck.eq(StorePlatformMenu::getMenuName, store.getMenuName())
|
|
|
+ .eq(StorePlatformMenu::getParentId, store.getParentId())
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0");
|
|
|
+ StorePlatformMenu duplicateMenu = storePlatformMenuMapper.selectOne(nameCheck);
|
|
|
+ if (duplicateMenu != null) {
|
|
|
+ log.warn("新增菜单失败:菜单名称已存在,菜单名称={}, parentId={}",
|
|
|
+ store.getMenuName(), store.getParentId());
|
|
|
+ return R.fail("该菜单名称在同级菜单中已存在,请更换其他名称");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 校验父菜单是否存在且未删除(如果不是一级菜单)
|
|
|
+ if (store.getParentId() != null && store.getParentId() != 0) {
|
|
|
+ StorePlatformMenu parentMenu = storePlatformMenuMapper.selectById(store.getParentId());
|
|
|
+ if (parentMenu == null) {
|
|
|
+ log.warn("新增菜单失败:父菜单不存在,parentId={}", store.getParentId());
|
|
|
+ return R.fail("父菜单不存在");
|
|
|
+ }
|
|
|
+ if (!"0".equals(parentMenu.getDelFlag())) {
|
|
|
+ log.warn("新增菜单失败:父菜单已被删除,parentId={}", store.getParentId());
|
|
|
+ return R.fail("父菜单已被删除");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 6. 设置默认值
|
|
|
+ setDefaultValues(store);
|
|
|
+
|
|
|
+ // 7. 设置排序值(自动设置为最大值+1)
|
|
|
+ Integer maxSort = getMaxSortByParentId(store.getParentId());
|
|
|
+ store.setMenuSort(maxSort != null ? maxSort + 1 : 0);
|
|
|
+
|
|
|
+ // 8. 设置创建时间和创建人
|
|
|
+ store.setCreatedTime(new Date());
|
|
|
+ store.setCreateBy("admin");
|
|
|
+
|
|
|
+ // 9. 执行新增操作
|
|
|
+ int insert = storePlatformMenuMapper.insert(store);
|
|
|
+ if (insert > 0) {
|
|
|
+ log.info("新增菜单成功,菜单ID={}, 菜单名称={}, parentId={}, level={}",
|
|
|
+ store.getMenuId(), store.getMenuName(), store.getParentId(), store.getLevel());
|
|
|
+
|
|
|
+ // 重新查询菜单信息(包含自动生成的ID等信息)
|
|
|
+ StorePlatformMenu savedMenu = storePlatformMenuMapper.selectById(store.getMenuId());
|
|
|
+ if (savedMenu != null) {
|
|
|
+ // 设置父菜单名称用于回显
|
|
|
+ setParentMenuNames(savedMenu);
|
|
|
+ return R.data(savedMenu);
|
|
|
+ }
|
|
|
+ return R.data(store);
|
|
|
+ } else {
|
|
|
+ log.warn("新增菜单失败,菜单名称={}", store.getMenuName());
|
|
|
+ return R.fail("新增菜单失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据一级、二级分类名称确定 parentId
|
|
|
+ *
|
|
|
+ * @param store 菜单对象(包含 firstLevelMenuName 和 secondLevelMenuName)
|
|
|
+ * @return parentId,如果是一级菜单则返回 0,如果无法确定则返回 null
|
|
|
+ */
|
|
|
+ private Long determineParentIdByMenuNames(StorePlatformMenu store) {
|
|
|
+ String firstLevelMenuName = store.getFirstLevelMenuName();
|
|
|
+ String secondLevelMenuName = store.getSecondLevelMenuName();
|
|
|
+
|
|
|
+ // 如果直接指定了 parentId,优先使用
|
|
|
+ if (store.getParentId() != null && store.getParentId() != 0) {
|
|
|
+ return store.getParentId();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果一级和二级分类名称都为空,说明是一级菜单
|
|
|
+ if (!StringUtils.hasText(firstLevelMenuName) && !StringUtils.hasText(secondLevelMenuName)) {
|
|
|
+ return 0L;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果只有一级分类名称,说明是二级菜单
|
|
|
+ if (StringUtils.hasText(firstLevelMenuName) && !StringUtils.hasText(secondLevelMenuName)) {
|
|
|
+ // 查找一级菜单
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(StorePlatformMenu::getMenuName, firstLevelMenuName)
|
|
|
+ .and(w -> w.isNull(StorePlatformMenu::getParentId)
|
|
|
+ .or().eq(StorePlatformMenu::getParentId, 0))
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0");
|
|
|
+ StorePlatformMenu firstLevelMenu = storePlatformMenuMapper.selectOne(queryWrapper);
|
|
|
+ if (firstLevelMenu != null) {
|
|
|
+ return firstLevelMenu.getMenuId();
|
|
|
+ }
|
|
|
+ log.warn("未找到一级菜单,菜单名称={}", firstLevelMenuName);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有一级和二级分类名称,说明是三级菜单
|
|
|
+ if (StringUtils.hasText(firstLevelMenuName) && StringUtils.hasText(secondLevelMenuName)) {
|
|
|
+ // 先查找一级菜单
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level1Query = new LambdaQueryWrapper<>();
|
|
|
+ level1Query.eq(StorePlatformMenu::getMenuName, firstLevelMenuName)
|
|
|
+ .and(w -> w.isNull(StorePlatformMenu::getParentId)
|
|
|
+ .or().eq(StorePlatformMenu::getParentId, 0))
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0");
|
|
|
+ StorePlatformMenu firstLevelMenu = storePlatformMenuMapper.selectOne(level1Query);
|
|
|
+ if (firstLevelMenu == null) {
|
|
|
+ log.warn("未找到一级菜单,菜单名称={}", firstLevelMenuName);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 再查找二级菜单
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> level2Query = new LambdaQueryWrapper<>();
|
|
|
+ level2Query.eq(StorePlatformMenu::getMenuName, secondLevelMenuName)
|
|
|
+ .eq(StorePlatformMenu::getParentId, firstLevelMenu.getMenuId())
|
|
|
+ .eq(StorePlatformMenu::getDelFlag, "0");
|
|
|
+ StorePlatformMenu secondLevelMenu = storePlatformMenuMapper.selectOne(level2Query);
|
|
|
+ if (secondLevelMenu != null) {
|
|
|
+ return secondLevelMenu.getMenuId();
|
|
|
+ }
|
|
|
+ log.warn("未找到二级菜单,菜单名称={}, 父菜单ID={}",
|
|
|
+ secondLevelMenuName, firstLevelMenu.getMenuId());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0L;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据 parentId 计算菜单层级
|
|
|
+ *
|
|
|
+ * @param parentId 父菜单ID
|
|
|
+ * @return 菜单层级(1一级菜单 2二级菜单 3三级菜单)
|
|
|
+ */
|
|
|
+ private Integer calculateLevelByParentId(Long parentId) {
|
|
|
+ if (parentId == null || parentId == 0) {
|
|
|
+ return 1; // 一级菜单
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询父菜单
|
|
|
+ StorePlatformMenu parentMenu = storePlatformMenuMapper.selectById(parentId);
|
|
|
+ if (parentMenu == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer parentLevel = parentMenu.getLevel();
|
|
|
+ if (parentLevel == null) {
|
|
|
+ // 如果父菜单没有层级信息,尝试根据parentId推断
|
|
|
+ if (parentMenu.getParentId() == null || parentMenu.getParentId() == 0) {
|
|
|
+ return 2; // 父菜单是一级菜单,当前是二级菜单
|
|
|
+ } else {
|
|
|
+ return 3; // 父菜单不是一级菜单,当前是三级菜单
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 层级不能超过3
|
|
|
+ int newLevel = parentLevel + 1;
|
|
|
+ if (newLevel > 3) {
|
|
|
+ log.warn("菜单层级超过3级,parentId={}, parentLevel={}", parentId, parentLevel);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return newLevel;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置默认值
|
|
|
+ */
|
|
|
+ private void setDefaultValues(StorePlatformMenu store) {
|
|
|
+ if (!StringUtils.hasText(store.getStatus())) {
|
|
|
+ store.setStatus("0"); // 默认开启
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(store.getDelFlag())) {
|
|
|
+ store.setDelFlag("0"); // 默认未删除
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(store.getVisible())) {
|
|
|
+ store.setVisible("0"); // 默认显示
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(store.getIsFrame())) {
|
|
|
+ store.setIsFrame("1"); // 默认非外链
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(store.getIsCache())) {
|
|
|
+ store.setIsCache("0"); // 默认缓存
|
|
|
+ }
|
|
|
+ if (store.getMenuSort() == null) {
|
|
|
+ store.setMenuSort(0); // 默认排序值
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(store.getMenuType())) {
|
|
|
+ // 根据层级设置默认菜单类型:一级菜单为M(目录),其他为C(菜单)
|
|
|
+ if (store.getLevel() != null && store.getLevel() == 1) {
|
|
|
+ store.setMenuType("M");
|
|
|
+ } else {
|
|
|
+ store.setMenuType("C");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定父菜单下的最大排序值
|
|
|
+ *
|
|
|
+ * @param parentId 父菜单ID
|
|
|
+ * @return 最大排序值,如果没有子菜单则返回null
|
|
|
+ */
|
|
|
+ private Integer getMaxSortByParentId(Long parentId) {
|
|
|
+ LambdaQueryWrapper<StorePlatformMenu> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ if (parentId == null || parentId == 0) {
|
|
|
+ queryWrapper.isNull(StorePlatformMenu::getParentId)
|
|
|
+ .or().eq(StorePlatformMenu::getParentId, 0);
|
|
|
+ } else {
|
|
|
+ queryWrapper.eq(StorePlatformMenu::getParentId, parentId);
|
|
|
+ }
|
|
|
+ queryWrapper.eq(StorePlatformMenu::getDelFlag, "0")
|
|
|
+ .orderByDesc(StorePlatformMenu::getMenuSort)
|
|
|
+ .last("LIMIT 1");
|
|
|
+
|
|
|
+ StorePlatformMenu menu = storePlatformMenuMapper.selectOne(queryWrapper);
|
|
|
+ return menu != null && menu.getMenuSort() != null ? menu.getMenuSort() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置父菜单名称用于回显
|
|
|
+ *
|
|
|
+ * @param menu 菜单对象
|
|
|
+ */
|
|
|
+ private void setParentMenuNames(StorePlatformMenu menu) {
|
|
|
+ if (menu.getParentId() == null || menu.getParentId() == 0) {
|
|
|
+ // 一级菜单,不需要设置父菜单名称
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询父菜单
|
|
|
+ StorePlatformMenu parentMenu = storePlatformMenuMapper.selectById(menu.getParentId());
|
|
|
+ if (parentMenu == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置二级父菜单名称
|
|
|
+ menu.setSecondLevelMenuName(parentMenu.getMenuName());
|
|
|
+
|
|
|
+ // 如果父菜单还有父菜单,设置一级父菜单名称
|
|
|
+ if (parentMenu.getParentId() != null && parentMenu.getParentId() != 0) {
|
|
|
+ StorePlatformMenu grandParentMenu = storePlatformMenuMapper.selectById(parentMenu.getParentId());
|
|
|
+ if (grandParentMenu != null) {
|
|
|
+ menu.setFirstLevelMenuName(grandParentMenu.getMenuName());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果父菜单是一级菜单,设置一级父菜单名称
|
|
|
+ menu.setFirstLevelMenuName(parentMenu.getMenuName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|