feat:(REQ-2227) 角色树增加id,增加角色排序

This commit is contained in:
lilong 2024-04-11 16:34:25 +08:00
parent 4b0f22b912
commit 91cf6fb11c
12 changed files with 285 additions and 9 deletions

View File

@ -10,6 +10,8 @@ import cn.axzo.tyr.client.model.req.QueryRoleByNameReq;
import cn.axzo.tyr.client.model.req.QuerySaasRoleReq; import cn.axzo.tyr.client.model.req.QuerySaasRoleReq;
import cn.axzo.tyr.client.model.req.RoleWithUserQueryReq; import cn.axzo.tyr.client.model.req.RoleWithUserQueryReq;
import cn.axzo.tyr.client.model.req.TreeRoleReq; import cn.axzo.tyr.client.model.req.TreeRoleReq;
import cn.axzo.tyr.client.model.req.UpdateRoleGroupOffsetReq;
import cn.axzo.tyr.client.model.req.UpdateRoleOffsetReq;
import cn.axzo.tyr.client.model.res.FeatureRoleRelationResp; import cn.axzo.tyr.client.model.res.FeatureRoleRelationResp;
import cn.axzo.tyr.client.model.res.IsSuperAdminRes; import cn.axzo.tyr.client.model.res.IsSuperAdminRes;
import cn.axzo.tyr.client.model.res.QueryBatchByIdentityIdTypeRes; import cn.axzo.tyr.client.model.res.QueryBatchByIdentityIdTypeRes;
@ -163,4 +165,13 @@ public interface TyrSaasRoleApi {
@PostMapping("/api/saasRole/tree") @PostMapping("/api/saasRole/tree")
ApiListResult<RoleTreeRes> treeSaasRole(@RequestBody @Valid TreeRoleReq req); ApiListResult<RoleTreeRes> treeSaasRole(@RequestBody @Valid TreeRoleReq req);
/**
* 更新角色的位置
* 向下移动则找到后面位置的分组替换sort
* 向上移动则找到前面位置的分组替换sort
* @param request
* @return
*/
@PostMapping("/api/role/group/update")
ApiResult<Void> updateRoleOffset(@Valid @RequestBody UpdateRoleOffsetReq request);
} }

View File

@ -11,5 +11,7 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor @AllArgsConstructor
public class TreeRoleReq { public class TreeRoleReq {
private String workspaceTypeCode;
private Boolean needRole; private Boolean needRole;
} }

View File

@ -0,0 +1,27 @@
package cn.axzo.tyr.client.model.req;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UpdateRoleOffsetReq {
/**
* 角色id
*/
@NotNull(message = "id不能为空")
private Long id;
/**
* 偏移量:向上移就是负数例如上移一位-1向下移就是正数例如下移一位1
*/
@NotNull(message = "offset不能为空")
private Integer offset;
}

View File

