heheng
2025-06-25 233c83462662cdf37a70cfed88a4a45c2739bedd
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
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.ApproveStatusEnum;
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.common.utils.StringUtils;
import com.gkhy.exam.system.domain.ExCourse;
import com.gkhy.exam.system.mapper.ExCourseChapterMapper;
import com.gkhy.exam.system.mapper.ExCourseMapper;
import com.gkhy.exam.system.mapper.ExCoursePhaseMapper;
import com.gkhy.exam.system.service.ExCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
/**
 * <p>
 * 课程表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-06-05 15:07:36
 */
@Service
public class ExCourseServiceImpl extends ServiceImpl<ExCourseMapper, ExCourse> implements ExCourseService {
    @Autowired
    private ExCourseChapterMapper courseChapterMapper;
    @Autowired
    private ExCoursePhaseMapper coursePhaseMapper;
 
    @Override
    public CommonPage selectCourseList(ExCourse course) {
        SysUser user= SecurityUtils.getLoginUser().getUser();
        if(!user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            course.setCompanyId(user.getCompanyId());
        }
        PageUtils.startPage();
        List<ExCourse> courseList=baseMapper.selectCourseList(course);
        return CommonPage.restPage(courseList);
    }
 
    @Override
    public CommonPage selectApproveCourseList(ExCourse course){
        course.setState(ApproveStatusEnum.APPROVED.getCode());
        return selectCourseList(course);
    }
 
    @Override
    public ExCourse selectCourseById(Long courseId) {
        ExCourse course= baseMapper.selectCourseById(courseId);
        if(course.getPrivatize().equals(PrivatizeEnum.PUBLIC.getCode())){
            return course;
        }
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            return course;
        }
        if(!course.getCompanyId().equals(currentUser.getCompanyId())){
            throw new ApiException("无权限查看其它企业课程");
        }
        return course;
    }
 
    @Override
    public int insertCourse(ExCourse course) {
        if(!checkNameUnique(course)){
            throw new ApiException("课程名称已存在");
        }
        checkUserAllowed(course);
        if(SecurityUtils.getLoginUser().getUser().getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            course.setState(ApproveStatusEnum.APPROVED.getCode());
            course.setPrivatize(PrivatizeEnum.PUBLIC.getCode());
        }else{
            course.setState(ApproveStatusEnum.TEMPORARY.getCode());
        }
        int row =baseMapper.insert(course);
        if(row<1){
            throw new ApiException("新增课程失败");
        }
        return row;
    }
 
    @Override
    public int updateCourse(ExCourse course) {
        if(!checkNameUnique(course)){
            throw new ApiException("课程名称已存在");
        }
        checkUserAllowed(baseMapper.selectById(course.getId()));
        int row =baseMapper.updateById(course);
        if(row<1){
            throw new ApiException("更新课程失败");
        }
        return row;
    }
 
    public void checkUserAllowed(ExCourse course) {
        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(!currentUser.getCompanyId().equals(course.getCompanyId())){
            throw new ApiException("没有权限操作其他企业课程");
        }
        if(course.getId()!=null){
            if(course.getState().equals(ApproveStatusEnum.APPROVING.getCode())){
                throw new ApiException("课程待审批状态不能再操作");
            }
            if(course.getState().equals(ApproveStatusEnum.APPROVED.getCode())){
                throw new ApiException("已审批的课程不能再操作");
            }
        }
    }
 
    @Override
    public int deleteCourseById(Long courseId) {
        //校验课程是否被分配/是否存在字章节
        checkUserAllowed(baseMapper.selectById(courseId));
        Integer phaseCount= coursePhaseMapper.selectCountByCourseId(courseId);
        if(phaseCount>0){
            throw new ApiException("课程已分配,不能删除");
        }
        Integer chapterCount=courseChapterMapper.selectCountByCourseId(courseId);
        if(chapterCount>0){
            throw new ApiException("课程下存在章节,不能删除");
        }
        int row =baseMapper.deleteByCourseId(courseId);
        if(row<1){
            throw new ApiException("删除课程失败");
        }
        return row;
    }
 
    @Override
    public boolean checkNameUnique(ExCourse course) {
        SysUser user=SecurityUtils.getLoginUser().getUser();
        Long courseId=course.getId()==null?-1L:course.getId();
        ExCourse cou= baseMapper.checkNameUnique(course.getName(),user.getCompanyId());
        if(cou!=null&&cou.getId().longValue()!=courseId.longValue()){
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
 
    @Override
    public int doApprove(ExCourse course) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        ExCourse dbCourse=getById(course.getId());
        if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
                throw new ApiException("没有权限操作");
            }
            if(!currentUser.getCompanyId().equals(dbCourse.getCompanyId())){
                throw new ApiException("没有权限操作其他企业课程");
            }
            if(dbCourse.getState().equals(ApproveStatusEnum.APPROVED.getCode())){
                throw new ApiException("已审批的课程不能再操作");
            }
        }
        if(StringUtils.isBlank(course.getMessage())){
            course.setMessage("");
        }
        int row=baseMapper.updateById(course);
        if(row<1){
            throw new ApiException("审批失败");
        }
        return row;
    }
 
    @Override
    public int changeStatus(ExCourse course) {
        if(course.getId()==null||course.getStatus()==null){
            throw new ApiException("参数传参缺失");
        }
        ExCourse entity=new ExCourse().setId(course.getId()).setStatus(course.getStatus());
        entity.setUpdateBy(SecurityUtils.getUsername());
        return baseMapper.updateById(entity);
    }
}