智慧保安后台管理-外网项目备份
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
 
package org.springblade.modules.exam.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.FTP.FtpUtil;
import org.springblade.modules.exam.entity.ExamAnswerRecord;
import org.springblade.modules.exam.entity.ExamSubjectChoices;
import org.springblade.modules.exam.entity.ExamSubjectOption;
import org.springblade.modules.exam.excel.ExamSubjectExcel;
import org.springblade.modules.exam.mapper.ExamSubjectChoicesMapper;
import org.springblade.modules.exam.service.ExamAnswerRecordService;
import org.springblade.modules.exam.service.ExamSubjectChoicesService;
import org.springblade.modules.exam.service.ExamSubjectOptionService;
import org.springblade.modules.exam.vo.ExamSubjectChoicesVO;
import org.springblade.modules.simulateexam.entity.SimulateExamAnswerRecord;
import org.springblade.modules.simulateexam.service.SimulateExamAnswerRecordService;
import org.springblade.modules.system.service.MyAsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
/**
 * 选择题服务实现类
 *
 * @author zhongrj
 */
@Service
public class ExamSubjectChoicesServiceImpl extends ServiceImpl<ExamSubjectChoicesMapper, ExamSubjectChoices> implements ExamSubjectChoicesService {
 
    @Autowired
    private ExamSubjectOptionService examSubjectOptionService;
 
    @Autowired
    private ExamAnswerRecordService examAnswerRecordService;
 
    @Autowired
    private SimulateExamAnswerRecordService simulateExamAnswerRecordService;
    @Autowired
    private MyAsyncService myAsyncService;
 
 
    @Override
    public IPage<ExamSubjectChoicesVO> selectExamSubjectChoicesPage(IPage<ExamSubjectChoicesVO> page, ExamSubjectChoicesVO examSubjectChoices) {
        return page.setRecords(baseMapper.selectExamSubjectChoicesPage(page, examSubjectChoices));
    }
 
    @Override
    public IPage<ExamSubjectChoicesVO> getEexPaperChoices(IPage<ExamSubjectChoicesVO> page, ExamSubjectChoicesVO examSubjectChoices) {
        return page.setRecords(baseMapper.getEexPaperChoices(page, examSubjectChoices));
    }
 
    /**
     * 详情
     *
     * @param examSubjectChoices 选择题信息对象
     */
    @Override
    public ExamSubjectChoicesVO selectExamSubjectChoicesInfo(ExamSubjectChoices examSubjectChoices) {
        return baseMapper.selectExamSubjectChoicesInfo(examSubjectChoices);
    }
 
    @Override
    public boolean updateChoicesValue(String id, String value) {
        return baseMapper.updateChoicesValue(id, value);
    }
 
