2021与蓝度共同重构项目,服务端
zhanzhiqin
2022-05-26 4e91adbb41c89688fe5b0ef03807cfe748a7d59d
ximon-admin/src/main/java/com/sandu/ximon/admin/service/RoleService.java
@@ -2,6 +2,7 @@
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;
@@ -12,6 +13,7 @@
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;
@@ -108,7 +110,23 @@
        Role role = new Role();
        role.setName(param.getName());
        role.setRemark(param.getRemark());
        role.setLevel(RoleLevelStatus.COMMON.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());
        }
        if (!save(role)) {
            throw new BusinessException("添加角色失败");
        }
@@ -125,11 +143,27 @@
            throw new BusinessException("找不到角色信息");
        }
        assertLevels(role.getLevel());
        Role update = new Role();
        update.setId(roleId);
        update.setName(param.getName());
        update.setRemark(param.getRemark());
        if (!updateById(update)) {
        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 (!updateById(role)) {
            throw new BusinessException("编辑角色失败");
        }
        roleMenuRelationService.remove(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId, roleId));
@@ -176,6 +210,10 @@
        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);
    }