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("重判失败!请重新尝试!");
}
}
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, "重判失败!请重新尝试!");
}
}
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(), "设置新头像成功!");
}
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, "修改密码失败!");
}
}
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, "取消成功");
}
}
Aggregations