    /**
     * 新增题目及选项信息
     *
     * @param examSubjectChoices 题目信息对象
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveSubjectChoicesAndOption(ExamSubjectChoicesVO examSubjectChoices) {
        boolean status = false;
        //新增
        if (null == examSubjectChoices.getId()) {
            ExamSubjectChoices subjectChoices = new ExamSubjectChoices();
            subjectChoices.setCreateDate(new Date());
            subjectChoices.setDelFlag(0);
            subjectChoices.setAnswer(examSubjectChoices.getAnswer());
            if (null != examSubjectChoices.getAnalysis() && examSubjectChoices.getAnalysis() != "") {
                subjectChoices.setAnalysis(examSubjectChoices.getAnalysis());
            }
            subjectChoices.setCreator(examSubjectChoices.getCreator());
            subjectChoices.setChoicesType(examSubjectChoices.getChoicesType());
            subjectChoices.setSubjectName(examSubjectChoices.getSubjectName());
            subjectChoices.setScore(examSubjectChoices.getScore());
            subjectChoices.setTktype(examSubjectChoices.getTktype());
            //题目新增
            status = this.save(subjectChoices);
            //判断类型,单选,多选,实操
            if (examSubjectChoices.getChoicesType() == 0 || examSubjectChoices.getChoicesType() == 1 || examSubjectChoices.getChoicesType() == 3) {
                //选项新增
                List<ExamSubjectOption> examSubjectOptions = examSubjectChoices.getExamSubjectOptions();
                examSubjectOptions.forEach(examSubjectOption -> {
                    examSubjectOption.setSubjectChoicesId(subjectChoices.getId());
                    examSubjectOption.setCreateDate(new Date());
                    examSubjectOption.setDelFlag(0);
                    //新增
                    examSubjectOptionService.save(examSubjectOption);
                });
                return true;
            }
        } else {
            //修改
            ExamSubjectChoices subjectChoices = new ExamSubjectChoices();
            subjectChoices.setId(examSubjectChoices.getId());
            subjectChoices.setModifyDate(new Date());
            subjectChoices.setAnswer(examSubjectChoices.getAnswer());
            if (null != examSubjectChoices.getAnalysis() && examSubjectChoices.getAnalysis() != "") {
                subjectChoices.setAnalysis(examSubjectChoices.getAnalysis());
            }
            subjectChoices.setModifier(examSubjectChoices.getCreator());
            subjectChoices.setChoicesType(examSubjectChoices.getChoicesType());
            subjectChoices.setSubjectName(examSubjectChoices.getSubjectName());
            subjectChoices.setScore(examSubjectChoices.getScore());
            subjectChoices.setTktype(examSubjectChoices.getTktype());
            //题目修改
            status = this.updateById(subjectChoices);
            //判断类型,单选,多选
            if (examSubjectChoices.getChoicesType() == 0 || examSubjectChoices.getChoicesType() == 1) {
                //选项修改
                List<ExamSubjectOption> examSubjectOptions = examSubjectChoices.getExamSubjectOptions();
                examSubjectOptions.forEach(examSubjectOption -> {
                    examSubjectOption.setSubjectChoicesId(subjectChoices.getId());
                    examSubjectOption.setModifyDate(new Date());
                    examSubjectOption.setModifier(examSubjectChoices.getCreator());
                    //修改
                    examSubjectOptionService.updateById(examSubjectOption);
                });
                return true;
            }
            //实操
            if (examSubjectChoices.getChoicesType() == 3) {
                //先将选项删除
                baseMapper.removeBySubjectId(examSubjectChoices.getId());
                //选项新增
                List<ExamSubjectOption> examSubjectOptions = examSubjectChoices.getExamSubjectOptions();
                examSubjectOptions.forEach(examSubjectOption -> {
                    examSubjectOption.setSubjectChoicesId(subjectChoices.getId());
                    examSubjectOption.setCreateDate(new Date());
                    examSubjectOption.setDelFlag(0);
                    //新增
                    examSubjectOptionService.save(examSubjectOption);
                });
                return true;
            }
        }
        return status;
    }
 
 
    /**
     * 题库导入
     *
     * @param data      导入数据
     * @param isCovered
     */
    @Override
    public void importSubject(List<ExamSubjectExcel> data, Boolean isCovered) {
        data.forEach(examSubjectExcel -> {
            //题目对象
            ExamSubjectChoices subjectChoices = new ExamSubjectChoices();
            subjectChoices.setCreateDate(new Date());
            subjectChoices.setDelFlag(0);
            subjectChoices.setAnswer(examSubjectExcel.getAnswer());
            if (null != examSubjectExcel.getAnalysis() && examSubjectExcel.getAnalysis() != "") {
                subjectChoices.setAnalysis(examSubjectExcel.getAnalysis());
            }
            subjectChoices.setChoicesType(examSubjectExcel.getChoicesType());
            subjectChoices.setSubjectName(examSubjectExcel.getSubjectName());
            subjectChoices.setScore(examSubjectExcel.getScore());
            subjectChoices.setTktype(examSubjectExcel.getTktype());
            //题目新增
            this.save(subjectChoices);
 
 
            //判断类型,单选,多选选项新增
            if (examSubjectExcel.getChoicesType() == 0 || examSubjectExcel.getChoicesType() == 1) {
                //A选项新增
                ExamSubjectOption examSubjectOption = new ExamSubjectOption();
                //选项新增
                examSubjectOption.setSubjectChoicesId(subjectChoices.getId());
                examSubjectOption.setCreateDate(new Date());
                examSubjectOption.setDelFlag(0);
                examSubjectOption.setOptionName("A");
                examSubjectOption.setOptionContent(examSubjectExcel.getOptionContentA());
                //新增
                examSubjectOptionService.save(examSubjectOption);
 
                //B选项新增
                ExamSubjectOption examSubjectOption1 = new ExamSubjectOption();
                //选项新增
                examSubjectOption1.setSubjectChoicesId(subjectChoices.getId());
                examSubjectOption1.setCreateDate(new Date());
                examSubjectOption1.setDelFlag(0);
                examSubjectOption1.setOptionName("B");
                examSubjectOption1.setOptionContent(examSubjectExcel.getOptionContentB());
                //新增
                examSubjectOptionService.save(examSubjectOption1);
 
                //C选项新增
                ExamSubjectOption examSubjectOption2 = new ExamSubjectOption();
                //选项新增
                examSubjectOption2.setSubjectChoicesId(subjectChoices.getId());
                examSubjectOption2.setCreateDate(new Date());
                examSubjectOption2.setDelFlag(0);
                examSubjectOption2.setOptionName("C");
                examSubjectOption2.setOptionContent(examSubjectExcel.getOptionContentC());
                //新增
                examSubjectOptionService.save(examSubjectOption2);
 
                //D选项新增
                ExamSubjectOption examSubjectOption3 = new ExamSubjectOption();
                //选项新增
                examSubjectOption3.setSubjectChoicesId(subjectChoices.getId());
                examSubjectOption3.setCreateDate(new Date());
                examSubjectOption3.setDelFlag(0);
                examSubjectOption3.setOptionName("D");
                examSubjectOption3.setOptionContent(examSubjectExcel.getOptionContentD());
                //新增
                examSubjectOptionService.save(examSubjectOption3);
            }
 
 
            //多选选项新增
            if (examSubjectExcel.getChoicesType() == 3) {
                //1选项新增
                if (null != examSubjectExcel.getOptionContentA() && examSubjectExcel.getOptionContentA() != "") {
                    ExamSubjectOption examSubjectOption = new ExamSubjectOption();
                    //选项新增
                    examSubjectOption.setSubjectChoicesId(subjectChoices.getId());
                    examSubjectOption.setCreateDate(new Date());
                    examSubjectOption.setDelFlag(0);
                    examSubjectOption.setOptionName("1");
                    examSubjectOption.setOptionContent(examSubjectExcel.getOptionContentA());
                    //新增
                    examSubjectOptionService.save(examSubjectOption);
                }
 
                //2选项新增
                if (null != examSubjectExcel.getOptionContentB() && examSubjectExcel.getOptionContentB() != "") {
                    ExamSubjectOption examSubjectOption1 = new ExamSubjectOption();
                    //选项新增
                    examSubjectOption1.setSubjectChoicesId(subjectChoices.getId());
                    examSubjectOption1.setCreateDate(new Date());
                    examSubjectOption1.setDelFlag(0);
                    examSubjectOption1.setOptionName("2");
                    examSubjectOption1.setOptionContent(examSubjectExcel.getOptionContentB());
                    //新增
                    examSubjectOptionService.save(examSubjectOption1);
                }
 
                //3选项新增
                if (null != examSubjectExcel.getOptionContentC() && examSubjectExcel.getOptionContentC() != "") {
                    ExamSubjectOption examSubjectOption2 = new ExamSubjectOption();
                    //选项新增
                    examSubjectOption2.setSubjectChoicesId(subjectChoices.getId());
                    examSubjectOption2.setCreateDate(new Date());
                    examSubjectOption2.setDelFlag(0);
                    examSubjectOption2.setOptionName("3");
                    examSubjectOption2.setOptionContent(examSubjectExcel.getOptionContentC());
                    //新增
                    examSubjectOptionService.save(examSubjectOption2);
                }
 
                //4选项新增
                if (null != examSubjectExcel.getOptionContentD() && examSubjectExcel.getOptionContentD() != "") {
                    ExamSubjectOption examSubjectOption3 = new ExamSubjectOption();
                    //选项新增
                    examSubjectOption3.setSubjectChoicesId(subjectChoices.getId());
                    examSubjectOption3.setCreateDate(new Date());
                    examSubjectOption3.setDelFlag(0);
                    examSubjectOption3.setOptionName("4");
                    examSubjectOption3.setOptionContent(examSubjectExcel.getOptionContentD());
                    //新增
                    examSubjectOptionService.save(examSubjectOption3);
                }
 
 
                //5选项新增
                if (null != examSubjectExcel.getOptionContentE() && examSubjectExcel.getOptionContentE() != "") {
                    ExamSubjectOption examSubjectOption4 = new ExamSubjectOption();
                    //选项新增
                    examSubjectOption4.setSubjectChoicesId(subjectChoices.getId());
                    examSubjectOption4.setCreateDate(new Date());
                    examSubjectOption4.setDelFlag(0);
                    examSubjectOption4.setOptionName("5");
                    examSubjectOption4.setOptionContent(examSubjectExcel.getOptionContentE());
                    //新增
                    examSubjectOptionService.save(examSubjectOption4);
                }
            }
        });
 
    }
 
 
    /**
     * 判断当前题目的答题结果
     *
     * @param preSubJectId 题目Id
     * @param preResult    提交的结果
     * @param scoreId      成绩id
     * @return
     */
    @Override
    public Integer getAnswerResult(Long preSubJectId, String preResult, Long scoreId) {
        //查询题目信息
        ExamSubjectChoices choices = this.getById(preSubJectId);
        //对比答案
        if (choices.getChoicesType() == 2 || choices.getChoicesType() == 3) {
            //保存答题记录
            boolean result = preResult.equals(choices.getAnswer());
            int status = 0;
            //判断题逻辑
            if (result) {
                status = 1;
            } else {
                status = 2;
            }
            //新增答题记录
            saveExamAns(choices, preSubJectId, preResult, scoreId, result);
            //返回
            return status;
        } else if (choices.getChoicesType() == 0 || choices.getChoicesType() == 1) {
            //处理多选题的答案排序
            String[] split = preResult.split(",");
            StringBuilder builder = new StringBuilder();
            for (String s : split) {
                builder.append(s);
            }
            char[] arrayCh = builder.toString().toCharArray();
            //利用数组帮助类自动排序
            Arrays.sort(arrayCh);
            String sub0 = Arrays.toString(arrayCh);
            String sub = sub0.substring(1, sub0.length() - 1).replaceAll(" ", "");
 
            //保存答题记录
            boolean result = sub.equals(choices.getAnswer());
            int status = 0;
            //判断题逻辑
            if (result) {
                status = 1;
            } else {
                status = 2;
            }
            //新增答题记录
            saveExamAns(choices, preSubJectId, preResult, scoreId, result);
            //返回
            return status;
        }
        return 2;
    }
 
