feat:角色用户关联--设置用户
This commit is contained in:
@@ -6,7 +6,7 @@ import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum UserErrorCode implements BaseErrorCode {
|
||||
public enum Code01UserErrorCode implements BaseErrorCode {
|
||||
|
||||
USER_NOT_EXISTS("A1000101", "用户不存在"),
|
||||
USER_EXISTS("A1000102", "用户已存在"),
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.xiang.xservice.auth.api.code;
|
||||
|
||||
import com.xiang.xservice.basic.exception.code.BaseErrorCode;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2025-08-29 16:27
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum Code02RoleErrorCode implements BaseErrorCode {
|
||||
|
||||
ROLE_NOT_EXISTS("A1000201", "角色不存在"),
|
||||
;
|
||||
|
||||
private final String code;
|
||||
private final String message;
|
||||
}
|
||||
@@ -1,4 +1,23 @@
|
||||
package com.xiang.xservice.auth.api.dto.req.role;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class RoleUserUpdateRequest {
|
||||
|
||||
/**
|
||||
* 用户id集合
|
||||
*/
|
||||
private List<Long> userIds;
|
||||
|
||||
/**
|
||||
* 角色id
|
||||
*/
|
||||
private Long roleId;
|
||||
}
|
||||
|
||||
@@ -84,7 +84,15 @@ public class RoleController {
|
||||
|
||||
@PostMapping("/private/role/setUser")
|
||||
public Result<Boolean> setUserRole(@RequestBody @Valid @NotNull(message = "请求参数不能为空") RoleUserUpdateRequest request) {
|
||||
return Result.success(true);
|
||||
try {
|
||||
return Result.success(roleService.setUserRole(request));
|
||||
} catch (BusinessException e) {
|
||||
log.error("角色用户关联异常,请求:{}", JSON.toJSONString(request), e);
|
||||
return Result.error(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("角色用户关联异常,请求:{}", JSON.toJSONString(request), e);
|
||||
}
|
||||
return Result.error();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.xiang.xservice.auth.service.repository.mapper;
|
||||
|
||||
import com.xiang.xservice.auth.service.entity.XRole;
|
||||
import com.xiang.xservice.auth.service.entity.XUserRole;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
@@ -18,5 +19,5 @@ public interface XRoleMapper {
|
||||
List<XRole> getRoleList(XRole record);
|
||||
XRole getRoleById(Long id);
|
||||
List<XRole> getRoleByIds(@Param("ids") List<Long> ids);
|
||||
|
||||
int addBatch(List<XUserRole> list);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,6 @@ public interface RoleService {
|
||||
|
||||
Boolean delRole(List<Long> ids);
|
||||
|
||||
Boolean setUserDept(RoleUserUpdateRequest request);
|
||||
Boolean setUserRole(RoleUserUpdateRequest request);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.xiang.xservice.auth.service.service.impl;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.xiang.xservice.auth.api.code.Code02RoleErrorCode;
|
||||
import com.xiang.xservice.auth.api.dto.req.role.RoleAddRequest;
|
||||
import com.xiang.xservice.auth.api.dto.req.role.RoleQueryRequest;
|
||||
import com.xiang.xservice.auth.api.dto.req.role.RoleUpdateRequest;
|
||||
@@ -8,18 +9,24 @@ import com.xiang.xservice.auth.api.dto.req.role.RoleUserUpdateRequest;
|
||||
import com.xiang.xservice.auth.api.dto.resp.RoleDTO;
|
||||
import com.xiang.xservice.auth.service.convert.XRoleConvert;
|
||||
import com.xiang.xservice.auth.service.entity.XRole;
|
||||
import com.xiang.xservice.auth.service.entity.XUserRole;
|
||||
import com.xiang.xservice.auth.service.repository.mapper.XRoleMapper;
|
||||
import com.xiang.xservice.auth.service.service.RoleService;
|
||||
import com.xiang.xservice.basic.exception.BusinessException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class XRoleServiceImpl implements RoleService {
|
||||
private final static int BATCH_SIZE = 500;
|
||||
private final XRoleMapper roleMapper;
|
||||
private final XRoleConvert roleConvert;
|
||||
|
||||
@@ -60,7 +67,32 @@ public class XRoleServiceImpl implements RoleService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean setUserDept(RoleUserUpdateRequest request) {
|
||||
return null;
|
||||
public Boolean setUserRole(RoleUserUpdateRequest request) {
|
||||
XRole role = roleMapper.getRoleById(request.getRoleId());
|
||||
if (Objects.isNull(role)) {
|
||||
log.error("查询角色信息不存在!角色id:{}", request.getRoleId());
|
||||
throw new BusinessException(Code02RoleErrorCode.ROLE_NOT_EXISTS);
|
||||
}
|
||||
List<XUserRole> params = Lists.newArrayList();
|
||||
for (Long userId : request.getUserIds()) {
|
||||
XUserRole xUserRole = new XUserRole();
|
||||
xUserRole.setRoleId(request.getRoleId());
|
||||
xUserRole.setUserId(userId);
|
||||
params.add(xUserRole);
|
||||
}
|
||||
Boolean flag = Boolean.TRUE;
|
||||
if (CollectionUtils.isNotEmpty(params)) {
|
||||
if (params.size() > BATCH_SIZE) {
|
||||
List<List<XUserRole>> partition = Lists.partition(params, BATCH_SIZE);
|
||||
for (List<XUserRole> list : partition) {
|
||||
if (roleMapper.addBatch(list) <= 0) {
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return roleMapper.addBatch(params) > 0;
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.xiang.xservice.auth.service.service.impl;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.xiang.xservice.auth.api.code.UserErrorCode;
|
||||
import com.xiang.xservice.auth.api.code.Code01UserErrorCode;
|
||||
import com.xiang.xservice.auth.api.dto.req.LoginRequest;
|
||||
import com.xiang.xservice.auth.api.dto.req.RegisterRequest;
|
||||
import com.xiang.xservice.auth.api.dto.req.user.UserAddRequest;
|
||||
@@ -91,7 +91,7 @@ public class XUserServiceImpl implements XUserService {
|
||||
public UserResp getUserByUsername(String username) {
|
||||
XUser user = userMapper.selectByUsername(username);
|
||||
if (Objects.isNull(user)) {
|
||||
throw new BusinessException(UserErrorCode.USER_NOT_EXISTS);
|
||||
throw new BusinessException(Code01UserErrorCode.USER_NOT_EXISTS);
|
||||
}
|
||||
|
||||
UserResp userResp = new UserResp();
|
||||
@@ -112,7 +112,7 @@ public class XUserServiceImpl implements XUserService {
|
||||
// todo 手机号验证码校验
|
||||
XUser user = userMapper.selectByUsername(request.getUsername());
|
||||
if (Objects.nonNull(user)) {
|
||||
throw new BusinessException(UserErrorCode.USER_EXISTS);
|
||||
throw new BusinessException(Code01UserErrorCode.USER_EXISTS);
|
||||
}
|
||||
user = new XUser();
|
||||
user.setName(request.getName());
|
||||
@@ -190,7 +190,7 @@ public class XUserServiceImpl implements XUserService {
|
||||
XUser user = userMapper.getUserById(request.getUserId());
|
||||
if (Objects.isNull(user)) {
|
||||
log.error("查询用户信息不存在!用户id:{}", request.getUserId());
|
||||
throw new BusinessException(UserErrorCode.USER_NOT_EXISTS);
|
||||
throw new BusinessException(Code01UserErrorCode.USER_NOT_EXISTS);
|
||||
}
|
||||
List<XUserRole> params = Lists.newArrayList();
|
||||
for (Long roleId : request.getRoleIds()) {
|
||||
|
||||
@@ -90,6 +90,12 @@
|
||||
</if>
|
||||
</trim>
|
||||
</insert>
|
||||
<insert id="addBatch">
|
||||
insert into x_role(user_id, role_id) values
|
||||
<foreach collection="list" item="item" separator=",">
|
||||
(#{item.userId}, #{item.roleId})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
<update id="delBatch" >
|
||||
update x_role set del_flag = 0, update_time = #{time}, update_by = #{operator} where id in
|
||||
|
||||
Reference in New Issue
Block a user