Search in sources :

Example 66 with UpdateWrapper

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

the class RejudgeServiceImpl method rejudgeContestProblem.

@Override
@Transactional(rollbackFor = Exception.class)
public CommonResult rejudgeContestProblem(Long cid, Long pid) {
    QueryWrapper<Judge> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("cid", cid).eq("pid", pid);
    List<Judge> rejudgeList = judgeService.list(queryWrapper);
    if (rejudgeList.size() == 0) {
        return CommonResult.errorResponse("当前该题目无提交,不可重判!");
    }
    List<Long> submitIdList = new LinkedList<>();
    HashMap<Long, Integer> idMapStatus = new HashMap<>();
    // 全部设置默认值
    for (Judge judge : rejudgeList) {
        idMapStatus.put(judge.getSubmitId(), 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);
        submitIdList.add(judge.getSubmitId());
    }
    boolean resetJudgeResult = judgeService.updateBatchById(rejudgeList);
    // 清除每个提交对应的测试点结果
    QueryWrapper<JudgeCase> judgeCaseQueryWrapper = new QueryWrapper<>();
    judgeCaseQueryWrapper.in("submit_id", submitIdList);
    judgeCaseService.remove(judgeCaseQueryWrapper);
    // 将对应比赛记录设置成默认值
    UpdateWrapper<ContestRecord> updateWrapper = new UpdateWrapper<>();
    updateWrapper.in("submit_id", submitIdList).setSql("status=null,score=null");
    boolean resetContestRecordResult = contestRecordService.update(updateWrapper);
    if (resetContestRecordResult && resetJudgeResult) {
        // 调用重判服务
        Problem problem = problemService.getById(pid);
        if (problem.getIsRemote()) {
            // 如果是远程oj判题
            for (Judge judge : rejudgeList) {
                // 进入重判队列,等待调用判题服务
                remoteJudgeDispatcher.sendTask(judge, judgeToken, problem.getProblemId(), judge.getCid() != 0, isHasSubmitIdRemoteRejudge(judge.getVjudgeSubmitId(), idMapStatus.get(judge.getSubmitId())));
            }
        } else {
            for (Judge judge : rejudgeList) {
                // 进入重判队列,等待调用判题服务
                judgeDispatcher.sendTask(judge, judgeToken, judge.getCid() != 0);
            }
        }
        return CommonResult.successResponse(null, "重判成功!该题目对应的全部提交已进入判题队列!");
    } else {
        return CommonResult.errorResponse("重判失败!请重新尝试!");
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HashMap(java.util.HashMap) ContestRecord(top.hcode.hoj.pojo.entity.contest.ContestRecord) LinkedList(java.util.LinkedList) JudgeCase(top.hcode.hoj.pojo.entity.judge.JudgeCase) Problem(top.hcode.hoj.pojo.entity.problem.Problem) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Transactional(org.springframework.transaction.annotation.Transactional)

Example 67 with UpdateWrapper

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

the class RejudgeServiceImpl method rejudge.

@Override
@Transactional(rollbackFor = Exception.class)
public CommonResult rejudge(Long submitId) {
    Judge judge = judgeService.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());
            userAcproblemService.remove(userAcproblemQueryWrapper);
        }
    } else {
        // 将对应比赛记录设置成默认值
        UpdateWrapper<ContestRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("submit_id", submitId).setSql("status=null,score=null");
        resetContestRecordResult = contestRecordService.update(updateWrapper);
    }
    // 清除该提交对应的测试点结果
    QueryWrapper<JudgeCase> judgeCaseQueryWrapper = new QueryWrapper<>();
    judgeCaseQueryWrapper.eq("submit_id", submitId);
    judgeCaseService.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 = judgeService.updateById(judge);
    if (result && resetContestRecordResult) {
        // 调用判题服务
        Problem problem = problemService.getById(judge.getPid());
        if (problem.getIsRemote()) {
            // 如果是远程oj判题
            remoteJudgeDispatcher.sendTask(judge, judgeToken, problem.getProblemId(), isContestSubmission, hasSubmitIdRemoteRejudge);
        } else {
            judgeDispatcher.sendTask(judge, judgeToken, isContestSubmission);
        }
        return CommonResult.successResponse(judge, "重判成功!该提交已进入判题队列!");
    } else {
        return CommonResult.successResponse(judge, "重判失败!请重新尝试!");
    }
}
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) UserAcproblem(top.hcode.hoj.pojo.entity.user.UserAcproblem) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Transactional(org.springframework.transaction.annotation.Transactional)

Example 68 with UpdateWrapper

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

the class ImageController method uploadAvatar.

