beetlsql3-dev
Mlxa0324 2 years ago
parent 9418dfc593
commit 15b0810044

@ -1,5 +1,6 @@
package com.ibeetl.jlw.entity;
import cn.hutool.core.util.ReflectUtil;
import cn.jlw.util.excel.ExcelSelector;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
@ -13,7 +14,10 @@ import lombok.EqualsAndHashCode;
import org.beetl.sql.annotation.entity.AssignID;
import javax.validation.constraints.NotNull;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import static cn.jlw.util.excel.ExcelSelector.ID_SEPARATOR;
@ -324,4 +328,22 @@ public class TeacherOpenCourseMergeResourcesQuestion extends BaseEntity {
public void setTeacherOpenCourseMergeCourseInfoId(Long teacherOpenCourseMergeCourseInfoId) {
this.teacherOpenCourseMergeCourseInfoId = teacherOpenCourseMergeCourseInfoId;
}
public List<String> takeQuestionOptionConcatList() {
List<String> result = new ArrayList<>();
for (Field field : optionFields) {
Object fieldValue = ReflectUtil.getFieldValue(this, field);
if (fieldValue != null) {
result.add(fieldValue.toString());
}
}
return result;
}
/**
*
*/
private final static Field[] optionFields = ReflectUtil.getFields(ResourcesQuestion.class, f -> f.getName().startsWith("questionOption"));
}

@ -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 "";
}
}
Loading…
Cancel
Save