kongzy
2024-06-24 4910e41f81a85c03a9dfc83f8ec9c1e71c123d49
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.gkhy.exam.system.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.common.api.CommonPage;
import com.gkhy.exam.common.constant.UserConstant;
import com.gkhy.exam.common.domain.entity.SysUser;
import com.gkhy.exam.common.enums.CodeTypeEnum;
import com.gkhy.exam.common.enums.QuestionAssignEnum;
import com.gkhy.exam.common.enums.QuestionTypeEnum;
import com.gkhy.exam.common.enums.UserTypeEnum;
import com.gkhy.exam.common.exception.ApiException;
import com.gkhy.exam.common.utils.PageUtils;
import com.gkhy.exam.common.utils.SecurityUtils;
import com.gkhy.exam.system.domain.ExExamPaper;
import com.gkhy.exam.system.domain.ExPaperQuestion;
import com.gkhy.exam.system.domain.ExQuestion;
import com.gkhy.exam.system.mapper.ExExamPaperMapper;
import com.gkhy.exam.system.mapper.ExPaperQuestionMapper;
import com.gkhy.exam.system.mapper.ExPaperStudentMapper;
import com.gkhy.exam.system.mapper.ExQuestionMapper;
import com.gkhy.exam.system.service.ExExamPaperService;
import com.gkhy.exam.system.utils.SequenceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 考卷(组卷)表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-06-05 15:07:36
 */
@Service
public class ExExamPaperServiceImpl extends ServiceImpl<ExExamPaperMapper, ExExamPaper> implements ExExamPaperService {
    @Autowired
    private ExPaperStudentMapper paperStudentMapper;
 
    @Autowired
    private ExPaperQuestionMapper paperQuestionMapper;
    @Autowired
    private ExQuestionMapper questionMapper;
 
    @Autowired
    private SequenceUtils sequenceUtils;
 
