Search in sources :

Example 86 with UpdateWrapper

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

the class RejudgeManager method rejudge.

@Transactional(rollbackFor = Exception.class)
public Judge rejudge(Long submitId) throws StatusFailException {
    Judge judge = judgeEntityService.getById(submitId);
    boolean isContestSubmission = judge.getCid() != 0;
    boolean resetContestRecordResult = true;
    // 如果是非比赛题目
    if (!isContestSubmission) {
        // 如果该题已经是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());
            userAcproblemEntityService.remove(userAcproblemQueryWrapper);
        }
    } else {
        // 将对应比赛记录设置成默认值
        UpdateWrapper<ContestRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("submit_id", submitId).setSql("status=null,score=null");
        resetContestRecordResult = contestRecordEntityService.update(updateWrapper);
    }
    // 清除该提交对应的测试点结果
    QueryWrapper<JudgeCase> judgeCaseQueryWrapper = new QueryWrapper<>();
    judgeCaseQueryWrapper.eq("submit_id", submitId);
    judgeCaseEntityService.remove(judgeCaseQueryWrapper);
    boolean hasSubmitIdRemoteRejudge = isHasSubmitIdRemoteRejudge(judge.getVjudgeSubmitId(), judge.getStatus());
    // 设置默认值
    // 开始进入判题队列
    judge.setStatus(Constants.Judge.STATUS_PENDING.getStatus());
    judge.setVersion(judge.getVersion() + 1);
    judge.setJudger("").setTime(null).setMemory(null).setErrorMessage(null).setOiRankScore(null).setScore(null);
    boolean result = judgeEntityService.updateById(judge);
    if (result && resetContestRecordResult) {
        // 调用判题服务
        Problem problem = problemEntityService.getById(judge.getPid());
        if (problem.getIsRemote()) {
            // 如果是远程oj判题
            remoteJudgeDispatcher.sendTask(judge, problem.getProblemId(), isContestSubmission, hasSubmitIdRemoteRejudge);
        } else {
            judgeDispatcher.sendTask(judge, isContestSubmission);
        }
        return judge;
    } else {
        throw new StatusFailException("重判失败!请重新尝试!");
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) JudgeCase(top.hcode.hoj.pojo.entity.judge.JudgeCase) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) ContestRecord(top.hcode.hoj.pojo.entity.contest.ContestRecord) Problem(top.hcode.hoj.pojo.entity.problem.Problem) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) UserAcproblem(top.hcode.hoj.pojo.entity.user.UserAcproblem) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Transactional(org.springframework.transaction.annotation.Transactional)

Example 87 with UpdateWrapper

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

the class ChooseUtils method chooseFixedServer.

@Transactional(rollbackFor = Exception.class)
public JudgeServer chooseFixedServer(Boolean isRemote, String fixedTag, Integer index, Integer total) {
    // 获取该微服务的所有健康实例
    List<Instance> instances = getInstances(JudgeServiceName);
    if (instances.size() <= 0) {
        return null;
    }
    List<String> keyList = new ArrayList<>();
    // 获取当前健康实例取出ip和port拼接
    for (Instance instance : instances) {
        keyList.add(instance.getIp() + ":" + instance.getPort());
    }
    // 过滤出小于或等于规定最大并发判题任务数的服务实例且健康的判题机
    QueryWrapper<JudgeServer> judgeServerQueryWrapper = new QueryWrapper<>();
    judgeServerQueryWrapper.in("url", keyList).eq("is_remote", isRemote).last(// 开启悲观锁
    "for update");
    /**
     * 如果一个条件无法通过索引快速过滤,存储引擎层面就会将所有记录加锁后返回,
     * 再由MySQL Server层进行过滤,但在实际使用过程当中,MySQL做了一些改进,
     * 在MySQL Server过滤条件,发现不满足后,会调用unlock_row方法,
     * 把不满足条件的记录释放锁 (违背了二段锁协议的约束)。
     */
    List<JudgeServer> judgeServerList = judgeServerEntityService.list(judgeServerQueryWrapper);
    // CF的VJ判題需要一机一题(根据序号保持一定的固定)
    int len = judgeServerList.size();
    for (int i = 0; i < len; i++) {
        if (i % total == index) {
            JudgeServer judgeServer = judgeServerList.get(i);
            UpdateWrapper<JudgeServer> judgeServerUpdateWrapper = new UpdateWrapper<>();
            judgeServerUpdateWrapper.set(fixedTag, false).eq("id", judgeServer.getId()).eq(fixedTag, true);
            boolean isOk = judgeServerEntityService.update(judgeServerUpdateWrapper);
            if (isOk) {
                return judgeServer;
            }
        }
    }
    return null;
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) Instance(com.alibaba.nacos.api.naming.pojo.Instance) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) JudgeServer(top.hcode.hoj.pojo.entity.judge.JudgeServer) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 88 with UpdateWrapper

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

