@@ -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