Search in sources :

Example 81 with UpdateWrapper

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

the class ImageManager method uploadAvatar.

@Transactional(rollbackFor = Exception.class)
public Map<Object, Object> uploadAvatar(MultipartFile image) throws StatusFailException, StatusSystemErrorException {
    if (image == null) {
        throw new StatusFailException("上传的头像图片文件不能为空!");
    }
    if (image.getSize() > 1024 * 1024 * 2) {
        throw new StatusFailException("上传的头像图片文件大小不能大于2M!");
    }
    // 获取文件后缀
    String suffix = image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf(".") + 1);
    if (!"jpg,jpeg,gif,png,webp".toUpperCase().contains(suffix.toUpperCase())) {
        throw new StatusFailException("请选择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);
        throw new StatusSystemErrorException("服务器异常:头像上传失败!");
    }
    // 获取当前登录用户
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 将当前用户所属的file表中avatar类型的实体的delete设置为1;
    fileEntityService.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());
    userInfoEntityService.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());
    fileEntityService.saveOrUpdate(imgFile);
    // 更新session
    userRolesVo.setAvatar(Constants.File.IMG_API.getPath() + filename);
    session.setAttribute("userInfo", userRolesVo);
    return 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) UserInfo(top.hcode.hoj.pojo.entity.user.UserInfo) StatusSystemErrorException(top.hcode.hoj.common.exception.StatusSystemErrorException) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) Role(top.hcode.hoj.pojo.entity.user.Role) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) StatusSystemErrorException(top.hcode.hoj.common.exception.StatusSystemErrorException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) Session(org.apache.shiro.session.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Example 82 with UpdateWrapper

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

the class AdminContestManager method updateContest.

public void updateContest(AdminContestVo adminContestVo) throws StatusForbiddenException, StatusFailException {
    // 获取当前登录的用户
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 是否为超级管理员
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    // 只有超级管理员和比赛拥有者才能操作
    if (!isRoot && !userRolesVo.getUid().equals(adminContestVo.getUid())) {
        throw new StatusForbiddenException("对不起,你无权限操作!");
    }
    Contest contest = BeanUtil.copyProperties(adminContestVo, Contest.class, "starAccount");
    JSONObject accountJson = new JSONObject();
    accountJson.set("star_account", adminContestVo.getStarAccount());
    contest.setStarAccount(accountJson.toString());
    Contest oldContest = contestEntityService.getById(contest.getId());
    boolean isOk = contestEntityService.saveOrUpdate(contest);
    if (isOk) {
        if (!contest.getAuth().equals(Constants.Contest.AUTH_PUBLIC.getCode())) {
            if (!Objects.equals(oldContest.getPwd(), contest.getPwd())) {
                // 改了比赛密码则需要删掉已有的注册比赛用户
                UpdateWrapper<ContestRegister> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("cid", contest.getId());
                contestRegisterEntityService.remove(updateWrapper);
            }
        }
    } else {
        throw new StatusFailException("修改失败");
    }
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) ContestRegister(top.hcode.hoj.pojo.entity.contest.ContestRegister) JSONObject(cn.hutool.json.JSONObject) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) 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 83 with UpdateWrapper

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

the class AdminProblemManager method updateProblem.

