package top.sharehome.share_study.service.impl;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.sharehome.share_study.common.constant.CommonConstant;
import top.sharehome.share_study.common.exception_handler.customize.CustomizeFileException;
import top.sharehome.share_study.common.exception_handler.customize.CustomizeReturnException;
import top.sharehome.share_study.common.exception_handler.customize.CustomizeTransactionException;
import top.sharehome.share_study.common.response.R;
import top.sharehome.share_study.common.response.RCodeEnum;
import top.sharehome.share_study.mapper.*;
import top.sharehome.share_study.model.dto.admin.AdminGetDto;
import top.sharehome.share_study.model.dto.admin.AdminGetSelfDto;
import top.sharehome.share_study.model.dto.admin.AdminPageDto;
import top.sharehome.share_study.model.dto.teacher.TeacherGetDto;
import top.sharehome.share_study.model.dto.teacher.TeacherLoginDto;
import top.sharehome.share_study.model.dto.teacher.TeacherPageDto;
import top.sharehome.share_study.model.dto.user.UserGetInfoDto;
import top.sharehome.share_study.model.entity.*;
import top.sharehome.share_study.model.vo.admin.AdminPageVo;
import top.sharehome.share_study.model.vo.admin.AdminUpdateSelfVo;
import top.sharehome.share_study.model.vo.admin.AdminUpdateVo;
import top.sharehome.share_study.model.vo.teacher.TeacherLoginVo;
import top.sharehome.share_study.model.vo.teacher.TeacherPageVo;
import top.sharehome.share_study.model.vo.teacher.TeacherRegisterVo;
import top.sharehome.share_study.model.vo.teacher.TeacherUpdateVo;
import top.sharehome.share_study.model.vo.user.UserUpdateInfoSelfVo;
import top.sharehome.share_study.service.FileOssService;
import top.sharehome.share_study.service.TeacherService;
import top.sharehome.share_study.utils.object.ObjectDataUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* 教师用户ServiceImpl
*
* @author AntonyCheng
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
@javax.annotation.Resource
private TeacherMapper teacherMapper;
@javax.annotation.Resource
private CollegeMapper collegeMapper;
@javax.annotation.Resource
private ResourceMapper resourceMapper;
@javax.annotation.Resource
private CommentMapper commentMapper;
@javax.annotation.Resource
private FileOssService fileOssService;
@javax.annotation.Resource
private CollectMapper collectMapper;
@javax.annotation.Resource
private ResourceCensorMapper resourceCensorMapper;
/**
* 注册加盐
*/
private static final String SALT = "share_study_platform";
@Override
@Transactional(rollbackFor = CustomizeTransactionException.class)
public void register(TeacherRegisterVo teacherRegisterVo) {
// 校验数据库中是否包含该用户
LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
teacherLambdaQueryWrapper.eq(Teacher::getAccount, teacherRegisterVo.getAccount());
Long resultFromTeacher = teacherMapper.selectCount(teacherLambdaQueryWrapper);
if (resultFromTeacher != 0) {
throw new CustomizeReturnException(R.failure(RCodeEnum.USERNAME_ALREADY_EXISTS), "数据库中已经包含该用户:" + teacherRegisterVo.getAccount());
}
College resultCollege = collegeMapper.selectById(teacherRegisterVo.getBelong());
if (resultCollege == null) {
throw new CustomizeReturnException(R.failure(RCodeEnum.COLLEGE_NOT_EXISTS), "没有该院校");
}
// 进行数据拷贝和插入
Teacher teacher = new Teacher();
teacher.setBelong(resultCollege.getId());
BeanUtils.copyProperties(teacherRegisterVo, teacher);
teacher.setPassword(DigestUtil.md5Hex(teacher.getPassword() + SALT));
if (StringUtils.isEmpty(teacher.getAvatar())) {
teacher.setAvatar("");
}
int insertResult = teacherMapper.insert(teacher);
// 判断数据库插入结果
if (insertResult == 0) {
throw new CustomizeReturnException(R.failure(RCodeEnum.DATA_ADDITION_FAILED), "注册插入用户失败,从数据库返回的影响行数为0,且在之前没有报出异常");
}
}
@Override
@Transactional(rollbackFor = CustomizeTransactionException.class)
public TeacherLoginDto login(TeacherLoginVo teacherLoginVo, HttpServletRequest request) {
// 首先获取对象中的基本属性
String accountBeforeLogin = teacherLoginVo.getAccount();
String passwordBeforeLogin = teacherLoginVo.getPassword();
// 根据账户查询该用户存在与否
LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Teacher::getAccount, accountBeforeLogin);
Teacher teacher = teacherMapper.selectOne(queryWrapper);
if (teacher == null) {
throw new CustomizeReturnException(R.failure(RCodeEnum.USER_ACCOUNT_DOES_NOT_EXIST));
}
// 查询用户状态是否为可用
if (teacher.getStatus() == 1) {
throw new CustomizeReturnException(R.failure(RCodeEnum.USER_ACCOUNT_BANNED));
}
// 对比密码是否一致
String passwordNeedCompare = DigestUtil.md5Hex(passwordBeforeLogin + SALT);
if (!passwordNeedCompare.equals(teacher.getPassword())) {
throw new CustomizeReturnException(R.failure(RCodeEnum.WRONG_USER_PASSWORD));
}
// 信息脱敏
TeacherLoginDto teacherLoginDto = new TeacherLoginDto();
teacherLoginDto.setId(teacher.getId());
teacherLoginDto.setAccount(teacher.getAccount());
teacherLoginDto.setName(teacher.getName());
teacherLoginDto.setAvatar(teacher.getAvatar());
teacherLoginDto.setGender(teacher.getGender());
String collegeName = collegeMapper.selectById(teacher.getBelong()).getName();
teacherLoginDto.setCollegeName(collegeName);
teacherLoginDto.setEmail(teacher.getEmail());
teacherLoginDto.setScore(teacher.getScore());
teacherLoginDto.setMessageNumber(teacher.getMessageTotal() - teacher.getMessageRead());
teacherLoginDto.setRole(teacher.getRole());
teacherLoginDto.setCreateTime(teacher.getCreateTime());
request.getSession().setAttribute(CommonConstant.USER_LOGIN_STATE, teacherLoginDto);
return teacherLoginDto;
}
@Override
@Transactional(rollbackFor = CustomizeTransactionException.class)
public TeacherLoginDto adminLogin(TeacherLoginVo teacherLoginVo, HttpServletRequest request) {
// 首先获取对象中的基本属性
String accountBeforeLogin = teacherLoginVo.getAccount();
String passwordBeforeLogin = teacherLoginVo.getPassword();
// 根据账户查询该用户存在与否
LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Teacher::getAccount, accountBeforeLogin);
Teacher teacher = teacherMapper.selectOne(queryWrapper);
if (teacher == null) {
throw n