From dda268997ca8f8a364f7c19b45d7a43a50a98efe Mon Sep 17 00:00:00 2001
From: liuhaonan <31457034@qq.com>
Date: 星期二, 25 十月 2022 18:00:11 +0800
Subject: [PATCH] changes

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/RoleService.java |  192 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 176 insertions(+), 16 deletions(-)

diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/RoleService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/RoleService.java
index 89e0a5b..3d35427 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/RoleService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/RoleService.java
@@ -2,23 +2,27 @@
 
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.system.UserInfo;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.github.pagehelper.PageHelper;
+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.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.common.util.BeanConvertUtil;
+import com.sandu.ximon.admin.param.RoleParam;
+import com.sandu.ximon.admin.security.SecurityUtils;
+import com.sandu.ximon.dao.domain.*;
+import com.sandu.ximon.dao.enums.AdministratorEnums;
+import com.sandu.ximon.dao.mapper.MenuMapper;
 import com.sandu.ximon.dao.mapper.RoleMapper;
 import lombok.AllArgsConstructor;
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -30,23 +34,30 @@
 public class RoleService extends BaseServiceImpl<RoleMapper, Role> {
 
     private final AdminRoleRelationService adminRoleRelationService;
+    private final ClientRoleRelationService clientRoleRelationService;
     private final RoleMenuRelationService roleMenuRelationService;
     private final MenuService menuService;
+    private final MenuMapper menuMapper;
+
 
     /**
      * 鑾峰彇鎸囧畾绠$悊鍛樼殑spring security鏉冮檺璁よ瘉鍒楄〃
      */
-    public Collection<GrantedAuthority> mapToGrantedAuthorities(Long adminId) {
-
-        // 鑾峰彇绠$悊鍛樼殑瑙掕壊id
-        List<Role> roles = listByAdminId(adminId);
+    public Collection<GrantedAuthority> mapToGrantedAuthorities(Long adminId, boolean flag) {
+        List<Role> roles;
+        if (flag) {
+            // 鑾峰彇绠$悊鍛樼殑瑙掕壊id
+            roles = listByAdminId(adminId);
+        } else {
+            roles = listByClientId(adminId);
+        }
 
         if (CollectionUtil.isEmpty(roles)) {
             throw new BusinessException("褰撳墠鐢ㄦ埛娌℃湁瑙掕壊");
         }
         List<Long> roleIdList = roles.stream().map(Role::getId).collect(Collectors.toList());
-        List<Long> menuIdList = roleMenuRelationService.list(Wrappers.lambdaQuery(RoleMenuRelation.class).in(RoleMenuRelation::getRoleId, roleIdList).select(RoleMenuRelation::getMenuId))
-                .stream().map(RoleMenuRelation::getMenuId).distinct().collect(Collectors.toList());
+        List<Long> menuIdList = roleMenuRelationService.list(Wrappers.lambdaQuery(RoleMenuRelation.class).in(RoleMenuRelation::getRoleId, roleIdList)
+                        .select(RoleMenuRelation::getMenuId)).stream().map(RoleMenuRelation::getMenuId).distinct().collect(Collectors.toList());
 
         Set<String> menuPermissions = new HashSet<>();
         // 鑿滃崟鏉冮檺
@@ -70,11 +81,160 @@
      */
     public List<Role> listByAdminId(Long adminId) {
         // 鑾峰彇绠$悊鍛樼殑瑙掕壊id
-        List<AdminRoleRelation> list = adminRoleRelationService.list(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId, adminId).select(AdminRoleRelation::getRoleId));
+        List<AdminRoleRelation> list = adminRoleRelationService.list(Wrappers.lambdaQuery(AdminRoleRelation.class)
+                .eq(AdminRoleRelation::getAdminId, adminId).select(AdminRoleRelation::getRoleId));
         List<Long> roleIdList = list.stream().map(AdminRoleRelation::getRoleId).collect(Collectors.toList());
         if (CollectionUtil.isEmpty(roleIdList)) {
-            throw new BusinessException("褰撳墠鐢ㄦ埛娌℃湁瑙掕壊");
+            return null;
         }
         return listByIds(roleIdList);
     }
+
+    /**
+     * 鑾峰彇鏌愪釜鏅�氱敤鎴风殑瑙掕壊鍒楄〃
+     */
+    public List<Role> listByClientId(Long clientId) {
+        // 鑾峰彇鏅�氱敤鎴风殑瑙掕壊id
+        List<ClientRoleRelation> list = clientRoleRelationService.list(Wrappers.lambdaQuery(ClientRoleRelation.class).eq(ClientRoleRelation::getClientId, clientId).select(ClientRoleRelation::getRoleId));
+        List<Long> roleIdList = list.stream().map(ClientRoleRelation::getRoleId).collect(Collectors.toList());
+        if (CollectionUtil.isEmpty(roleIdList)) {
+            return null;
+        }
+        return listByIds(roleIdList);
+    }
+
+    @Transactional(rollbackFor = Exception.class)
+    public boolean addRole(RoleParam param) {
+        if (param.getMenuIdList().isEmpty()) {
+            throw new BusinessException("鏉冮檺鍙傛暟寮傚父");
+        }
+        Role role = new Role();
+        role.setName(param.getName());
+        role.setRemark(param.getRemark());
+        //璁剧疆瑙掕壊鏉冮檺绛夌骇
+        if (param.getLevel() != null) {
+            if (param.getLevel().equals(RoleLevelStatus.SUPER.getCode())) {
+                throw new BusinessException("鏃犳硶娣诲姞瓒呯骇绠$悊鍛樿鑹�");
+            } else if (param.getLevel().equals(RoleLevelStatus.COMMON.getCode())) {
+                role.setLevel(RoleLevelStatus.COMMON.getCode());
+            } else if (param.getLevel().equals(RoleLevelStatus.NORMAL.getCode())) {
+                role.setLevel(RoleLevelStatus.NORMAL.getCode());
+            } else if (param.getLevel().equals(RoleLevelStatus.NORMAL2.getCode())) {
+                role.setLevel(RoleLevelStatus.NORMAL2.getCode());
+            } else {
+                role.setLevel(RoleLevelStatus.NORMAL.getCode());
+            }
+        } else {
+            role.setLevel(RoleLevelStatus.NORMAL.getCode());
+        }
+
+        if (!save(role)) {
+            throw new BusinessException("娣诲姞瑙掕壊澶辫触");
+        }
+
+        roleMenuRelationService.addRoleMenuList(role.getId(), param.getMenuIdList());
+
+        return true;
+    }
+
+    @Transactional(rollbackFor = Exception.class)
+    public boolean updateRole(Long roleId, RoleParam param) {
+        Role role = getById(roleId);
+        if (role == null) {
+            throw new BusinessException("鎵句笉鍒拌鑹蹭俊鎭�");
+        }
+        //鍒ゆ柇鏄湁鏉冮檺淇敼
+        assertLevels(role.getLevel());
+
+        role.setName(param.getName());
+        role.setRemark(param.getRemark());
+
+        if (!role.getLevel().equals(RoleLevelStatus.SUPER.getCode())) {
+            //璁剧疆瑙掕壊鏉冮檺绛夌骇
+            if (param.getLevel() != null) {
+                if (param.getLevel().equals(RoleLevelStatus.SUPER.getCode())) {
+                    throw new BusinessException("鏃犳硶淇敼鎴愯秴绾х鐞嗗憳瑙掕壊");
+                } else if (param.getLevel().equals(RoleLevelStatus.COMMON.getCode())) {
+                    role.setLevel(RoleLevelStatus.COMMON.getCode());
+                } else if (param.getLevel().equals(RoleLevelStatus.NORMAL.getCode())) {
+                    role.setLevel(RoleLevelStatus.NORMAL.getCode());
+                } else if (param.getLevel().equals(RoleLevelStatus.NORMAL2.getCode())) {
+                    role.setLevel(RoleLevelStatus.NORMAL2.getCode());
+                } else {
+                    role.setLevel(RoleLevelStatus.NORMAL.getCode());
+                }
+            } else {
+                role.setLevel(RoleLevelStatus.NORMAL.getCode());
+            }
+        } else if (!param.getLevel().equals(RoleLevelStatus.SUPER.getCode())) {
+            throw new BusinessException("瓒呯骇绠$悊鍛樿鑹茬瓑绾ф棤娉曚慨鏀癸紒");
+        }
+
+
+        if (!updateById(role)) {
+            throw new BusinessException("缂栬緫瑙掕壊澶辫触");
+        }
+        roleMenuRelationService.remove(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId, roleId));
+
+        roleMenuRelationService.addRoleMenuList(role.getId(), param.getMenuIdList());
+        return true;
+    }
+
+
+    /**
+     * 鍒ゆ柇褰撳墠瑙掕壊鑳戒笉鑳芥搷浣滅洰鏍囩敤鎴� 涓嶈鐨勮瘽鐩存帴鎶涘嚭寮傚父
+     *
+     * @param roleLevel 鐩爣鐢ㄦ埛鐨勬渶楂樿鑹茬瓑绾�
+     * @return 鎿嶄綔浜虹殑鏈�楂樿鑹茬瓑绾�
+     */
+    public int assertLevels(Integer roleLevel) {
+        Long userId = SecurityUtils.getUserId();
+        List<Role> roles = listByAdminId(userId);
+        if (CollectionUtil.isEmpty(roles)) {
+            throw new BusinessException("褰撳墠鐢ㄦ埛娌℃湁瑙掕壊");
+        }
+        List<Integer> levels = roles.stream().map(Role::getLevel).collect(Collectors.toList());
+        int min = Collections.min(levels);
+        if (roleLevel != null) {
+            if (roleLevel < min) {
+                throw new BusinessException("鏉冮檺涓嶈冻锛屼綘鐨勮鑹茬骇鍒細" + min + "锛屼綆浜庢搷浣滅殑瑙掕壊绾у埆锛�" + roleLevel);
+            }
+        }
+        return min;
+    }
+
+    public boolean delRole(Long roleId) {
+        Role role = getById(roleId);
+        if (role == null) {
+            throw new BusinessException("鎵句笉鍒拌鑹�");
+        }
+        //瓒呯骇绠$悊鍛樹笉鑳藉垹闄�
+        if (RoleLevelStatus.SUPER.getCode().equals(role.getLevel())) {
+            throw new BusinessException("褰撳墠瑙掕壊涓鸿秴绾х鐞嗗憳锛屾棤娉曞垹闄�");
+        }
+
+        assertLevels(role.getLevel());
+        List<AdminRoleRelation> list = adminRoleRelationService.list(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getRoleId, role));
+        if (CollectionUtil.isNotEmpty(list)) {
+            throw new BusinessException("褰撳墠瑙掕壊鏈夌鐞嗗憳浣跨敤锛屾棤娉曞垹闄�");
+        }
+        List<ClientRoleRelation> listClient = clientRoleRelationService.list(Wrappers.lambdaQuery(ClientRoleRelation.class).eq(ClientRoleRelation::getRoleId, role));
+        if (CollectionUtil.isNotEmpty(listClient)) {
+            throw new BusinessException("褰撳墠瑙掕壊鏈夌敤鎴蜂娇鐢紝鏃犳硶鍒犻櫎");
+        }
+        roleMenuRelationService.remove(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId, roleId));
+        return removeById(roleId);
+    }
+
+    public List<RoleDetail> listRole(int pageNo, int pageSize) {
+        PageHelper.startPage(pageNo, pageSize);
+        List<Long> roleIdList = list().stream().map(Role::getId).collect(Collectors.toList());
+        if (CollectionUtil.isEmpty(roleIdList)) {
+            return null;
+        }
+
+        List<RoleDetail> list = baseMapper.listRole(roleIdList);
+
+        return list;
+    }
 }

--
Gitblit v1.9.3