🚀 根据用户分配角色
This commit is contained in:
parent
49273d8055
commit
241edbc2ea
|
@ -4,8 +4,10 @@ package com.atguigu.auth.service.impl;
|
|||
import com.atguigu.auth.mapper.SysRoleMapper;
|
||||
import com.atguigu.auth.mapper.SysUserRoleMapper;
|
||||
import com.atguigu.auth.service.SysRoleService;
|
||||
import com.atguigu.auth.service.SysUserRoleService;
|
||||
import com.atguigu.model.system.RoleByUser;
|
||||
import com.atguigu.model.system.SysRole;
|
||||
import com.atguigu.model.system.SysUserRole;
|
||||
import com.atguigu.vo.system.AssginRoleVo;
|
||||
import com.atguigu.vo.system.SysRoleQueryVo;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
|
@ -14,14 +16,18 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
|
||||
@Autowired
|
||||
private SysUserRoleMapper sysUserRoleMapper;
|
||||
@Autowired
|
||||
private SysUserRoleService sysUserRoleService;
|
||||
|
||||
/**
|
||||
* 角色条件分页查询
|
||||
|
@ -63,8 +69,26 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
*
|
||||
* @param vo 分配条件
|
||||
*/
|
||||
@Transactional
|
||||
@Override
|
||||
public void doAssign(AssginRoleVo vo) {
|
||||
// 删除原有数据
|
||||
LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(SysUserRole::getUserId, vo.getUserId());
|
||||
sysUserRoleService.remove(wrapper);
|
||||
|
||||
// 为用户分配角色
|
||||
ArrayList<SysUserRole> userRoleArrayList = new ArrayList<>();
|
||||
vo.getRoleIdList().forEach(roleId -> {
|
||||
if (!StringUtils.isEmpty(roleId)) {
|
||||
SysUserRole sysUserRole = new SysUserRole();
|
||||
sysUserRole.setUserId(vo.getUserId());
|
||||
sysUserRole.setRoleId(roleId);
|
||||
userRoleArrayList.add(sysUserRole);
|
||||
}
|
||||
});
|
||||
|
||||
// 批量插入用户角色信息
|
||||
sysUserRoleService.saveBatch(userRoleArrayList);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue