Search in sources :

Example 16 with UpdateWrapper

use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.

the class UserRecordServiceImpl method tryAgainUpdate.

@Transactional(isolation = Isolation.READ_COMMITTED)
public boolean tryAgainUpdate(String uid, Integer score) {
    boolean retryable;
    int attemptNumber = 0;
    do {
        // 查询最新版本号
        QueryWrapper<Judge> judgeQueryWrapper = new QueryWrapper<>();
        // 非比赛提交
        judgeQueryWrapper.isNotNull("score").orderByDesc("score").isNull("cid").last("limit 1");
        Judge lastHighScoreJudge = judgeService.getOne(judgeQueryWrapper);
        // 更新
        boolean success = true;
        if (lastHighScoreJudge == null) {
            UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
            userRecordUpdateWrapper.set("total_score", score).eq("uid", uid);
            success = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
        } else if (lastHighScoreJudge.getScore() < score) {
            // 如果之前该题目最高得分的提交比现在得分低,也需要修改
            int addValue = score - lastHighScoreJudge.getScore();
            UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
            userRecordUpdateWrapper.setSql("total_score=total_score+" + addValue).eq("uid", uid);
            success = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
        }
        if (success) {
            return true;
        } else {
            attemptNumber++;
            retryable = attemptNumber < 8;
            if (attemptNumber == 8) {
                log.error("更新user_record表超过最大重试次数");
                break;
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                log.error(e.getMessage());
            }
        }
    } while (retryable);
    return false;
}
Also used : UserRecord(top.hcode.hoj.pojo.entity.user.UserRecord) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with UpdateWrapper

use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.

the class DiscussionController method addDiscussionLike.

@GetMapping("/discussion-like")
@Transactional(rollbackFor = Exception.class)
@RequiresAuthentication
public CommonResult addDiscussionLike(@RequestParam("did") Integer did, @RequestParam("toLike") Boolean toLike, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    QueryWrapper<DiscussionLike> discussionLikeQueryWrapper = new QueryWrapper<>();
    discussionLikeQueryWrapper.eq("did", did).eq("uid", userRolesVo.getUid());
    DiscussionLike discussionLike = discussionLikeService.getOne(discussionLikeQueryWrapper, false);
    if (toLike) {
        // 添加点赞
        if (discussionLike == null) {
            // 如果不存在就添加
            boolean isSave = discussionLikeService.saveOrUpdate(new DiscussionLike().setUid(userRolesVo.getUid()).setDid(did));
            if (!isSave) {
                return CommonResult.errorResponse("点赞失败,请重试尝试!");
            }
        }
        // 点赞+1
        Discussion discussion = discussionService.getById(did);
        if (discussion != null) {
            discussion.setLikeNum(discussion.getLikeNum() + 1);
            discussionService.updateById(discussion);
            // 更新点赞消息
            discussionService.updatePostLikeMsg(discussion.getUid(), userRolesVo.getUid(), did);
        }
        return CommonResult.successResponse(null, "点赞成功");
    } else {
        // 取消点赞
        if (discussionLike != null) {
            // 如果存在就删除
            boolean isDelete = discussionLikeService.removeById(discussionLike.getId());
            if (!isDelete) {
                return CommonResult.errorResponse("取消点赞失败,请重试尝试!");
            }
        }
        // 点赞-1
        UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
        discussionUpdateWrapper.setSql("like_num=like_num-1").eq("id", did);
        discussionService.update(discussionUpdateWrapper);
        return CommonResult.successResponse(null, "取消成功");
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) DiscussionLike(top.hcode.hoj.pojo.entity.discussion.DiscussionLike) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with UpdateWrapper

use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.

the class DiscussionController method getDiscussion.

@GetMapping("/discussion")
public CommonResult getDiscussion(@RequestParam(value = "did", required = true) Integer did, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    String uid = null;
    if (userRolesVo != null) {
        uid = userRolesVo.getUid();
    }
    DiscussionVo discussion = discussionService.getDiscussion(did, uid);
    if (discussion == null) {
        return CommonResult.errorResponse("对不起,该讨论不存在!", CommonResult.STATUS_NOT_FOUND);
    }
    if (discussion.getStatus() == 1) {
        return CommonResult.errorResponse("对不起,该讨论已被封禁!", CommonResult.STATUS_FORBIDDEN);
    }
    // 浏览量+1
    UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
    discussionUpdateWrapper.setSql("view_num=view_num+1").eq("id", discussion.getId());
    discussionService.update(discussionUpdateWrapper);
    discussion.setViewNum(discussion.getViewNum() + 1);
    return CommonResult.successResponse(discussion, "获取成功");
}
Also used : DiscussionVo(top.hcode.hoj.pojo.vo.DiscussionVo) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion)

Example 19 with UpdateWrapper

use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.

the class JudgeController method resubmit.

/**
 * @MethodName resubmit
 * @Params * @param null
 * @Description 调用判题服务器提交失败超过60s后,用户点击按钮重新提交判题进入的方法
 * @Return
 * @Since 2021/2/12
 */
