用户答题接收答案,并存入数据库
接收方式:键值对接收
@GetMapping("/answers")
@ApiImplicitParams({
@ApiImplicitParam(name = "djPaperid", value = "试卷编号", required = true, dataType = "Integer"),
@ApiImplicitParam(name = "djUserid", value = "答题者Id", required = true, dataType = "String"),
@ApiImplicitParam(name = "djQuestionsids", value = "答题ID", required = true, dataType = "String"),
@ApiImplicitParam(name = "djAnswers", value = "答题内容", required = true, dataType = "String"),
@ApiImplicitParam(name = "djStarttime", value = "开始时间", required = true, dataType = "Date")})
public void answers(HttpServletResponse response,
Integer djPaperid,
String djUserid,
String djAnswers,
String djStarttime) throws Exception {
BigDecimal b0 = new BigDecimal(0);
Paper paper = paperService.selectPaperById(djPaperid);
if (paper != null) {
List<JrAnswTwo> twoList = JSONObject.parseArray(djAnswers, JrAnswTwo.class);
StringBuilder stringBuilder = new StringBuilder();
StringBuilder str = null;
for (JrAnswTwo jrAnswTwo : twoList) {
JrAnswTwo answTwo = new JrAnswTwo();
answTwo.setText(jrAnswTwo.getText());
answTwo.setIsJD(jrAnswTwo.getIsJD());
jrAnswTwoService.insertJrAnswTwo(answTwo);
str = stringBuilder.append(answTwo.getId()).append(",");
//1 单选 2 判断 3 多选 4 简答
Questions question = questionsService.selectQuestionsById(jrAnswTwo.getId().toString());
String stAnswer = question.getStAnswer();//正确答案
if ("4".equals(question.getQuestionType())) continue;
if ("3".equals(question.getQuestionType())){
String[] one = jrAnswTwo.getText().split(",");//考试答案
String[] two = stAnswer.split(",");//正确答案
if (one.length != two.length) continue;
for (int i = 0; i < one.length; i++) {
if (one[i].equals(two[i])){
String s = StringUtil.isEmpty(paper.getManyScore()) ? "0" : paper.getManyScore();
b0 = b0.add(new BigDecimal(s));
}
}
continue;
}
if ("2".equals(question.getQuestionType())){
String s = StringUtil.isEmpty(paper.getJudgmentScore()) ? "0" : paper.getJudgmentScore();
b0 = b0.add(new BigDecimal(s));
continue;
}
if ("1".equals(question.getQuestionType())){
String s = StringUtil.isEmpty(paper.getSingleScore()) ? "0" : paper.getSingleScore();
b0 = b0.add(new BigDecimal(s));
continue;
}
}
Answers answer = new Answers();
answer.setDjId(UUIDUtils.getByUUId());
answer.setDjEndtime(new Date());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date strtodate = formatter.parse(djStarttime);
answer.setDjStarttime(strtodate);
String s = str.toString();
answer.setDjContent(s.substring(0,s.length() - 1));
answer.setDjPaperid(djPaperid);
answer.setDjScore(b0);
answer.setDjUserid(djUserid);
answersService.insertAnswers(answer);
}
writeJSON(b0, response, "200", "成功");
}