heheng
6 天以前 d8012ee77b6a9e86611aae9074d5925826f4210d
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
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.config.MinioConfig;
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.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.ExCourseChapter;
import com.gkhy.exam.system.domain.ExCourseChapterPeriod;
import com.gkhy.exam.system.domain.ExResource;
import com.gkhy.exam.system.mapper.ExCourseChapterMapper;
import com.gkhy.exam.system.mapper.ExCourseChapterPeriodMapper;
import com.gkhy.exam.system.mapper.ExCourseMapper;
import com.gkhy.exam.system.service.ExCourseChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
/**
 * <p>
 * 课程章节表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-06-05 15:07:36
 */
@Service
public class ExCourseChapterServiceImpl extends ServiceImpl<ExCourseChapterMapper, ExCourseChapter> implements ExCourseChapterService {
    @Autowired
    private ExCourseChapterPeriodMapper courseChapterPeriodMapper;
    @Autowired
    private ExCourseMapper courseMapper;
 
    @Autowired
    private MinioConfig minioConfig;
 
    @Override
    public CommonPage selectChapterList(ExCourseChapter chapter) {
        if(chapter.getCourseId()==null){
            throw new ApiException("课程id不能为空");
        }
        PageUtils.startPage();
        List<ExCourseChapter> chapterList=baseMapper.selectChapterList(chapter);
        return CommonPage.restPage(chapterList);
    }
 
    @Override
    public ExCourseChapter selectChapterById(Long chapterId) {
        return baseMapper.selectChapterById(chapterId);
    }
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int insertChapter(ExCourseChapter chapter) {
        checkUserAllowed(chapter);
        if(!checkNameUnique(chapter)){
            throw new ApiException("章节名称已存在");
        }
        SysUser user= SecurityUtils.getLoginUser().getUser();
        Long companyId=!user.getUserType().equals(UserTypeEnum.SYSTEM_USER)?user.getCompanyId():null;
        chapter.setCompanyId(companyId);
        int row=baseMapper.insert(chapter);
        if(row<1){
            throw new ApiException("新增章节失败");
        }
        return row;
    }
 
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int updateChapter(ExCourseChapter chapter) {
        checkUserAllowed(chapter);
        if(!checkNameUnique(chapter)){
            throw new ApiException("章节名称已存在");
        }
        chapter.setUpdateBy(SecurityUtils.getUsername());
        int row=baseMapper.updateById(chapter);
        if(row<1){
            throw new ApiException("更新章节失败");
        }
        return row;
    }
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteChapterById(Long chapterId) {
        //校验课程是否已被分配 ,章节下面是否存在课时
        checkUserAllowed(baseMapper.selectById(chapterId));
 
        int periodCount=baseMapper.selectPeriodCountById(chapterId);
        if(periodCount>0){
            throw new ApiException("章节下已绑定课时,不能删除");
        }
        int row=baseMapper.deleteById(chapterId);
        if(row<1){
            throw new ApiException("删除章节失败");
        }
        courseChapterPeriodMapper.deletePeriodByChapterId(chapterId);
        return row;
    }
 
    @Override
    public boolean checkNameUnique(ExCourseChapter chapter) {
        if(chapter.getCourseId()==null){
            throw new ApiException("课程id不能为空");
        }
        Long chapterId=chapter.getId()==null?-1L:chapter.getId();
        ExCourseChapter cc= baseMapper.checkNameUnique(chapter.getName(),chapter.getCourseId());
        if(cc!=null&&cc.getId().longValue()!=chapterId.longValue()){
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
 
    @Override
    public List<ExCourseChapter> selectChapterByCourseId(Long courseId,Integer status) {
        List<ExCourseChapter> courseChapterList= baseMapper.selectChapterByCourseId(courseId,null);
        for(ExCourseChapter courseChapter:courseChapterList){
            if(courseChapter.getChapterPeriods()!=null&& !courseChapter.getChapterPeriods().isEmpty()){
                List<ExCourseChapterPeriod> courseChapterPeriodList=courseChapter.getChapterPeriods();
                for(ExCourseChapterPeriod courseChapterPeriod:courseChapterPeriodList){
                    if(courseChapterPeriod.getResource()!=null){
                        ExResource resource=courseChapterPeriod.getResource();
                        resource.setResourcePath(minioConfig.getEndpoint()+minioConfig.getBucketName()+"/"+resource.getResourcePath());
                    }
                }
            }
        }
        return courseChapterList;
    }
 
 
    public void checkUserAllowed(ExCourseChapter courseChapter) {
        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(courseChapter.getCompanyId()!=null&&!currentUser.getCompanyId().equals(courseChapter.getCompanyId())){
            throw new ApiException("没有权限操作其他企业课程");
        }
        int state=courseMapper.selectCourseState(courseChapter.getCourseId());
        if(state==ApproveStatusEnum.APPROVED.getCode()){
            throw new ApiException("已审批的课程不能再操作");
        }
        if(state==ApproveStatusEnum.APPROVING.getCode()){
            throw new ApiException("待审批的课程不能再操作");
        }
 
    }
}