| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | import javax.validation.Validator; |
| | | |
| | | import com.ruoyi.common.basic.PageInfo; |
| | | import com.ruoyi.system.query.SysUserQuery; |
| | | import com.ruoyi.system.vo.SysUserVO; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.ruoyi.common.annotation.DataScope; |
| | | import com.ruoyi.common.basic.PageInfo; |
| | | import com.ruoyi.common.constant.UserConstants; |
| | | import com.ruoyi.common.core.domain.entity.SysRole; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | |
| | | import com.ruoyi.system.mapper.SysUserMapper; |
| | | import com.ruoyi.system.mapper.SysUserPostMapper; |
| | | import com.ruoyi.system.mapper.SysUserRoleMapper; |
| | | import com.ruoyi.system.mapper.TDeptMapper; |
| | | import com.ruoyi.system.mapper.TDeptToUserMapper; |
| | | import com.ruoyi.system.model.TDept; |
| | | import com.ruoyi.system.model.TDeptToUser; |
| | | import com.ruoyi.system.query.SysUserQuery; |
| | | import com.ruoyi.system.service.ISysConfigService; |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | import com.ruoyi.system.service.TDeptToUserService; |
| | | import com.ruoyi.system.vo.SysUserVO; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import javax.validation.Validator; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 用户 业务层处理 |
| | |
| | | |
| | | @Autowired |
| | | private SysUserPostMapper userPostMapper; |
| | | @Autowired |
| | | private TDeptToUserService deptToUserService; |
| | | @Autowired |
| | | private TDeptToUserMapper deptToUserMapper; |
| | | |
| | | @Autowired |
| | | private ISysConfigService configService; |
| | | @Autowired |
| | | private TDeptMapper deptMapper; |
| | | |
| | | @Autowired |
| | | protected Validator validator; |
| | |
| | | // 新增用户信息 |
| | | int rows = userMapper.insertUser(user); |
| | | // 新增用户岗位关联 |
| | | // insertUserPost(user); |
| | | insertUserDept(user); |
| | | // 新增用户与角色管理 |
| | | insertUserRoleId(user); |
| | | return rows; |
| | |
| | | userRoleMapper.deleteUserRoleByUserId(userId); |
| | | // 新增用户与角色管理 |
| | | insertUserRoleId(user); |
| | | // 删除用户与岗位关联 |
| | | // userPostMapper.deleteUserPostByUserId(userId); |
| | | // 新增用户与岗位管理 |
| | | // insertUserPost(user); |
| | | // 删除用户与部门关联 |
| | | deptToUserMapper.deleteUserDeptByUserId(userId); |
| | | // 新增用户与部门管理 |
| | | insertUserDept(user); |
| | | return userMapper.updateUser(user); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 新增用户角色信息 |
| | | * |
| | | * @param user 用户对象 |
| | | */ |
| | | public void insertUserDept(SysUser user) |
| | | { |
| | | this.insertUserDept(user.getUserId(), user.getDeptIds()); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 新增用户岗位信息 |
| | | * |
| | | * @param user 用户对象 |
| | |
| | | ur.setUserId(userId); |
| | | ur.setRoleId(roleId); |
| | | userRoleMapper.insertUserRole(ur); |
| | | } |
| | | } |
| | | /** |
| | | * 新增用户部门信息 |
| | | * |
| | | * @param userId 用户ID |
| | | * @param deptIds 部门id集合 |
| | | */ |
| | | public void insertUserDept(Long userId, List<String> deptIds) |
| | | { |
| | | if (Objects.nonNull(userId) && !CollectionUtils.isEmpty(deptIds)){ |
| | | List<TDeptToUser> deptToUserList = new ArrayList<>(); |
| | | for (String deptId : deptIds) { |
| | | // 新增用户与角色管理 |
| | | TDeptToUser deptToUser = new TDeptToUser(); |
| | | deptToUser.setUserId(userId); |
| | | deptToUser.setDeptId(deptId); |
| | | deptToUserList.add(deptToUser); |
| | | } |
| | | deptToUserService.saveBatch(deptToUserList); |
| | | } |
| | | } |
| | | |
| | |
| | | @Override |
| | | public PageInfo<SysUserVO> pageList(SysUserQuery query) { |
| | | PageInfo<SysUserVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize()); |
| | | String businessDeptId = SecurityUtils.getBusinessDeptId(); |
| | | query.setBusinessDeptId(businessDeptId); |
| | | List<SysUserVO> list = userMapper.pageList(query,pageInfo); |
| | | if(CollectionUtils.isEmpty(list)){ |
| | | return pageInfo; |
| | | } |
| | | List<Long> userIds = list.stream().map(SysUserVO::getUserId).collect(Collectors.toList()); |
| | | // 查询所有部门 |
| | | List<TDept> depts = deptMapper.selectList(Wrappers.lambdaQuery(TDept.class)); |
| | | List<TDeptToUser> tDeptToUsers = deptToUserService.list(Wrappers.lambdaQuery(TDeptToUser.class) |
| | | .in(TDeptToUser::getUserId, userIds)); |
| | | for (SysUserVO sysUserVO : list) { |
| | | tDeptToUsers.stream().filter(tDeptToUser -> tDeptToUser.getUserId().equals(sysUserVO.getUserId())).forEach(tDeptToUser -> { |
| | | sysUserVO.setDeptList(depts.stream().filter(tDept -> tDept.getId().equals(tDeptToUser.getDeptId())).map(TDept::getDeptName).collect(Collectors.toList())); |
| | | sysUserVO.setDeptIds(depts.stream().map(TDept::getId).filter(id -> id.equals(tDeptToUser.getDeptId())).collect(Collectors.toList())); |
| | | }); |
| | | } |
| | | pageInfo.setRecords(list); |
| | | return pageInfo; |
| | | } |