kongzy
2024-06-26 daf7acb4f107a427e4a83ba1eb26e5e6012cbdaf
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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;
 
/**
 * <p>
 * 学员表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-06-06 13:53:17
 */
@Service
public class ExStudentServiceImpl extends ServiceImpl<ExStudentMapper, ExStudent> 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<ExStudent> 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("没有权限操作其他企业学员");
        }
    }
 
}