|
|
|
@ -0,0 +1,130 @@
|
|
|
|
|
package com.ibeetl.jlw.validator;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
import cn.jlw.util.ToolUtils;
|
|
|
|
|
import com.ibeetl.jlw.entity.TeacherOpenCourseMergeResourcesQuestion;
|
|
|
|
|
import org.apache.commons.lang3.Range;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.apache.logging.log4j.util.Strings;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import static com.ibeetl.jlw.service.strategy.StrategyContext.QuestionTypeConcatEnum.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 问题实体必填验证
|
|
|
|
|
*
|
|
|
|
|
* @author mlx
|
|
|
|
|
* @date 2022/6/1
|
|
|
|
|
**/
|
|
|
|
|
@Component
|
|
|
|
|
public class OpenCourseMergeResourcesQuestionValidator {
|
|
|
|
|
/**
|
|
|
|
|
* 判断选项是否在这个区间
|
|
|
|
|
*/
|
|
|
|
|
private Range<String> options = Range.between("A", "Z");
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 必填校验
|
|
|
|
|
* @param question
|
|
|
|
|
*/
|
|
|
|
|
public String checkQuestion(TeacherOpenCourseMergeResourcesQuestion question) {
|
|
|
|
|
try {
|
|
|
|
|
checkQuestionThrow(question);
|
|
|
|
|
}catch (IllegalArgumentException e) {
|
|
|
|
|
/** 动态对象中,传入一份errMsg */
|
|
|
|
|
question.set("errMsg", e.getMessage());
|
|
|
|
|
return e.getMessage();
|
|
|
|
|
}
|
|
|
|
|
return Strings.EMPTY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 必填校验 抛出异常
|
|
|
|
|
* @param question
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public void checkQuestionThrow(TeacherOpenCourseMergeResourcesQuestion question) {
|
|
|
|
|
Assert.notNull(question, "题目为空");
|
|
|
|
|
List<String> questionOptionConcatList = question.takeQuestionOptionConcatList();
|
|
|
|
|
String error = optionIncludeAnswer(question, questionOptionConcatList,question.getQuestionAnswer());
|
|
|
|
|
Assert.isTrue(StringUtils.isBlank(error), error);
|
|
|
|
|
Assert.notBlank(question.getQuestionStem(), "题干为空");
|
|
|
|
|
Assert.notBlank(question.getQuestionAnswer(), "答案为空");
|
|
|
|
|
// 有空值,返回异常
|
|
|
|
|
Assert.isFalse(optionIsBlank(questionOptionConcatList), "选项有空值");
|
|
|
|
|
// 判断题
|
|
|
|
|
if(MULTIPLE.getTypeConcat().equals(question.getQuestionType())){
|
|
|
|
|
Assert.isFalse(null == questionOptionConcatList || questionOptionConcatList.size()<2, "至少要有两个选项");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断选项内容是否为空
|
|
|
|
|
* @param questionOptionConcatList
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Boolean optionIsBlank(List<String> questionOptionConcatList) {
|
|
|
|
|
if(ObjectUtil.isEmpty(questionOptionConcatList)){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<String> tempList = ToolUtils.deepCopyList(questionOptionConcatList);
|
|
|
|
|
List<String> nullList = new ArrayList<>();
|
|
|
|
|
nullList.add(null);
|
|
|
|
|
List<String> blackList = new ArrayList<>();
|
|
|
|
|
blackList.add("");
|
|
|
|
|
|
|
|
|
|
tempList.remove(nullList);
|
|
|
|
|
tempList.remove(blackList);
|
|
|
|
|
if(tempList.size() != questionOptionConcatList.size()){
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 答案是否在选项内容中
|
|
|
|
|
*
|
|
|
|
|
* @param question
|
|
|
|
|
* @param questionOptionConcatList
|
|
|
|
|
* @param questionAnswer
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private String optionIncludeAnswer(TeacherOpenCourseMergeResourcesQuestion question, List<String> questionOptionConcatList, String questionAnswer) {
|
|
|
|
|
Integer questionType = question.getQuestionType();
|
|
|
|
|
|
|
|
|
|
// 单选和多选的选项不能为空
|
|
|
|
|
if(Arrays.asList(SINGLE.getTypeConcat(), MULTIPLE.getTypeConcat()).contains(questionType) && ObjectUtil.isEmpty(questionOptionConcatList)) {
|
|
|
|
|
return "选项为空";
|
|
|
|
|
}
|
|
|
|
|
if(StringUtils.isBlank(questionAnswer)){
|
|
|
|
|
return "答案为空";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 是判断题
|
|
|
|
|
if (DECIDE.getTypeConcat().equals(questionType)) {
|
|
|
|
|
// 答案只能是对或错,前后不能有空格
|
|
|
|
|
return !questionAnswer.matches("^[对错]$") ? "判断题答案有误": "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 是单选题
|
|
|
|
|
if (SINGLE.getTypeConcat().equals(questionType)) {
|
|
|
|
|
// 单选题只能有一个答案
|
|
|
|
|
return questionAnswer.length() != 1 ? "单选题只能有一个答案": "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (String s : questionAnswer.split(",")) {
|
|
|
|
|
if (!options.contains(s.toUpperCase())) {
|
|
|
|
|
return "答案不在选项内";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|