    @Override
    public CommonPage selectExamPaperList(ExExamPaper examPaper) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!Objects.equals(currentUser.getUserType(), UserTypeEnum.SYSTEM_USER.getCode())){
            examPaper.setCompanyId(currentUser.getCompanyId());
        }
        PageUtils.startPage();
        List<ExExamPaper> paperList=baseMapper.selectExamPaperList(examPaper);
        return CommonPage.restPage(paperList);
    }
 
    @Override
    public ExExamPaper selectExamPaperById(Long paperId) {
        ExExamPaper examPaper= baseMapper.selectExamPaperById(paperId);
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            return examPaper;
        }
        if(!examPaper.getCompanyId().equals(currentUser.getCompanyId())){
            throw new ApiException("无权限查看其它企业考卷信息");
        }
        return examPaper;
    }
 
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int insertExamPaper(ExExamPaper examPaper) {
        checkUserAllowed(examPaper);
        if(!checkNameUnique(examPaper)){
            throw new ApiException("试卷名称已存在");
        }
        //生成编码
        examPaper.setCode(sequenceUtils.getNextSequence(CodeTypeEnum.EXAM_PAPER.getCode()));
        examPaper.setCreateBy(SecurityUtils.getUsername());
        if(examPaper.getLimitTime()>0){
            examPaper.setLimit(1);
        }
        int row=baseMapper.insert(examPaper);
        if(row<1){
            throw new ApiException("新增试卷失败");
        }
        //分配试题
        assignSingleQuestion(examPaper);
        assignMultiQuestion(examPaper);
        assignJudgeQuestion(examPaper);
        return row;
    }
 
    public void checkUserAllowed(ExExamPaper examPaper) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            throw new ApiException("系统用户不能创建、修改试卷");
        }
        if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
            throw new ApiException("没有权限操作");
        }
        if(!currentUser.getCompanyId().equals(examPaper.getCompanyId())){
            throw new ApiException("没有权限操作其他企业试卷");
        }
    }
 
    //分配单选题
    public void assignSingleQuestion(ExExamPaper examPaper){
        List<ExPaperQuestion> paperQuestionList = getPaperQuestions(examPaper.getId(), examPaper.getSingleBankId(),
                examPaper.getSingleMethod(),examPaper.getSingleNum(), examPaper.getSingleScore(),QuestionTypeEnum.SINGLE);
        paperQuestionMapper.batchInsert(paperQuestionList);
    }
 
    //分配多选题
    public void assignMultiQuestion(ExExamPaper examPaper){
        List<ExPaperQuestion> paperQuestionList = getPaperQuestions(examPaper.getId(), examPaper.getMultiBankId(),
                examPaper.getMultiMethod(),examPaper.getMultiNum(), examPaper.getMultiScore(),QuestionTypeEnum.MULTI);
        paperQuestionMapper.batchInsert(paperQuestionList);
    }
 
    //分配判断题
    public void assignJudgeQuestion(ExExamPaper examPaper){
        List<ExPaperQuestion> paperQuestionList = getPaperQuestions(examPaper.getId(), examPaper.getJudgeBankId(),
                examPaper.getJudgeMethod(),examPaper.getJudgeNum(), examPaper.getJudgeScore(),QuestionTypeEnum.JUDGE);
        paperQuestionMapper.batchInsert(paperQuestionList);
    }
 
    private List<ExPaperQuestion> getPaperQuestions(Long paperId,Long bankId,Integer questionMethod,Integer questionCount,Integer questionScore, QuestionTypeEnum questionTypeEnum) {
        SysUser user=SecurityUtils.getLoginUser().getUser();
        Integer totalQuestionCount=questionMapper.selectCountByBankId(user.getCompanyId(), bankId, questionTypeEnum.getCode());
        if(totalQuestionCount< questionCount){
            throw new ApiException(String.format("所选题库<%s>数量不足,无法分配",questionTypeEnum.getInfo()));
        }
        List<ExQuestion> questions=new ArrayList<>();
        if(Objects.equals(questionMethod, QuestionAssignEnum.RANDOM.getCode())){//随机分配
            questions=questionMapper.selectRandomQuestion(user.getCompanyId(), bankId,questionTypeEnum.getCode(), questionCount);
        }else{//顺序分配
            int totalPage=questionCount/ questionMethod;//不能整除,忽略最后一页
            Random random=new Random();
            int startIndex=random.nextInt(totalPage)+1;
            questions=questionMapper.selectQuestionWithLimit(user.getCompanyId(), bankId,questionTypeEnum.getCode(),startIndex, questionCount);
        }
        List<ExPaperQuestion> paperQuestionList=questions.stream().map(item -> {
            ExPaperQuestion exPaperQuestion=new ExPaperQuestion();
            exPaperQuestion.setPaperId(paperId);
            exPaperQuestion.setQuestionId(item.getId());
            exPaperQuestion.setScore(questionScore);
            return exPaperQuestion;
        }).collect(Collectors.toList());
        return paperQuestionList;
    }
 
 
    @Override
    public int updateExamPaper(ExExamPaper examPaper) {
        if(!checkNameUnique(examPaper)){
            throw new ApiException("试卷名称已存在");
        }
        //校验考卷下是否有学员
        if(checkPaperHasStudent(examPaper.getId())){
            throw new ApiException("该试卷下已分配学员,不能编辑");
        }
        examPaper.setCode(null);//编号不能修改
        if(examPaper.getLimitTime()==0){
            examPaper.setLimit(0);
        }
        int row=baseMapper.updateById(examPaper);
        if(row<1){
            throw new ApiException("更新试卷失败");
        }
        //重新分配题
        if(examPaper.getSingleRebuild()==1){
            deletePaperQuestion(examPaper.getId(),QuestionTypeEnum.SINGLE);
            assignSingleQuestion(examPaper);
        }
        if(examPaper.getMultiRebuild()==1) {
           deletePaperQuestion(examPaper.getId(),QuestionTypeEnum.MULTI);
           assignMultiQuestion(examPaper);
        }
        if(examPaper.getJudgeRebuild()==1) {
           deletePaperQuestion(examPaper.getId(),QuestionTypeEnum.JUDGE);
           assignJudgeQuestion(examPaper);
        }
        return row;
    }
 
    public void deletePaperQuestion(Long paperId,QuestionTypeEnum questionTypeEnum){
        paperQuestionMapper.deletePaperQuestion(paperId,questionTypeEnum!=null?questionTypeEnum.getCode():null);
    }
 
    @Override
    public int deleteExamPaperById(Long paperId) {
        //查询该试卷分配的学员人数
        if(checkPaperHasStudent(paperId)){
            throw new ApiException("该试卷下已分配学员,不能删除");
        }
        int row=baseMapper.deleteById(paperId);
        if(row<1){
            throw new ApiException("删除试卷失败");
        }
        //删除考卷试题
        paperQuestionMapper.deletebyPapaerId(paperId);
        return row;
    }
 
    public Boolean checkPaperHasStudent(Long paperId){
        //查询该试卷分配的学员人数
        int studentCount=paperStudentMapper.countByPaperId(paperId);
        if(studentCount>0){
            return true;
        }
        return false;
    }
 
    @Override
    public boolean checkNameUnique(ExExamPaper examPaper) {
        Long paperId=examPaper.getId()==null?-1L:examPaper.getId();
        ExExamPaper paper= baseMapper.checkNameUnique(examPaper.getName());
        if(paper!=null&&paper.getId().longValue()!=paperId.longValue()){
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
 
    @Override
    public int changeExamPaperStatus(Long paperId, Integer status) {
        return baseMapper.updateById(new ExExamPaper().setId(paperId).setStatus(status).setUpdateBy(SecurityUtils.getUsername()));
    }
 
 
 
}