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

View File

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