kongzy
2024-09-14 f0f00e9ba8a755e4317e029d73b69a92ad9f9df1
exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExStudentServiceImpl.java
@@ -12,16 +12,24 @@
import com.gkhy.exam.common.utils.PageUtils;
import com.gkhy.exam.common.utils.RedisUtils;
import com.gkhy.exam.common.utils.SecurityUtils;
import com.gkhy.exam.common.utils.StringUtils;
import com.gkhy.exam.system.domain.ExPaperStudent;
import com.gkhy.exam.system.domain.ExPhaseStudent;
import com.gkhy.exam.system.domain.ExStudent;
import com.gkhy.exam.system.domain.SysCompany;
import com.gkhy.exam.system.domain.vo.TrainRecordVO;
import com.gkhy.exam.system.mapper.ExPaperStudentMapper;
import com.gkhy.exam.system.mapper.ExPhaseStudentMapper;
import com.gkhy.exam.system.mapper.ExStudentMapper;
import com.gkhy.exam.system.mapper.SysUserMapper;
import com.gkhy.exam.system.service.ExStudentService;
import com.gkhy.exam.system.service.SysCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
 * <p>
@@ -37,12 +45,34 @@
    private SysCompanyService companyService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private ExPhaseStudentMapper phaseStudentMapper;
    @Autowired
    private ExPaperStudentMapper paperStudentMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Override
    public CommonPage selectStudentList(ExStudent student) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            student.setCompanyId(currentUser.getCompanyId());
            Map<String,Object> paramsMap=new HashMap<>();
            if(currentUser.getUserType().equals(UserTypeEnum.DEPART_USER.getCode())) {//部门级用户
                List<Long> workshopUserIds=userMapper.selectWorkshopUserIds(currentUser.getId());
                if(workshopUserIds==null){
                    workshopUserIds=new ArrayList<>();
                }
                workshopUserIds.add(currentUser.getId());
                paramsMap.put("createIds",workshopUserIds);
                student.setParams(paramsMap);
            }else if(currentUser.getUserType().equals(UserTypeEnum.WORKSHOP_USER.getCode())){//车间级用户
                List<Long> workshopUserIds=new ArrayList<>();
                workshopUserIds.add(currentUser.getId());
                workshopUserIds.add(currentUser.getParentId());
                paramsMap.put("createIds",workshopUserIds);
                student.setParams(paramsMap);
            }
        }
        PageUtils.startPage();
        List<ExStudent> studentList=baseMapper.selectStudentList(student);
