张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
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
package com.thhy.secure.modules.biz.exam.service.impl;
 
import com.thhy.general.common.BasicStatus;
import com.thhy.general.config.SysUserInfo;
import com.thhy.general.exception.BasicException;
import com.thhy.general.utils.DeepCopyUtils;
import com.thhy.general.utils.PagingUtils;
import com.thhy.general.utils.UserInfoUtils;
import com.thhy.secure.modules.biz.exam.entity.*;
import com.thhy.secure.modules.biz.exam.mapper.*;
import com.thhy.secure.modules.biz.exam.service.ExamService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.Serializable;
import java.util.*;
 
/**
 *  服务实现类
 * @author zhang_xiao_bo
 * @since 2023-05-26 09:14:42
 */
@Service
public class ExamServiceImpl implements ExamService {
 
 
 
    @Autowired
    private ExamMapper examMapper;
 
    @Autowired
    private ExamRecordMapper recordMapper;
 
    @Autowired
    private QuestionMapper questionMapper;
 
    @Autowired
    private AnswerMapper answerMapper;
 
    @Autowired
    private ExamReAnswerMapper reAnswerMapper;
 
    public Exam get(Serializable id){
        return examMapper.queryById(id);
    }
 
    public List<ExamListVo> findList(ExamDto examDto){
        SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo();
        if(examDto==null)examDto = new ExamDto();
        examDto.setCompanyId(userInfo.getCompanyId());
        return examMapper.findList(examDto);
    }
 
    /**
     * 小程序考核列表
     * @param examDto
     * @return
     */
    @Override
    public List<ExamListVo> miniList(ExamDto examDto) {
        SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo();
        if(examDto==null)examDto = new ExamDto();
        examDto.setCompanyId(userInfo.getCompanyId());
 
        if(userInfo.getUserType()==1){
            //官服人员
            return examMapper.miniList(examDto);
        }else {
            //劳务人员
            String platUserId = examMapper.queryPlatUser(userInfo.getUserId());
            examDto.setUserId(platUserId);
            return examMapper.miniList(examDto);
        }
 
    }
 
    /**
     * 增加和修改
     * @param exam
     */
    @Transactional
    public void addExam(Exam exam){
        SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo();
        if(exam.getExamId() == null){
            //增加操作
            exam.setCreateUser(userInfo.getUserId());
            exam.setCompanyId(userInfo.getCompanyId());
            examMapper.insert(exam);
        }else{
            //修改操作
            exam.setUpdateTime(new Date());
            examMapper.update(exam);
        }
 
    }
 
    /**
     * 修改
     * @param exam
     */
    public void update(Exam exam){
        examMapper.update(exam);
    }
 
    /**
     * 删除
     * @param examId
     */
    public void delete(Serializable examId){
        examMapper.deletelogic(examId);
    }
 
    @Override
    public List<ExamSampleVo> examList(ExamDto examDto) {
        SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo();
        if(examDto==null)examDto = new ExamDto();
        examDto.setCompanyId(userInfo.getCompanyId());
        return examMapper.examList(examDto);
    }
 
    @Override
    public List<ExamSampleListVo> examReportList(ExamDto examDto) {
        SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo();
        if(examDto==null)examDto=new ExamDto();
        examDto.setCompanyId(userInfo.getCompanyId());
        List<ExamSampleListVo> reportList = examMapper.examReportList(examDto);
        for(ExamSampleListVo es : reportList){
            examDto.setExamId(es.getExamId());
            int planExamUser = recordMapper.planUserCountByExam(examDto);
            int totalExamUser = recordMapper.countByExam(examDto);
            examDto.setIsPass(1);
            int passExamUser = recordMapper.countByExam(examDto);
            int noPassExanUser = totalExamUser - passExamUser;
            es.setPlanUserCount(planExamUser);
            es.setTotalUserCount(totalExamUser);
            es.setPassUserCount(passExamUser);
            es.setNoPassUserCount(noPassExanUser);
            es.setNoExamUserCount(planExamUser-totalExamUser);
        }
        return reportList;
    }
 
