Search in sources :

Example 36 with UpdateWrapper

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

the class AdminTrainingProblemManager method importTrainingRemoteOJProblem.

public void importTrainingRemoteOJProblem(String name, String problemId, Long tid) throws StatusFailException {
    QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("problem_id", name.toUpperCase() + "-" + problemId);
    Problem problem = problemEntityService.getOne(queryWrapper, false);
    // 如果该题目不存在,需要先导入
    if (problem == null) {
        Session session = SecurityUtils.getSubject().getSession();
        UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
        try {
            ProblemStrategy.RemoteProblemInfo otherOJProblemInfo = remoteProblemManager.getOtherOJProblemInfo(name.toUpperCase(), problemId, userRolesVo.getUsername());
            if (otherOJProblemInfo != null) {
                problem = remoteProblemManager.adminAddOtherOJProblem(otherOJProblemInfo, name);
                if (problem == null) {
                    throw new StatusFailException("导入新题目失败!请重新尝试!");
                }
            } else {
                throw new StatusFailException("导入新题目失败!原因:可能是与该OJ链接超时或题号格式错误!");
            }
        } catch (Exception e) {
            throw new StatusFailException(e.getMessage());
        }
    }
    QueryWrapper<TrainingProblem> trainingProblemQueryWrapper = new QueryWrapper<>();
    Problem finalProblem = problem;
    trainingProblemQueryWrapper.eq("tid", tid).and(wrapper -> wrapper.eq("pid", finalProblem.getId()).or().eq("display_id", finalProblem.getProblemId()));
    TrainingProblem trainingProblem = trainingProblemEntityService.getOne(trainingProblemQueryWrapper, false);
    if (trainingProblem != null) {
        throw new StatusFailException("添加失败,该题目已添加或者题目的训练展示ID已存在!");
    }
    TrainingProblem newTProblem = new TrainingProblem();
    boolean isOk = trainingProblemEntityService.saveOrUpdate(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("添加失败!");
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Training(top.hcode.hoj.pojo.entity.training.Training) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Problem(top.hcode.hoj.pojo.entity.problem.Problem) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) ProblemStrategy(top.hcode.hoj.crawler.problem.ProblemStrategy) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Session(org.apache.shiro.session.Session)

Example 37 with UpdateWrapper

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

the class DiscussionManager method getDiscussion.

public DiscussionVo getDiscussion(Integer did) throws StatusNotFoundException, StatusForbiddenException, AccessException {
    // 获取当前登录的用户
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    Discussion discussion = discussionEntityService.getById(did);
    if (discussion.getGid() != null) {
        accessValidator.validateAccess(HOJAccessEnum.GROUP_DISCUSSION);
        if (!isRoot && !discussion.getUid().equals(userRolesVo.getUid()) && !groupValidator.isGroupMember(userRolesVo.getUid(), discussion.getGid())) {
            throw new StatusForbiddenException("对不起,您无权限操作!");
        }
    } else {
        accessValidator.validateAccess(HOJAccessEnum.PUBLIC_DISCUSSION);
    }
    String uid = null;
    if (userRolesVo != null) {
        uid = userRolesVo.getUid();
    }
    DiscussionVo discussionVo = discussionEntityService.getDiscussion(did, uid);
    if (discussionVo == null) {
        throw new StatusNotFoundException("对不起,该讨论不存在!");
    }
    if (discussionVo.getStatus() == 1) {
        throw new StatusForbiddenException("对不起,该讨论已被封禁!");
    }
    // 浏览量+1
    UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
    discussionUpdateWrapper.setSql("view_num=view_num+1").eq("id", discussionVo.getId());
    discussionEntityService.update(discussionUpdateWrapper);
    discussionVo.setViewNum(discussionVo.getViewNum() + 1);
    return discussionVo;
}
Also used : DiscussionVo(top.hcode.hoj.pojo.vo.DiscussionVo) 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) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) Session(org.apache.shiro.session.Session)

Example 38 with UpdateWrapper

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

the class UserRecordEntityServiceImpl 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 = judgeEntityService.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 39 with UpdateWrapper

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

