heheng
9 天以前 9a646862455de8f2c4c77d5fca3d44e23c4c360e
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
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.PrivatizeEnum;
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.ExQuestionBank;
import com.gkhy.exam.system.mapper.ExExerciseAnswerMapper;
import com.gkhy.exam.system.mapper.ExQuestionBankMapper;
import com.gkhy.exam.system.service.ExQuestionBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
/**
 * <p>
 * 题库表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-06-18 10:09:52
 */
@Service
public class ExQuestionBankServiceImpl extends ServiceImpl<ExQuestionBankMapper, ExQuestionBank> implements ExQuestionBankService {
    @Autowired
    private ExExerciseAnswerMapper exerciseAnswerMapper;
 
    @Override
    public CommonPage selectQuestionBankList(ExQuestionBank questionBank) {
        SysUser user= SecurityUtils.getLoginUser().getUser();
        if(!user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            questionBank.setCompanyId(user.getCompanyId());
        }
        PageUtils.startPage();
        List<ExQuestionBank> bankList=baseMapper.selectQuestionBankList(questionBank);
        return CommonPage.restPage(bankList);
    }
 
    @Override
    public ExQuestionBank selectQuestionBankById(Long bankId) {
        ExQuestionBank questionBank= baseMapper.selectById(bankId);
        if(questionBank.getPrivatize().equals(PrivatizeEnum.PUBLIC.getCode())){
            return questionBank;
        }
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            return questionBank;
        }
        if(!questionBank.getCompanyId().equals(currentUser.getCompanyId())){
            throw new ApiException("无权限查看其它企业题库");
        }
        return questionBank;
    }
 
    @Override
    public int insertQuestionBank(ExQuestionBank questionBank) {
        checkUserAllowed(questionBank);
        if(!checkNameUnique(questionBank)){
            throw new ApiException("题库名称已存在");
        }
        SysUser user=SecurityUtils.getLoginUser().getUser();
        if(user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            questionBank.setPrivatize(PrivatizeEnum.PUBLIC.getCode());
        }else{
            questionBank.setCompanyId(user.getCompanyId());
            questionBank.setPrivatize(PrivatizeEnum.PRIVATE.getCode());
        }
        int row =baseMapper.insert(questionBank);
        if(row<1){
            throw new ApiException("新增题库失败");
        }
        return row;
    }
 
    @Override
    public int updateQuestionBank(ExQuestionBank questionBank) {
        checkUserAllowed(questionBank);
        if(!checkNameUnique(questionBank)){
            throw new ApiException("题库名称已存在");
        }
        int row =baseMapper.updateById(questionBank);
        if(row<1){
            throw new ApiException("编辑题库失败");
        }
        return row;
    }
 
    public void checkUserAllowed(ExQuestionBank questionBank) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            return;
        }
        if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
            throw new ApiException("没有权限操作");
        }
        if(questionBank.getCompanyId()!=null&&!currentUser.getCompanyId().equals(questionBank.getCompanyId())){
            throw new ApiException("没有权限操作其他企业课程");
        }
    }
 
    @Override
    public int deleteQuestionBankById(Long bankId) {
        checkUserAllowed(baseMapper.selectById(bankId));
        return baseMapper.deleteByBankId(bankId);
    }
 
    @Override
    public boolean checkNameUnique(ExQuestionBank questionBank) {
        SysUser user= SecurityUtils.getLoginUser().getUser();
        ExQuestionBank bank=null;
        Long bankId=questionBank.getId()==null?-1L:questionBank.getId();
        if(user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            bank= baseMapper.checkNameUniqueForAdmin(questionBank.getName());
        }else{
            bank= baseMapper.checkNameUnique(questionBank.getName(),user.getCompanyId());
        }
        if(bank!=null&&bank.getId().longValue()!=bankId.longValue()){
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
 
    @Override
    public CommonPage selectQuestionBankListForStudent(ExQuestionBank questionBank) {
        SysUser user= SecurityUtils.getLoginUser().getUser();
        if(!user.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
            throw new ApiException("非学员用户,无法查看");
        }
        questionBank.setCompanyId(user.getCompanyId());
        questionBank.setStudentId(user.getId());
        PageUtils.startPage();
        List<ExQuestionBank> bankList=baseMapper.selectQuestionBankListForStudent(questionBank);
        return CommonPage.restPage(bankList);
    }
 
    @Override
    public ExQuestionBank selectQuestionBankByIdForStudent(Long bankId) {
        SysUser user= SecurityUtils.getLoginUser().getUser();
        if(!user.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
            throw new ApiException("非学员用户,无法查看");
        }
        return baseMapper.selectQuestionBankByIdForStudent(bankId,user.getId());
    }
 
    @Override
    public int clearExerciseRecord(Long bankId) {
        return exerciseAnswerMapper.clearExerciseRecord(bankId,SecurityUtils.getUserId());
    }
}