@Transactional(rollbackFor = Exception.class)
public void updateProblem(ProblemDto problemDto) throws StatusForbiddenException, StatusFailException {
    // 获取当前登录的用户
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    boolean isProblemAdmin = SecurityUtils.getSubject().hasRole("problem_admin");
    // 只有超级管理员和题目管理员、题目创建者才能操作
    if (!isRoot && !isProblemAdmin && !userRolesVo.getUsername().equals(problemDto.getProblem().getAuthor())) {
        throw new StatusForbiddenException("对不起,你无权限修改题目!");
    }
    String problemId = problemDto.getProblem().getProblemId().toUpperCase();
    QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("problem_id", problemId);
    Problem problem = problemEntityService.getOne(queryWrapper);
    // 如果problem_id不是原来的且已存在该problem_id,则修改失败!
    if (problem != null && problem.getId().longValue() != problemDto.getProblem().getId()) {
        throw new StatusFailException("当前的Problem ID 已被使用,请重新更换新的!");
    }
    // 记录修改题目的用户
    problemDto.getProblem().setModifiedUser(userRolesVo.getUsername());
    boolean result = problemEntityService.adminUpdateProblem(problemDto);
    if (result) {
        // 更新成功
        if (problem == null) {
            // 说明改了problemId,同步一下judge表
            UpdateWrapper<Judge> judgeUpdateWrapper = new UpdateWrapper<>();
            judgeUpdateWrapper.eq("pid", problemDto.getProblem().getId()).set("display_pid", problemId);
            judgeEntityService.update(judgeUpdateWrapper);
        }
    } else {
        throw new StatusFailException("修改失败");
    }
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Session(org.apache.shiro.session.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Example 84 with UpdateWrapper

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

the class AdminProblemManager method changeProblemAuth.

public void changeProblemAuth(Problem problem) throws StatusFailException, StatusForbiddenException {
    // 普通管理员只能将题目变成隐藏题目和比赛题目
    boolean root = SecurityUtils.getSubject().hasRole("root");
    boolean problemAdmin = SecurityUtils.getSubject().hasRole("problem_admin");
    if (!problemAdmin && !root && problem.getAuth() == 1) {
        throw new StatusForbiddenException("修改失败!你无权限公开题目!");
    }
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    UpdateWrapper<Problem> problemUpdateWrapper = new UpdateWrapper<>();
    problemUpdateWrapper.eq("id", problem.getId()).set("auth", problem.getAuth()).set("modified_user", userRolesVo.getUsername());
    boolean isOk = problemEntityService.update(problemUpdateWrapper);
    if (!isOk) {
        throw new StatusFailException("修改失败");
    }
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Session(org.apache.shiro.session.Session)

Example 85 with UpdateWrapper

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

the class RejudgeManager method rejudgeContestProblem.

@Transactional(rollbackFor = Exception.class)
public void rejudgeContestProblem(Long cid, Long pid) throws StatusFailException {
    QueryWrapper<Judge> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("cid", cid).eq("pid", pid);
    List<Judge> rejudgeList = judgeEntityService.list(queryWrapper);
    if (rejudgeList.size() == 0) {
        throw new StatusFailException("当前该题目无提交,不可重判!");
    }
    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 = judgeEntityService.updateBatchById(rejudgeList);
    // 清除每个提交对应的测试点结果
    QueryWrapper<JudgeCase> judgeCaseQueryWrapper = new QueryWrapper<>();
    judgeCaseQueryWrapper.in("submit_id", submitIdList);
    judgeCaseEntityService.remove(judgeCaseQueryWrapper);
    // 将对应比赛记录设置成默认值
    UpdateWrapper<ContestRecord> updateWrapper = new UpdateWrapper<>();
    updateWrapper.in("submit_id", submitIdList).setSql("status=null,score=null");
    boolean resetContestRecordResult = contestRecordEntityService.update(updateWrapper);
    if (resetContestRecordResult && resetJudgeResult) {
        // 调用重判服务
        Problem problem = problemEntityService.getById(pid);
        if (problem.getIsRemote()) {
            // 如果是远程oj判题
            for (Judge judge : rejudgeList) {
                // 进入重判队列,等待调用判题服务
                remoteJudgeDispatcher.sendTask(judge, problem.getProblemId(), judge.getCid() != 0, isHasSubmitIdRemoteRejudge(judge.getVjudgeSubmitId(), idMapStatus.get(judge.getSubmitId())));
            }
        } else {
            for (Judge judge : rejudgeList) {
                // 进入重判队列,等待调用判题服务
                judgeDispatcher.sendTask(judge, judge.getCid() != 0);
            }
        }
    } else {
        throw new StatusFailException("重判失败!请重新尝试!");
    }
}
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) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Problem(top.hcode.hoj.pojo.entity.problem.Problem) Judge(top.hcode.hoj.pojo.entity.judge.Judge) 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