1. 程式人生 > 資料庫 >使用者答題接收答案,並存入資料庫

使用者答題接收答案,並存入資料庫

使用者答題接收答案,並存入資料庫

接收方式:鍵值對接收

 @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", "成功");
    }