Skip to content

Commit c813f2e

Browse files
committed
refactor(system): 重构系统模块的唯一性校验逻辑
- 将原有的 isExists 方法改为 checkRepeat 方法,提高代码可读性 - 优化了错误信息的提示,使用具体的字段名称 - 移除了冗余的代码,简化了逻辑结构 - 统一了异常处理的方式,使用 CheckUtils 抛出异常
1 parent 33d8943 commit c813f2e

File tree

7 files changed

+81
-89
lines changed

7 files changed

+81
-89
lines changed

continew-system/src/main/java/top/continew/admin/system/service/impl/DeptServiceImpl.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,13 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptRes
6161

6262
@Override
6363
public void beforeCreate(DeptReq req) {
64-
String name = req.getName();
65-
boolean isExists = this.isNameExists(name, req.getParentId(), null);
66-
CheckUtils.throwIf(isExists, "新增失败,[{}] 已存在", name);
64+
this.checkNameRepeat(req.getName(), req.getParentId(), null);
6765
req.setAncestors(this.getAncestors(req.getParentId()));
6866
}
6967

7068
@Override
7169
public void beforeUpdate(DeptReq req, Long id) {
72-
String name = req.getName();
73-
boolean isExists = this.isNameExists(name, req.getParentId(), id);
74-
CheckUtils.throwIf(isExists, "修改失败,[{}] 已存在", name);
70+
this.checkNameRepeat(req.getName(), req.getParentId(), id);
7571
DeptDO oldDept = super.getById(id);
7672
String oldName = oldDept.getName();
7773
DisEnableStatusEnum newStatus = req.getStatus();
@@ -140,19 +136,18 @@ public int countByNames(List<String> deptNames) {
140136
}
141137

142138
/**
143-
* 名称是否存在
139+
* 检查名称是否重复
144140
*
145141
* @param name 名称
146142
* @param parentId 上级 ID
147143
* @param id ID
148-
* @return 是否存在
149144
*/
150-
private boolean isNameExists(String name, Long parentId, Long id) {
151-
return baseMapper.lambdaQuery()
145+
private void checkNameRepeat(String name, Long parentId, Long id) {
146+
CheckUtils.throwIf(baseMapper.lambdaQuery()
152147
.eq(DeptDO::getName, name)
153148
.eq(DeptDO::getParentId, parentId)
154149
.ne(id != null, DeptDO::getId, id)
155-
.exists();
150+
.exists(), "名称为 [{}] 的部门已存在", name);
156151
}
157152

158153
/**

continew-system/src/main/java/top/continew/admin/system/service/impl/DictItemServiceImpl.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,13 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
5858

5959
@Override
6060
public void beforeCreate(DictItemReq req) {
61-
String value = req.getValue();
62-
CheckUtils.throwIf(this.isValueExists(value, null, req.getDictId()), "新增失败,字典值 [{}] 已存在", value);
61+
this.checkValueRepeat(req.getValue(), null, req.getDictId());
6362
RedisUtils.deleteByPattern(CacheConstants.DICT_KEY_PREFIX + StringConstants.ASTERISK);
6463
}
6564

6665
@Override
6766
public void beforeUpdate(DictItemReq req, Long id) {
68-
String value = req.getValue();
69-
CheckUtils.throwIf(this.isValueExists(value, id, req.getDictId()), "修改失败,字典值 [{}] 已存在", value);
67+
this.checkValueRepeat(req.getValue(), id, req.getDictId());
7068
RedisUtils.deleteByPattern(CacheConstants.DICT_KEY_PREFIX + StringConstants.ASTERISK);
7169
}
7270

@@ -91,19 +89,18 @@ public List<String> listEnumDictNames() {
9189
}
9290

9391
/**
94-
* 字典值是否存在
92+
* 检查字典值是否重复
9593
*
9694
* @param value 字典值
9795
* @param id ID
9896
* @param dictId 字典 ID
99-
* @return 是否存在
10097
*/
101-
private boolean isValueExists(String value, Long id, Long dictId) {
102-
return baseMapper.lambdaQuery()
98+
private void checkValueRepeat(String value, Long id, Long dictId) {
99+
CheckUtils.throwIf(baseMapper.lambdaQuery()
103100
.eq(DictItemDO::getValue, value)
104101
.eq(DictItemDO::getDictId, dictId)
105102
.ne(id != null, DictItemDO::getId, id)
106-
.exists();
103+
.exists(), "字典值为 [{}] 的字典项已存在", value);
107104
}
108105

109106
/**

continew-system/src/main/java/top/continew/admin/system/service/impl/DictServiceImpl.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,13 @@ public class DictServiceImpl extends BaseServiceImpl<DictMapper, DictDO, DictRes
4747

4848
@Override
4949
public void beforeCreate(DictReq req) {
50-
String name = req.getName();
51-
CheckUtils.throwIf(this.isNameExists(name, null), "新增失败,[{}] 已存在", name);
52-
String code = req.getCode();
53-
CheckUtils.throwIf(this.isCodeExists(code, null), "新增失败,[{}] 已存在", code);
50+
this.checkNameRepeat(req.getName(), null);
51+
this.checkCodeRepeat(req.getCode(), null);
5452
}
5553

5654
@Override
5755
public void beforeUpdate(DictReq req, Long id) {
58-
String name = req.getName();
59-
CheckUtils.throwIf(this.isNameExists(name, id), "修改失败,[{}] 已存在", name);
56+
this.checkNameRepeat(req.getName(), id);
6057
DictDO oldDict = super.getById(id);
6158
CheckUtils.throwIfNotEqual(req.getCode(), oldDict.getCode(), "不允许修改字典编码");
6259
}
@@ -80,24 +77,28 @@ public List<LabelValueResp> listEnumDict() {
8077
}
8178

8279
/**
83-
* 名称是否存在
80+
* 检查名称是否重复
8481
*
8582
* @param name 名称
8683
* @param id ID
87-
* @return 是否存在
8884
*/
89-
private boolean isNameExists(String name, Long id) {
90-
return baseMapper.lambdaQuery().eq(DictDO::getName, name).ne(id != null, DictDO::getId, id).exists();
85+
private void checkNameRepeat(String name, Long id) {
86+
CheckUtils.throwIf(baseMapper.lambdaQuery()
87+
.eq(DictDO::getName, name)
88+
.ne(id != null, DictDO::getId, id)
89+
.exists(), "名称为 [{}] 的字典已存在", name);
9190
}
9291

9392
/**
94-
* 编码是否存在
93+
* 检查编码是否重复
9594
*
9695
* @param code 编码
9796
* @param id ID
98-
* @return 是否存在
9997
*/
100-
private boolean isCodeExists(String code, Long id) {
101-
return baseMapper.lambdaQuery().eq(DictDO::getCode, code).ne(id != null, DictDO::getId, id).exists();
98+
private void checkCodeRepeat(String code, Long id) {
99+
CheckUtils.throwIf(baseMapper.lambdaQuery()
100+
.eq(DictDO::getCode, code)
101+
.ne(id != null, DictDO::getId, id)
102+
.exists(), "编码为 [{}] 的字典已存在", code);
102103
}
103104
}

continew-system/src/main/java/top/continew/admin/system/service/impl/MenuServiceImpl.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@ public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDO, MenuRes
6262

6363
@Override
6464
public Long create(MenuReq req) {
65-
String title = req.getTitle();
66-
CheckUtils.throwIf(this.isTitleExists(title, req.getParentId(), null), "新增失败,标题 [{}] 已存在", title);
65+
this.checkTitleRepeat(req.getTitle(), req.getParentId(), null);
6766
// 目录和菜单的组件名称不能重复
6867
if (!MenuTypeEnum.BUTTON.equals(req.getType())) {
69-
String name = req.getName();
70-
CheckUtils.throwIf(this.isNameExists(name, null), "新增失败,组件名称 [{}] 已存在", name);
68+
this.checkNameRepeat(req.getName(), null);
7169
}
7270
// 目录类型菜单,默认为 Layout
7371
if (MenuTypeEnum.DIR.equals(req.getType())) {
@@ -79,12 +77,10 @@ public Long create(MenuReq req) {
7977

8078
@Override
8179
public void update(MenuReq req, Long id) {
82-
String title = req.getTitle();
83-
CheckUtils.throwIf(this.isTitleExists(title, req.getParentId(), id), "修改失败,标题 [{}] 已存在", title);
80+
this.checkTitleRepeat(req.getTitle(), req.getParentId(), id);
8481
// 目录和菜单的组件名称不能重复
8582
if (!MenuTypeEnum.BUTTON.equals(req.getType())) {
86-
String name = req.getName();
87-
CheckUtils.throwIf(this.isNameExists(name, id), "修改失败,组件名称 [{}] 已存在", name);
83+
this.checkNameRepeat(req.getName(), id);
8884
}
8985
MenuDO oldMenu = super.getById(id);
9086
CheckUtils.throwIfNotEqual(req.getType(), oldMenu.getType(), "不允许修改菜单类型");
@@ -168,33 +164,31 @@ public void addTenantMenu(MenuDO menu, MenuDO parentMenu) {
168164
}
169165

170166
/**
171-
* 标题是否存在
167+
* 检查标题是否重复
172168
*
173169
* @param title 标题
174170
* @param parentId 上级 ID
175171
* @param id ID
176-
* @return true:存在;false:不存在
177172
*/
178-
private boolean isTitleExists(String title, Long parentId, Long id) {
179-
return baseMapper.lambdaQuery()
173+
private void checkTitleRepeat(String title, Long parentId, Long id) {
174+
CheckUtils.throwIf(baseMapper.lambdaQuery()
180175
.eq(MenuDO::getTitle, title)
181176
.eq(MenuDO::getParentId, parentId)
182177
.ne(id != null, MenuDO::getId, id)
183-
.exists();
178+
.exists(), "标题为 [{}] 的菜单已存在", title);
184179
}
185180

186181
/**
187-
* 名称是否存在
182+
* 检查组件名称是否重复
188183
*
189-
* @param name 标题
184+
* @param name 组件名称
190185
* @param id ID
191-
* @return true:存在;false:不存在
192186
*/
193-
private boolean isNameExists(String name, Long id) {
194-
return baseMapper.lambdaQuery()
187+
private void checkNameRepeat(String name, Long id) {
188+
CheckUtils.throwIf(baseMapper.lambdaQuery()
195189
.eq(MenuDO::getName, name)
196190
.ne(MenuDO::getType, MenuTypeEnum.BUTTON)
197191
.ne(id != null, MenuDO::getId, id)
198-
.exists();
192+
.exists(), "组件名称为 [{}] 的菜单已存在", name);
199193
}
200194
}

continew-system/src/main/java/top/continew/admin/system/service/impl/RoleServiceImpl.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,11 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleRes
6565
@Override
6666
@Transactional(rollbackFor = Exception.class)
6767
public Long create(RoleReq req) {
68-
String name = req.getName();
69-
CheckUtils.throwIf(this.isNameExists(name, null), "新增失败,[{}] 已存在", name);
68+
this.checkNameRepeat(req.getName(), null);
7069
String code = req.getCode();
71-
CheckUtils.throwIf(this.isCodeExists(code, null), "新增失败,[{}] 已存在", code);
70+
this.checkCodeRepeat(code, null);
7271
// 防止租户添加超管
73-
CheckUtils.throwIf(SysConstants.SUPER_ROLE_CODE.equals(code), "新增失败,编码 [{}] 禁止使用", code);
72+
CheckUtils.throwIfEqual(SysConstants.SUPER_ROLE_CODE, code, "编码 [{}] 禁止使用", code);
7473
// 新增信息
7574
Long roleId = super.create(req);
7675
// 保存角色和部门关联
@@ -81,8 +80,7 @@ public Long create(RoleReq req) {
8180
@Override
8281
@Transactional(rollbackFor = Exception.class)
8382
public void update(RoleReq req, Long id) {
84-
String name = req.getName();
85-
CheckUtils.throwIf(this.isNameExists(name, id), "修改失败,[{}] 已存在", name);
83+
this.checkNameRepeat(req.getName(), id);
8684
RoleDO oldRole = super.getById(id);
8785
CheckUtils.throwIfNotEqual(req.getCode(), oldRole.getCode(), "角色编码不允许修改", oldRole.getName());
8886
DataScopeEnum oldDataScope = oldRole.getDataScope();
@@ -210,25 +208,29 @@ public int countByNames(List<String> roleNames) {
210208
}
211209

212210
/**
213-
* 名称是否存在
211+
* 检查名称是否重复
214212
*
215213
* @param name 名称
216214
* @param id ID
217-
* @return 是否存在
218215
*/
219-
private boolean isNameExists(String name, Long id) {
220-
return baseMapper.lambdaQuery().eq(RoleDO::getName, name).ne(id != null, RoleDO::getId, id).exists();
216+
private void checkNameRepeat(String name, Long id) {
217+
CheckUtils.throwIf(baseMapper.lambdaQuery()
218+
.eq(RoleDO::getName, name)
219+
.ne(id != null, RoleDO::getId, id)
220+
.exists(), "名称为 [{}] 的角色已存在", name);
221221
}
222222

223223
/**
224-
* 编码是否存在
224+
* 检查编码是否重复
225225
*
226226
* @param code 编码
227227
* @param id ID
228-
* @return 是否存在
229228
*/
230-
private boolean isCodeExists(String code, Long id) {
231-
return baseMapper.lambdaQuery().eq(RoleDO::getCode, code).ne(id != null, RoleDO::getId, id).exists();
229+
private void checkCodeRepeat(String code, Long id) {
230+
CheckUtils.throwIf(baseMapper.lambdaQuery()
231+
.eq(RoleDO::getCode, code)
232+
.ne(id != null, RoleDO::getId, id)
233+
.exists(), "编码为 [{}] 的角色已存在", code);
232234
}
233235

234236
/**

continew-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ public void beforeCreate(StorageReq req) {
7575
storageType.validate(req);
7676
storageType.pretreatment(req);
7777
// 校验存储编码
78-
String code = req.getCode();
79-
CheckUtils.throwIf(this.isCodeExists(code, null), "新增失败,[{}] 已存在", code);
78+
this.checkCodeRepeat(req.getCode(), null);
8079
// 需要独立操作来指定默认存储
8180
req.setIsDefault(false);
8281
// 加载存储引擎
@@ -245,13 +244,15 @@ private String decryptSecretKey(String encryptSecretKey, StorageDO oldStorage) {
245244
}
246245

247246
/**
248-
* 编码是否存在
247+
* 检查编码是否重复
249248
*
250249
* @param code 编码
251250
* @param id ID
252-
* @return 是否存在
253251
*/
254-
private boolean isCodeExists(String code, Long id) {
255-
return baseMapper.lambdaQuery().eq(StorageDO::getCode, code).ne(id != null, StorageDO::getId, id).exists();
252+
private void checkCodeRepeat(String code, Long id) {
253+
CheckUtils.throwIf(baseMapper.lambdaQuery()
254+
.eq(StorageDO::getCode, code)
255+
.ne(id != null, StorageDO::getId, id)
256+
.exists(), "编码为 [{}] 的存储配置已存在", code);
256257
}
257258
}

continew-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ public PageResp<UserResp> page(UserQuery query, PageQuery pageQuery) {
138138

139139
@Override
140140
public void beforeCreate(UserReq req) {
141-
final String errorMsgTemplate = "新增失败,[{}] 已存在";
142-
String username = req.getUsername();
143-
CheckUtils.throwIf(this.isNameExists(username, null), errorMsgTemplate, username);
141+
this.checkUsernameRepeat(req.getUsername(), null);
144142
String email = req.getEmail();
145-
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, null), errorMsgTemplate, email);
143+
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, null), "邮箱为 [%s] 的用户已存在"
144+
.formatted(email));
146145
String phone = req.getPhone();
147-
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, null), errorMsgTemplate, phone);
146+
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, null), "手机号为 [%s] 的用户已存在"
147+
.formatted(phone));
148148
}
149149

150150
@Override
@@ -159,13 +159,13 @@ public void afterCreate(UserReq req, UserDO user) {
159159
@Transactional(rollbackFor = Exception.class)
160160
@CacheUpdate(key = "#id", value = "#req.nickname", name = CacheConstants.USER_KEY_PREFIX)
161161
public void update(UserReq req, Long id) {
162-
final String errorMsgTemplate = "修改失败,[{}] 已存在";
163-
String username = req.getUsername();
164-
CheckUtils.throwIf(this.isNameExists(username, id), errorMsgTemplate, username);
162+
this.checkUsernameRepeat(req.getUsername(), id);
165163
String email = req.getEmail();
166-
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, id), errorMsgTemplate, email);
164+
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, id), "邮箱为 [%s] 的用户已存在"
165+
.formatted(email));
167166
String phone = req.getPhone();
168-
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, id), errorMsgTemplate, phone);
167+
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, id), "手机号为 [%s] 的用户已存在"
168+
.formatted(phone));
169169
DisEnableStatusEnum newStatus = req.getStatus();
170170
CheckUtils.throwIf(DisEnableStatusEnum.DISABLE.equals(newStatus) && ObjectUtil.equal(id, UserContextHolder
171171
.getUserId()), "不允许禁用当前用户");
@@ -670,14 +670,16 @@ private int checkPassword(String password, UserDO user) {
670670
}
671671

672672
/**
673-
* 名称是否存在
673+
* 检查用户名是否重复
674674
*
675-
* @param name 名称
676-
* @param id ID
677-
* @return 是否存在
675+
* @param username 用户名
676+
* @param id ID
678677
*/
679-
private boolean isNameExists(String name, Long id) {
680-
return baseMapper.lambdaQuery().eq(UserDO::getUsername, name).ne(id != null, UserDO::getId, id).exists();
678+
private void checkUsernameRepeat(String username, Long id) {
679+
CheckUtils.throwIf(baseMapper.lambdaQuery()
680+
.eq(UserDO::getUsername, username)
681+
.ne(id != null, UserDO::getId, id)
682+
.exists(), "用户名为 [{}] 的用户已存在", username);
681683
}
682684

683685
/**

0 commit comments

Comments
 (0)