mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-08 12:57:13 +08:00
refactor(system): 重构系统模块的唯一性校验逻辑
- 将原有的 isExists 方法改为 checkRepeat 方法,提高代码可读性 - 优化了错误信息的提示,使用具体的字段名称 - 移除了冗余的代码,简化了逻辑结构 - 统一了异常处理的方式,使用 CheckUtils 抛出异常
This commit is contained in:
@@ -61,17 +61,13 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptRes
|
||||
|
||||
@Override
|
||||
public void beforeCreate(DeptReq req) {
|
||||
String name = req.getName();
|
||||
boolean isExists = this.isNameExists(name, req.getParentId(), null);
|
||||
CheckUtils.throwIf(isExists, "新增失败,[{}] 已存在", name);
|
||||
this.checkNameRepeat(req.getName(), req.getParentId(), null);
|
||||
req.setAncestors(this.getAncestors(req.getParentId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeUpdate(DeptReq req, Long id) {
|
||||
String name = req.getName();
|
||||
boolean isExists = this.isNameExists(name, req.getParentId(), id);
|
||||
CheckUtils.throwIf(isExists, "修改失败,[{}] 已存在", name);
|
||||
this.checkNameRepeat(req.getName(), req.getParentId(), id);
|
||||
DeptDO oldDept = super.getById(id);
|
||||
String oldName = oldDept.getName();
|
||||
DisEnableStatusEnum newStatus = req.getStatus();
|
||||
@@ -140,19 +136,18 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptRes
|
||||
}
|
||||
|
||||
/**
|
||||
* 名称是否存在
|
||||
* 检查名称是否重复
|
||||
*
|
||||
* @param name 名称
|
||||
* @param parentId 上级 ID
|
||||
* @param id ID
|
||||
* @return 是否存在
|
||||
*/
|
||||
private boolean isNameExists(String name, Long parentId, Long id) {
|
||||
return baseMapper.lambdaQuery()
|
||||
private void checkNameRepeat(String name, Long parentId, Long id) {
|
||||
CheckUtils.throwIf(baseMapper.lambdaQuery()
|
||||
.eq(DeptDO::getName, name)
|
||||
.eq(DeptDO::getParentId, parentId)
|
||||
.ne(id != null, DeptDO::getId, id)
|
||||
.exists();
|
||||
.exists(), "名称为 [{}] 的部门已存在", name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -58,15 +58,13 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
|
||||
|
||||
@Override
|
||||
public void beforeCreate(DictItemReq req) {
|
||||
String value = req.getValue();
|
||||
CheckUtils.throwIf(this.isValueExists(value, null, req.getDictId()), "新增失败,字典值 [{}] 已存在", value);
|
||||
this.checkValueRepeat(req.getValue(), null, req.getDictId());
|
||||
RedisUtils.deleteByPattern(CacheConstants.DICT_KEY_PREFIX + StringConstants.ASTERISK);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeUpdate(DictItemReq req, Long id) {
|
||||
String value = req.getValue();
|
||||
CheckUtils.throwIf(this.isValueExists(value, id, req.getDictId()), "修改失败,字典值 [{}] 已存在", value);
|
||||
this.checkValueRepeat(req.getValue(), id, req.getDictId());
|
||||
RedisUtils.deleteByPattern(CacheConstants.DICT_KEY_PREFIX + StringConstants.ASTERISK);
|
||||
}
|
||||
|
||||
@@ -91,19 +89,18 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
|
||||
}
|
||||
|
||||
/**
|
||||
* 字典值是否存在
|
||||
* 检查字典值是否重复
|
||||
*
|
||||
* @param value 字典值
|
||||
* @param id ID
|
||||
* @param dictId 字典 ID
|
||||
* @return 是否存在
|
||||
*/
|
||||
private boolean isValueExists(String value, Long id, Long dictId) {
|
||||
return baseMapper.lambdaQuery()
|
||||
private void checkValueRepeat(String value, Long id, Long dictId) {
|
||||
CheckUtils.throwIf(baseMapper.lambdaQuery()
|
||||
.eq(DictItemDO::getValue, value)
|
||||
.eq(DictItemDO::getDictId, dictId)
|
||||
.ne(id != null, DictItemDO::getId, id)
|
||||
.exists();
|
||||
.exists(), "字典值为 [{}] 的字典项已存在", value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -47,16 +47,13 @@ public class DictServiceImpl extends BaseServiceImpl<DictMapper, DictDO, DictRes
|
||||
|
||||
@Override
|
||||
public void beforeCreate(DictReq req) {
|
||||
String name = req.getName();
|
||||
CheckUtils.throwIf(this.isNameExists(name, null), "新增失败,[{}] 已存在", name);
|
||||
String code = req.getCode();
|
||||
CheckUtils.throwIf(this.isCodeExists(code, null), "新增失败,[{}] 已存在", code);
|
||||
this.checkNameRepeat(req.getName(), null);
|
||||
this.checkCodeRepeat(req.getCode(), null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeUpdate(DictReq req, Long id) {
|
||||
String name = req.getName();
|
||||
CheckUtils.throwIf(this.isNameExists(name, id), "修改失败,[{}] 已存在", name);
|
||||
this.checkNameRepeat(req.getName(), id);
|
||||
DictDO oldDict = super.getById(id);
|
||||
CheckUtils.throwIfNotEqual(req.getCode(), oldDict.getCode(), "不允许修改字典编码");
|
||||
}
|
||||
@@ -80,24 +77,28 @@ public class DictServiceImpl extends BaseServiceImpl<DictMapper, DictDO, DictRes
|
||||
}
|
||||
|
||||
/**
|
||||
* 名称是否存在
|
||||
* 检查名称是否重复
|
||||
*
|
||||
* @param name 名称
|
||||
* @param id ID
|
||||
* @return 是否存在
|
||||
*/
|
||||
private boolean isNameExists(String name, Long id) {
|
||||
return baseMapper.lambdaQuery().eq(DictDO::getName, name).ne(id != null, DictDO::getId, id).exists();
|
||||
private void checkNameRepeat(String name, Long id) {
|
||||
CheckUtils.throwIf(baseMapper.lambdaQuery()
|
||||
.eq(DictDO::getName, name)
|
||||
.ne(id != null, DictDO::getId, id)
|
||||
.exists(), "名称为 [{}] 的字典已存在", name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码是否存在
|
||||
* 检查编码是否重复
|
||||
*
|
||||
* @param code 编码
|
||||
* @param id ID
|
||||
* @return 是否存在
|
||||
*/
|
||||
private boolean isCodeExists(String code, Long id) {
|
||||
return baseMapper.lambdaQuery().eq(DictDO::getCode, code).ne(id != null, DictDO::getId, id).exists();
|
||||
private void checkCodeRepeat(String code, Long id) {
|
||||
CheckUtils.throwIf(baseMapper.lambdaQuery()
|
||||
.eq(DictDO::getCode, code)
|
||||
.ne(id != null, DictDO::getId, id)
|
||||
.exists(), "编码为 [{}] 的字典已存在", code);
|
||||
}
|
||||
}
|
@@ -62,12 +62,10 @@ public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDO, MenuRes
|
||||
|
||||
@Override
|
||||
public Long create(MenuReq req) {
|
||||
String title = req.getTitle();
|
||||
CheckUtils.throwIf(this.isTitleExists(title, req.getParentId(), null), "新增失败,标题 [{}] 已存在", title);
|
||||
this.checkTitleRepeat(req.getTitle(), req.getParentId(), null);
|
||||
// 目录和菜单的组件名称不能重复
|
||||
if (!MenuTypeEnum.BUTTON.equals(req.getType())) {
|
||||
String name = req.getName();
|
||||
CheckUtils.throwIf(this.isNameExists(name, null), "新增失败,组件名称 [{}] 已存在", name);
|
||||
this.checkNameRepeat(req.getName(), null);
|
||||
}
|
||||
// 目录类型菜单,默认为 Layout
|
||||
if (MenuTypeEnum.DIR.equals(req.getType())) {
|
||||
@@ -79,12 +77,10 @@ public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDO, MenuRes
|
||||
|
||||
@Override
|
||||
public void update(MenuReq req, Long id) {
|
||||
String title = req.getTitle();
|
||||
CheckUtils.throwIf(this.isTitleExists(title, req.getParentId(), id), "修改失败,标题 [{}] 已存在", title);
|
||||
this.checkTitleRepeat(req.getTitle(), req.getParentId(), id);
|
||||
// 目录和菜单的组件名称不能重复
|
||||
if (!MenuTypeEnum.BUTTON.equals(req.getType())) {
|
||||
String name = req.getName();
|
||||
CheckUtils.throwIf(this.isNameExists(name, id), "修改失败,组件名称 [{}] 已存在", name);
|
||||
this.checkNameRepeat(req.getName(), id);
|
||||
}
|
||||
MenuDO oldMenu = super.getById(id);
|
||||
CheckUtils.throwIfNotEqual(req.getType(), oldMenu.getType(), "不允许修改菜单类型");
|
||||
@@ -168,33 +164,31 @@ public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDO, MenuRes
|
||||
}
|
||||
|
||||
/**
|
||||
* 标题是否存在
|
||||
* 检查标题是否重复
|
||||
*
|
||||
* @param title 标题
|
||||
* @param parentId 上级 ID
|
||||
* @param id ID
|
||||
* @return true:存在;false:不存在
|
||||
*/
|
||||
private boolean isTitleExists(String title, Long parentId, Long id) {
|
||||
return baseMapper.lambdaQuery()
|
||||
private void checkTitleRepeat(String title, Long parentId, Long id) {
|
||||
CheckUtils.throwIf(baseMapper.lambdaQuery()
|
||||
.eq(MenuDO::getTitle, title)
|
||||
.eq(MenuDO::getParentId, parentId)
|
||||
.ne(id != null, MenuDO::getId, id)
|
||||
.exists();
|
||||
.exists(), "标题为 [{}] 的菜单已存在", title);
|
||||
}
|
||||
|
||||
/**
|
||||
* 名称是否存在
|
||||
* 检查组件名称是否重复
|
||||
*
|
||||
* @param name 标题
|
||||
* @param name 组件名称
|
||||
* @param id ID
|
||||
* @return true:存在;false:不存在
|
||||
*/
|
||||
private boolean isNameExists(String name, Long id) {
|
||||
return baseMapper.lambdaQuery()
|
||||
private void checkNameRepeat(String name, Long id) {
|
||||
CheckUtils.throwIf(baseMapper.lambdaQuery()
|
||||
.eq(MenuDO::getName, name)
|
||||
.ne(MenuDO::getType, MenuTypeEnum.BUTTON)
|
||||
.ne(id != null, MenuDO::getId, id)
|
||||
.exists();
|
||||
.exists(), "组件名称为 [{}] 的菜单已存在", name);
|
||||
}
|
||||
}
|
||||
|
@@ -65,12 +65,11 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleRes
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long create(RoleReq req) {
|
||||
String name = req.getName();
|
||||
CheckUtils.throwIf(this.isNameExists(name, null), "新增失败,[{}] 已存在", name);
|
||||
this.checkNameRepeat(req.getName(), null);
|
||||
String code = req.getCode();
|
||||
CheckUtils.throwIf(this.isCodeExists(code, null), "新增失败,[{}] 已存在", code);
|
||||
this.checkCodeRepeat(code, null);
|
||||
// 防止租户添加超管
|
||||
CheckUtils.throwIf(SysConstants.SUPER_ROLE_CODE.equals(code), "新增失败,编码 [{}] 禁止使用", code);
|
||||
CheckUtils.throwIfEqual(SysConstants.SUPER_ROLE_CODE, code, "编码 [{}] 禁止使用", code);
|
||||
// 新增信息
|
||||
Long roleId = super.create(req);
|
||||
// 保存角色和部门关联
|
||||
@@ -81,8 +80,7 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleRes
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(RoleReq req, Long id) {
|
||||
String name = req.getName();
|
||||
CheckUtils.throwIf(this.isNameExists(name, id), "修改失败,[{}] 已存在", name);
|
||||
this.checkNameRepeat(req.getName(), id);
|
||||
RoleDO oldRole = super.getById(id);
|
||||
CheckUtils.throwIfNotEqual(req.getCode(), oldRole.getCode(), "角色编码不允许修改", oldRole.getName());
|
||||
DataScopeEnum oldDataScope = oldRole.getDataScope();
|
||||
@@ -210,25 +208,29 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleRes
|
||||
}
|
||||
|
||||
/**
|
||||
* 名称是否存在
|
||||
* 检查名称是否重复
|
||||
*
|
||||
* @param name 名称
|
||||
* @param id ID
|
||||
* @return 是否存在
|
||||
*/
|
||||
private boolean isNameExists(String name, Long id) {
|
||||
return baseMapper.lambdaQuery().eq(RoleDO::getName, name).ne(id != null, RoleDO::getId, id).exists();
|
||||
private void checkNameRepeat(String name, Long id) {
|
||||
CheckUtils.throwIf(baseMapper.lambdaQuery()
|
||||
.eq(RoleDO::getName, name)
|
||||
.ne(id != null, RoleDO::getId, id)
|
||||
.exists(), "名称为 [{}] 的角色已存在", name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码是否存在
|
||||
* 检查编码是否重复
|
||||
*
|
||||
* @param code 编码
|
||||
* @param id ID
|
||||
* @return 是否存在
|
||||
*/
|
||||
private boolean isCodeExists(String code, Long id) {
|
||||
return baseMapper.lambdaQuery().eq(RoleDO::getCode, code).ne(id != null, RoleDO::getId, id).exists();
|
||||
private void checkCodeRepeat(String code, Long id) {
|
||||
CheckUtils.throwIf(baseMapper.lambdaQuery()
|
||||
.eq(RoleDO::getCode, code)
|
||||
.ne(id != null, RoleDO::getId, id)
|
||||
.exists(), "编码为 [{}] 的角色已存在", code);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -75,8 +75,7 @@ public class StorageServiceImpl extends BaseServiceImpl<StorageMapper, StorageDO
|
||||
storageType.validate(req);
|
||||
storageType.pretreatment(req);
|
||||
// 校验存储编码
|
||||
String code = req.getCode();
|
||||
CheckUtils.throwIf(this.isCodeExists(code, null), "新增失败,[{}] 已存在", code);
|
||||
this.checkCodeRepeat(req.getCode(), null);
|
||||
// 需要独立操作来指定默认存储
|
||||
req.setIsDefault(false);
|
||||
// 加载存储引擎
|
||||
@@ -245,13 +244,15 @@ public class StorageServiceImpl extends BaseServiceImpl<StorageMapper, StorageDO
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码是否存在
|
||||
* 检查编码是否重复
|
||||
*
|
||||
* @param code 编码
|
||||
* @param id ID
|
||||
* @return 是否存在
|
||||
*/
|
||||
private boolean isCodeExists(String code, Long id) {
|
||||
return baseMapper.lambdaQuery().eq(StorageDO::getCode, code).ne(id != null, StorageDO::getId, id).exists();
|
||||
private void checkCodeRepeat(String code, Long id) {
|
||||
CheckUtils.throwIf(baseMapper.lambdaQuery()
|
||||
.eq(StorageDO::getCode, code)
|
||||
.ne(id != null, StorageDO::getId, id)
|
||||
.exists(), "编码为 [{}] 的存储配置已存在", code);
|
||||
}
|
||||
}
|
@@ -138,13 +138,13 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
|
||||
|
||||
@Override
|
||||
public void beforeCreate(UserReq req) {
|
||||
final String errorMsgTemplate = "新增失败,[{}] 已存在";
|
||||
String username = req.getUsername();
|
||||
CheckUtils.throwIf(this.isNameExists(username, null), errorMsgTemplate, username);
|
||||
this.checkUsernameRepeat(req.getUsername(), null);
|
||||
String email = req.getEmail();
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, null), errorMsgTemplate, email);
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, null), "邮箱为 [%s] 的用户已存在"
|
||||
.formatted(email));
|
||||
String phone = req.getPhone();
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, null), errorMsgTemplate, phone);
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, null), "手机号为 [%s] 的用户已存在"
|
||||
.formatted(phone));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -159,13 +159,13 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@CacheUpdate(key = "#id", value = "#req.nickname", name = CacheConstants.USER_KEY_PREFIX)
|
||||
public void update(UserReq req, Long id) {
|
||||
final String errorMsgTemplate = "修改失败,[{}] 已存在";
|
||||
String username = req.getUsername();
|
||||
CheckUtils.throwIf(this.isNameExists(username, id), errorMsgTemplate, username);
|
||||
this.checkUsernameRepeat(req.getUsername(), id);
|
||||
String email = req.getEmail();
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, id), errorMsgTemplate, email);
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, id), "邮箱为 [%s] 的用户已存在"
|
||||
.formatted(email));
|
||||
String phone = req.getPhone();
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, id), errorMsgTemplate, phone);
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, id), "手机号为 [%s] 的用户已存在"
|
||||
.formatted(phone));
|
||||
DisEnableStatusEnum newStatus = req.getStatus();
|
||||
CheckUtils.throwIf(DisEnableStatusEnum.DISABLE.equals(newStatus) && ObjectUtil.equal(id, UserContextHolder
|
||||
.getUserId()), "不允许禁用当前用户");
|
||||
@@ -670,14 +670,16 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
|
||||
}
|
||||
|
||||
/**
|
||||
* 名称是否存在
|
||||
* 检查用户名是否重复
|
||||
*
|
||||
* @param name 名称
|
||||
* @param id ID
|
||||
* @return 是否存在
|
||||
* @param username 用户名
|
||||
* @param id ID
|
||||
*/
|
||||
private boolean isNameExists(String name, Long id) {
|
||||
return baseMapper.lambdaQuery().eq(UserDO::getUsername, name).ne(id != null, UserDO::getId, id).exists();
|
||||
private void checkUsernameRepeat(String username, Long id) {
|
||||
CheckUtils.throwIf(baseMapper.lambdaQuery()
|
||||
.eq(UserDO::getUsername, username)
|
||||
.ne(id != null, UserDO::getId, id)
|
||||
.exists(), "用户名为 [{}] 的用户已存在", username);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user