1. 请求参数:TeamJoinRequest
package com.example.usercenter.model.request;
import lombok.Data;
import java.io.Serializable;
/**
* 加入队伍请求参数封装类
*/
@Data
public class TeamJoinRequest implements Serializable {
private static final long serialVersionUID = 4442151141560122843L;
/**
* 队伍 id
*/
private Long teamId;
/**
* 密码
*/
private String password;
}
2. 请求路径:/team/join
3. 请求方式:POST
4. 响应数据:BaseResponse<Boolean>
1. 最多加入 5 个队伍
2. 队伍必须存在,只能加入未满、未过期的队伍
3. 不能加入自己的队伍,不能重复加入已加入的队伍(幂等性)
4. 禁止加入私有的队伍
5. 如果加入的队伍是加密的,必须密码匹配才可以加入
6. 新增队伍 - 用户关联信息
优化:将涉及到数据库查询的校验放到后面进行,如果前面的校验未通过,直接抛出异常,减少数据库资源的消耗
@Override
public boolean joinTeam(TeamJoinRequest teamJoinRequest, User loginUser) {
if (teamJoinRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
// 1. 用户最多创建和加入 5 个队伍
Long userId = loginUser.getId();
QueryWrapper<UserTeam> userTeamQueryWrapper = new QueryWrapper<>();
userTeamQueryWrapper.eq("user_id", userId);
long hasJoinNum = userTeamService.count(userTeamQueryWrapper);
if (hasJoinNum >= 5) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户最多创建和加入 5 个队伍");
}
// 2. 队伍必须存在,只能加入未满、未过期的队伍
Long teamId = teamJoinRequest.getTeamId();
Team team = this.getById(teamId);
if (team == null) {
throw new BusinessException(ErrorCode.NULL_ERROR, "队伍不存在");
}
userTeamQueryWrapper = new QueryWrapper<>();
userTeamQueryWrapper.eq("team_id", teamId);
long teamHasJoinNum = userTeamService.count(userTeamQueryWrapper);
if (teamHasJoinNum >= team.getMaxNum()) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍人数已满");
}
Date expireTime = team.getExpireTime();
if (expireTime != null && expireTime.before(new Date())) {
throw new BusinessException(ErrorCode.PARAMS_ERROR,"队伍已过期");
}
// 3. 不能加入自己的队伍,不能重复加入已加入的队伍(幂等性)
// if (team.getUserId() == userId) {
// throw new BusinessException(ErrorCode.PARAMS_ERROR,"不能加入自己创建的队伍");
// }
userTeamQueryWrapper = new QueryWrapper<>();
userTeamQueryWrapper.eq("team_id", teamId);
userTeamQueryWrapper.eq("user_id", userId);
long alreadyJoinNum = userTeamService.count(userTeamQueryWrapper);
if (alreadyJoinNum > 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户已加入该队伍");
}
// 4. 禁止加入私有的队伍
Integer status = team.getStatus();
TeamStatusEnum teamStatusEnum = TeamStatusEnum.getTeamEnumByValue(status);
if (teamStatusEnum.equals(TeamStatusEnum.PRIVATE)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "禁止加入私有的队伍");
}
// 5. 如果加入的队伍是加密的,必须密码匹配才可以
String password = teamJoinRequest.getPassword();
if (teamStatusEnum.equals(TeamStatusEnum.SECRET)) {
if (StringUtils.isBlank(password) || !password.equals(team.getPassword())) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码错误");
}
}
// 6. 新增队伍 - 用户关联信息
UserTeam userTeam = new UserTeam();
userTeam.setUserId(userId);
userTeam.setTeamId(teamId);
userTeam.setJoinTime(new Date());
return userTeamService.save(userTeam);
}