    /**
     * 新增答题记录
     *
     * @param choices      题目
     * @param preSubJectId 上一题题目id
     * @param preResult    上一题答题结果
     * @param scoreId      成绩 id
     * @param result       答题是否正确
     */
    private void saveExamAns(ExamSubjectChoices choices, Long preSubJectId, String preResult, Long scoreId, boolean result) {
        ExamAnswerRecord examAnswerRecord = new ExamAnswerRecord();
        examAnswerRecord.setScoreId(scoreId);
        examAnswerRecord.setAnswerOption(preResult);
        examAnswerRecord.setSubjectChoicesId(preSubJectId);
        examAnswerRecord.setAnswer(choices.getAnswer());
        examAnswerRecord.setSubjectChoicesType(choices.getChoicesType());
        examAnswerRecord.setAnswerTime(new Date());
        int status = 0;
        //判断题逻辑
        if (result) {
            status = 1;
            examAnswerRecord.setAnswerScore(choices.getScore());
        } else {
            status = 2;
            examAnswerRecord.setAnswerScore(0);
        }
        examAnswerRecord.setAnswerResult(status);
        //新增
        examAnswerRecordService.save(examAnswerRecord);
 
        //内网数据同步...
        String s = "insert into exam_answer_record(id,subject_choices_id,subject_choices_type,answer_option,answer" +
            ",answer_time,answer_score,answer_result,score_id) " +
            "values(" + "'" + examAnswerRecord.getId() + "'" +
            "," + "'" + examAnswerRecord.getSubjectChoicesId() + "'" +
            "," + "'" + examAnswerRecord.getSubjectChoicesType() + "'" +
            "," + "'" + examAnswerRecord.getAnswerOption() + "'" +
            "," + "'" + examAnswerRecord.getAnswer() + "'" +
            "," + "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(examAnswerRecord.getAnswerTime()) + "'" +
            "," + "'" + examAnswerRecord.getAnswerScore() + "'" +
            "," + "'" + examAnswerRecord.getAnswerResult() + "'" +
            "," + "'" + examAnswerRecord.getScoreId() + "'" + ")";
        //FtpUtil.sqlFileUpload(s);
        myAsyncService.dataSync(s);
    }
 
