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
package com.gkhy.exam.system.service.impl;
 
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.common.api.CommonPage;
import com.gkhy.exam.common.domain.entity.SysUser;
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.ExPaperStudent;
import com.gkhy.exam.system.domain.ExStudent;
import com.gkhy.exam.system.mapper.ExExamPaperMapper;
import com.gkhy.exam.system.mapper.ExPaperStudentMapper;
import com.gkhy.exam.system.mapper.ExStudentAnswerMapper;
import com.gkhy.exam.system.mapper.ExStudentMapper;
import com.gkhy.exam.system.service.ExPaperStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Collections;
import java.util.List;
import java.util.Objects;
 
/**
 * <p>
 * 学员与考试题目关系表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-06-06 13:53:17
 */
@Service
public class ExPaperStudentServiceImpl extends ServiceImpl<ExPaperStudentMapper, ExPaperStudent> implements ExPaperStudentService {
    @Autowired
    private ExExamPaperMapper examPaperMapper;
    @Autowired
    private ExStudentAnswerMapper studentAnswerMapper;
    @Autowired
    private ExStudentMapper studentMapper;
 
 
    @Override
    public CommonPage selectPaperStudentListForStudent(ExPaperStudent paperStudent) {
        SysUser user= SecurityUtils.getLoginUser().getUser();
        if(!Objects.equals(user.getUserType(), UserTypeEnum.STUDENT.getCode())){
            throw new ApiException("非学员用户,不能查看");
        }
        paperStudent.setStudentId(user.getId());
        PageUtils.startPage();
        List<ExPaperStudent> paperList=baseMapper.selectPaperStudentList(paperStudent);
        return CommonPage.restPage(paperList);
    }
 
    @Override
    public ExPaperStudent selectPaperStudentById(Long paperStudentId) {
        return baseMapper.selectPaperStudentById(paperStudentId);
    }
 
    @Override
    public ExPaperStudent selectPaperStudentById(ExPaperStudent paperStudent) {
        return baseMapper.selectByPaperStudentId(paperStudent);
    }
 
    @Override
    public int addPaperStudent(ExPaperStudent paperStudent) {
        checkStudentUnique(Collections.singletonList(paperStudent));
        paperStudent.setCreateId(SecurityUtils.getUserId());
        int row=baseMapper.insert(paperStudent);
        if(row<1){
            throw new ApiException("分配学员失败");
        }
        return row;
    }
 
    @Override
    public int batchAddPaperStudent(List<ExPaperStudent> paperStudents) {
        checkStudentUnique(paperStudents);
        int row=baseMapper.batchInsert(paperStudents);
        if(row<1){
            throw new ApiException("批量分配学员失败");
        }
        return row;
    }
 
    @Override
    public void checkStudentUnique(List<ExPaperStudent> paperStudents){
        for(ExPaperStudent paperStudent:paperStudents) {
            Integer existCount = baseMapper.selectCountByPaperStudentId(paperStudent.getPaperId(), paperStudent.getStudentId());
            if (existCount > 0) {
                ExStudent student = studentMapper.selectById(paperStudent.getStudentId());
                throw new ApiException(String.format("待分配学员<%s>已存在", student.getName()));
            }
        }
    }
 
    @Override
    public CommonPage selectPaperStudentList(ExPaperStudent paperStudent) {
        if(paperStudent.getPaperId()==null){
            throw new ApiException("试卷id不能为空");
        }
        PageUtils.startPage();
        List<ExPaperStudent> paperStudentList=baseMapper.selectPaperStudentList(paperStudent);
        return CommonPage.restPage(paperStudentList);
    }
 
    @Override
    public int deletePaperStudent(Long paperStudentId) {
        ExPaperStudent paperStudent=baseMapper.selectPaperStudentById(paperStudentId);
        if(ObjectUtil.isNull(paperStudent)){
            throw new ApiException(String.format("该试卷下不存在该学员<>",paperStudent.getStudentName()));
        }
        int studentAnswerCount=studentAnswerMapper.countByPaperId(paperStudent.getPaperId(),paperStudent.getStudentId());
        if(studentAnswerCount>0){
            throw new ApiException(String.format("学员<%s>已进行答题,不能删除",paperStudent.getStudentName()));
        }
        //删除学员与试卷关系
        return baseMapper.deleteById(paperStudentId);
    }
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void batchDeletePaperStudent(List<Long> paperStudentIds) {
        for(Long paperStudentId:paperStudentIds){
            deletePaperStudent(paperStudentId);
        }
    }
 
 
 
 
    @Override
    public void endExam(ExPaperStudent paperStudent) {
        Long endTime=System.currentTimeMillis();
        //异步计算
        //计算考试成绩
        ExExamPaper paper=examPaperMapper.selectById(paperStudent.getPaperId());
        int singleCount=studentAnswerMapper.selectPassCount(paperStudent.getPaperId(),paperStudent.getStudentId(), QuestionTypeEnum.SINGLE.getCode());
        int multiCount=studentAnswerMapper.selectPassCount(paperStudent.getPaperId(),paperStudent.getStudentId(), QuestionTypeEnum.MULTI.getCode());
        int judgeCount=studentAnswerMapper.selectPassCount(paperStudent.getPaperId(),paperStudent.getStudentId(), QuestionTypeEnum.JUDGE.getCode());
        int score=singleCount*paper.getSingleScore()+multiCount*paper.getMultiScore()+judgeCount*paper.getJudgeScore();
        ExPaperStudent exPaperStudent = new ExPaperStudent()
                .setPaperId(paperStudent.getPaperId())
                .setStudentId(paperStudent.getStudentId())
                .setCompleted(1)
                .setEndTime(endTime)
                .setScore(score);
        if(score>=paper.getPassScore()){
            exPaperStudent.setPassed(1);
        }else{
            exPaperStudent.setPassed(0);
        }
        int row=baseMapper.updateById(exPaperStudent);
        if(row<1){
            throw new ApiException("提交考试失败");
        }
    }
 
 
 
}