Role业务
一、Role业务概括
- 角色列表分页查询
- 查询角色:对于role对象的获取通过仓库
- 新增角色:验证参数是否符合要求,验证角色名称、编码是否存在,存储role,保存role与permission关系(对于role对象的获取通过工厂或仓库)
- 修改角色:验证参数是否符合要求,验证角色名称、编码是否存在、判断当前修改者与创建者是否一致,根据id列表删除角色与菜单关系,再存储角色与菜单新关系
- 删除角色:判断当前删除者与创建者是否一致,删除角色、角色与菜单的关联、角色与用户的关系
二、分页查询
接口层
/**
* 角色分页查询
*/
@ApiOperation("角色分页查询")
@GetMapping("/list")
@PreAuthorize("hasAuthority('sys:role:list')")
public Result list(@RequestParam Map<String, Object> params) {
Page page = roleQueryService.queryPage(params);
return Result.ok().put(PageConstant.PAGE, page);
}
应用层服务
@Override
public Page queryPage(Map<String, Object> params) {
IPage<SysRoleDO> page = sysRoleMapper.queryList(new Query().getPage(params), params);
return PageAssembler.toPage(page);
}
其中getPage进行分页处理
public IPage<T> getPage(Map<String, Object> params) {
return this.getPage(params, null, false);
}
public IPage<T> getPage(Map<String, Object> params, String defaultOrderField, boolean isAsc) {
//分页参数
long curPage = 1;
long limit = 10;
if (params.get(PageConstant.PAGE) != null) {
curPage = Long.parseLong((String) params.get(PageConstant.PAGE));
}
if (params.get(PageConstant.LIMIT) != null) {
limit = Long.parseLong((String) params.get(PageConstant.LIMIT));
}
//分页对象
Page<T> page = new Page<>(curPage, limit);
//分页参数
params.put(PageConstant.PAGE, page);
//排序字段
//防止SQL注入(因为sidx、order是通过拼接SQL实现排序的,会有SQL注入风险)
String orderField = (String) params.get(PageConstant.ORDER_FIELD);
String order = (String) params.get(PageConstant.ORDER);
//前端字段排序
if (StringUtils.isNotEmpty(orderField) && StringUtils.isNotEmpty(order)) {
if (PageConstant.ASC.equalsIgnoreCase(order)) {
return page.addOrder(OrderItem.asc(orderField));
} else {
return page.addOrder(OrderItem.desc(orderField));
}
}
//没有排序字段,则不排序
if (StringUtils.isBlank(defaultOrderField)) {
return page;
}
//默认排序
if (isAsc) {
page.addOrder(OrderItem.asc(defaultOrderField));
} else {
page.addOrder(OrderItem.desc(defaultOrderField));
}
return page;
}
领域层Manager(仓库的功能扩展)的mapper
<select id="queryList" parameterType="Map" resultType="com.xtoon.cloud.sys.infrastructure.persistence.entity.SysRoleDO">
select r.* from acl_role r
<where>
and r.is_deleted = '0'
<if test="params.roleCode != null and params.roleCode != ''"> and r.role_code = #{params.roleCode} </if>
<if test="params.roleName != null and params.roleName != ''"> and r.role_name = #{params.roleName} </if>
<if test="params.status != null and params.status != ''"> and r.status = #{params.status} </if>
</where>
ORDER BY r.id desc
</select>
三、保存role
接口层:验证输入数据是否符合要求,调用服务
/**
* 保存角色
*/
@ApiOperation("保存角色")
@SysLog("保存角色")
@PostMapping("/save")
@PreAuthorize("hasAuthority('sys:role:save')")
public Result save(@RequestBody RoleCommand roleCommand) {
ValidatorUtils.validateEntity(roleCommand);
roleApplicationService.saveOrUpdate(roleCommand);
return Result.ok();
}
应用层service
@Override
@Transactional(rollbackFor = Exception.class)
public void saveOrUpdate(RoleCommand roleCommand) {
Role role = RoleDTOAssembler.toRole(roleCommand);
RoleCreateSpecification roleCreateSpecification = new RoleCreateSpecification(roleRepository);
roleCreateSpecification.isSatisfiedBy(role);
roleRepository.store(role);
}
邻域层聚合的工厂存储对象方法
@Override
public RoleId store(Role role) {
SysRoleDO sysRoleDO = RoleConverter.fromRole(role);
this.saveOrUpdate(sysRoleDO);
String roleId = sysRoleDO.getId();
//先删除角色与菜单关系
List<String> roleIds = new ArrayList<>();
roleIds.add(roleId);
sysRolePermissionMapper.deleteByRoleIds(roleIds);
List<PermissionId> permissionIds = role.getPermissionIds();
if (permissionIds != null && !permissionIds.isEmpty()) {
//保存角色与菜单关系
for (PermissionId permissionId : permissionIds) {
SysRolePermissionDO sysRolePermissionDO = new SysRolePermissionDO();
sysRolePermissionDO.setPermissionId(permissionId.getId());
sysRolePermissionDO.setRoleId(roleId);
sysRolePermissionMapper.insert(sysRolePermissionDO);
}
}
return new RoleId(sysRoleDO.getId());
}
领域层Manager(仓库的功能扩展)的mapper
<delete id="deleteByRoleIds">
delete from acl_role_permission where role_id in
<foreach item="roleId" collection="list" open="(" separator="," close=")">
#{roleId}
</foreach>
</delete>
修改与删除类似
转载自:https://juejin.cn/post/7169495789721370655