Blame view

StudentServiceImpl.java 4.53 KB
涂亚平 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
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);
    }
}