use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.
the class CommentManager method addCommentLike.
@Transactional(rollbackFor = Exception.class)
public void addCommentLike(Integer cid, Boolean toLike, Integer sourceId, String sourceType) throws StatusFailException {
// 获取当前登录的用户
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
QueryWrapper<CommentLike> commentLikeQueryWrapper = new QueryWrapper<>();
commentLikeQueryWrapper.eq("cid", cid).eq("uid", userRolesVo.getUid());
CommentLike commentLike = commentLikeEntityService.getOne(commentLikeQueryWrapper, false);
if (toLike) {
// 添加点赞
if (commentLike == null) {
// 如果不存在就添加
boolean isSave = commentLikeEntityService.saveOrUpdate(new CommentLike().setUid(userRolesVo.getUid()).setCid(cid));
if (!isSave) {
throw new StatusFailException("点赞失败,请重试尝试!");
}
}
// 点赞+1
Comment comment = commentEntityService.getById(cid);
if (comment != null) {
comment.setLikeNum(comment.getLikeNum() + 1);
commentEntityService.updateById(comment);
// 当前的评论要不是点赞者的 才发送点赞消息
if (!userRolesVo.getUsername().equals(comment.getFromName())) {
commentEntityService.updateCommentLikeMsg(comment.getFromUid(), userRolesVo.getUid(), sourceId, sourceType);
}
}
} else {
// 取消点赞
if (commentLike != null) {
// 如果存在就删除
boolean isDelete = commentLikeEntityService.removeById(commentLike.getId());
if (!isDelete) {
throw new StatusFailException("取消点赞失败,请重试尝试!");
}
}
// 点赞-1
UpdateWrapper<Comment> commentUpdateWrapper = new UpdateWrapper<>();
commentUpdateWrapper.setSql("like_num=like_num-1").eq("id", cid);
commentEntityService.update(commentUpdateWrapper);
}
}
use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.
the class GroupContestProblemManager method deleteContestProblem.
public void deleteContestProblem(Long pid, Long cid) throws StatusNotFoundException, StatusForbiddenException, StatusFailException {
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
Contest contest = contestEntityService.getById(cid);
if (contest == null) {
throw new StatusNotFoundException("该比赛不存在!");
}
Long gid = contest.getGid();
Group group = groupEntityService.getById(gid);
if (group == null || group.getStatus() == 1 && !isRoot) {
throw new StatusNotFoundException("该团队不存在或已被封禁!");
}
if (!userRolesVo.getUid().equals(contest.getUid()) && !isRoot && !groupValidator.isGroupRoot(userRolesVo.getUid(), gid)) {
throw new StatusForbiddenException("对不起,您无权限操作!");
}
QueryWrapper<ContestProblem> contestProblemQueryWrapper = new QueryWrapper<>();
contestProblemQueryWrapper.eq("cid", cid).eq("pid", pid);
boolean isOk = contestProblemEntityService.remove(contestProblemQueryWrapper);
if (isOk) {
UpdateWrapper<Judge> judgeUpdateWrapper = new UpdateWrapper<>();
judgeUpdateWrapper.eq("cid", cid).eq("pid", pid);
judgeEntityService.remove(judgeUpdateWrapper);
} else {
throw new StatusFailException("删除失败!");
}
}
use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.
the class GroupProblemManager method changeProblemAuth.
public void changeProblemAuth(Long pid, Integer auth) throws StatusForbiddenException, StatusNotFoundException, StatusFailException {
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
Problem problem = problemEntityService.getById(pid);
if (problem == null) {
throw new StatusNotFoundException("该题目不存在!");
}
Long gid = problem.getGid();
Group group = groupEntityService.getById(gid);
if (group == null || group.getStatus() == 1 && !isRoot) {
throw new StatusNotFoundException("该团队不存在或已被封禁!");
}
if (!userRolesVo.getUsername().equals(problem.getAuthor()) && !isRoot && !groupValidator.isGroupRoot(userRolesVo.getUid(), gid)) {
throw new StatusForbiddenException("对不起,您无权限操作!");
}
UpdateWrapper<Problem> problemUpdateWrapper = new UpdateWrapper<>();
problemUpdateWrapper.eq("id", pid).set("auth", auth).set("modified_user", userRolesVo.getUsername());
boolean isOk = problemEntityService.update(problemUpdateWrapper);
if (!isOk) {
throw new StatusFailException("修改失败");
}
}
use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.
the class GroupTrainingManager method updateTraining.
@Transactional(rollbackFor = Exception.class)
public void updateTraining(TrainingDto trainingDto) throws StatusForbiddenException, StatusNotFoundException, StatusFailException {
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
Long tid = trainingDto.getTraining().getId();
Training training = trainingEntityService.getById(tid);
if (training == null) {
throw new StatusNotFoundException("该训练不存在!");
}
Long gid = training.getGid();
Group group = groupEntityService.getById(gid);
if (group == null || group.getStatus() == 1 && !isRoot) {
throw new StatusNotFoundException("该团队不存在或已被封禁!");
}
if (!userRolesVo.getUsername().equals(training.getAuthor()) && !isRoot && !groupValidator.isGroupRoot(userRolesVo.getUid(), gid)) {
throw new StatusForbiddenException("对不起,您无权限操作!");
}
trainingDto.getTraining().setIsGroup(training.getIsGroup());
trainingEntityService.updateById(trainingDto.getTraining());
if (trainingDto.getTraining().getAuth().equals(Constants.Training.AUTH_PRIVATE.getValue())) {
if (!Objects.equals(training.getPrivatePwd(), trainingDto.getTraining().getPrivatePwd())) {
UpdateWrapper<TrainingRegister> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("tid", tid);
trainingRegisterEntityService.remove(updateWrapper);
}
}
TrainingCategory trainingCategory = trainingDto.getTrainingCategory();
if (trainingCategory.getGid() != null && trainingCategory.getGid().longValue() != gid) {
throw new StatusForbiddenException("对不起,您无权限操作!");
}
if (trainingCategory.getId() == null) {
try {
trainingCategory.setGid(gid);
trainingCategoryEntityService.save(trainingCategory);
} catch (Exception ignored) {
QueryWrapper<TrainingCategory> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", trainingCategory.getName());
trainingCategory = trainingCategoryEntityService.getOne(queryWrapper, false);
}
}
MappingTrainingCategory mappingTrainingCategory = mappingTrainingCategoryEntityService.getOne(new QueryWrapper<MappingTrainingCategory>().eq("tid", training.getId()), false);
if (mappingTrainingCategory == null) {
mappingTrainingCategoryEntityService.save(new MappingTrainingCategory().setTid(training.getId()).setCid(trainingCategory.getId()));
adminTrainingRecordManager.checkSyncRecord(trainingDto.getTraining());
} else {
if (!mappingTrainingCategory.getCid().equals(trainingCategory.getId())) {
UpdateWrapper<MappingTrainingCategory> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("tid", training.getId()).set("cid", trainingCategory.getId());
boolean isOk = mappingTrainingCategoryEntityService.update(null, updateWrapper);
if (isOk) {
adminTrainingRecordManager.checkSyncRecord(trainingDto.getTraining());
} else {
throw new StatusFailException("修改失败");
}
}
}
}
use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.
the class GroupTrainingProblemManager method addProblemFromGroup.
@Transactional(rollbackFor = Exception.class)
public void addProblemFromGroup(String problemId, Long tid) throws StatusNotFoundException, StatusForbiddenException, StatusFailException {
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
Training training = trainingEntityService.getById(tid);
if (training == null) {
throw new StatusNotFoundException("该训练不存在!");
}
Long gid = training.getGid();
Group group = groupEntityService.getById(gid);
if (group == null || group.getStatus() == 1 && !isRoot) {
throw new StatusNotFoundException("该团队不存在或已被封禁!");
}
if (!userRolesVo.getUsername().equals(training.getAuthor()) && !isRoot && !groupValidator.isGroupRoot(userRolesVo.getUid(), gid)) {
throw new StatusForbiddenException("对不起,您无权限操作!");
}
QueryWrapper<Problem> problemQueryWrapper = new QueryWrapper<>();
problemQueryWrapper.eq("problem_id", problemId).eq("gid", gid);
Problem problem = problemEntityService.getOne(problemQueryWrapper);
if (problem == null) {
throw new StatusNotFoundException("该题目不存在或不是团队题目!");
}
QueryWrapper<TrainingProblem> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("tid", tid).and(wrapper -> wrapper.eq("pid", problem.getId()).or().eq("display_id", problem.getProblemId()));
TrainingProblem trainingProblem = trainingProblemEntityService.getOne(queryWrapper);
if (trainingProblem != null) {
throw new StatusFailException("添加失败,该题目已添加或者题目的训练展示ID已存在!");
}
TrainingProblem newTProblem = new TrainingProblem();
boolean isOk = trainingProblemEntityService.save(newTProblem.setTid(tid).setPid(problem.getId()).setDisplayId(problem.getProblemId()));
if (isOk) {
UpdateWrapper<Training> trainingUpdateWrapper = new UpdateWrapper<>();
trainingUpdateWrapper.set("gmt_modified", new Date()).eq("id", tid);
trainingEntityService.update(trainingUpdateWrapper);
adminTrainingRecordManager.syncAlreadyRegisterUserRecord(tid, problem.getId(), newTProblem.getId());
} else {
throw new StatusFailException("添加失败!");
}
}
Aggregations