fix(Bug修复): 在后台管理给专区分配角色,接口报错

原因:由于编辑组织可选角色时虚拟角色默认不可见,导致前端提交可选角色时不会携带虚拟角色,但此组织可能存在虚拟角色用户,导致删除改虚拟角色失败
解决方案:虚拟角色不删除
This commit is contained in:
OTTO 2024-09-09 14:39:15 +08:00
parent a604507f12
commit 9a7a4f5b26
4 changed files with 70 additions and 41 deletions

View File

@ -1,8 +1,10 @@
package com.microservices.system.domain;
import com.microservices.common.core.web.domain.BaseEntity;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.microservices.system.api.domain.SysDept;
import com.microservices.system.api.domain.SysRole;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* 部门下可选角色对象 sys_dept_role
@ -10,6 +12,7 @@ import org.apache.commons.lang3.builder.ToStringStyle;
* @author otto
* @date 2023-04-12
*/
@Data
public class SysDeptRole extends BaseEntity {
public SysDeptRole(Long deptId, Long roleId) {
this.deptId = deptId;
@ -27,28 +30,15 @@ public class SysDeptRole extends BaseEntity {
* 角色ID
*/
private Long roleId;
/**
* 部门对象
*/
@ApiModelProperty(value = "部门对象", hidden = true)
private SysDept sysDept;
public void setDeptId(Long deptId) {
this.deptId = deptId;
}
public Long getDeptId() {
return deptId;
}
public void setRoleId(Long roleId) {
this.roleId = roleId;
}
public Long getRoleId() {
return roleId;
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
.append("deptId", getDeptId())
.append("roleId", getRoleId())
.toString();
}
/**
* 角色对象
*/
@ApiModelProperty(value = "角色对象", hidden = true)
private SysRole sysRole;
}

View File

@ -30,7 +30,7 @@ public interface ISysDeptRoleService {
* @param roleIds 角色主键列表
* @return 结果
*/
int addSysRoleListForSysDept(Long deptId, Long[] roleIds);
boolean addSysRoleListForSysDept(Long deptId, Long[] roleIds);
/**
* 批量删除部门下可选角色

View File

@ -186,8 +186,7 @@ public class SysDeptRoleServiceImpl implements ISysDeptRoleService {
@Override
@Transactional(rollbackFor = Exception.class)
public int addSysRoleListForSysDept(Long deptId, Long[] roleIds) {
int success = 0;
public boolean addSysRoleListForSysDept(Long deptId, Long[] roleIds) {
selectSysDeptById(deptId);
List<Long> newRoleIdList = Arrays.asList(roleIds);
List<SysDeptRole> oldDeptRoleList = sysDeptRoleMapper.selectSysDeptRoleListByDeptId(deptId, null);
@ -197,6 +196,13 @@ public class SysDeptRoleServiceImpl implements ISysDeptRoleService {
if (oldDeptRoleList != null && !oldDeptRoleList.isEmpty()) {
List<Long> oldRoleIdList = oldDeptRoleList
.stream()
.filter(x -> {
SysRole sysRole = x.getSysRole();
if (sysRole != null) {
return !SystemRole.isVirtual(sysRole.getRoleKey());
}
return false;
})
.map(SysDeptRole::getRoleId)
.collect(Collectors.toList());
@ -206,7 +212,7 @@ public class SysDeptRoleServiceImpl implements ISysDeptRoleService {
List<Long> delRoleIdList = new ArrayList<>(oldRoleIdList);
delRoleIdList.removeAll(newRoleIdList);
if (!delRoleIdList.isEmpty()) {
success += deleteSysRoleListForSysDept(deptId, delRoleIdList.toArray(new Long[0]));
deleteSysRoleListForSysDept(deptId, delRoleIdList.toArray(new Long[0]));
}
}
@ -216,12 +222,10 @@ public class SysDeptRoleServiceImpl implements ISysDeptRoleService {
.selectSysDeptRoleByDeptIdAndRoleId(deptId, roleId);
if (sysDeptRole == null) {
sysDeptRole = new SysDeptRole(deptId, roleId);
success += sysDeptRoleMapper.insertSysDeptRole(sysDeptRole);
} else {
success++;
sysDeptRoleMapper.insertSysDeptRole(sysDeptRole);
}
}
return success;
return true;
}
@Override

View File

@ -7,6 +7,29 @@
<resultMap type="com.microservices.system.domain.SysDeptRole" id="SysDeptRoleResult">
<result property="deptId" column="dept_id"/>
<result property="roleId" column="role_id"/>
<association property="sysDept" column="dept_id" javaType="com.microservices.system.api.domain.SysDept"
resultMap="deptResult"/>
<association property="sysRole" column="role_id" javaType="com.microservices.system.api.domain.SysRole"
resultMap="roleResult"/>
</resultMap>
<resultMap id="deptResult" type="com.microservices.system.api.domain.SysDept">
<id property="deptId" column="dept_id"/>
<result property="parentId" column="parent_id"/>
<result property="deptName" column="dept_name"/>
<result property="ancestors" column="ancestors"/>
<result property="orderNum" column="order_num"/>
<result property="leader" column="leader"/>
<result property="status" column="dept_status"/>
<result property="deptType" column="dept_type"/>
</resultMap>
<resultMap id="roleResult" type="com.microservices.system.api.domain.SysRole">
<id property="roleId" column="role_id"/>
<result property="roleName" column="role_name"/>
<result property="roleKey" column="role_key"/>
<result property="roleSort" column="role_sort"/>
<result property="dataScope" column="data_scope"/>
<result property="status" column="role_status"/>
</resultMap>
<sql id="selectSysDeptRoleVo">
@ -14,12 +37,24 @@
from sys_dept_role
</sql>
<sql id="selectSysDeptRoleAliasVo">
SELECT sdr.dept_id,
sdr.role_id,
sr.role_name
FROM sys_dept_role sdr
LEFT JOIN sys_role sr ON sr.role_id = sdr.role_id
<sql id="selectSysDeptRoleDetailVo">
select sd.dept_id,
sd.parent_id,
sd.ancestors,
sd.dept_name,
sd.order_num,
sd.dept_type,
sd.leader,
sd.status as dept_status,
sr.role_id,
sr.role_name,
sr.role_key,
sr.role_sort,
sr.data_scope,
sr.status as role_status
from sys_dept_role sdr
left join sys_dept sd on sdr.dept_id = sd.dept_id
left join sys_role sr on sr.role_id = sdr.role_id
</sql>
<select id="selectSysDeptRoleList" parameterType="com.microservices.system.domain.SysDeptRole"
@ -32,7 +67,7 @@
where dept_id = #{deptId}
</select>
<select id="selectSysDeptRoleListByDeptId" parameterType="Long" resultMap="SysDeptRoleResult">
<include refid="selectSysDeptRoleAliasVo"/>
<include refid="selectSysDeptRoleDetailVo"/>
where sdr.dept_id = #{deptId}
<if test="roleName != null and roleName != ''">
<choose>
@ -58,7 +93,7 @@
WHERE role_id = #{roleId}
</select>
<select id="selectSysDeptRoleByDeptIdAndRoleName" resultMap="SysDeptRoleResult">
<include refid="selectSysDeptRoleAliasVo"/>
<include refid="selectSysDeptRoleDetailVo"/>
where sdr.dept_id=#{deptId}
and sr.role_name=#{roleName}
</select>