heheng
4 天以前 4a91f037d3488ed7bc00664e1acc004810651f43
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.gkhy.exam.system.service.impl;
 
import cn.hutool.core.util.ObjectUtil;
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.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.*;
import com.gkhy.exam.system.domain.vo.StudentStudyVO;
import com.gkhy.exam.system.mapper.*;
import com.gkhy.exam.system.service.ExCompanyPeriodService;
import com.gkhy.exam.system.service.ExPhaseStudentService;
import com.gkhy.exam.system.service.ExStudentStudyService;
import com.gkhy.exam.system.service.SysCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
 
/**
 * <p>
 * 课时批次与学员关系表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-06-06 13:53:17
 */
@Service
public class ExPhaseStudentServiceImpl extends ServiceImpl<ExPhaseStudentMapper, ExPhaseStudent> implements ExPhaseStudentService {
    @Autowired
    private ExStudentStudyMapper studentStudyMapper;
    @Autowired
    private ExStudentStudyService studentStudyService;
 
    @Autowired
    private ExStudentMapper studentMapper;
    @Autowired
    private ExCoursePhaseMapper coursePhaseMapper;
    @Autowired
    private SysCompanyService companyService;
    @Autowired
    private ExCompanyPeriodService companyPeriodService;
    @Autowired
    private ExCourseChapterPeriodMapper courseChapterPeriodMapper;
 
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int addPhaseStudent(ExPhaseStudent phaseStudent) {
        checkUserAllowed(phaseStudent);
        checkStudentUnique(Collections.singletonList(phaseStudent));
        //判断企业课时是否充足
        phaseStudent.setCreateId(SecurityUtils.getUserId());
        int row=baseMapper.insert(phaseStudent);
        if(row<1){
            throw new ApiException("分配学员失败");
        }
        //扣减企业学时,
        addCompanyPeriod(phaseStudent.getPhaseId(),1);
        return row;
    }
 
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int batchAddPhaseStudent(List<ExPhaseStudent> phaseStudents) {
        if(phaseStudents==null|| phaseStudents.isEmpty()){
            throw new ApiException("学员数据不能为空");
        }
        checkUserAllowed(phaseStudents.get(0));
        checkStudentUnique(phaseStudents);
        for(ExPhaseStudent phaseStudent:phaseStudents){
            phaseStudent.setCreateId(SecurityUtils.getUserId());
        }
        //判断企业课时是否充足
        int row =baseMapper.batchInsert(phaseStudents);
        if(row<1){
            throw new ApiException("批量分配学员失败");
        }
        //扣减企业学时,
        addCompanyPeriod(phaseStudents.get(0).getPhaseId(),phaseStudents.size());
        return row;
    }
 
    public void checkUserAllowed(ExPhaseStudent phaseStudent) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            return;
        }
        if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
            throw new ApiException("没有权限操作");
        }
        ExCoursePhase coursePhase=coursePhaseMapper.selectById(phaseStudent.getPhaseId());
        if(!coursePhase.getCompanyId().equals(currentUser.getCompanyId())){
            throw new ApiException("没有权限操作其他企业批次");
        }
 
    }
 
 
    @Override
    public CommonPage selectPhaseStudentList(ExPhaseStudent phaseStudent) {
        if(phaseStudent.getPhaseId()==null){
            throw new ApiException("批次id不能为空");
        }
        PageUtils.startPage();
        List<ExPhaseStudent> phaseStudentList=baseMapper.selectPhaseStudentList(phaseStudent);
        if(phaseStudentList.size()>0) {
            //获取课程所有课时数量
            int count = courseChapterPeriodMapper.selectCountByCourseId(phaseStudentList.get(0).getCourse().getId());
            if(count>0) {
                for (ExPhaseStudent ps : phaseStudentList) {
                    if (ps.getTotalProgress() != null) {
                        ps.setTotalProgress(ps.getTotalProgress().divide(BigDecimal.valueOf(count), BigDecimal.ROUND_UP));
                    }
                }
            }
        }
        return CommonPage.restPage(phaseStudentList);
    }
 
    @Override
    public CommonPage selectPhaseStudentListForStudent(ExPhaseStudent phaseStudent) {
        SysUser user= SecurityUtils.getLoginUser().getUser();
        if(!Objects.equals(user.getUserType(), UserTypeEnum.STUDENT.getCode())){
            throw new ApiException("非学员用户,不能查看");
        }
        phaseStudent.setStudentId(user.getId());
        PageUtils.startPage();
        List<ExPhaseStudent> phaseStudentList=baseMapper.selectPhaseStudentList(phaseStudent);
        return CommonPage.restPage(phaseStudentList);
    }
 
    @Override
    public int deletePhaseStudent(Long phaseStudentId) {
        ExPhaseStudent phaseStudent=baseMapper.selectPhaseStudentById(phaseStudentId);
        if(ObjectUtil.isNull(phaseStudent)){
            throw new ApiException("该批次下不存在该学员id");
        }
        checkUserAllowed(phaseStudent);
        int studentStudyCount=studentStudyMapper.countByPhaseId(phaseStudent.getPhaseId(),phaseStudent.getStudentId());
        if(studentStudyCount>0){
            throw new ApiException(String.format("学员<%s>已进行该批次学习,不能删除",phaseStudent.getStudentName()));
        }
        //删除学员与批次关系
        return baseMapper.deleteById(phaseStudentId);
    }
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void batchDeletePhaseStudent(List<Long> phaseStudentIds) {
        for(Long phaseStudentId :phaseStudentIds){
            deletePhaseStudent(phaseStudentId);
        }
    }
 
    @Override
    public void checkStudentUnique(List<ExPhaseStudent> phaseStudents){
        for(ExPhaseStudent phaseStudent:phaseStudents) {
            Integer existCount = baseMapper.selectCountByPhaseStudentId(phaseStudent.getPhaseId(), phaseStudent.getStudentId());
            if (existCount > 0) {
                ExStudent student = studentMapper.selectById(phaseStudent.getStudentId());
                throw new ApiException(String.format("待分配学员<%s>已存在", student.getName()));
            }
        }
    }
 
    @Override
    public List<StudentStudyVO> getPhaseStudentById(Long phaseStudentId) {
        ExPhaseStudent phaseStudent=getById(phaseStudentId);
        return studentStudyService.selectStudyByPhaseAndStundentId(phaseStudent.getPhaseId(),phaseStudent.getStudentId(), UserConstant.ENABLE);
    }
 
 
    public void addCompanyPeriod(Long phaseId,Integer studentCount){
        //扣减企业学时
        ExCoursePhase coursePhase= coursePhaseMapper.selectCoursePhaseById(phaseId);
        ExCompanyPeriod companyPeriod=new ExCompanyPeriod();
        companyPeriod.setPhaseId(phaseId);
        companyPeriod.setModifyPeriod(-1L*studentCount*coursePhase.getCoursePeriod());
        companyPeriod.setOrigin("\""+coursePhase.getName()+"\"新增学员消耗");
        SysCompany company=companyService.getById(coursePhase.getCompanyId());
        Long remainPeriod=company.getRemainPeriod()+companyPeriod.getModifyPeriod();
        if(remainPeriod<0){
            throw new ApiException("企业剩余课时不足");
        }
        companyPeriod.setCompanyId(company.getId());
        companyPeriod.setRemainPeriod(remainPeriod);
        companyPeriodService.save(companyPeriod);
        company.setRemainPeriod(companyPeriod.getRemainPeriod());
        companyService.updateById(company);
    }
 
 
}