StudentServiceImpl.java
5.01 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.zhongzhi.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongzhi.common.configure.DbContextHolder;
import com.zhongzhi.common.constant.Code;
import com.zhongzhi.common.constant.DBTypeEnum;
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.dao.CollegesDictDAO;
import com.zhongzhi.dao.SmsCodeDAO;
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.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 SmsCodeDAO smsCodeDAO;
@Autowired
private CollegesDictDAO collegesDictDAO;
@Override
public StudentInfoVO register(RegisterDTO registerDTO) {
DbContextHolder.setDbType(DBTypeEnum.db2);
StudentInfoVO studentInfoVO = new StudentInfoVO();
SmsCodeDO smsCodeDO = smsCodeDAO.getOneByTelePhone(registerDTO.getTelephone(), SmsCode.register, SmsCode.student);
if (smsCodeDO == null) {
throw new HttpException(10025);
}
if (!smsCodeDO.getCode().equals(registerDTO.getCode())) {
throw new HttpException(10021);
}
smsCodeDAO.deleteById(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("上海电子信息职业技术学院");
student.setEducation("高职");
student.setClassName(registerDTO.getClassName());
this.baseMapper.insert(student);
String token = JwtUtil.generateToken(student.getId(), SmsCode.student);
BeanUtils.copyProperties(registerDTO, studentInfoVO);
studentInfoVO.setCollege(student.getCollege());
studentInfoVO.setEducation(student.getEducation());
studentInfoVO.setToken(token);
studentInfoVO.setClassName(student.getClassName());
studentInfoVO.setSex(student.getSex());
return studentInfoVO;
}
@Override
public StudentInfoVO login(LoginDTO loginDTO) {
DbContextHolder.setDbType(DBTypeEnum.db2);
StudentInfoVO studentInfoVO = new StudentInfoVO();
SmsCodeDO smsCodeDO = smsCodeDAO.getOneByTelePhone(loginDTO.getTelephone(), SmsCode.login, SmsCode.student);
if (smsCodeDO == null) {
throw new HttpException(10025);
}
if (!smsCodeDO.getCode().equals(loginDTO.getCode())) {
throw new HttpException(10021);
}
smsCodeDAO.deleteById(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 = collegesDictDAO.selectOne(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) {
DbContextHolder.setDbType(DBTypeEnum.db2);
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);
}
}