StudentServiceImpl.java 4.53 KB
package com.zhongzhi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongzhi.common.constant.Code;
import com.zhongzhi.common.constant.SmsCode;
import com.zhongzhi.common.exception.HttpException;
import com.zhongzhi.common.utils.JwtUtil;
import com.zhongzhi.common.utils.Localstorage;
import com.zhongzhi.dto.student.LoginDTO;
import com.zhongzhi.dto.student.RegisterDTO;
import com.zhongzhi.model.CollegesDictDO;
import com.zhongzhi.model.SmsCodeDO;
import com.zhongzhi.model.StudentDO;
import com.zhongzhi.dao.StudentDAO;
import com.zhongzhi.service.CollegesDictService;
import com.zhongzhi.service.SmsCodeService;
import com.zhongzhi.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongzhi.vo.student.StudentInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2021-04-28
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentDAO, StudentDO> implements StudentService {

    @Autowired
    private SmsCodeService smsCodeService;

    @Autowired
    private CollegesDictService collegesDictService;

    @Override
    public StudentInfoVO register(RegisterDTO registerDTO) {
        StudentInfoVO studentInfoVO = new StudentInfoVO();
        SmsCodeDO smsCodeDO = smsCodeService.getOneByTelePhone(registerDTO.getTelephone(), SmsCode.register, SmsCode.student);
        if (smsCodeDO == null) {
            throw new HttpException(10025);
        }

        if (!smsCodeDO.getCode().equals(registerDTO.getCode())) {
            throw new HttpException(10021);
        }

        smsCodeService.removeById(smsCodeDO.getId());

        if (!registerDTO.getTelephone().equals(smsCodeDO.getTelephone())) {
            throw new HttpException(10022);
        }

        StudentDO telephone = this.baseMapper.selectOne(new QueryWrapper<StudentDO>()
                .lambda()
                .eq(StudentDO::getTelephone, registerDTO.getTelephone()));
        if (telephone != null) {
            throw new HttpException(10020);
        }

        Integer count = this.baseMapper.selectCount(new QueryWrapper<StudentDO>()
                .lambda()
                .eq(StudentDO::getIdCard, registerDTO.getIdCard()));
        if (count > 0) {
            throw new HttpException(10023);
        }

        StudentDO student = new StudentDO();
        BeanUtils.copyProperties(registerDTO, student);
        student.setCollege(registerDTO.getSchool());
        this.baseMapper.insert(student);

        String token = JwtUtil.generateToken(student.getId(), SmsCode.student);
        BeanUtils.copyProperties(registerDTO, studentInfoVO);
        studentInfoVO.setCollege(registerDTO.getSchool());
        studentInfoVO.setToken(token);
        return studentInfoVO;
    }

    @Override
    public StudentInfoVO login(LoginDTO loginDTO) {
        StudentInfoVO studentInfoVO = new StudentInfoVO();
        SmsCodeDO smsCodeDO = smsCodeService.getOneByTelePhone(loginDTO.getTelephone(), SmsCode.login, SmsCode.student);
        if (smsCodeDO == null) {
            throw new HttpException(10025);
        }

        if (!smsCodeDO.getCode().equals(loginDTO.getCode())) {
            throw new HttpException(10021);
        }

        smsCodeService.removeById(smsCodeDO.getId());

        StudentDO studentDO = this.baseMapper.selectOne(new QueryWrapper<StudentDO>()
                .lambda()
                .eq(StudentDO::getTelephone, loginDTO.getTelephone()));
        if (studentDO == null) {
            throw new HttpException(10024);
        }

        CollegesDictDO collegesDictDO = collegesDictService.getOne(new QueryWrapper<CollegesDictDO>()
                .lambda()
                .eq(CollegesDictDO::getName, studentDO.getCollege()));
        if(collegesDictDO.getLoginStatus() == 0) {
            throw new HttpException(10067);
        }

        String token = JwtUtil.generateToken(studentDO.getId(), SmsCode.student);
        BeanUtils.copyProperties(studentDO, studentInfoVO);
        studentInfoVO.setToken(token);
        return studentInfoVO;
    }

    @Override
    public void updateStudentInfo(StudentDO studentDO) {
        StudentDO student = (StudentDO) Localstorage.getUser();
        if (studentDO == null) {
            throw new HttpException(Code.TOKEN_EXPIRED.getCode(), Code.TOKEN_EXPIRED.getMessage());
        }

        studentDO.setId(student.getId());
        this.baseMapper.updateById(studentDO);
    }
}