“djh”
3 天以前 9bc1958825de5c9427659a8824a9e86864c2a457
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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.common.utils.StringUtils;
import com.gkhy.exam.system.domain.ExPaperStudent;
import com.gkhy.exam.system.domain.ExPhaseStudent;
import com.gkhy.exam.system.domain.ExStudent;
import com.gkhy.exam.system.domain.SysCompany;
import com.gkhy.exam.system.domain.vo.TrainRecordVO;
import com.gkhy.exam.system.mapper.ExPaperStudentMapper;
import com.gkhy.exam.system.mapper.ExPhaseStudentMapper;
import com.gkhy.exam.system.mapper.ExStudentMapper;
import com.gkhy.exam.system.mapper.SysUserMapper;
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.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
/**
 * <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;
    @Autowired
    private ExPhaseStudentMapper phaseStudentMapper;
    @Autowired
    private ExPaperStudentMapper paperStudentMapper;
    @Autowired
    private SysUserMapper userMapper;
 
    @Override
    public CommonPage selectStudentList(ExStudent student) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            student.setCompanyId(currentUser.getCompanyId());
            Map<String,Object> paramsMap=new HashMap<>();
            if(currentUser.getUserType().equals(UserTypeEnum.DEPART_USER.getCode())) {//部门级用户
                List<Long> workshopUserIds=userMapper.selectWorkshopUserIds(currentUser.getId());
                if(workshopUserIds==null){
                    workshopUserIds=new ArrayList<>();
                }
                workshopUserIds.add(currentUser.getId());
                paramsMap.put("createIds",workshopUserIds);
                student.setParams(paramsMap);
            }else if(currentUser.getUserType().equals(UserTypeEnum.WORKSHOP_USER.getCode())){//车间级用户
                List<Long> workshopUserIds=new ArrayList<>();
                workshopUserIds.add(currentUser.getId());
                workshopUserIds.add(currentUser.getParentId());
                paramsMap.put("createIds",workshopUserIds);
                student.setParams(paramsMap);
            }
        }
        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;
        }else if (currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
            if(!Objects.equals(studentId, currentUser.getId())){
                throw new ApiException("无权查看其它学员信息");
            }
        }
        if(!student.getCompanyId().equals(currentUser.getCompanyId())){
            throw new ApiException("无权限查看其它企业学员");
        }
        return student;
 
    }
 
    @Override
    public int insertStudent(ExStudent student) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        student.setCompanyId(currentUser.getCompanyId());
        checkUserAllowed(student);
        if(!checkPhoneUnique(student)){
            throw new ApiException("手机号已存在");
        }
        if(!checkIdNoUnique(student)){
            throw new ApiException("身份证号已存在");
        }
        if(currentUser.getUserType().equals(UserTypeEnum.COMPANY_USER.getCode())){
            if(student.getCreateId()==null){
                throw new ApiException("部门id为空");
            }
        }else if(currentUser.getUserType().equals(UserTypeEnum.DEPART_USER.getCode())){
            student.setCreateId(currentUser.getId());
        }else{//当前用户为车间级用户
            if(currentUser.getParentId()==null){
                throw new ApiException("当前用户部门id为空");
            }
            student.setCreateId(currentUser.getParentId());
        }
        student.setPassword(SecurityUtils.encryptPassword(Base64.decodeStr(student.getPassword())));
        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());
        student.setPassword(null);
        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 Map checkIdNoUnique(String idNo) {
        if(StringUtils.isBlank(idNo)){
            throw new ApiException("身份证号不能为空");
        }
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        ExStudent stu= baseMapper.checkIdNoUnique(idNo);
        Map<String,Object> resMap=new HashMap<>();
        Integer status=0;//默认不存在
        if(stu!=null){
            status=1; //存在,且同一个公司
            resMap.put("studentId",stu.getId());
            resMap.put("studentName",stu.getName());
            if(stu.getCompanyId()!=currentUser.getCompanyId()){
                status=2;  //存在,不同公司
                SysCompany company=companyService.selectCompanyById(stu.getCompanyId());
                if(company==null){
                    throw new ApiException("学员公司不存在");
                }
                resMap.put("companyId",company.getId());
                resMap.put("companyName",company.getName());
            }
        }
        resMap.put("status",status);
        return resMap;
    }
 
    @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);
    }
 
    @Override
    public void changeStudentCompany(Map<String, Long> bodyMap) {
        Long studentId=bodyMap.get("studentId");
        Long companyId=bodyMap.get("companyId");
        if(studentId==null||companyId==null){
            throw new ApiException("学员id或者公司id不能为空");
        }
        ExStudent student = baseMapper.selectById(studentId);
        if(student==null){
            throw new ApiException("学员不存在");
        }
        SysCompany company=companyService.selectCompanyById(companyId);
        if(company==null){
            throw new ApiException("公司不存在");
        }
        ExStudent stu=new ExStudent().setId(studentId).setCompanyId(companyId);
        stu.setUpdateBy(SecurityUtils.getUsername());
        baseMapper.updateById(stu);
    }
 
    @Override
    public List<TrainRecordVO> trainRecord(Long studentId) {
        List<TrainRecordVO> trainRecordVOList=new ArrayList<>();
        //查询培训记录
        List<ExPhaseStudent> phaseStudentList=phaseStudentMapper.selectPhaseStudentByStudentId(studentId);
        if(phaseStudentList.size()>0){
            trainRecordVOList.addAll(phaseStudentList.stream().map(item -> {
                TrainRecordVO trainRecordVO=new TrainRecordVO();
                trainRecordVO.setStudentId(item.getStudentId());
                trainRecordVO.setTrainType(1);
                trainRecordVO.setName(item.getPhaseName());
                trainRecordVO.setCreateTime(item.getCreateTime());
                trainRecordVO.setCompanyId(item.getCompanyId());
                trainRecordVO.setCompanyName(item.getCompanyName());
                return trainRecordVO;
            }).collect(Collectors.toList()));
        }
        //查询考试记录
        List<ExPaperStudent> paperStudentList=paperStudentMapper.selectByStudentId(studentId);
        if(paperStudentList.size()>0){
            trainRecordVOList.addAll(paperStudentList.stream().map(item -> {
                TrainRecordVO trainRecordVO=new TrainRecordVO();
                trainRecordVO.setStudentId(item.getStudentId());
                trainRecordVO.setTrainType(2);
                trainRecordVO.setName(item.getExamPaper().getName());
                trainRecordVO.setCreateTime(item.getCreateTime());
                trainRecordVO.setCompanyId(item.getCompanyId());
                trainRecordVO.setCompanyName(item.getCompanyName());
                return trainRecordVO;
            }).collect(Collectors.toList()));
        }
        //排序
        trainRecordVOList=trainRecordVOList.stream().sorted(Comparator.comparing(TrainRecordVO::getCreateTime)).collect(Collectors.toList());;
        return trainRecordVOList;
    }
 
    @Override
    public List<ExStudent> selectStudentCheckAll(ExStudent student) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            student.setCompanyId(currentUser.getCompanyId());
            Map<String,Object> paramsMap=new HashMap<>();
            if(currentUser.getUserType().equals(UserTypeEnum.DEPART_USER.getCode())) {//部门级用户
                List<Long> workshopUserIds=userMapper.selectWorkshopUserIds(currentUser.getId());
                if(workshopUserIds==null){
                    workshopUserIds=new ArrayList<>();
                }
                workshopUserIds.add(currentUser.getId());
                paramsMap.put("createIds",workshopUserIds);
                student.setParams(paramsMap);
            }else if(currentUser.getUserType().equals(UserTypeEnum.WORKSHOP_USER.getCode())){//车间级用户
                List<Long> workshopUserIds=new ArrayList<>();
                workshopUserIds.add(currentUser.getId());
                workshopUserIds.add(currentUser.getParentId());
                paramsMap.put("createIds",workshopUserIds);
                student.setParams(paramsMap);
            }
        }
        return baseMapper.selectStudentList(student);
    }
 
    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(student.getId()!=null){
            if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
                return;
            }
            if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode()) ){
                if(!Objects.equals(currentUser.getId(), student.getId())){
                    throw new ApiException("没有权限操作");
                }else{
                    return;
                }
            }
        }else{
            if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
                throw new ApiException("系统管理员没有权限操作");
            }
            if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
                throw new ApiException("没有权限操作");
            }
        }
 
        if(student.getCompanyId()!=null&&!currentUser.getCompanyId().equals(student.getCompanyId())){
            throw new ApiException("没有权限操作其他企业学员");
        }
    }
 
}