From fb842a99734726e628bd0981ccdf80a253534eaa Mon Sep 17 00:00:00 2001
From: liuhaonan <31457034@qq.com>
Date: 星期二, 17 五月 2022 17:06:07 +0800
Subject: [PATCH] 日志

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/MenuService.java |   95 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 93 insertions(+), 2 deletions(-)

diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/MenuService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/MenuService.java
index 7ed7d10..2aa8384 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/MenuService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/MenuService.java
@@ -2,16 +2,27 @@
 
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.util.StrUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.sandu.common.enums.RoleLevelStatus;
+import com.sandu.common.execption.BusinessException;
+import com.sandu.common.execption.EntityExistException;
 import com.sandu.common.service.impl.BaseServiceImpl;
+import com.sandu.common.util.SpringContextHolder;
+import com.sandu.ximon.admin.param.MenuParam;
+import com.sandu.ximon.admin.security.SecurityUtils;
+import com.sandu.ximon.dao.bo.MenuNode;
 import com.sandu.ximon.dao.domain.AdminRoleRelation;
 import com.sandu.ximon.dao.domain.Menu;
+import com.sandu.ximon.dao.domain.Role;
 import com.sandu.ximon.dao.domain.RoleMenuRelation;
 import com.sandu.ximon.dao.mapper.AdminRoleRelationMapper;
 import com.sandu.ximon.dao.mapper.MenuMapper;
 import lombok.AllArgsConstructor;
+import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 
+import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 import java.util.stream.Collectors;
@@ -25,6 +36,8 @@
 public class MenuService extends BaseServiceImpl<MenuMapper, Menu> {
 
     private final RoleMenuRelationService roleMenuRelationService;
+    private MenuMapper menuMapper;
+
     /**
      * 鑾峰彇瑙掕壊涓嬬殑鑿滃崟
      */
@@ -34,7 +47,85 @@
         if (CollectionUtil.isEmpty(menuIdList)) {
             return null;
         }
-        List<Menu> menus = listByIds(menuIdList);
-        return menus;
+
+        return menuMapper.listMenuById(menuIdList);
+    }
+
+    public boolean addMenu(MenuParam param) {
+        Menu menu = new Menu();
+        BeanUtils.copyProperties(param, menu);
+        return save(menu);
+    }
+
+    private MenuNode covertMenuNode(Menu menu, List<Menu> menuList) {
+        MenuNode node = new MenuNode();
+        BeanUtils.copyProperties(menu, node);
+        List<MenuNode> children = menuList.stream()
+                .filter(subMenu -> subMenu.getPid().equals(menu.getId()))
+                .map(subMenu -> covertMenuNode(subMenu, menuList)).collect(Collectors.toList());
+        node.setChildren(children);
+        return node;
+    }
+
+    public boolean updateMenu(Long menuId, MenuParam param) {
+        Menu one = getById(menuId);
+        if (one == null) {
+            throw new BusinessException("鎵句笉鍒拌彍鍗曚俊鎭�");
+        }
+
+        List<Role> roles = SpringContextHolder.getBean(RoleService.class).listByAdminId(SecurityUtils.getUserId());
+        Integer roleLevel = roles.stream().map(Role::getLevel).min(Integer::compareTo).orElse(RoleLevelStatus.COMMON.getCode());
+        if (!RoleLevelStatus.SUPER.getCode().equals(roleLevel)) {
+            throw new BusinessException("瓒呯骇绠$悊鍛樻墠鍙互缂栬緫鑿滃崟");
+        }
+
+        Menu menu = new Menu();
+        BeanUtils.copyProperties(param, menu);
+        menu.setId(menuId);
+        return updateById(menu);
+    }
+
+    public List<MenuNode> treeList() {
+        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = Wrappers.lambdaQuery(Menu.class).orderByAsc(Menu::getSeq);
+        List<Menu> list = list(menuLambdaQueryWrapper);
+        return list.stream()
+                .filter(menu -> menu.getPid().equals(0L))
+                .map(menu -> covertMenuNode(menu, list)).collect(Collectors.toList());
+    }
+
+    public boolean delMenu(Long menuId) {
+        Menu one = getById(menuId);
+        if (one == null) {
+            throw new BusinessException("鎵句笉鍒拌彍鍗曚俊鎭�");
+        }
+
+        List<Role> roles = SpringContextHolder.getBean(RoleService.class).listByAdminId(SecurityUtils.getUserId());
+        Integer roleLevel = roles.stream().map(Role::getLevel).min(Integer::compareTo).orElse(RoleLevelStatus.COMMON.getCode());
+        if (!RoleLevelStatus.SUPER.getCode().equals(roleLevel)) {
+            throw new BusinessException("瓒呯骇绠$悊鍛樻墠鍙互鍒犻櫎鑿滃崟");
+        }
+        List<Long> list = menuMapper.listMenuIfBinding(menuId);
+        List<Long> list1 = menuMapper.listMenuIfBinding1(menuId);
+
+        if (list.size() == 0 && list1.size() == 0) {
+            return removeById(menuId);
+        } else {
+            throw new BusinessException("鑿滃崟姝e湪浣跨敤锛岃鍕垮垹闄�");
+        }
+
+    }
+
+    /**
+     * 閫氳繃鑿滃崟ID闆嗗悎鑾峰彇鑿滃崟瀹炰綋鍒楄〃
+     *
+     * @param menuIds 鑿滃崟ID闆嗗悎
+     */
+    public List<MenuNode> getUserPermissionListById(List<Long> menuIds) {
+        List<Menu> menuList = menuMapper.getUserPermissionListById(menuIds);
+
+        List<MenuNode> resultList = menuList.stream()
+                .filter(menu -> menu.getPid().equals(0L))
+                .map(menu -> covertMenuNode(menu, menuList)).collect(Collectors.toList());
+        return resultList;
     }
 }

--
Gitblit v1.9.3