import cn.hutool.core.collection.CollUtil;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.dto.system.UserOrgManageDTO;import com.entity.system.SysUser;import com.entity.system.SysUserOrg;import com.enums.DeleteFlagEnum;import com.enums.ExceptionEnums;import com.exception.CustomException;import com.mapper.system.SysUserOrgMapper;import com.service.system.ISysUserOrgService;import com.service.system.ISysUserService;import com.vo.system.UserOrgScopeVO;import lombok.extern.slf4j.Slf4j;import org.apache.commons.collections4.CollectionUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import java.util.*;import java.util.function.Function;import java.util.stream.Collectors;/** * 系统用户组织范围Service实现类 * @author huangweilin * @create 2025-46-10-14:46 */@Slf4j@Servicepublic class SysUserOrgServiceImpl extends ServiceImpl<SysUserOrgMapper, SysUserOrg> implements ISysUserOrgService { @Autowired private ISysUserService sysUserService; @Override @Transactional(rollbackFor = Exception.class) public void saveUserOrgManageScope(UserOrgManageDTO dto) { Long userId = dto.getUserId(); // 校验用户是否存在 validateUserExists(userId); List<Integer> orgIds = extractOrgIdsFromDTO(dto); // 校验组织ID列表不能有重复的 validateOrgIdsNotDuplicate(orgIds); // 查询已配置的组织 List<SysUserOrg> dbUserOrgList = getUserOrgManageScope(userId); if (CollUtil.isEmpty(dbUserOrgList)) { // 如果没有已配置的组织,直接批量插入 List<SysUserOrg> sysUserOrgList = toSysUserOrg(dto); if (CollUtil.isNotEmpty(sysUserOrgList)) { this.saveBatch(sysUserOrgList); } return; } // 比较数据 // 需要删除的id集合 Set<Integer> needDeleteIdList = getNeedDeleteIdList(dbUserOrgList, orgIds); // 需要新增或更新的数据集合 List<SysUserOrg> needSaveList = getNeedSaveList(dto, dbUserOrgList); if (CollectionUtils.isNotEmpty(needDeleteIdList)) { deleteByIds(needDeleteIdList); } if (CollectionUtils.isNotEmpty(needSaveList)) { saveOrUpdateBatch(needSaveList); } } /** * 获取需要删除的id集合 * * @param dbUserOrgList 数据库数据 * @param orgIds 前端组织ID列表 * @return 删除的id集合 */ private Set<Integer> getNeedDeleteIdList(List<SysUserOrg> dbUserOrgList, List<Integer> orgIds) { return dbUserOrgList.stream() .filter(dbUserOrg -> !orgIds.contains(dbUserOrg.getOrgId())) .map(SysUserOrg::getId) .collect(Collectors.toSet()); } /** * 获取需要保存的数据集合 * * @param dto 用户组织管理DTO * @param dbUserOrgList 数据库数据 * @return 保存的数据集合 */ private List<SysUserOrg> getNeedSaveList(UserOrgManageDTO dto, List<SysUserOrg> dbUserOrgList) { Map<Integer, SysUserOrg> map = dbUserOrgList.stream() .collect(Collectors.toMap(SysUserOrg::getOrgId, Function.identity())); return dto.getOrgDTOList().stream().map(orgDto -> { SysUserOrg dbUserOrg = map.get(orgDto.getOrgId()); if (dbUserOrg == null) { // 新增记录 dbUserOrg = new SysUserOrg(); dbUserOrg.setUserId(dto.getUserId()); toSysUserOrg(dbUserOrg, orgDto); } else { // 更新记录,只有内容发生变化时才更新 if (!Objects.equals(dbUserOrg.getOrgName(), orgDto.getOrgName()) || !Objects.equals(dbUserOrg.getOrgFullPath(), orgDto.getOrgFullPath())) { toSysUserOrg(dbUserOrg, orgDto); } } return dbUserOrg; }).collect(Collectors.toList()); } /** * 校验DTO中的组织ID是否存在重复 * @param orgIds 用户组织管理ID * @throws CustomException 如果存在重复的组织ID */ private void validateOrgIdsNotDuplicate(List<Integer> orgIds) { // 检查是否有重复的组织ID Set<Integer> uniqueOrgIds = new HashSet<>(orgIds); if (uniqueOrgIds.size() != orgIds.size()) { throw new CustomException(ExceptionEnums.ORG_ID_REPEAT); } } /** * 校验用户是否存在 * @param userId 用户ID */ private void validateUserExists(Long userId) { SysUser sysUser = sysUserService.selectUserById(userId); if (Objects.isNull(sysUser)) { throw new CustomException(ExceptionEnums.USER_INFO_NO_EXIST); } } /** * 获取用户组织ID列表 * @param userId 用户ID * @return 组织ID列表 */ @Override public List<Integer> getUserOrgIdList(Long userId){ return getUserOrgManageScope(userId).stream() .map(SysUserOrg::getOrgId) .collect(Collectors.toList()); } /** * 转换为系统用户组织范围 * @param dto 用户组织管理DTO * @return 系统用户组织范围列表 */ private List<SysUserOrg> toSysUserOrg(UserOrgManageDTO dto) { if (Objects.isNull(dto.getUserId()) || CollUtil.isEmpty(dto.getOrgDTOList())) { return new ArrayList<>(); } Long userId = dto.getUserId(); List<UserOrgManageDTO.UserOrgDTO> orgDTOList = dto.getOrgDTOList(); List<SysUserOrg> userOrgList = new ArrayList<>(); for (UserOrgManageDTO.UserOrgDTO orgDto : orgDTOList) { if (orgDto.getOrgId() != null) { SysUserOrg userOrg = new SysUserOrg(); toSysUserOrg(userOrg, orgDto); userOrg.setUserId(userId); userOrg.setDelFlag(DeleteFlagEnum.NO_DEL.getType()); userOrgList.add(userOrg); } } return userOrgList; } /** * 转换为系统用户组织范围 * @param userOrg 用户组织实体 * @param orgDto 用户组织DTO */ private void toSysUserOrg(SysUserOrg userOrg, UserOrgManageDTO.UserOrgDTO orgDto) { userOrg.setOrgId(orgDto.getOrgId()); userOrg.setOrgName(orgDto.getOrgName()); userOrg.setOrgFullPath(orgDto.getOrgFullPath()); } @Override public void deleteByIds(Collection<Integer> ids) { if (CollectionUtils.isEmpty(ids)) { return; } lambdaUpdate() .set(SysUserOrg::getDelFlag, DeleteFlagEnum.DEL.getType()) .in(SysUserOrg::getId, ids) .update(); } @Transactional @Override public void deleteByUserId(Long userId) { lambdaUpdate() .set(SysUserOrg::getDelFlag, DeleteFlagEnum.DEL.getType()) .eq(SysUserOrg::getUserId, userId) .eq(SysUserOrg::getDelFlag, DeleteFlagEnum.NO_DEL.getType()) .update(); } /** * 获取用户组织范围VO列表 * @param userId 用户ID * @return 用户组织范围VO列表 */ @Override public List<UserOrgScopeVO> getUserOrgScopeVOList(Long userId) { List<SysUserOrg> userOrgList = list(new LambdaQueryWrapper<SysUserOrg>() .eq(SysUserOrg::getDelFlag, DeleteFlagEnum.NO_DEL.getType()) .eq(SysUserOrg::getUserId, userId)); // 转换为VO对象 List<UserOrgScopeVO> userOrgScopeVOList = userOrgList.stream() .map(this::convertToUserOrgScopeVO) .collect(Collectors.toList()); return userOrgScopeVOList; } /** * 获取用户组织管理范围 * @param userId 用户ID * @return 用户组织管理范围列表 */ public List<SysUserOrg> getUserOrgManageScope(Long userId) { return list(new LambdaQueryWrapper<SysUserOrg>() .eq(SysUserOrg::getDelFlag, DeleteFlagEnum.NO_DEL.getType()) .eq(SysUserOrg::getUserId, userId)); } /** * 将SysUserOrg转换为UserOrgScopeVO * @param sysUserOrg 系统用户组织实体 * @return 用户组织范围VO */ private UserOrgScopeVO convertToUserOrgScopeVO(SysUserOrg sysUserOrg) { UserOrgScopeVO vo = new UserOrgScopeVO(); vo.setId(sysUserOrg.getId()); vo.setOrgId(sysUserOrg.getOrgId()); vo.setOrgFullPath(sysUserOrg.getOrgFullPath()); vo.setOrgName(sysUserOrg.getOrgName()); vo.setCreateTime(sysUserOrg.getCreateTime()); return vo; } /** * 从UserOrgManageDTO中提取组织ID列表 * @param dto 用户组织管理DTO * @return 组织ID列表 */ private List<Integer> extractOrgIdsFromDTO(UserOrgManageDTO dto) { if (dto == null || CollectionUtils.isEmpty(dto.getOrgDTOList())) { return new ArrayList<>(); } return dto.getOrgDTOList().stream() .map(UserOrgManageDTO.UserOrgDTO::getOrgId) .filter(Objects::nonNull) .collect(Collectors.toList()); }} 一对多关系增删查改
2025-09-25
评论0
暂时没有评论