    @Override
    public Map<String,Object> goExam(ExamDto examDto) {
        Map<String,Object> map = new HashMap<>();
        Exam exam = examMapper.queryById(examDto.getExamId());
        List<QuestionAnswerVo> qaList = questionMapper.findAllQue(examDto.getExamId());
 
        if(qaList.size()<1){
            throw new BasicException(BasicStatus.QUESTION_COUNT_IS_ZERO);
        }
        Integer queCount = 0;
        /*if(qaList.size()<exam.getQuestionCount()){
            qaList.forEach(ql->{
                List<Answer> answerList = answerMapper.findAllByQueId(new Answer().setQueId(ql.getQueId()));
                ql.setAnswerList(answerList);
            });
            map.put("queList",qaList);
            queCount = qaList.size();
        }else {
            List<QuestionAnswerVo> newList = new ArrayList<>();
            //int[] randomArray = new int[exam.getQuestionCount()];
            Random random = new Random();
            for(int i= 0;i<exam.getQuestionCount();i++){
                int rand = random.nextInt(qaList.size());
                newList.add(i,qaList.get(rand));
            }
            newList.forEach(nl->{
                List<Answer> answerList = answerMapper.findAllByQueId(new Answer().setQueId(nl.getQueId()));
                nl.setAnswerList(answerList);
            });
            map.put("queList",newList);
            queCount = newList.size();
        }*/
        qaList.forEach(ql->{
            List<Answer> answerList = answerMapper.findAllByQueId(new Answer().setQueId(ql.getQueId()));
            ql.setAnswerList(answerList);
        });
        map.put("queList",qaList);
        queCount = qaList.size();
 
        SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo();
 
        ExamRecord examRecord = new ExamRecord();
        examRecord.setExamId(examDto.getExamId());
        examRecord.setUserId(userInfo.getUserId());
        examRecord.setStartTime(new Date());
        examRecord.setQueCount(queCount);
 
        recordMapper.insert(examRecord);
        map.put("recordId",examRecord.getRecordId());
        return map;
    }
 
    @Override
    public Map<String,Object> commitAnswer(CommitAnswerDto commitAnswerDto) {
        Map<String,Object> map = new HashMap<>();
        String answerIds = commitAnswerDto.getAnswerIds();
        ExamReAnswer examReAnswer = new ExamReAnswer();
        DeepCopyUtils.copy(commitAnswerDto,examReAnswer);
        ExamRecord examRecord = recordMapper.queryById(examReAnswer.getRecordId());
 
        String[] answerArray = new String[]{answerIds};
        if(answerIds.contains(",")){
            answerArray = answerIds.split(",");
        }
        for(String answerId : answerArray){
            examReAnswer.setExamId(examRecord.getExamId());
            examReAnswer.setAnswerId(answerId);
            reAnswerMapper.insert(examReAnswer);
        }
        int commitedCount = reAnswerMapper.countByRecordId(examReAnswer.getRecordId());
        System.out.println("当前答题数量"+commitedCount);
        System.out.println("题库数量"+examRecord.getQueCount());
        if(commitedCount < examRecord.getQueCount()){
            //继续答题
            map.put("isFinish",0);
        }else if(commitedCount == examRecord.getQueCount()){
            //答题结束
            map.put("isFinish",1);
            double totalScore = reAnswerMapper.examScoreCul(examReAnswer.getRecordId());
 
            map.put("totalScore",totalScore);
            recordMapper.ExamFinish(examRecord.setIsFinish(1).setScore(totalScore));
        }
        return map;
    }
 
    public static void main(String[] args) {
        int[] randomArray = new int[20];
        Random random = new Random();
        for(int i= 0;i<20;i++){
            randomArray[i] = random.nextInt(1000);
        }
        for(int i= 0;i<randomArray.length;i++){
            System.out.println(randomArray[i]);
        }
    }
}