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.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.ExCoursePhase;
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;
/**
*
* 课时批次表 服务实现类
*
*
* @author kzy
* @since 2024-06-05 15:07:36
*/
@Service
public class ExCoursePhaseServiceImpl extends ServiceImpl implements ExCoursePhaseService {
@Autowired
private ExPhaseStudentMapper phaseStudentMapper;
@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 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);
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);
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(!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("当前账户无权限新增事业部级级批次");
}
}
@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;
}
}