refactor(SysRolePermissionServiceImpl.java): 权限保存添加数据是否改动判断

This commit is contained in:
郝先瑞 2022-02-21 23:16:50 +08:00
parent e39a7ce180
commit 0f99c07c8f
2 changed files with 20 additions and 21 deletions

View File

@ -1,13 +1,13 @@
package com.youlai.admin.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.admin.mapper.SysRolePermissionMapper;
import com.youlai.admin.pojo.form.RolePermsForm;
import com.youlai.admin.pojo.entity.SysRolePermission;
import com.youlai.admin.pojo.form.RolePermsForm;
import com.youlai.admin.service.ISysRolePermissionService;
import com.youlai.common.web.exception.BizException;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@ -47,20 +47,24 @@ public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionM
List<Long> oldPermIds = this.listPermIds(menuId, roleId);
// 验证权限数据是否改变
List<Long> sortedPermIds = permIds.stream().sorted().collect(Collectors.toList());
List<Long> sortedOldPermIds = oldPermIds.stream().sorted().collect(Collectors.toList());
boolean permDataChangeFlag = !CollectionUtil.isEqualList(sortedPermIds, sortedOldPermIds);
Assert.isTrue(permDataChangeFlag, "提交失败,权限数据无改动!");
// 删除此次保存移除的权限
boolean isDel = false;
boolean updateFlag = false;
if (CollectionUtil.isNotEmpty(oldPermIds)) {
List<Long> removePermIds = oldPermIds.stream().filter(id -> !permIds.contains(id)).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(removePermIds)) {
this.remove(new LambdaQueryWrapper<SysRolePermission>()
updateFlag = this.remove(new LambdaQueryWrapper<SysRolePermission>()
.eq(SysRolePermission::getRoleId, roleId)
.in(SysRolePermission::getPermissionId, removePermIds));
isDel = true;
}
}
// 新增数据库不存在的权限
boolean isAdd = false;
if (CollectionUtil.isNotEmpty(permIds)) {
List<Long> newPermIds = permIds.stream().filter(id -> !oldPermIds.contains(id)).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(newPermIds)) {
@ -69,15 +73,10 @@ public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionM
SysRolePermission rolePerm = new SysRolePermission(roleId, permId);
rolePerms.add(rolePerm);
}
this.saveBatch(rolePerms);
isAdd = true;
updateFlag = this.saveBatch(rolePerms);
}
}
// add chenyanwu 2022年2月20日 11点49分 解决角色管理中权限数据维护的异常问题
if(!isDel&&!isAdd) {
throw new BizException("权限数据未变化!");
}
return true;
return updateFlag;
}

View File

@ -3,12 +3,12 @@ package com.youlai.common.enums;
import lombok.Getter;
/**
* 认证方式枚举
* 认证身份标识枚举
*
* @author <a href="mailto:xianrui0365@163.com">haoxr</a>
* @date 2021/10/4
*/
public enum AuthenticationMethodEnum {
public enum AuthenticationIdentityEnum {
USERNAME("username", "用户名"),
MOBILE("mobile", "手机号"),
@ -20,20 +20,20 @@ public enum AuthenticationMethodEnum {
@Getter
private String label;
AuthenticationMethodEnum(String value, String label) {
AuthenticationIdentityEnum(String value, String label) {
this.value = value;
this.label = label;
}
public static AuthenticationMethodEnum getByValue(String value) {
AuthenticationMethodEnum authenticationMethodEnum = null;
for (AuthenticationMethodEnum item : values()) {
public static AuthenticationIdentityEnum getByValue(String value) {
AuthenticationIdentityEnum authenticationIdentityEnum = null;
for (AuthenticationIdentityEnum item : values()) {
if (item.getValue().equals(value)) {
authenticationMethodEnum = item;
authenticationIdentityEnum = item;
continue;
}
}
return authenticationMethodEnum;
return authenticationIdentityEnum;
}
}