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.system.domain.ExStudent;
import com.gkhy.exam.system.domain.SysCompany;
import com.gkhy.exam.system.mapper.ExStudentMapper;
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.concurrent.TimeUnit;
/**
*
* 学员表 服务实现类
*
*
* @author kzy
* @since 2024-06-06 13:53:17
*/
@Service
public class ExStudentServiceImpl extends ServiceImpl implements ExStudentService {
@Autowired
private SysCompanyService companyService;
@Autowired
private RedisUtils redisUtils;
@Override
public CommonPage selectStudentList(ExStudent student) {
SysUser currentUser= SecurityUtils.getLoginUser().getUser();
if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
student.setCompanyId(currentUser.getCompanyId());
}
PageUtils.startPage();
List 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;
}
if(!student.getCompanyId().equals(currentUser.getCompanyId())){
throw new ApiException("无权限查看其它企业学员");
}
return student;
}
@Override
public int insertStudent(ExStudent student) {
SysUser currentUser= SecurityUtils.getLoginUser().getUser();
checkUserAllowed(student);
if(!checkPhoneUnique(student)){
throw new ApiException("手机号已存在");
}
if(!checkIdNoUnique(student)){
throw new ApiException("身份证号已存在");
}
student.setCreateId(currentUser.getId());
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());
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 ExStudent checkIdNoUnique(String idNo) {
ExStudent stu= baseMapper.checkIdNoUnique(idNo);
if(stu!=null){
SysCompany company=companyService.selectCompanyById(stu.getCompanyId());
stu.setCompany(company);
}
return stu;
}
@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);
}
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(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
throw new ApiException("系统管理员没有权限操作");
}
if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
throw new ApiException("没有权限操作");
}
if(!currentUser.getCompanyId().equals(student.getCompanyId())){
throw new ApiException("没有权限操作其他企业学员");
}
}
}