@RequiresAuthentication
@GetMapping(value = "/resubmit")
@Transactional(rollbackFor = Exception.class)
public CommonResult resubmit(@RequestParam("submitId") Long submitId, HttpServletRequest request) {
    Judge judge = judgeService.getById(submitId);
    if (judge == null) {
        return CommonResult.errorResponse("此提交数据不存在!");
    }
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    if (!judge.getUid().equals(userRolesVo.getUid())) {
        // 不是本人无法重新提交
        return CommonResult.errorResponse("对不起!您并非提交数据的本人,无法重新提交!");
    }
    Problem problem = problemService.getById(judge.getPid());
    // 如果是非比赛题目
    if (judge.getCid() == 0) {
        // 如果该题已经是AC通过状态,更新该题目的用户ac做题表 user_acproblem
        if (judge.getStatus().intValue() == Constants.Judge.STATUS_ACCEPTED.getStatus().intValue()) {
            QueryWrapper<UserAcproblem> userAcproblemQueryWrapper = new QueryWrapper<>();
            userAcproblemQueryWrapper.eq("submit_id", judge.getSubmitId());
            userAcproblemService.remove(userAcproblemQueryWrapper);
        }
    } else {
        if (problem.getIsRemote()) {
            // 将对应比赛记录设置成默认值
            UpdateWrapper<ContestRecord> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("submit_id", submitId).setSql("status=null,score=null");
            contestRecordService.update(updateWrapper);
        } else {
            return CommonResult.errorResponse("错误!非vJudge题目在比赛过程无权限重新提交");
        }
    }
    boolean isHasSubmitIdRemoteRejudge = false;
    if (Objects.nonNull(judge.getVjudgeSubmitId()) && (judge.getStatus().intValue() == Constants.Judge.STATUS_SUBMITTED_FAILED.getStatus() || judge.getStatus().intValue() == Constants.Judge.STATUS_PENDING.getStatus() || judge.getStatus().intValue() == Constants.Judge.STATUS_JUDGING.getStatus() || judge.getStatus().intValue() == Constants.Judge.STATUS_COMPILING.getStatus() || judge.getStatus().intValue() == Constants.Judge.STATUS_SYSTEM_ERROR.getStatus())) {
        isHasSubmitIdRemoteRejudge = true;
    }
    // 重新进入等待队列
    judge.setStatus(Constants.Judge.STATUS_PENDING.getStatus());
    judge.setVersion(judge.getVersion() + 1);
    judge.setErrorMessage(null).setOiRankScore(null).setScore(null).setTime(null).setJudger("").setMemory(null);
    judgeService.updateById(judge);
    // 将提交加入任务队列
    if (problem.getIsRemote()) {
        // 如果是远程oj判题
        remoteJudgeDispatcher.sendTask(judge, judgeToken, problem.getProblemId(), judge.getCid() != 0, isHasSubmitIdRemoteRejudge);
    } else {
        judgeDispatcher.sendTask(judge, judgeToken, judge.getCid() != 0);
    }
    return CommonResult.successResponse(judge, "重新提交成功!");
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) ContestRecord(top.hcode.hoj.pojo.entity.contest.ContestRecord) Problem(top.hcode.hoj.pojo.entity.problem.Problem) UserAcproblem(top.hcode.hoj.pojo.entity.user.UserAcproblem) Judge(top.hcode.hoj.pojo.entity.judge.Judge) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 20 with UpdateWrapper

use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.

the class AdminUserController method editUser.

@PutMapping("/edit-user")
@RequiresPermissions("user_admin")
@RequiresAuthentication
@Transactional(rollbackFor = Exception.class)
public CommonResult editUser(@RequestBody Map<String, Object> params, HttpServletRequest request) {
    String username = (String) params.get("username");
    String uid = (String) params.get("uid");
    String realname = (String) params.get("realname");
    String email = (String) params.get("email");
    String password = (String) params.get("password");
    int type = (int) params.get("type");
    int status = (int) params.get("status");
    boolean setNewPwd = (boolean) params.get("setNewPwd");
    UpdateWrapper<UserInfo> updateWrapper1 = new UpdateWrapper<>();
    updateWrapper1.eq("uuid", uid).set("username", username).set("realname", realname).set("email", email).set(setNewPwd, "password", SecureUtil.md5(password)).set("status", status);
    boolean result1 = userInfoService.update(updateWrapper1);
    QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
    userRoleQueryWrapper.eq("uid", uid);
    UserRole userRole = userRoleService.getOne(userRoleQueryWrapper, false);
    boolean result2 = false;
    int oldType = userRole.getRoleId().intValue();
    if (userRole.getRoleId().intValue() != type) {
        userRole.setRoleId(Long.valueOf(type));
        result2 = userRoleService.updateById(userRole);
    }
    if (result1) {
        // 需要重新登录
        userRoleService.deleteCache(uid, true);
    } else if (result2) {
        // 需要重新授权
        userRoleService.deleteCache(uid, false);
    }
    if (result2) {
        // 获取当前登录的用户
        HttpSession session = request.getSession();
        UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
        String title = "权限变更通知(Authority Change Notice)";
        String content = userRoleService.getAuthChangeContent(oldType, type);
        adminSysNoticeService.addSingleNoticeToUser(userRolesVo.getUid(), uid, title, content, "Sys");
    }
    return CommonResult.successResponse(null, "修改成功!");
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) UserRole(top.hcode.hoj.pojo.entity.user.UserRole) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) UserInfo(top.hcode.hoj.pojo.entity.user.UserInfo) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)97 Transactional (org.springframework.transaction.annotation.Transactional)41 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)40 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)34 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)28 Session (org.apache.shiro.session.Session)24 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)21 Judge (top.hcode.hoj.pojo.entity.judge.Judge)17 LambdaUpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper)16 HttpSession (javax.servlet.http.HttpSession)14 Problem (top.hcode.hoj.pojo.entity.problem.Problem)14 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)13 StatusNotFoundException (top.hcode.hoj.common.exception.StatusNotFoundException)11 Group (top.hcode.hoj.pojo.entity.group.Group)11 Date (java.util.Date)10 Discussion (top.hcode.hoj.pojo.entity.discussion.Discussion)10 Contest (top.hcode.hoj.pojo.entity.contest.Contest)8 User (com.baomidou.mybatisplus.samples.wrapper.entity.User)5 Result (org.jeecg.common.api.vo.Result)5 LoginUser (org.jeecg.common.system.vo.LoginUser)5