|
@@ -2,85 +2,164 @@ package com.example.onlinejudge.service.impl;
|
|
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.example.onlinejudge.common.ErrorCode;
|
|
|
+import com.example.onlinejudge.constant.CommonConstant;
|
|
|
+import com.example.onlinejudge.exception.BusinessException;
|
|
|
+import com.example.onlinejudge.exception.ThrowUtils;
|
|
|
+import com.example.onlinejudge.mapper.QuestionMapper;
|
|
|
import com.example.onlinejudge.mapper.QuestionSubmitMapper;
|
|
|
+import com.example.onlinejudge.model.VO.QuestionVO;
|
|
|
+import com.example.onlinejudge.model.VO.UserVO;
|
|
|
+import com.example.onlinejudge.model.dto.question.QuestionQueryRequest;
|
|
|
+import com.example.onlinejudge.model.entity.Question;
|
|
|
import com.example.onlinejudge.model.entity.QuestionSubmit;
|
|
|
+import com.example.onlinejudge.model.entity.User;
|
|
|
import com.example.onlinejudge.model.entity.result.Result;
|
|
|
import com.example.onlinejudge.model.entity.result.ResultCode;
|
|
|
import com.example.onlinejudge.model.enums.JudgeInfoMessageEnum;
|
|
|
import com.example.onlinejudge.model.enums.QuestionSubmitLanguageEnum;
|
|
|
import com.example.onlinejudge.service.QuestionService;
|
|
|
+import com.example.onlinejudge.service.UserService;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+//import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
@Service
|
|
|
-public class QuestionServiceImpl implements QuestionService {
|
|
|
- @Autowired
|
|
|
- private QuestionSubmitMapper questionSubmitMapper;
|
|
|
+public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question>
|
|
|
+ implements QuestionService{
|
|
|
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验题目是否合法
|
|
|
+ * @param question
|
|
|
+ * @param add
|
|
|
+ */
|
|
|
@Override
|
|
|
- public Result receiveCode(Long questionID, String code, String language, String userCases, Integer judgeMode) {
|
|
|
- Long userID = StpUtil.getLoginIdAsLong();
|
|
|
- if (questionID == null)
|
|
|
- return Result.error(ResultCode.PARAM_IS_INVALID);
|
|
|
- String chooseLanguage;
|
|
|
- if (QuestionSubmitLanguageEnum.JAVA.getValue().equals(language)) {
|
|
|
- chooseLanguage = QuestionSubmitLanguageEnum.JAVA.getValue();
|
|
|
- } else if (QuestionSubmitLanguageEnum.C.getValue().equals(language)) {
|
|
|
- chooseLanguage = QuestionSubmitLanguageEnum.C.getValue();
|
|
|
- } else if (QuestionSubmitLanguageEnum.CPP.getValue().equals(language)) {
|
|
|
- chooseLanguage = QuestionSubmitLanguageEnum.CPP.getValue();
|
|
|
- } else {
|
|
|
- return Result.error(ResultCode.PARAM_IS_INVALID.getCode(), "空或未知的编程语言");
|
|
|
+ public void validQuestion(Question question, boolean add) {
|
|
|
+ if (question == null) {
|
|
|
+ throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
|
|
+ }
|
|
|
+ String title = question.getTitle();
|
|
|
+ String content = question.getContent();
|
|
|
+ String tags = question.getTags();
|
|
|
+ String answer = question.getAnswer();
|
|
|
+ String judgeCase = question.getJudgeCase();
|
|
|
+ String judgeConfig = question.getJudgeConfig();
|
|
|
+ // 创建时,参数不能为空
|
|
|
+ if (add) {
|
|
|
+ ThrowUtils.throwIf(StringUtils.isAnyBlank(title, content, tags), ErrorCode.PARAMS_ERROR);
|
|
|
+ }
|
|
|
+ // 有参数则校验
|
|
|
+ if (StringUtils.isNotBlank(title) && title.length() > 80) {
|
|
|
+ throw new BusinessException(ErrorCode.PARAMS_ERROR, "标题过长");
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(content) && content.length() > 8192) {
|
|
|
+ throw new BusinessException(ErrorCode.PARAMS_ERROR, "内容过长");
|
|
|
}
|
|
|
- if (!checkIsSubmitValid(userID, questionID))
|
|
|
- return Result.error(ResultCode.SYSTEM_ERROR.getCode(), "频繁提交,请稍后再试");
|
|
|
- QuestionSubmit questionSubmit;
|
|
|
- if (judgeMode == 0) {
|
|
|
- questionSubmit = new QuestionSubmit(
|
|
|
- null,
|
|
|
- chooseLanguage,
|
|
|
- code,
|
|
|
- JudgeInfoMessageEnum.WAITING.getValue(),
|
|
|
- 0, questionID, userID,
|
|
|
- new Date(),
|
|
|
- new Date(),
|
|
|
- null,
|
|
|
- null,
|
|
|
- 0,
|
|
|
- 0);
|
|
|
- } else {
|
|
|
- questionSubmit = new QuestionSubmit(
|
|
|
- null,
|
|
|
- chooseLanguage,
|
|
|
- code,
|
|
|
- JudgeInfoMessageEnum.WAITING.getValue(),
|
|
|
- 0, questionID, userID,
|
|
|
- new Date(),
|
|
|
- new Date(),
|
|
|
- userCases,
|
|
|
- null,
|
|
|
- 1,
|
|
|
- 0);
|
|
|
+ if (StringUtils.isNotBlank(answer) && answer.length() > 8192) {
|
|
|
+ throw new BusinessException(ErrorCode.PARAMS_ERROR, "答案过长");
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(judgeCase) && judgeCase.length() > 8192) {
|
|
|
+ throw new BusinessException(ErrorCode.PARAMS_ERROR, "判题用例过长");
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(judgeConfig) && judgeConfig.length() > 8192) {
|
|
|
+ throw new BusinessException(ErrorCode.PARAMS_ERROR, "判题配置过长");
|
|
|
}
|
|
|
- questionSubmitMapper.insert(questionSubmit);
|
|
|
- System.out.println(questionSubmit);
|
|
|
- // TODO: 1.提交代码到判题系统 2.更改数据库刷新状态 3.返回判题信息
|
|
|
- return Result.success();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取查询包装类(用户根据哪些字段查询,根据前端传来的请求对象,得到 mybatis 框架支持的查询 QueryWrapper 类)
|
|
|
+ *
|
|
|
+ * @param questionQueryRequest
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@Override
|
|
|
- public boolean checkIsSubmitValid(Long userId, Long questionID) {
|
|
|
- QueryWrapper<QuestionSubmit> queryWrapper = new QueryWrapper<>();
|
|
|
- queryWrapper.eq("user_id", userId).eq("question_id", questionID).orderByDesc("create_time").last("limit 1");
|
|
|
- QuestionSubmit record = questionSubmitMapper.selectOne(queryWrapper);
|
|
|
- System.out.println(record);
|
|
|
- if (record == null) {
|
|
|
- return true;
|
|
|
- } else if (record.getStatus() == 0 || record.getStatus() == 1) {
|
|
|
- return false;
|
|
|
- } else return System.currentTimeMillis() - record.getCreateTime().getTime() >= 2000;
|
|
|
+ public QueryWrapper<Question> getQueryWrapper(QuestionQueryRequest questionQueryRequest) {
|
|
|
+ QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
|
|
|
+ if (questionQueryRequest == null) {
|
|
|
+ return queryWrapper;
|
|
|
+ }
|
|
|
+ Long id = questionQueryRequest.getId();
|
|
|
+ String title = questionQueryRequest.getTitle();
|
|
|
+ String content = questionQueryRequest.getContent();
|
|
|
+ List<String> tags = questionQueryRequest.getTags();
|
|
|
+ String answer = questionQueryRequest.getAnswer();
|
|
|
+ Long userId = questionQueryRequest.getUserId();
|
|
|
+ String sortField = questionQueryRequest.getSortField();
|
|
|
+ String sortOrder = questionQueryRequest.getSortOrder();
|
|
|
+
|
|
|
+ // 拼接查询条件
|
|
|
+ queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
|
|
|
+ queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
|
|
|
+ queryWrapper.like(StringUtils.isNotBlank(answer), "answer", answer);
|
|
|
+// if (CollectionUtils.isNotEmpty(tags)) {
|
|
|
+// for (String tag : tags) {
|
|
|
+// queryWrapper.like("tags", "\"" + tag + "\"");
|
|
|
+// }
|
|
|
+// }
|
|
|
+ queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
|
|
|
+ queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
|
|
|
+ queryWrapper.eq("isDelete", false);
|
|
|
+// queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
|
|
|
+// sortField);
|
|
|
+ return queryWrapper;
|
|
|
}
|
|
|
-}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public QuestionVO getQuestionVO(Question question, HttpServletRequest request) {
|
|
|
+ QuestionVO questionVO = QuestionVO.objToVo(question);
|
|
|
+ // 1. 关联查询用户信息
|
|
|
+ Long userId = question.getUserId();
|
|
|
+ User user = null;
|
|
|
+// if (userId != null && userId > 0) {
|
|
|
+// user = userService.getById(userId);
|
|
|
+// }
|
|
|
+// UserVO userVO = userService.getUserVO(user);
|
|
|
+// questionVO.setUserVO(userVO);
|
|
|
+ return questionVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<QuestionVO> getQuestionVOPage(Page<Question> questionPage, HttpServletRequest request) {
|
|
|
+ List<Question> questionList = questionPage.getRecords();
|
|
|
+ Page<QuestionVO> questionVOPage = new Page<>(questionPage.getCurrent(), questionPage.getSize(), questionPage.getTotal());
|
|
|
+// if (CollectionUtils.isEmpty(questionList)) {
|
|
|
+// return questionVOPage;
|
|
|
+// }
|
|
|
+ // 1. 关联查询用户信息
|
|
|
+ Set<Long> userIdSet = questionList.stream().map(Question::getUserId).collect(Collectors.toSet());
|
|
|
+// Map<Long, List<User>> userIdUserListMap = userService.listByIds(userIdSet).stream()
|
|
|
+// .collect(Collectors.groupingBy(User::getId));
|
|
|
+ // 填充信息
|
|
|
+ List<QuestionVO> questionVOList = questionList.stream().map(question -> {
|
|
|
+ QuestionVO questionVO = QuestionVO.objToVo(question);
|
|
|
+ Long userId = question.getUserId();
|
|
|
+ User user = null;
|
|
|
+// if (userIdUserListMap.containsKey(userId)) {
|
|
|
+// user = userIdUserListMap.get(userId).get(0);
|
|
|
+// }
|
|
|
+// questionVO.setUserVO(userService.getUserVO(user));
|
|
|
+ return questionVO;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ questionVOPage.setRecords(questionVOList);
|
|
|
+ return questionVOPage;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|