package com.cloudroam.service.impl; import com.cloudroam.bo.GroupPermissionBO; import com.cloudroam.common.util.BeanCopyUtil; import com.cloudroam.dto.admin.*; import com.cloudroam.model.*; import com.cloudroam.service.*; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.cloudroam.dto.admin.*; import com.cloudroam.model.*; import com.cloudroam.service.*; import io.github.talelin.autoconfigure.exception.ForbiddenException; import io.github.talelin.autoconfigure.exception.NotFoundException; import com.cloudroam.common.enumeration.GroupLevelEnum; import com.cloudroam.common.mybatis.LinPage; import com.cloudroam.dto.admin.*; import com.cloudroam.mapper.GroupPermissionMapper; import com.cloudroam.mapper.UserGroupMapper; import com.cloudroam.model.*; import com.cloudroam.service.*; 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.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * @author * @author * @author * 管理员服务实现类 */ @Service public class AdminServiceImpl implements AdminService { @Autowired private UserService userService; @Autowired private UserIdentityService userIdentityService; @Autowired private GroupService groupService; @Autowired private PermissionService permissionService; @Autowired private GroupPermissionMapper groupPermissionMapper; @Autowired private UserGroupMapper userGroupMapper; @Override public IPage getUserPageByGroupId(Integer groupId, Integer count, Integer page) { LinPage pager = new LinPage<>(page, count); IPage iPage; // 如果group_id为空,则以分页的形式返回所有用户 if (groupId == null) { QueryWrapper wrapper = new QueryWrapper<>(); Integer rootUserId = userService.getRootUserId(); wrapper.lambda().ne(UserDO::getId, rootUserId); iPage = userService.page(pager, wrapper); } else { iPage = userService.getUserPageByGroupId(pager, groupId); } return iPage; } @Override public boolean changeUserPassword(Integer id, ResetPasswordDTO dto) { throwUserNotExistById(id); return userIdentityService.changePassword(id, dto.getNewPassword()); } @Transactional(rollbackFor = Exception.class) @Override public boolean deleteUser(Integer id) { throwUserNotExistById(id); if (userService.getRootUserId().equals(id)) { throw new ForbiddenException(10079); } boolean userRemoved = userService.removeById(id); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda().eq(UserIdentityDO::getUserId, id); // 删除用户,还应当将 user_group表中的数据删除 boolean deleteResult = userGroupMapper.deleteByUserId(id) > 0; return userRemoved && userIdentityService.remove(wrapper) && deleteResult; } @Override public boolean updateUserInfo(Integer id, UpdateUserInfoDTO validator) { UserDO userDO=new UserDO(); userDO.setId(id); BeanCopyUtil.copyNonNullProperties(validator,userDO); // 更新用户基础信息 userService.updateById(userDO); // 更新用户分组信息 List newGroupIds = validator.getGroupIds(); Integer rootGroupId = groupService.getParticularGroupIdByLevel(GroupLevelEnum.ROOT); boolean anyMatch = newGroupIds.stream().anyMatch(it -> it.equals(rootGroupId)); if (anyMatch) { throw new ForbiddenException(10073); } List existGroupIds = groupService.getUserGroupIdsByUserId(id); // 删除existGroupIds有,而newGroupIds没有的 List deleteIds = existGroupIds.stream().filter(it -> !newGroupIds.contains(it)).collect(Collectors.toList()); // 添加newGroupIds有,而existGroupIds没有的 List addIds = newGroupIds.stream().filter(it -> !existGroupIds.contains(it)).collect(Collectors.toList()); return groupService.deleteUserGroupRelations(id, deleteIds) && groupService.addUserGroupRelations(id, addIds); } @Override public IPage getGroupPage(Integer page, Integer count) { return groupService.getGroupPage(page, count); } @Override public GroupPermissionBO getGroup(Integer id) { throwGroupNotExistById(id); return groupService.getGroupAndPermissions(id); } @Transactional(rollbackFor = Exception.class) @Override public boolean createGroup(NewGroupDTO dto) { throwGroupNameExist(dto.getName()); GroupDO group = GroupDO.builder().name(dto.getName()).info(dto.getInfo()).build(); groupService.save(group); if (dto.getPermissionIds() != null && !dto.getPermissionIds().isEmpty()) { List relations = dto.getPermissionIds().stream() .map(id -> new GroupPermissionDO(group.getId(), id)) .collect(Collectors.toList()); groupPermissionMapper.insertBatch(relations); } return true; } @Override public boolean updateGroup(Integer id, UpdateGroupDTO dto) { // bug 如果只修改info,不修改name,则name已经存在,此时不应该报错 GroupDO exist = groupService.getById(id); if (exist == null) { throw new NotFoundException(10024); } if (!exist.getName().equals(dto.getName())) { throwGroupNameExist(dto.getName()); } GroupDO group = GroupDO.builder().name(dto.getName()).info(dto.getInfo()).build(); group.setId(id); return groupService.updateById(group); } @Override public boolean deleteGroup(Integer id) { Integer rootGroupId = groupService.getParticularGroupIdByLevel(GroupLevelEnum.ROOT); Integer guestGroupId = groupService.getParticularGroupIdByLevel(GroupLevelEnum.GUEST); if (id.equals(rootGroupId)) { throw new ForbiddenException(10074); } if (id.equals(guestGroupId)) { throw new ForbiddenException(10075); } throwGroupNotExistById(id); List groupUserIds = groupService.getGroupUserIds(id); if(!groupUserIds.isEmpty()) { throw new ForbiddenException(10027); } return groupService.removeById(id); } @Override public boolean dispatchPermission(DispatchPermissionDTO dto) { GroupPermissionDO groupPermission = new GroupPermissionDO(dto.getGroupId(), dto.getPermissionId()); return groupPermissionMapper.insert(groupPermission) > 0; } @Override public boolean dispatchPermissions(DispatchPermissionsDTO dto) { List relations = dto.getPermissionIds().stream() .map(id -> new GroupPermissionDO(dto.getGroupId(), id)) .collect(Collectors.toList()); return groupPermissionMapper.insertBatch(relations) > 0; } @Override public boolean removePermissions(RemovePermissionsDTO dto) { return groupPermissionMapper.deleteBatchByGroupIdAndPermissionId(dto.getGroupId(), dto.getPermissionIds()) > 0; } @Override public List getAllGroups() { QueryWrapper wrapper = new QueryWrapper<>(); Integer rootGroupId = groupService.getParticularGroupIdByLevel(GroupLevelEnum.ROOT); wrapper.lambda().ne(GroupDO::getId, rootGroupId); return groupService.list(wrapper); } @Override public List getAllPermissions() { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda().eq(PermissionDO::getMount, true); return permissionService.list(wrapper); } @Override public Map> getAllStructuralPermissions() { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda().eq(PermissionDO::getMount, true); List permissions = getAllPermissions(); Map> res = new HashMap<>(); permissions.forEach(permission -> { if (res.containsKey(permission.getModule())) { res.get(permission.getModule()).add(permission); } else { ArrayList t = new ArrayList<>(); t.add(permission); res.put(permission.getModule(), t); } }); return res; } @Override public List getGroupsUserList(Integer id) { IPage page = this.getUserPageByGroupId(id, 1000, 0); return page.getRecords(); } @Override public List getGroupsUserListOnJob(Integer groupId) { IPage page = this.getUserPageByGroupId(groupId, 1000, 0); List list= page.getRecords(); List resultList =list.stream().filter(userDO -> userDO.getStaffStatus()==1).collect(Collectors.toList()); return resultList; } private void throwUserNotExistById(Integer id) { boolean exist = userService.checkUserExistById(id); if (!exist) { throw new NotFoundException(10021); } } private void throwGroupNotExistById(Integer id) { boolean exist = groupService.checkGroupExistById(id); if (!exist) { throw new NotFoundException(10024); } } private void throwGroupNameExist(String name) { boolean exist = groupService.checkGroupExistByName(name); if (exist) { throw new ForbiddenException(10072); } } }