the class GroupContestManager method updateContest.

public void updateContest(AdminContestVo adminContestVo) throws StatusForbiddenException, StatusNotFoundException, StatusFailException {
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    Long cid = adminContestVo.getId();
    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("对不起,您无权限操作!");
    }
    Contest contest1 = BeanUtil.copyProperties(adminContestVo, Contest.class, "starAccount");
    JSONObject accountJson = new JSONObject();
    accountJson.set("star_account", adminContestVo.getStarAccount());
    contest1.setStarAccount(accountJson.toString());
    Contest oldContest = contestEntityService.getById(contest1.getId());
    boolean isOk = contestEntityService.saveOrUpdate(contest1);
    if (isOk) {
        if (!contest1.getAuth().equals(Constants.Contest.AUTH_PUBLIC.getCode())) {
            if (!Objects.equals(oldContest.getPwd(), contest1.getPwd())) {
                UpdateWrapper<ContestRegister> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("cid", contest1.getId());
                contestRegisterEntityService.remove(updateWrapper);
            }
        }
    } else {
        throw new StatusFailException("修改失败");
    }
}
Also used : Group(top.hcode.hoj.pojo.entity.group.Group) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) ContestRegister(top.hcode.hoj.pojo.entity.contest.ContestRegister) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) StatusNotFoundException(top.hcode.hoj.common.exception.StatusNotFoundException) JSONObject(cn.hutool.json.JSONObject) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Session(org.apache.shiro.session.Session)

Example 89 with UpdateWrapper

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

the class GroupContestManager method changeContestVisible.

public void changeContestVisible(Long cid, Boolean visible) throws StatusForbiddenException, StatusNotFoundException, 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("对不起,您无权限操作!");
    }
    UpdateWrapper<Contest> contestUpdateWrapper = new UpdateWrapper<>();
    contestUpdateWrapper.eq("id", cid).set("visible", visible);
    boolean isOK = contestEntityService.update(contestUpdateWrapper);
    if (!isOK) {
        throw new StatusFailException("修改失败");
    }
}
Also used : Group(top.hcode.hoj.pojo.entity.group.Group) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) StatusNotFoundException(top.hcode.hoj.common.exception.StatusNotFoundException) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Session(org.apache.shiro.session.Session)

Example 90 with UpdateWrapper

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

the class GroupProblemManager method applyPublic.

public void applyPublic(Long pid, Boolean isApplied) throws StatusNotFoundException, StatusForbiddenException, 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);
    if (isApplied) {
        // 申请
        problemUpdateWrapper.set("apply_public_progress", 1);
    } else {
        // 取消
        problemUpdateWrapper.set("apply_public_progress", null);
        problemUpdateWrapper.set("is_group", true);
    }
    boolean isOk = problemEntityService.update(problemUpdateWrapper);
    if (!isOk) {
        throw new StatusFailException("修改失败");
    }
}
Also used : Group(top.hcode.hoj.pojo.entity.group.Group) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) StatusNotFoundException(top.hcode.hoj.common.exception.StatusNotFoundException) Problem(top.hcode.hoj.pojo.entity.problem.Problem) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Session(org.apache.shiro.session.Session)

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