@ -18,6 +18,8 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
public class CommonDictResp { public class CommonDictResp {
private Long id;
/** /**
* 业务域 * 业务域
*/ */

View File

@ -0,0 +1,105 @@
package cn.axzo.tyr.client.model.res;
import cn.axzo.tyr.client.model.vo.SaasPermissionGroupVO;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.List;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SaasRoleRes {
private Long id;
/**
* 角色名称
*/
private String name;
/**
* 角色描述
*/
private String description;
/**
* 角色类型common 普通角色 super_admin超级管理员(禁止删除) admin子管理员(禁止删除) init初始化内置角色
*/
private String roleType;
private Long workspaceId;
/**
* 废弃
*/
@Deprecated
private Integer workspaceType;
private Long ownerOuId;
/**
* 产品单位类型
* 1:总包 2:建设单位 3:监理单位 4:劳务分包 5:专业分包 6:OMS通用 7:企业通用 8:企业内班组 9:项目内班组
*/
private Integer productUnitType;
/**
* 创建者
*/
private Long createBy;
/**
* 更新者
*/
private Long updateBy;
/**
* 来源的模版角色id(1052上线后可删除)
*/
@Deprecated
private Long fromPreRoleId;
/**
* 适用单位类型 1:总包 2:建设单位 4:监理单位 8:劳务分包 16:专业分包 0都可以用 只会挂在最末级
* (1052上线后可删除)
*/
@Deprecated
private Long fitOuTypeBit;
/**
* '角色编码'
*/
private String roleCode;
/**
* '是否显示'
*/
private Boolean isDisplay;
/**
* '是否启用'
*/
private Boolean enabled;
/**
* 排序
*/
private Integer sort;
/**
* 权限组
*/
private List<SaasPermissionGroupVO> permissionGroup;
/**
* 是否删除
*/
private Long isDelete;
private Date createAt;
private Date updateAt;
}

View File

@ -78,6 +78,12 @@ public class SaasRoleVO {
*/ */
private Boolean enabled; private Boolean enabled;
/**
* 排序
*/
private Integer sort;
/** /**
* 获取角色对应所用的菜单不管例外 * 获取角色对应所用的菜单不管例外
* *

View File

@ -1,15 +1,18 @@
package cn.axzo.tyr.client.model.vo; package cn.axzo.tyr.client.model.vo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@Data @Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SaveOrUpdateRoleVO { public class SaveOrUpdateRoleVO {
/** /**
@ -85,6 +88,8 @@ public class SaveOrUpdateRoleVO {
*/ */
private Boolean enabled; private Boolean enabled;
private Integer sort;
@Data @Data
public static class GroupInfoVO { public static class GroupInfoVO {

View File

@ -14,6 +14,7 @@ import cn.axzo.tyr.client.model.req.QuerySaasRoleGroupReq;
import cn.axzo.tyr.client.model.req.QuerySaasRoleReq; import cn.axzo.tyr.client.model.req.QuerySaasRoleReq;
import cn.axzo.tyr.client.model.req.RoleWithUserQueryReq; import cn.axzo.tyr.client.model.req.RoleWithUserQueryReq;
import cn.axzo.tyr.client.model.req.TreeRoleReq; import cn.axzo.tyr.client.model.req.TreeRoleReq;
import cn.axzo.tyr.client.model.req.UpdateRoleOffsetReq;
import cn.axzo.tyr.client.model.res.CommonDictResp; import cn.axzo.tyr.client.model.res.CommonDictResp;
import cn.axzo.tyr.client.model.res.FeatureRoleRelationResp; import cn.axzo.tyr.client.model.res.FeatureRoleRelationResp;
import cn.axzo.tyr.client.model.res.IsSuperAdminRes; import cn.axzo.tyr.client.model.res.IsSuperAdminRes;
@ -35,12 +36,14 @@ import cn.axzo.tyr.server.repository.entity.SaasRoleGroupRelation;
import cn.axzo.tyr.server.service.PermissionCacheService; import cn.axzo.tyr.server.service.PermissionCacheService;
import cn.axzo.tyr.server.service.RoleService; import cn.axzo.tyr.server.service.RoleService;
import cn.axzo.tyr.server.service.SaasCommonDictService; import cn.axzo.tyr.server.service.SaasCommonDictService;
import cn.axzo.tyr.server.service.SaasRoleGroupRelationService;
import cn.axzo.tyr.server.service.SaasRoleGroupService; import cn.axzo.tyr.server.service.SaasRoleGroupService;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -77,6 +80,8 @@ public class SaasRoleController implements TyrSaasRoleApi {
private SaasRoleGroupService saasRoleGroupService; private SaasRoleGroupService saasRoleGroupService;
@Autowired @Autowired
private SaasRoleGroupRelationDao saasRoleGroupRelationDao; private SaasRoleGroupRelationDao saasRoleGroupRelationDao;
@Autowired
private SaasRoleGroupRelationService saasRoleGroupRelationService;
/** /**
* 角色组里面parentId = 0 * 角色组里面parentId = 0
@ -182,10 +187,11 @@ public class SaasRoleController implements TyrSaasRoleApi {
public ApiListResult<RoleTreeRes> treeSaasRole(TreeRoleReq req) { public ApiListResult<RoleTreeRes> treeSaasRole(TreeRoleReq req) {
// 因为根节点在roleGroup里面没有都是workspaceTypeCode描述是放在字典表里 // 因为根节点在roleGroup里面没有都是workspaceTypeCode描述是放在字典表里
List<CommonDictResp> commonDicts = listRootRole(); List<CommonDictResp> commonDicts = listRootRole(req);
List<RoleTreeRes> roots = commonDicts.stream() List<RoleTreeRes> roots = commonDicts.stream()
.map(e -> RoleTreeRes.builder() .map(e -> RoleTreeRes.builder()
.id(e.getId())
.workspaceTypeCode(e.getDictCode()) .workspaceTypeCode(e.getDictCode())
.name(e.getDictValue()) .name(e.getDictValue())
.type("ROOT") .type("ROOT")
@ -200,6 +206,79 @@ public class SaasRoleController implements TyrSaasRoleApi {
return ApiListResult.ok(result); return ApiListResult.ok(result);
} }
@Override
public ApiResult<Void> updateRoleOffset(UpdateRoleOffsetReq request) {
if (request.getOffset() != 1 && request.getOffset() != -1) {
throw new ServiceException("暂时只支持移动一个位置");
}
SaasRole saasRole = roleService.getById(request.getId());
if (saasRole == null) {
throw new ServiceException("角色信息不存在");
}
List<SaasRoleGroupRelation> saasRoleGroupRelations = saasRoleGroupRelationService.list(SaasRoleGroupRelationService.ListSaasRoleGroupRelationParam.builder()
.roleId(Lists.newArrayList(saasRole.getId()))
.build());
if (CollectionUtils.isEmpty(saasRoleGroupRelations)) {
throw new ServiceException("角色分组信息不存在");
}
if (saasRoleGroupRelations.size() != 1) {
throw new ServiceException("角色分组信息不唯一,请检查异常");
}
List<SaasRoleVO> saasRoles = roleService.query(QuerySaasRoleReq.builder()
.sassRoleGroupIds(Lists.transform(saasRoleGroupRelations, SaasRoleGroupRelation::getSaasRoleGroupId))
.build())
.stream()
.sorted(Comparator.comparing(SaasRoleVO::getSort))
.collect(Collectors.toList());
SaasRoleVO exchangeRole = findExchangeRole(request, saasRoles);
if (exchangeRole == null) {
throw new ServiceException("未找到可以移动的位置");
}
roleService.saveOrUpdate(SaveOrUpdateRoleVO.builder()
.id(saasRole.getId())
.sort(exchangeRole.getSort())
.build());
roleService.saveOrUpdate(SaveOrUpdateRoleVO.builder()
.id(exchangeRole.getId())
.sort(saasRole.getSort())
.build());
return ApiResult.ok();
}
/**
* 只支持移动一位
* @param request
* @param roles
* @return
*/
private SaasRoleVO findExchangeRole(UpdateRoleOffsetReq request,
List<SaasRoleVO> roles) {
List<Long> ids = roles.stream()
.map(SaasRoleVO::getId)
.collect(Collectors.toList());
int currentIndex = ids.indexOf(request.getId());
// 下移一位
if (request.getOffset() == 1) {
int exchangeIndex = currentIndex + 1;
if (roles.size() > exchangeIndex) {
return roles.get(exchangeIndex);
}
} else if (request.getOffset() == -1) {
if (currentIndex == 0) {
return null;
}
int exchangeIndex = currentIndex - 1;
return roles.get(exchangeIndex);
}
return null;
}
/** /**
* 查询角色 * 查询角色
* Map<角色分组Id, List<RoleTreeRes>> * Map<角色分组Id, List<RoleTreeRes>>
@ -229,7 +308,10 @@ public class SaasRoleController implements TyrSaasRoleApi {
.stream() .stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue()
.stream() .stream()
.map(value -> toRoleTree(saasRoles.get(value.getRoleId()))) .map(value -> saasRoles.get(value.getRoleId()))
.filter(Objects::nonNull)
.sorted(Comparator.comparing(SaasRole::getSort))
.map(this::toRoleTree)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.collect(Collectors.toList()))); .collect(Collectors.toList())));
} }
@ -303,8 +385,9 @@ public class SaasRoleController implements TyrSaasRoleApi {
childrenRoleGroups.forEach(e -> appendChildren(e, roleGroups, roles)); childrenRoleGroups.forEach(e -> appendChildren(e, roleGroups, roles));
} }
private List<CommonDictResp> listRootRole() { private List<CommonDictResp> listRootRole(TreeRoleReq req) {
return saasCommonDictService.query(CommonDictQueryReq.builder() return saasCommonDictService.query(CommonDictQueryReq.builder()
.codes(StringUtils.isBlank(req.getWorkspaceTypeCode()) ? null : Lists.newArrayList(req.getWorkspaceTypeCode()))
.scope("role") .scope("role")
.build()); .build());
} }

View File

@ -74,7 +74,7 @@ public class SaasRoleGroupController implements SaasRoleGroupApi {
.sorted(Comparator.comparing(SaasRoleGroupVO::getSort)) .sorted(Comparator.comparing(SaasRoleGroupVO::getSort))
.collect(Collectors.toList()); .collect(Collectors.toList());
SaasRoleGroupVO exchangeRoleGroup = findExchangeRoleGroup(request, saasRoleGroup, roleGroupList); SaasRoleGroupVO exchangeRoleGroup = findExchangeRoleGroup(request, roleGroupList);
if (exchangeRoleGroup == null) { if (exchangeRoleGroup == null) {
throw new ServiceException("未找到可以移动的位置"); throw new ServiceException("未找到可以移动的位置");
} }
@ -93,12 +93,10 @@ public class SaasRoleGroupController implements SaasRoleGroupApi {
/** /**
* 只支持移动一位 * 只支持移动一位
* @param request * @param request
* @param saasRoleGroup
* @param roleGroupList * @param roleGroupList
* @return * @return
*/ */
private SaasRoleGroupVO findExchangeRoleGroup(UpdateRoleGroupOffsetReq request, private SaasRoleGroupVO findExchangeRoleGroup(UpdateRoleGroupOffsetReq request,
SaasRoleGroupVO saasRoleGroup,
List<SaasRoleGroupVO> roleGroupList) { List<SaasRoleGroupVO> roleGroupList) {
List<Long> ids = roleGroupList.stream() List<Long> ids = roleGroupList.stream()
.map(SaasRoleGroupVO::getId) .map(SaasRoleGroupVO::getId)

View File

@ -1,6 +1,7 @@
package cn.axzo.tyr.server.repository.entity; package cn.axzo.tyr.server.repository.entity;
import cn.axzo.pokonyan.config.mybatisplus.BaseEntity; import cn.axzo.pokonyan.config.mybatisplus.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
@ -89,6 +90,11 @@ public class SaasRole extends BaseEntity<SaasRole> {
*/ */
private Boolean enabled; private Boolean enabled;
/**
* 排序
*/
private Integer sort;
/** /**
* 获取主键值 * 获取主键值
* *

View File

@ -134,6 +134,12 @@ public interface RoleService extends IService<SaasRole> {
@CriteriaField(field = "id", operator = Operator.IN) @CriteriaField(field = "id", operator = Operator.IN)
private List<Long> roleIds; private List<Long> roleIds;
@CriteriaField(ignore = true)
private Boolean needPermission;
@CriteriaField(ignore = true)
private Boolean needRoleGroup;
} }
@SuperBuilder @SuperBuilder

View File

@ -533,9 +533,34 @@ public class RoleServiceImpl extends ServiceImpl<SaasRoleMapper, SaasRole>
} }
}); });
} }
saasRole.setSort(saveOrUpdateRole.getSort());
assembleSort(saveOrUpdateRole, saasRole);
return saasRole; return saasRole;
} }
private void assembleSort(SaveOrUpdateRoleVO saveOrUpdateRole, SaasRole saasRole) {
if (saasRole.getSort() != null || saasRole.getId() != null || CollectionUtils.isEmpty(saveOrUpdateRole.getGroupTree())) {
return;
}
List<SaasRoleGroupRelation> saasRoleGroupRelations = saasRoleGroupRelationDao.getByGroupIds(Lists.transform(saveOrUpdateRole.getGroupTree(), SaveOrUpdateRoleVO.GroupInfoVO::getId));
if (CollectionUtils.isEmpty(saasRoleGroupRelations)) {
saasRole.setSort(1);
return;
}
Optional<SaasRole> last = this.lambdaQuery()
.eq(SaasRole::getId, Lists.transform(saasRoleGroupRelations, SaasRoleGroupRelation::getRoleId))
.eq(SaasRole::getIsDelete, TableIsDeleteEnum.NORMAL.value)
.orderByDesc(SaasRole::getSort)
.last("limit 1")
.list()
.stream()
.findFirst();
saasRole.setSort(last.map(e -> e.getSort() == null ? 1 : e.getSort() + 1).orElse(1));
}
/** /**
* 产品单位类型 * 产品单位类型
*/ */