“djh”
9 天以前 9f3e19a5d5d526d8a22af43d38770e519c78b230
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
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.CodeTypeEnum;
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.ExCourse;
import com.gkhy.exam.system.domain.ExCoursePhase;
import com.gkhy.exam.system.mapper.ExCourseMapper;
import com.gkhy.exam.system.mapper.ExCoursePhaseMapper;
import com.gkhy.exam.system.mapper.ExPhaseStudentMapper;
import com.gkhy.exam.system.service.ExCoursePhaseService;
import com.gkhy.exam.system.utils.SequenceUtils;
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 ExCoursePhaseServiceImpl extends ServiceImpl<ExCoursePhaseMapper, ExCoursePhase> implements ExCoursePhaseService {
    @Autowired
    private ExPhaseStudentMapper phaseStudentMapper;
    @Autowired
    private ExCourseMapper courseMapper;
 
 
 
    @Autowired
    private SequenceUtils sequenceUtils;
 
    @Override
    public CommonPage selectCoursePhaseList(ExCoursePhase coursePhase) {
        SysUser user= SecurityUtils.getLoginUser().getUser();
        if(!user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            coursePhase.setCompanyId(user.getCompanyId());
        }
        PageUtils.startPage();
        List<ExCoursePhase> courseList=baseMapper.selectCoursePhaseList(coursePhase);
        return CommonPage.restPage(courseList);
    }
 
    @Override
    public ExCoursePhase selectCoursePhaseById(Long phaseId) {
        ExCoursePhase coursePhase= baseMapper.selectCoursePhaseById(phaseId);
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            return coursePhase;
        }
        if(!coursePhase.getCompanyId().equals(currentUser.getCompanyId())){
            throw new ApiException("无权限查看其它企业批次信息");
        }
        return coursePhase;
    }
 
    @Override
    public int insertCoursePhase(ExCoursePhase coursePhase) {
        checkUserAllowed(coursePhase);
        checkCourseStatus(coursePhase);
        if(!checkNameUnique(coursePhase)){
            throw new ApiException("批次名称已存在");
        }
        coursePhase.setCode(sequenceUtils.getNextSequence(CodeTypeEnum.COUSE_PHASE.getCode()));
        coursePhase.setCreateBy(SecurityUtils.getUsername());
        int row=baseMapper.insert(coursePhase);
        if(row<1){
            throw new ApiException("新增批次失败");
        }
        return row;
    }
 
    @Override
    public int updateCoursePhase(ExCoursePhase coursePhase) {
        checkUserAllowed(coursePhase);
        checkCourseStatus(coursePhase);
        if(!checkNameUnique(coursePhase)){
            throw new ApiException("批次名称已存在");
        }
        coursePhase.setCode(null);
        int row=baseMapper.updateById(coursePhase);
        if(row<1){
            throw new ApiException("更新批次失败");
        }
        return row;
    }
 
    public void checkUserAllowed(ExCoursePhase coursePhase) {
        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(coursePhase.getCompanyId()!=null&&!currentUser.getCompanyId().equals(coursePhase.getCompanyId())){
            throw new ApiException("没有权限操作其他企业批次");
        }
        int level=coursePhase.getLevel();
        if((level==1 ||level==4) && currentUser.getUserType()>UserTypeEnum.COMPANY_USER.getCode()){
            throw new ApiException("当前账户无权限新增公司级批次");
        }
        if(level==2 && currentUser.getUserType()>UserTypeEnum.DEPART_USER.getCode()){
            throw new ApiException("当前账户无权限新增事业部级级批次");
        }
    }
 
 
    private void checkCourseStatus(ExCoursePhase coursePhase){
        Long courseId=coursePhase.getCourseId();
        ExCourse course= courseMapper.selectById(courseId);
        if(course.getStatus().equals(UserConstant.DISENABLE)){
            throw new ApiException("课程已禁用,不能分配");
        }
        if(!course.getState().equals(ApproveStatusEnum.APPROVED.getCode())){
            throw new ApiException("课程审批未通过,不能分配");
        }
    }
 
    @Override
    public int deleteCoursePhaseById(Long phaseId) {
        checkUserAllowed(baseMapper.selectById(phaseId));
        //查询该批次分配的学员人数
        int studentCount=phaseStudentMapper.countByPhaseId(phaseId);
        if(studentCount>0){
            throw new ApiException("该批次下已分配学员,不能删除");
        }
        int row=baseMapper.deleteByPhaseId(phaseId);
        if(row<1){
            throw new ApiException("删除批次失败");
        }
        return row;
    }
 
 
    @Override
    public boolean checkNameUnique(ExCoursePhase coursePhase) {
        Long phaseId=coursePhase.getId()==null?-1L:coursePhase.getId();
        ExCoursePhase phase= baseMapper.checkNameUnique(coursePhase.getName());
        if(phase!=null&&phase.getId().longValue()!=phaseId.longValue()){
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
}