@@ -73,6 +103,10 @@
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            return student;
        }else if (currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
            if(!Objects.equals(studentId, currentUser.getId())){
                throw new ApiException("无权查看其它学员信息");
            }
        }
        if(!student.getCompanyId().equals(currentUser.getCompanyId())){
            throw new ApiException("无权限查看其它企业学员");
@@ -84,6 +118,7 @@
    @Override
    public int insertStudent(ExStudent student) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        student.setCompanyId(currentUser.getCompanyId());
        checkUserAllowed(student);
        if(!checkPhoneUnique(student)){
            throw new ApiException("手机号已存在");
@@ -91,7 +126,19 @@
        if(!checkIdNoUnique(student)){
            throw new ApiException("身份证号已存在");
        }
        student.setCreateId(currentUser.getId());
        if(currentUser.getUserType().equals(UserTypeEnum.COMPANY_USER.getCode())){
            if(student.getCreateId()==null){
                throw new ApiException("部门id为空");
            }
        }else if(currentUser.getUserType().equals(UserTypeEnum.DEPART_USER.getCode())){
            student.setCreateId(currentUser.getId());
        }else{//当前用户为车间级用户
            if(currentUser.getParentId()==null){
                throw new ApiException("当前用户部门id为空");
            }
            student.setCreateId(currentUser.getParentId());
        }
        student.setPassword(SecurityUtils.encryptPassword(Base64.decodeStr(student.getPassword())));
        int row=baseMapper.insert(student);
        if(row<0){
            throw new ApiException("新增学员失败");
@@ -109,6 +156,7 @@
            throw new ApiException("身份证号已存在");
        }
        ExStudent existStudent=checkUserDataScope(student.getId());
        student.setPassword(null);
        int row=baseMapper.updateById(student);
        if(row<0){
            throw new ApiException("更新学员失败");
@@ -150,13 +198,30 @@
    }
    @Override
    public ExStudent checkIdNoUnique(String idNo) {
        ExStudent stu= baseMapper.checkIdNoUnique(idNo);
        if(stu!=null){
            SysCompany company=companyService.selectCompanyById(stu.getCompanyId());
            stu.setCompany(company);
    public Map checkIdNoUnique(String idNo) {
        if(StringUtils.isBlank(idNo)){
            throw new ApiException("身份证号不能为空");
        }
        return stu;
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        ExStudent stu= baseMapper.checkIdNoUnique(idNo);
        Map<String,Object> resMap=new HashMap<>();
        Integer status=0;//默认不存在
        if(stu!=null){
            status=1; //存在,且同一个公司
            resMap.put("studentId",stu.getId());
            resMap.put("studentName",stu.getName());
            if(stu.getCompanyId()!=currentUser.getCompanyId()){
                status=2;  //存在,不同公司
                SysCompany company=companyService.selectCompanyById(stu.getCompanyId());
                if(company==null){
                    throw new ApiException("学员公司不存在");
                }
                resMap.put("companyId",company.getId());
                resMap.put("companyName",company.getName());
            }
        }
        resMap.put("status",status);
        return resMap;
    }
    @Override
@@ -167,6 +232,62 @@
        su.setUpdateBy(SecurityUtils.getUsername());
        delCacheByPhone(existStudent.getPhone());
        return updateById(su);
    }
    @Override
    public void changeStudentCompany(Map<String, Long> bodyMap) {
        Long studentId=bodyMap.get("studentId");
        Long companyId=bodyMap.get("companyId");
        if(studentId==null||companyId==null){
            throw new ApiException("学员id或者公司id不能为空");
        }
        ExStudent student = baseMapper.selectById(studentId);
        if(student==null){
            throw new ApiException("学员不存在");
        }
        SysCompany company=companyService.selectCompanyById(companyId);
        if(company==null){
            throw new ApiException("公司不存在");
        }
        ExStudent stu=new ExStudent().setId(studentId).setCompanyId(companyId);
        stu.setUpdateBy(SecurityUtils.getUsername());
        baseMapper.updateById(stu);
    }
    @Override
    public List<TrainRecordVO> trainRecord(Long studentId) {
        List<TrainRecordVO> trainRecordVOList=new ArrayList<>();
        //查询培训记录
        List<ExPhaseStudent> phaseStudentList=phaseStudentMapper.selectPhaseStudentByStudentId(studentId);
        if(phaseStudentList.size()>0){
            trainRecordVOList.addAll(phaseStudentList.stream().map(item -> {
                TrainRecordVO trainRecordVO=new TrainRecordVO();
                trainRecordVO.setStudentId(item.getStudentId());
                trainRecordVO.setTrainType(1);
                trainRecordVO.setName(item.getPhaseName());
                trainRecordVO.setCreateTime(item.getCreateTime());
                trainRecordVO.setCompanyId(item.getCompanyId());
                trainRecordVO.setCompanyName(item.getCompanyName());
                return trainRecordVO;
            }).collect(Collectors.toList()));
        }
        //查询考试记录
        List<ExPaperStudent> paperStudentList=paperStudentMapper.selectByStudentId(studentId);
        if(paperStudentList.size()>0){
            trainRecordVOList.addAll(paperStudentList.stream().map(item -> {
                TrainRecordVO trainRecordVO=new TrainRecordVO();
                trainRecordVO.setStudentId(item.getStudentId());
                trainRecordVO.setTrainType(2);
                trainRecordVO.setName(item.getExamPaper().getName());
                trainRecordVO.setCreateTime(item.getCreateTime());
                trainRecordVO.setCompanyId(item.getCompanyId());
                trainRecordVO.setCompanyName(item.getCompanyName());
                return trainRecordVO;
            }).collect(Collectors.toList()));
        }
        //排序
        trainRecordVOList=trainRecordVOList.stream().sorted(Comparator.comparing(TrainRecordVO::getCreateTime)).collect(Collectors.toList());;
        return trainRecordVOList;
    }
    public ExStudent checkUserDataScope(Long studentId) {
@@ -183,13 +304,27 @@
    public void checkUserAllowed(ExStudent student) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            throw new ApiException("系统管理员没有权限操作");
        if(student.getId()!=null){
            if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
                return;
            }
            if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode()) ){
                if(!Objects.equals(currentUser.getId(), student.getId())){
                    throw new ApiException("没有权限操作");
                }else{
                    return;
                }
            }
        }else{
            if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
                throw new ApiException("系统管理员没有权限操作");
            }
            if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
                throw new ApiException("没有权限操作");
            }
        }
        if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
            throw new ApiException("没有权限操作");
        }
        if(!currentUser.getCompanyId().equals(student.getCompanyId())){
        if(student.getCompanyId()!=null&&!currentUser.getCompanyId().equals(student.getCompanyId())){
            throw new ApiException("没有权限操作其他企业学员");
        }
    }