@RequestMapping(value = "/upload-avatar", method = RequestMethod.POST)
@RequiresAuthentication
@ResponseBody
@Transactional(rollbackFor = Exception.class)
public CommonResult uploadAvatar(@RequestParam("image") MultipartFile image, HttpServletRequest request) {
    if (image == null) {
        return CommonResult.errorResponse("上传的头像图片文件不能为空!");
    }
    if (image.getSize() > 1024 * 1024 * 2) {
        return CommonResult.errorResponse("上传的头像图片文件大小不能大于2M!");
    }
    // 获取文件后缀
    String suffix = image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf(".") + 1);
    if (!"jpg,jpeg,gif,png,webp".toUpperCase().contains(suffix.toUpperCase())) {
        return CommonResult.errorResponse("请选择jpg,jpeg,gif,png,webp格式的头像图片!");
    }
    // 若不存在该目录,则创建目录
    FileUtil.mkdir(Constants.File.USER_AVATAR_FOLDER.getPath());
    // 通过UUID生成唯一文件名
    String filename = IdUtil.simpleUUID() + "." + suffix;
    try {
        // 将文件保存指定目录
        image.transferTo(FileUtil.file(Constants.File.USER_AVATAR_FOLDER.getPath() + File.separator + filename));
    } catch (Exception e) {
        log.error("头像文件上传异常-------------->", e);
        return CommonResult.errorResponse("服务器异常:头像上传失败!", CommonResult.STATUS_ERROR);
    }
    // 获取当前登录用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 将当前用户所属的file表中avatar类型的实体的delete设置为1;
    fileService.updateFileToDeleteByUidAndType(userRolesVo.getUid(), "avatar");
    // 更新user_info里面的avatar
    UpdateWrapper<UserInfo> userInfoUpdateWrapper = new UpdateWrapper<>();
    userInfoUpdateWrapper.set("avatar", Constants.File.IMG_API.getPath() + filename).eq("uuid", userRolesVo.getUid());
    userInfoService.update(userInfoUpdateWrapper);
    // 插入file表记录
    top.hcode.hoj.pojo.entity.common.File imgFile = new top.hcode.hoj.pojo.entity.common.File();
    imgFile.setName(filename).setFolderPath(Constants.File.USER_AVATAR_FOLDER.getPath()).setFilePath(Constants.File.USER_AVATAR_FOLDER.getPath() + File.separator + filename).setSuffix(suffix).setType("avatar").setUid(userRolesVo.getUid());
    fileService.saveOrUpdate(imgFile);
    // 更新session
    userRolesVo.setAvatar(Constants.File.IMG_API.getPath() + filename);
    session.setAttribute("userInfo", userRolesVo);
    return CommonResult.successResponse(MapUtil.builder().put("uid", userRolesVo.getUid()).put("username", userRolesVo.getUsername()).put("nickname", userRolesVo.getNickname()).put("avatar", Constants.File.IMG_API.getPath() + filename).put("email", userRolesVo.getEmail()).put("number", userRolesVo.getNumber()).put("school", userRolesVo.getSchool()).put("course", userRolesVo.getCourse()).put("signature", userRolesVo.getSignature()).put("realname", userRolesVo.getRealname()).put("github", userRolesVo.getGithub()).put("blog", userRolesVo.getBlog()).put("cfUsername", userRolesVo.getCfUsername()).put("roleList", userRolesVo.getRoles().stream().map(Role::getRole)).map(), "设置新头像成功!");
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) HttpSession(javax.servlet.http.HttpSession) UserInfo(top.hcode.hoj.pojo.entity.user.UserInfo) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Transactional(org.springframework.transaction.annotation.Transactional)

Example 69 with UpdateWrapper

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

the class AccountController method changePassword.

/**
 * @MethodName changePassword
 * @Params * @param null
 * @Description 修改密码的操作,连续半小时内修改密码错误5次,则需要半个小时后才可以再次尝试修改密码
 * @Return
 * @Since 2021/1/8
 */