    /**
     * 随机查询题库120道
     *
     * @return
     */
    @Override
    public List<String> getExamSubjectChoicesList() {
        return baseMapper.getExamSubjectChoicesList();
    }
 
    /**
     * 判断当前题目的答题结果
     *
     * @param preSubJectId   题目 id
     * @param preResult      提交的结果
     * @param simulateExamId 模拟考试记录 id
     * @return
     */
    @Override
    public Integer getAnswerResultBySimulate(Long preSubJectId, String preResult, Long simulateExamId) {
        //查询题目信息
        ExamSubjectChoices choices = this.getById(preSubJectId);
        //对比答案
        if (choices.getChoicesType() == 2 || choices.getChoicesType() == 3) {
            //保存答题记录
            boolean result = preResult.equals(choices.getAnswer());
            int status = 0;
            //判断题逻辑
            if (result) {
                status = 1;
            } else {
                status = 2;
            }
            //新增模拟考试答题记录
            saveSimulateExamAns(choices, preSubJectId, preResult, simulateExamId, result);
            //返回
            return status;
        } else if (choices.getChoicesType() == 0 || choices.getChoicesType() == 1) {
            //处理多选题的答案排序
            String[] split = preResult.split(",");
            StringBuilder builder = new StringBuilder();
            for (String s : split) {
                builder.append(s);
            }
            char[] arrayCh = builder.toString().toCharArray();
            //利用数组帮助类自动排序
            Arrays.sort(arrayCh);
            String sub0 = Arrays.toString(arrayCh);
            String sub = sub0.substring(1, sub0.length() - 1).replaceAll(" ", "");
 
            //保存模拟考试答题记录
            boolean result = sub.equals(choices.getAnswer());
            int status = 0;
            //判断题逻辑
            if (result) {
                status = 1;
            } else {
                status = 2;
            }
            //新增模拟考试答题记录
            saveSimulateExamAns(choices, preSubJectId, preResult, simulateExamId, result);
            //返回
            return status;
        }
        return 2;
    }
 
