kongzy
2024-09-14 f0f00e9ba8a755e4317e029d73b69a92ad9f9df1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.gkhy.exam.admin;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.gkhy.exam.system.domain.ExQuestion;
import com.gkhy.exam.system.service.ExQuestionService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GkhyAdminApplication.class)
@ActiveProfiles("dev")
@Slf4j
public class QuestionTest {
    @Autowired
    private ExQuestionService questionService;
 
    // 定义选项数量
    private static final int OPTION_COUNT = 4;
    // 随机生成器
    private static final Random random = new Random();
 
    @Test
    public void questionData(){
        List<ExQuestion>questions=new ArrayList<>();
        for(int i=0;i<300;i++){
            ExQuestion question=generateSingleQuestion();
            questions.add(question);
            question=generateMultiQuestion();
            questions.add(question);
            question=generateJudgeQuestion();
            questions.add(question);
            if(questions.size()>100){
                questionService.saveBatch(questions);
                questions.clear();
            }
        }
        if(questions.size()>0){
            questionService.saveBatch(questions);
            questions.clear();
        }
//        ExQuestion question=generateSingleQuestion();
//        System.out.println(question);
//        question=generateMultiQuestion();
//        System.out.println(question);
//        question=generateJudgeQuestion();
//        System.out.println(question);
    }
 
    // 生成单选题目
    public  ExQuestion generateSingleQuestion() {
        int a=random.nextInt(1000);
        int b=random.nextInt(1000);
        int sum=a+b;
        List<String> options=new ArrayList<>();
        options.add("A");
        options.add("B");
        options.add("C");
        options.add("D");
        int index=random.nextInt(4);
        String title="算术运算题,整数"+a+"与整数"+b+"之和,等于多少?";
        JSONObject object=new JSONObject();
        object.put("analyze","无");
        JSONArray jsonArray=new JSONArray();
        for(int i=0;i<4;i++){
            if(i==index){
                JSONObject itemObject=new JSONObject();
                itemObject.put("prefix",options.get(index));
                itemObject.put("content",sum);
                jsonArray.add(itemObject);
            }else{
                JSONObject itemObject=new JSONObject();
                itemObject.put("prefix",options.get(i));
                itemObject.put("content",randomInt(sum));
                jsonArray.add(itemObject);
            }
        }
        object.put("items",jsonArray);
        ExQuestion question=new ExQuestion();
        question.setQuestionType(1);
        question.setBankId(16L);
        question.setCompanyId(21L);
        question.setAnswer(options.get(index));
        question.setTitle(title);
        question.setPrivatize(0);
        question.setContent(JSONObject.toJSONString(object));
        return question;
    }
 
    // 生成多选题目
    public  ExQuestion generateMultiQuestion() {
        int a=random.nextInt(1000);
        int b=random.nextInt(1000);
        int sum=a+b;
        List<String> options=new ArrayList<>();
        options.add("A");
        options.add("B");
        options.add("C");
        options.add("D");
        options.add("E");
        int indexa=random.nextInt(5);
        int indexb=random.nextInt(5);
        String title="算术运算题,整数"+a+"与整数"+b+"之和,等于多少?";
        JSONObject object=new JSONObject();
        object.put("analyze","无");
        JSONArray jsonArray=new JSONArray();
        for(int i=0;i<5;i++){
            if(i==indexa){
                JSONObject itemObject=new JSONObject();
                itemObject.put("prefix",options.get(indexa));
                itemObject.put("content",sum);
                jsonArray.add(itemObject);
            }else if(i==indexb) {
                JSONObject itemObject=new JSONObject();
                itemObject.put("prefix",options.get(indexb));
                itemObject.put("content",sum);
                jsonArray.add(itemObject);
            }else{
                JSONObject itemObject=new JSONObject();
                itemObject.put("prefix",options.get(i));
                itemObject.put("content",randomInt(sum));
                jsonArray.add(itemObject);
            }
        }
        object.put("items",jsonArray);
        ExQuestion question=new ExQuestion();
        question.setQuestionType(2);
        question.setBankId(16L);
        question.setCompanyId(21L);
        String answer="";
        if(indexa==indexb){
            answer=options.get(indexa);
        }else{
            if(indexa>indexb){
                answer=options.get(indexb)+","+options.get(indexa);
            }else{
                answer=options.get(indexa)+","+options.get(indexb);
            }
        }
        question.setAnswer(answer);
        question.setTitle(title);
        question.setPrivatize(0);
        question.setContent(JSONObject.toJSONString(object));
        return question;
    }
 
    // 生成判断题目
    public  ExQuestion generateJudgeQuestion() {
        int a=random.nextInt(1000);
        int b=random.nextInt(1000);
        int sum=a+b;
        List<String> options=new ArrayList<>();
        options.add("A");
        options.add("B");
        int index=random.nextInt(2);
        String title="算术运算题,整数"+a+"与整数"+b+"之和,等于="+(index==0?sum:randomInt(sum));
        JSONObject object=new JSONObject();
        object.put("analyze","无");
        JSONArray jsonArray=new JSONArray();
        JSONObject itemObject=new JSONObject();
        itemObject.put("prefix",options.get(0));
        itemObject.put("content","是");
        jsonArray.add(itemObject);
        itemObject=new JSONObject();
        itemObject.put("prefix",options.get(1));
        itemObject.put("content","否");
        jsonArray.add(itemObject);
        object.put("items",jsonArray);
        ExQuestion question=new ExQuestion();
        question.setQuestionType(3);
        question.setBankId(16L);
        question.setCompanyId(21L);
        question.setAnswer(index==0?options.get(0):options.get(1));
        question.setTitle(title);
        question.setPrivatize(0);
        question.setContent(JSONObject.toJSONString(object));
        return question;
    }
 
    public String randomInt(int answer){
        int a=random.nextInt(2000);
        while (a==answer) {
            a = random.nextInt(2000);
        }
        return a+"";
    }
 
 
}