the class StartupRunner method run.

@Override
@Transactional
public void run(String... args) {
    log.info("IP  of the current judge server:" + ip);
    log.info("Port of the current judge server:" + port);
    if (maxTaskNum == -1) {
        maxTaskNum = cpuNum + 1;
    }
    if (ip.equals("-1")) {
        ip = IpUtils.getLocalIpv4Address();
    }
    UpdateWrapper<JudgeServer> judgeServerQueryWrapper = new UpdateWrapper<>();
    judgeServerQueryWrapper.eq("ip", ip).eq("port", port);
    judgeServerEntityService.remove(judgeServerQueryWrapper);
    boolean isOk1 = judgeServerEntityService.save(new JudgeServer().setCpuCore(cpuNum).setIp(ip).setPort(port).setUrl(ip + ":" + port).setMaxTaskNumber(maxTaskNum).setIsRemote(false).setName(name));
    boolean isOk2 = true;
    if (openRemoteJudge) {
        if (maxRemoteTaskNum == -1) {
            maxRemoteTaskNum = cpuNum * 2 + 1;
        }
        isOk2 = judgeServerEntityService.save(new JudgeServer().setCpuCore(cpuNum).setIp(ip).setPort(port).setUrl(ip + ":" + port).setMaxTaskNumber(maxRemoteTaskNum).setIsRemote(true).setName(name));
    }
    if (!isOk1 || !isOk2) {
        log.error("初始化判题机信息到数据库失败,请重新启动试试!");
    } else {
        HashMap<String, Object> judgeServerInfo = judgeServerEntityService.getJudgeServerInfo();
        log.info("HOJ-JudgeServer had successfully started! The judge config and sandbox config Info:" + judgeServerInfo);
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) JudgeServer(top.hcode.hoj.pojo.entity.judge.JudgeServer) Transactional(org.springframework.transaction.annotation.Transactional)

Example 40 with UpdateWrapper

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

the class ScheduleServiceImpl method getCodeforcesRating.

/**
 * 每天3点获取codeforces的rating分数
 */
@Scheduled(cron = "0 0 3 * * *")
// @Scheduled(cron = "0/5 * * * * *")
@Override
public void getCodeforcesRating() {
    String codeforcesUserInfoAPI = "https://codeforces.com/api/user.info?handles=%s";
    QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
    // 查询cf_username不为空的数据
    userInfoQueryWrapper.isNotNull("cf_username");
    List<UserInfo> userInfoList = userInfoEntityService.list(userInfoQueryWrapper);
    for (UserInfo userInfo : userInfoList) {
        // 获取cf名字
        String cfUsername = userInfo.getCfUsername();
        // 获取uuid
        String uuid = userInfo.getUuid();
        // 格式化api
        String ratingAPI = String.format(codeforcesUserInfoAPI, cfUsername);
        try {
            // 连接api,获取json格式对象
            JSONObject resultObject = getCFUserInfo(ratingAPI);
            // 获取状态码
            String status = resultObject.getStr("status");
            // 如果查无此用户,则跳过
            if ("FAILED".equals(status)) {
                continue;
            }
            // 用户信息存放在result列表中的第0个
            JSONObject cfUserInfo = resultObject.getJSONArray("result").getJSONObject(0);
            // 获取cf的分数
            Integer cfRating = cfUserInfo.getInt("rating", null);
            UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
            // 将对应的cf分数修改
            userRecordUpdateWrapper.eq("uid", uuid).set("rating", cfRating);
            boolean result = userRecordEntityService.update(userRecordUpdateWrapper);
            if (!result) {
                log.error("插入UserRecord表失败------------------------------->");
            }
        } catch (Exception e) {
            log.error("爬虫爬取Codeforces Rating分数异常----------------------->{}", e.getMessage());
        }
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    log.info("获取Codeforces Rating成功!");
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) UserInfo(top.hcode.hoj.pojo.entity.user.UserInfo) JSONObject(cn.hutool.json.JSONObject) UserRecord(top.hcode.hoj.pojo.entity.user.UserRecord) Scheduled(org.springframework.scheduling.annotation.Scheduled)

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