    /**
     * 新增模拟考试答题记录
     *
     * @param choices        题目信息
     * @param preSubJectId   题目id
     * @param preResult      提交的答案
     * @param simulateExamId 模拟考试id
     * @param result         答题的结果
     */
    private void saveSimulateExamAns(ExamSubjectChoices choices, Long preSubJectId, String preResult, Long simulateExamId, boolean result) {
        SimulateExamAnswerRecord simulateExamAnswerRecord = new SimulateExamAnswerRecord();
        simulateExamAnswerRecord.setSimulateExamId(simulateExamId);
        simulateExamAnswerRecord.setAnswerOption(preResult);
        simulateExamAnswerRecord.setSubjectChoicesId(preSubJectId);
        simulateExamAnswerRecord.setAnswer(choices.getAnswer());
        simulateExamAnswerRecord.setSubjectChoicesType(choices.getChoicesType());
        simulateExamAnswerRecord.setAnswerTime(new Date());
        int status = 0;
        //判断题逻辑
        if (result) {
            status = 1;
            simulateExamAnswerRecord.setAnswerScore(choices.getScore());
        } else {
            status = 2;
            simulateExamAnswerRecord.setAnswerScore(0);
        }
        simulateExamAnswerRecord.setAnswerResult(status);
        //新增
        simulateExamAnswerRecordService.save(simulateExamAnswerRecord);
    }
}