199 lines
5.6 KiB
Java

package com.designhub.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.designhub.entity.Role;
import com.designhub.entity.RolePermission;
import com.designhub.entity.UserRole;
import com.designhub.exception.BusinessException;
import com.designhub.mapper.RoleMapper;
import com.designhub.mapper.RolePermissionMapper;
import com.designhub.mapper.UserRoleMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* 角色管理Service
*/
@Service
@RequiredArgsConstructor
public class RoleService {
private final RoleMapper roleMapper;
private final RolePermissionMapper rolePermissionMapper;
private final UserRoleMapper userRoleMapper;
/**
* 获取所有角色列表
*/
public List<Role> getAllRoles() {
return roleMapper.selectList(
new LambdaQueryWrapper<Role>()
.eq(Role::getStatus, 1)
.orderByAsc(Role::getSortOrder)
);
}
/**
* 根据ID获取角色
*/
public Role getRoleById(Long id) {
return roleMapper.selectById(id);
}
/**
* 根据用户ID获取角色列表
*/
public List<Role> getRolesByUserId(Long userId) {
return roleMapper.selectRolesByUserId(userId);
}
/**
* 创建角色
*/
@Transactional(rollbackFor = Exception.class)
public Role createRole(Role role) {
// 验证角色编码唯一性
long count = roleMapper.selectCount(
new LambdaQueryWrapper<Role>()
.eq(Role::getRoleCode, role.getRoleCode())
);
if (count > 0) {
throw new BusinessException("角色编码已存在");
}
// 设置默认值
if (role.getStatus() == null) {
role.setStatus(1);
}
if (role.getBuiltIn() == null) {
role.setBuiltIn(0);
}
if (role.getDataScope() == null) {
role.setDataScope("self");
}
roleMapper.insert(role);
return role;
}
/**
* 更新角色
*/
@Transactional(rollbackFor = Exception.class)
public boolean updateRole(Role role) {
Role existRole = getRoleById(role.getId());
if (existRole == null) {
throw new BusinessException("角色不存在");
}
// 内置角色不允许修改编码和级别
if (existRole.getBuiltIn() == 1) {
role.setRoleCode(null);
role.setLevel(null);
role.setBuiltIn(null);
}
return roleMapper.updateById(role) > 0;
}
/**
* 删除角色
*/
@Transactional(rollbackFor = Exception.class)
public boolean deleteRole(Long id) {
Role role = getRoleById(id);
if (role == null) {
throw new BusinessException("角色不存在");
}
// 内置角色不允许删除
if (role.getBuiltIn() == 1) {
throw new BusinessException("内置角色不允许删除");
}
// 检查是否有用户使用此角色
long userCount = userRoleMapper.selectCount(
new LambdaQueryWrapper<UserRole>()
.eq(UserRole::getRoleId, id)
);
if (userCount > 0) {
throw new BusinessException("该角色下还有用户,无法删除");
}
// 删除角色权限关联
rolePermissionMapper.delete(
new LambdaQueryWrapper<RolePermission>()
.eq(RolePermission::getRoleId, id)
);
return roleMapper.deleteById(id) > 0;
}
/**
* 为角色分配权限
*/
@Transactional(rollbackFor = Exception.class)
public boolean assignPermissions(Long roleId, List<Long> permissionIds) {
Role role = getRoleById(roleId);
if (role == null) {
throw new BusinessException("角色不存在");
}
// 删除原有权限
rolePermissionMapper.delete(
new LambdaQueryWrapper<RolePermission>()
.eq(RolePermission::getRoleId, roleId)
);
// 分配新权限
if (permissionIds != null && !permissionIds.isEmpty()) {
for (Long permissionId : permissionIds) {
RolePermission rolePermission = new RolePermission();
rolePermission.setRoleId(roleId);
rolePermission.setPermissionId(permissionId);
rolePermissionMapper.insert(rolePermission);
}
}
return true;
}
/**
* 获取角色的权限ID列表
*/
public List<Long> getRolePermissionIds(Long roleId) {
List<RolePermission> rolePermissions = rolePermissionMapper.selectList(
new LambdaQueryWrapper<RolePermission>()
.eq(RolePermission::getRoleId, roleId)
);
return rolePermissions.stream()
.map(RolePermission::getPermissionId)
.toList();
}
/**
* 启用/禁用角色
*/
@Transactional(rollbackFor = Exception.class)
public boolean toggleRoleStatus(Long id, Integer status) {
Role role = getRoleById(id);
if (role == null) {
throw new BusinessException("角色不存在");
}
// 内置角色不允许禁用
if (role.getBuiltIn() == 1 && status == 0) {
throw new BusinessException("内置角色不允许禁用");
}
Role updateRole = new Role();
updateRole.setId(id);
updateRole.setStatus(status);
return roleMapper.updateById(updateRole) > 0;
}
}