package com.gkhy.exam.system.service.impl;
|
|
import cn.hutool.core.codec.Base64;
|
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.CacheConstant;
|
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.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.*;
|
import java.util.concurrent.TimeUnit;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 学员表 服务实现类
|
* </p>
|
*
|
* @author kzy
|
* @since 2024-06-06 13:53:17
|
*/
|
@Service
|
public class ExStudentServiceImpl extends ServiceImpl<ExStudentMapper, ExStudent> implements ExStudentService {
|
@Autowired
|
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);
|
return CommonPage.restPage(studentList);
|
}
|
|
@Override
|
public ExStudent selectStudentByPhone(String phone) {
|
String key=redisUtils.generateKey(CacheConstant.SYS_STUDENT_USER_NAME+phone);
|
ExStudent student =null;
|
if(redisUtils.hasKey(key)){
|
student= (ExStudent) redisUtils.get(key);
|
}else {
|
student = baseMapper.selectStudentByPhone(phone);
|
redisUtils.set(key,student,10, TimeUnit.MINUTES);
|
}
|
return student;
|
}
|
|
public void delCacheByPhone(String phone){
|
String key=redisUtils.generateKey(CacheConstant.SYS_STUDENT_USER_NAME+phone);
|
redisUtils.del(key);
|
}
|
|
@Override
|
public ExStudent selectStudentById(Long studentId) {
|
ExStudent student= baseMapper.selectStudentById(studentId);
|
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("无权限查看其它企业学员");
|
}
|
return student;
|
|
}
|
|
@Override
|
public int insertStudent(ExStudent student) {
|
SysUser currentUser= SecurityUtils.getLoginUser().getUser();
|
student.setCompanyId(currentUser.getCompanyId());
|
checkUserAllowed(student);
|
if(!checkPhoneUnique(student)){
|
throw new ApiException("手机号已存在");
|
}
|
if(!checkIdNoUnique(student)){
|
throw new ApiException("身份证号已存在");
|
}
|
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("新增学员失败");
|
}
|
return row;
|
}
|
|
@Override
|
public int updateStudent(ExStudent student) {
|
checkUserAllowed(student);
|
if(!checkPhoneUnique(student)){
|
throw new ApiException("手机号已存在");
|
}
|
if(!checkIdNoUnique(student)){
|
throw new ApiException("身份证号已存在");
|
}
|
ExStudent existStudent=checkUserDataScope(student.getId());
|
student.setPassword(null);
|
int row=baseMapper.updateById(student);
|
if(row<0){
|
throw new ApiException("更新学员失败");
|
}
|
delCacheByPhone(existStudent.getPhone());
|
return row;
|
}
|
|
@Override
|
public int deleteStudentById(Long studentId) {
|
ExStudent existStudent=checkUserDataScope(studentId);
|
checkUserAllowed(existStudent);
|
int row=baseMapper.deleteByStudentId(studentId);
|
if(row<0){
|
throw new ApiException("删除学员失败");
|
}
|
delCacheByPhone(existStudent.getPhone());
|
return row;
|
}
|
|
@Override
|
public boolean checkPhoneUnique(ExStudent student) {
|
Long studentId=student.getId()==null?-1L:student.getId();
|
ExStudent stu= baseMapper.checkPhoneUnique(student.getPhone());
|
if(stu!=null&&stu.getId().longValue()!=studentId.longValue()){
|
return UserConstant.NOT_UNIQUE;
|
}
|
return UserConstant.UNIQUE;
|
}
|
|
@Override
|
public boolean checkIdNoUnique(ExStudent student) {
|
Long studentId=student.getId()==null?-1L:student.getId();
|
ExStudent stu= baseMapper.checkIdNoUnique(student.getIdNo());
|
if(stu!=null&&stu.getId().longValue()!=studentId.longValue()){
|
return UserConstant.NOT_UNIQUE;
|
}
|
return UserConstant.UNIQUE;
|
}
|
|
@Override
|
public Map checkIdNoUnique(String idNo) {
|
if(StringUtils.isBlank(idNo)){
|
throw new ApiException("身份证号不能为空");
|
}
|
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
|
public boolean resetUserPwd(ExStudent student) {
|
ExStudent existStudent=getById(student.getId());
|
checkUserAllowed(existStudent);
|
ExStudent su=new ExStudent().setId(student.getId()).setPassword(SecurityUtils.encryptPassword(Base64.decodeStr(student.getPassword())));
|
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) {
|
if(studentId==null){
|
throw new ApiException("学员id为空!");
|
}
|
ExStudent student = getById(studentId);
|
if (ObjectUtil.isNull(studentId))
|
{
|
throw new ApiException("学员数据不存在!");
|
}
|
return student;
|
}
|
|
public void checkUserAllowed(ExStudent student) {
|
SysUser currentUser= SecurityUtils.getLoginUser().getUser();
|
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(student.getCompanyId()!=null&&!currentUser.getCompanyId().equals(student.getCompanyId())){
|
throw new ApiException("没有权限操作其他企业学员");
|
}
|
}
|
|
}
|