heheng
9 天以前 bd605ac1e79b73db3b501f323d6f4a10c0f27dd5
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
package com.gkhy.exam.system.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.SecurityUtils;
import com.gkhy.exam.system.domain.ExCourseChapterPeriod;
import com.gkhy.exam.system.mapper.ExCourseChapterPeriodMapper;
import com.gkhy.exam.system.mapper.ExCourseMapper;
import com.gkhy.exam.system.service.ExCourseChapterPeriodService;
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 ExCourseChapterPeriodServiceImpl extends ServiceImpl<ExCourseChapterPeriodMapper, ExCourseChapterPeriod> implements ExCourseChapterPeriodService {
    @Autowired
    private ExCourseMapper courseMapper;
    @Override
    public ExCourseChapterPeriod selectPeriodById(Long periodId) {
        return baseMapper.selectPeriodById(periodId);
    }
 
    @Override
    public int insertPeriod(ExCourseChapterPeriod period) {
        checkUserAllowed(period);
        if(!checkNameUnique(period)){
            throw new ApiException("学时名称已存在");
        }
        SysUser user= SecurityUtils.getLoginUser().getUser();
        Long companyId=!user.getUserType().equals(UserTypeEnum.SYSTEM_USER)?user.getCompanyId():null;
        period.setCompanyId(companyId);
        int row=baseMapper.insert(period);
        if(row<1){
            throw new ApiException("学时保存失败");
        }
        return row;
    }
 
    @Override
    public int updatePeriod(ExCourseChapterPeriod period) {
        checkUserAllowed(period);
        if(!checkNameUnique(period)){
            throw new ApiException("学时名称已存在");
        }
        period.setUpdateBy(SecurityUtils.getUsername());
        int row=baseMapper.updateById(period);
        if(row<1){
            throw new ApiException("更新学时失败");
        }
        return row;
    }
 
    @Override
    public int deletePeriodById(Long periodId) {
        //校验课程是否被分配
        int row=baseMapper.deleteById(periodId);
        if(row<1){
            throw new ApiException("删除学时失败");
        }
        return row;
    }
 
    @Override
    public boolean checkNameUnique(ExCourseChapterPeriod period) {
        if(period.getChapterId()==null||period.getCourseId()==null){
            throw new ApiException("章节id或者课程id不能为空");
        }
        Long periodId=period.getId()==null?-1L:period.getId();
        ExCourseChapterPeriod ccp= baseMapper.checkNameUnique(period.getName(),period.getChapterId(),period.getCourseId());
        if(ccp!=null&&ccp.getId().longValue()!=periodId.longValue()){
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
 
    @Override
    public List<ExCourseChapterPeriod> selectChapterPeriodByChapterId(Long chapterId) {
 
        return baseMapper.selectPeriodByChapterId(chapterId);
    }
 
 
    public void checkUserAllowed(ExCourseChapterPeriod courseChapterPeriod) {
        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(courseChapterPeriod.getCompanyId()!=null&&!currentUser.getCompanyId().equals(courseChapterPeriod.getCompanyId())){
            throw new ApiException("没有权限操作其他企业课程");
        }
        int state=courseMapper.selectCourseState(courseChapterPeriod.getCourseId());
        if(state== ApproveStatusEnum.APPROVED.getCode()){
            throw new ApiException("已审批的课程不能再操作");
        }
        if(state== ApproveStatusEnum.APPROVING.getCode()){
            throw new ApiException("待审批的课程不能再操作");
        }
    }
 
}