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);
|
}
|
|
|
}
|