若依RuoYi框架中实现考试试题批量导⼊、业务逻辑处理以及
错误格式试题信息提醒功能
validation框架⼀、业务背景介绍:最近在开发⼀个考试功能模块,在试题录⼊部分需要做批量导⼊试题的功能,试题类型有:单选题、多选题、判断题、
简答题已经应⽤题,现在做的是⼀张表⾥可以按要求填⼊多种类型试题,也就是⼀次可以导⼊多种类型试题。导⼊模板截图如下:
⼆、实现思路:由于后台试题关联的表有两张表,因此模板和后台试题实体类并不是⼀⼀对应的关系,因此需要在后台定义⼀个类型于试题
实体类但和模板字段对应的试题类,之后再在后台解析这种模板实体类集合,拼接成现有试题增加⽅法⾥需要的格式,进⾏⼀⼀添加。
package am.viewmodel.admin.question;
import java.io.Serializable;
import straints.NotBlank;
import com.ruoyi.framework.aspectj.lang.annotation.Excel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
perimental.Accessors;
@Data
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@Accessors(chain = true)
public class QuestionTemplateVM implements Serializable{
private static final long serialVersionUID = 1L;
//试题id
private Integer id;
/**题⽬*/
@NotBlank
@Excel(name = "试题名称", height= 40)
private String title;
/
**题⽬类型*/
@NotBlank
@Excel(name = "试题类型", prompt="请填对应的数字 1.单选题 2.多选题 3.判断题 5.简答题 6.应⽤题", height= 40)
//1.单选题 2.多选题 3.判断题 5简答题 6.应⽤题
private Integer questionType;
/**试题等级*/
@NotBlank
@Excel(name = "试题等级", prompt="请填对应的数字 1.⼀级 2.⼆级 3.三级 4.四级 5.五级 6.六级 7.七级 8.⼋级", height= 40)
private Integer questionLevel;
/**考核类别*/
@NotBlank
@Excel(name = "考核类别", height= 40, prompt="请填对应的数字 4.⼀级测评 5.⼆级测评 6.三级测评 7.四级测评 8.五级测评 9.六级测评 10.七级测评 11.⼋级测评    private Integer testId;
/**选项A*/
@Excel(name = "选项A", height= 40)
@Excel(name = "选项A", height= 40)
private String optionA;
/**选项B*/
@Excel(name = "选项B", height= 40)
private String optionB;
/**选项C*/
@Excel(name = "选项C", height= 40)
private String optionC;
/**选项D*/
@Excel(name = "选项D", height= 40)
private String optionD;
/**选项E*/
@Excel(name = "选项E", height= 40)
private String optionE;
/**选项F*/
@Excel(name = "选项F", height= 40)
private String optionF;
/**正确答案*/
@Excel(name = "单/判/填/应/简的答案", width=24, height= 40, prompt="1.单选/多选/判断/简答/应⽤题的答案  2.为ABCD其中的⼀个字母 3.或者是⼀段⽂字")    //单选题/判断题/填空题/应⽤题答案, 例如:A
private String correct;
/**正确答案*/
@Excel(name = "多选题答案", width=20, height= 40, prompt="1.多选题答案  2.⾄少两个选项以上 3.且以英⽂逗号隔开 4.例如:A,B")
//多选题答案,例如:A,B 以英⽂逗号隔开
private String correctArray;
/**解析*/
@NotBlank
@Excel(name = "答案解析", height= 40)
private String analyze;
/**分数*/
@NotBlank
@Excel(name = "分数", height= 40)
private String score;
/**难度星数*/
@NotBlank
@Excel(name = "难度星数",  height= 40, prompt="1.代表难度星星的数量 2.填数字 3.最⼤5,最⼩1")
//难度星数:1到5的数字
private Integer difficult;
}
三、关键代码展⽰(QuestionController 和 QuestionServiceImpl)
A.QuestionController ⽅法代码展⽰如下:
/**
* PC端后台系统试题批量导⼊功能
* @param file
* @param updateSupport
* @return
* @throws Exception
*/
@Log(title = "试题导⼊管理", businessType = BusinessType.IMPORT)
@PreAuthorize("@ss.hasPermi('exam:question:import')")
@PostMapping("/importData")
@PostMapping("/importData")
public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
//解析Excel表格,拿到导⼊实体集合List<QuestionTemplateVM>
ExcelUtil<QuestionTemplateVM> util = new ExcelUtil<QuestionTemplateVM>(QuestionTemplateVM.class); List<QuestionTemplateVM> abKhstList = util.InputStream());
//这是⼀个带选项前缀、选项对应内容以及选项对应分数的实体类
List<QItemVO> items = null;
//QRequestVO此格式就是后台试题增加⽅法⾥需要的格式
List<QRequestVO> QRequestVOList = new ArrayList<>();
for(QuestionTemplateVM questionTemplateVM : abKhstList) {
//导⼊试题标题和试题类型不能为空,要不然不进⽅法
Title() != "" && QuestionType() != null) {
items = new ArrayList<>();
QRequestVO qRequestVO = new QRequestVO();
QItemVO qItemVO = null;
//说明是单选题
QuestionType() == 1) {
for (int i = 0; i < 4; i++) {
switch (String.valueOf(i)) {
case "0":
qItemVO = new QItemVO();
qItemVO.setPrefix("A");
qItemVO.OptionA());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "1":
qItemVO = new QItemVO();
qItemVO.setPrefix("B");
qItemVO.OptionB());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "2":
qItemVO = new QItemVO();
qItemVO.setPrefix("C");
qItemVO.OptionC());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "3":
qItemVO = new QItemVO();
qItemVO.setPrefix("D");
qItemVO.OptionD());
qItemVO.setScore(null);
items.add(qItemVO);
break;
default:
break;
}
}
}else QuestionType() == 2) {//说明多选题
OptionF() != "") {//说明有6个选项
for (int i = 0; i < 6; i++) {
switch (String.valueOf(i)) {
case "0":
qItemVO = new QItemVO();
qItemVO.setPrefix("A");
qItemVO.OptionA());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "1":
qItemVO = new QItemVO();
qItemVO.setPrefix("B");
qItemVO.OptionB());
qItemVO.OptionB());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "2":
qItemVO = new QItemVO();
qItemVO.setPrefix("C");
qItemVO.OptionC());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "3":
qItemVO = new QItemVO();
qItemVO.setPrefix("D");
qItemVO.OptionD());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "4":
qItemVO = new QItemVO();
qItemVO.setPrefix("E");
qItemVO.OptionE());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "5":
qItemVO = new QItemVO();
qItemVO.setPrefix("F");
qItemVO.OptionF());
qItemVO.setScore(null);
items.add(qItemVO);
break;
default:
break;
}
}
}else OptionF() == "" && OptionE() != "") {//说明有5个选项    for (int i = 0; i < 6; i++) {
switch (String.valueOf(i)) {
case "0":
qItemVO = new QItemVO();
qItemVO.setPrefix("A");
qItemVO.OptionA());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "1":
qItemVO = new QItemVO();
qItemVO.setPrefix("B");
qItemVO.OptionB());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "2":
qItemVO = new QItemVO();
qItemVO.setPrefix("C");
qItemVO.OptionC());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "3":
qItemVO = new QItemVO();
qItemVO.setPrefix("D");
qItemVO.OptionD());
qItemVO.setScore(null);
items.add(qItemVO);
items.add(qItemVO);
break;
case "4":
qItemVO = new QItemVO();
qItemVO.setPrefix("E");
qItemVO.OptionE());
qItemVO.setScore(null);
items.add(qItemVO);
break;
default:
break;
}
}
}else {//必须有四个选项
for (int i = 0; i < 4; i++) {
switch (String.valueOf(i)) {
case "0":
qItemVO = new QItemVO();
qItemVO.setPrefix("A");
qItemVO.OptionA());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "1":
qItemVO = new QItemVO();
qItemVO.setPrefix("B");
qItemVO.OptionB());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "2":
qItemVO = new QItemVO();
qItemVO.setPrefix("C");
qItemVO.OptionC());
qItemVO.setScore(null);
items.add(qItemVO);
break;
case "3":
qItemVO = new QItemVO();
qItemVO.setPrefix("D");
qItemVO.OptionD());
qItemVO.setScore(null);
items.add(qItemVO);
break;
default:
break;
}
}
}
//试题模板中多选题正确答案是以逗号隔开字符串格式,后台需要转成数组格式进⾏保存    CorrectArray() != "") {
String cArrayString = CorrectArray();
String[] arrayValue = cArrayString.split(",");
List<String> strs = Arrays.asList(arrayValue);
qRequestVO.setCorrectArray(strs);
}
}else QuestionType() == 3) {//说明判断题
for (int i = 0; i < 2; i++) {
switch (String.valueOf(i)) {
case "0":
qItemVO = new QItemVO();
qItemVO.setPrefix("A");
qItemVO.OptionA());
qItemVO.setScore(null);
items.add(qItemVO);
break;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。