一、接口设计
1. 请求参数:封装 TeamQuery
package com.example.usercenter.model.dto;import com.example.usercenter.common.PageRequest;
import lombok.Data;/*** @author 乐小鑫* @version 1.0* @Date 2024-01-22-20:14*/
@Data
public class TeamQuery extends PageRequest {private static final long serialVersionUID = -8434935321943948180L;/*** id*/private Long id;/*** 查询关键词(同时查询 name 和 description)*/private String searchText;/*** 队伍名称*/private String name;/*** 队伍描述*/private String description;/*** 最大人数*/private Integer maxNum;/*** 用户id*/private Long userId;/*** 0 - 公开,1 - 私有,2 - 加密*/private Integer status;
}
2. 请求路径:/team/list
3. 请求方式:GET
4. 响应数据:List<TeamUserVO> 队伍和关联用户信息的列表
- 封装 TeamUserVO 统一返回类
- 封装 UserVO 返回脱敏用户信息
package com.example.usercenter.model.vo;import lombok.Data;import java.io.Serializable;
import java.util.Date;/*** 队伍和用户信息返回封装类(脱敏)* @author 乐小鑫* @version 1.0* @Date 2024-01-23-15:23*/
@Data
public class TeamUserVO implements Serializable {private static final long serialVersionUID = -8855840933732067014L;/*** id*/private Long id;/*** 队伍名称*/private String name;/*** 描述*/private String description;/*** 最大人数*/private Integer maxNum;/*** 过期时间*/private Date expireTime;/*** 用户id*/private Long userId;/*** 0 - 公开,1 - 私有,2 - 加密*/private Integer status;/*** 创建时间*/private Date createTime;/*** 更新时间*/private Date updateTime;/*** 队伍创建用户列表*/UserVO createUser;
}
二、业务需求
1. 请求参数中取出队伍名称,存在则放入查询构造器
2. 筛掉已经过期的队伍,返回删除后的队伍列表
3. 可以通过某个关键词同时对名称和队伍描述查询
4. 只有管理员才能查看加密的 / 私密的队伍
5. 关联查询队伍中已加入的用户信息传给前端进行展示
/*** 查询队伍列表* @param teamQuery 查询条件封装类* @param isAdmin 是否是管理员* @return 队伍结果列表*/@Overridepublic List<TeamUserVO> listTeams(TeamQuery teamQuery, boolean isAdmin) {// 组合查询条件QueryWrapper<Team> queryWrapper = new QueryWrapper<>();if (teamQuery != null) {Long teamID = teamQuery.getId();if (teamID != null && teamID > 0) {queryWrapper.eq("id", teamID);}// 可以通过某个 关键词 searchText 同时对名称和队伍描述查询String searchText = teamQuery.getSearchText();if (StringUtils.isNotBlank(searchText)) {queryWrapper.and(qw -> qw.like("name", searchText).or().like("description", searchText));}String name = teamQuery.getName();if (StringUtils.isNotBlank(name)) {queryWrapper.like("name", name);}String description = teamQuery.getDescription();if (StringUtils.isNotBlank(description)) {queryWrapper.like("description", description);}Integer maxNum = teamQuery.getMaxNum();if (maxNum != null && maxNum > 0) {queryWrapper.eq("max_num", maxNum);}Long userId = teamQuery.getUserId();if (userId != null && userId >= 0) {queryWrapper.eq("user_id", userId);}// 只有管理员才能查看加密的 / 私密的队伍Integer status = teamQuery.getStatus();TeamStatusEnum teamStatus = TeamStatusEnum.getTeamEnumByValue(status);if (teamStatus == null) {teamStatus = TeamStatusEnum.PUBLIC;}if (!isAdmin && teamStatus.equals(TeamStatusEnum.PRIVATE)) {throw new BusinessException(ErrorCode.NO_AUTH);}queryWrapper.eq("status", teamStatus.getValue());}// 不展示已过期的队伍queryWrapper.and(qw -> qw.gt("expire_time", new Date()).or().isNull("expire_time"));// 根据查询条件查询队伍列表List<Team> teamList = this.list(queryWrapper);if (CollectionUtils.isEmpty(teamList)) {return new ArrayList<>();}List<TeamUserVO> teamUserVOList = new ArrayList<>();// 关联查询队伍创建人的用户信息for (Team team : teamList) {Long userId = team.getUserId();if (userId == null) {continue;}User user = userService.getById(userId);// 用户信息脱敏UserVO userVO = new UserVO();if (user != null) {BeanUtils.copyProperties(user, userVO);}TeamUserVO teamUserVO = new TeamUserVO();BeanUtils.copyProperties(team, teamUserVO);teamUserVO.setCreateUser(userVO);teamUserVOList.add(teamUserVO);}return teamUserVOList;}