@PostMapping("/change-password")
@RequiresAuthentication
public CommonResult changePassword(@RequestBody Map params, HttpServletRequest request) {
    String oldPassword = (String) params.get("oldPassword");
    String newPassword = (String) params.get("newPassword");
    // 数据可用性判断
    if (StringUtils.isEmpty(oldPassword) || StringUtils.isEmpty(newPassword)) {
        return CommonResult.errorResponse("请求参数不能为空!");
    }
    if (newPassword.length() < 6 || newPassword.length() > 20) {
        return CommonResult.errorResponse("新密码长度应该为6~20位!");
    }
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 如果已经被锁定半小时不能修改
    String lockKey = Constants.Account.CODE_CHANGE_PASSWORD_LOCK + userRolesVo.getUid();
    // 统计失败的key
    String countKey = Constants.Account.CODE_CHANGE_PASSWORD_FAIL + userRolesVo.getUid();
    HashMap<String, Object> resp = new HashMap<>();
    if (redisUtils.hasKey(lockKey)) {
        long expire = redisUtils.getExpire(lockKey);
        Date now = new Date();
        long minute = expire / 60;
        long second = expire % 60;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        resp.put("code", 403);
        Date afterDate = new Date(now.getTime() + expire * 1000);
        String msg = "由于您多次修改密码失败,修改密码功能已锁定,请在" + minute + "分" + second + "秒后(" + formatter.format(afterDate) + ")再进行尝试!";
        resp.put("msg", msg);
        return CommonResult.successResponse(resp, "修改密码失败!");
    }
    // 与当前登录用户的密码进行比较判断
    if (userRolesVo.getPassword().equals(SecureUtil.md5(oldPassword))) {
        // 如果相同,则进行修改密码操作
        UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
        // 数据库用户密码全部用md5加密
        updateWrapper.set("password", SecureUtil.md5(newPassword)).eq("uuid", userRolesVo.getUid());
        boolean result = userInfoDao.update(updateWrapper);
        if (result) {
            resp.put("code", 200);
            resp.put("msg", "修改密码成功!您将于5秒钟后退出进行重新登录操作!");
            // 清空记录
            redisUtils.del(countKey);
            // 更新session
            userRolesVo.setPassword(SecureUtil.md5(newPassword));
            session.setAttribute("userInfo", userRolesVo);
            return CommonResult.successResponse(resp, "修改密码成功!");
        } else {
            return CommonResult.errorResponse("系统错误:修改密码失败!", CommonResult.STATUS_ERROR);
        }
    } else {
        // 如果不同,则进行记录,当失败次数达到5次,半个小时后才可重试
        Integer count = (Integer) redisUtils.get(countKey);
        if (count == null) {
            // 三十分钟不尝试,该限制会自动清空消失
            redisUtils.set(countKey, 1, 60 * 30);
            count = 0;
        } else if (count < 5) {
            redisUtils.incr(countKey, 1);
        }
        count++;
        if (count == 5) {
            // 清空统计
            redisUtils.del(countKey);
            // 设置锁定更改
            redisUtils.set(lockKey, "lock", 60 * 30);
        }
        resp.put("code", 400);
        resp.put("msg", "原始密码错误!您已累计修改密码失败" + count + "次...");
        return CommonResult.successResponse(resp, "修改密码失败!");
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) SimpleDateFormat(java.text.SimpleDateFormat) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Example 70 with UpdateWrapper

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

the class CommentController method addDiscussionLike.

@GetMapping("/comment-like")
@RequiresAuthentication
@Transactional
public CommonResult addDiscussionLike(@RequestParam("cid") Integer cid, @RequestParam("toLike") Boolean toLike, @RequestParam("sourceId") Integer sourceId, @RequestParam("sourceType") String sourceType, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    QueryWrapper<CommentLike> commentLikeQueryWrapper = new QueryWrapper<>();
    commentLikeQueryWrapper.eq("cid", cid).eq("uid", userRolesVo.getUid());
    CommentLike commentLike = commentLikeService.getOne(commentLikeQueryWrapper, false);
    if (toLike) {
        // 添加点赞
        if (commentLike == null) {
            // 如果不存在就添加
            boolean isSave = commentLikeService.saveOrUpdate(new CommentLike().setUid(userRolesVo.getUid()).setCid(cid));
            if (!isSave) {
                return CommonResult.errorResponse("点赞失败,请重试尝试!");
            }
        }
        // 点赞+1
        Comment comment = commentService.getById(cid);
        if (comment != null) {
            comment.setLikeNum(comment.getLikeNum() + 1);
            commentService.updateById(comment);
            commentService.updateCommentLikeMsg(comment.getFromUid(), userRolesVo.getUid(), sourceId, sourceType);
        }
        return CommonResult.successResponse(null, "点赞成功");
    } else {
        // 取消点赞
        if (commentLike != null) {
            // 如果存在就删除
            boolean isDelete = commentLikeService.removeById(commentLike.getId());
            if (!isDelete) {
                return CommonResult.errorResponse("取消点赞失败,请重试尝试!");
            }
        }
        // 点赞-1
        UpdateWrapper<Comment> commentUpdateWrapper = new UpdateWrapper<>();
        commentUpdateWrapper.setSql("like_num=like_num-1").eq("id", cid);
        commentService.update(commentUpdateWrapper);
        return CommonResult.successResponse(null, "取消成功");
    }
}
Also used : CommentLike(top.hcode.hoj.pojo.entity.discussion.CommentLike) Comment(top.hcode.hoj.pojo.entity.discussion.Comment) 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) 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