package github.benjamin.equipreservebackend.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import github.benjamin.equipreservebackend.dto.UserDTO; import github.benjamin.equipreservebackend.entity.Role; import github.benjamin.equipreservebackend.entity.Team; import github.benjamin.equipreservebackend.entity.User; import github.benjamin.equipreservebackend.entity.UserRole; import github.benjamin.equipreservebackend.exception.ApiException; import github.benjamin.equipreservebackend.mapper.RoleMapper; import github.benjamin.equipreservebackend.mapper.TeamMapper; import github.benjamin.equipreservebackend.mapper.UserMapper; import github.benjamin.equipreservebackend.mapper.UserRoleMapper; import github.benjamin.equipreservebackend.response.ResponseCode; import github.benjamin.equipreservebackend.security.SecurityUser; import github.benjamin.equipreservebackend.service.UserService; import github.benjamin.equipreservebackend.utils.JwtUtil; import github.benjamin.equipreservebackend.utils.PageUtil; import github.benjamin.equipreservebackend.utils.PasswordUtil; import github.benjamin.equipreservebackend.vo.UserVO; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.function.Function; import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class UserServiceImpl implements UserService { private final UserMapper userMapper; private final RoleMapper roleMapper; private final TeamMapper teamMapper; private final UserRoleMapper userRoleMapper; private final JwtUtil jwtUtil; @Override public User login(String username, String password) { User user = userMapper.selectOne(new LambdaQueryWrapper().eq(User::getUsername, username)); if (Objects.isNull(user)) { throw new ApiException(ResponseCode.USER_NOT_EXIST, "用户不存在"); } if (!PasswordUtil.matches(password, user.getPassword())) { throw new ApiException(ResponseCode.PASSWORD_ERROR, "密码错误"); } return user; } @Override public SecurityUser loadSecurityUserById(Long userId) { User user = userMapper.selectOne(new LambdaQueryWrapper().eq(User::getId, userId)); List roles = roleMapper.selectRoleByUserId(userId); return new SecurityUser(user, roles); } @Override public UserVO getUserVO(Long id) { User user = userMapper.selectById(id); Team team = teamMapper.selectById(user.getTeamId()); return new UserVO(user, team); } @Override public void updateUser(Long id, UserDTO dto) { boolean hasUpdate = false; LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper() .eq(User::getId, id); if (StringUtils.hasText(dto.getUsername())) { wrapper.set(User::getUsername, dto.getUsername()); hasUpdate = true; } if (StringUtils.hasText(dto.getName())) { wrapper.set(User::getName, dto.getName()); hasUpdate = true; } if (StringUtils.hasText(dto.getPhone())) { wrapper.set(User::getPhone, dto.getPhone()); hasUpdate = true; } if (Objects.nonNull(dto.getTeamId())) { wrapper.set(User::getTeamId, dto.getTeamId()); hasUpdate = true; } if (StringUtils.hasText(dto.getPassword())) { String encoded = PasswordUtil.encode(dto.getPassword()); wrapper.set(User::getPassword, encoded); hasUpdate = true; } if (hasUpdate) { userMapper.update(wrapper); } if (Objects.nonNull(dto.getRoleId())) { userRoleMapper.update(new LambdaUpdateWrapper() .eq(UserRole::getUserId, id) .set(UserRole::getRoleId, dto.getRoleId())); } } @Override public Page getUserVOs(Page pageRequest, String name) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper() .orderByAsc(User::getName); if (StringUtils.hasText(name)) { wrapper.like(User::getName, name); } Page users = userMapper.selectPage(pageRequest, wrapper); Map teamMap = teamMapper.selectList(null).stream() .collect(Collectors.toMap(Team::getId, Function.identity())); Map roleMap = userRoleMapper.selectList(null).stream() .collect(Collectors.toMap(UserRole::getUserId, UserRole::getRoleId, (v1, v2) -> v1)); Page res = PageUtil.copyPage(users); res.setRecords(users.getRecords().stream().map(u -> new UserVO(u, teamMap, roleMap)).toList()); return res; } @Override public void deleteById(Long userId) { userMapper.deleteById(userId); } @Override public void addUser(UserDTO dto) { String encoded = PasswordUtil.encode(dto.getPassword()); dto.setPassword(encoded); User user = new User(dto); userMapper.insert(user); userRoleMapper.insert(new UserRole(user.getId(), dto.getRoleId())); } @Override public List getTeamUsers(Long teamId) { return userMapper.selectList(new LambdaQueryWrapper() .eq(User::getTeamId, teamId) .orderByAsc(User::getName)) .stream() .map(User::getName) .toList(); } }