heheng
2 天以前 338f2f5afc7762dabf2409f296d5d023462ed4b5
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package com.gkhy.exam.system.service.impl;
 
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.common.annotation.DataScope;
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.SysRole;
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.ExStudent;
import com.gkhy.exam.system.domain.SysUserRole;
import com.gkhy.exam.system.mapper.SysUserMapper;
import com.gkhy.exam.system.mapper.SysUserRoleMapper;
import com.gkhy.exam.system.service.ExStudentService;
import com.gkhy.exam.system.service.SysConfigService;
import com.gkhy.exam.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.validation.Validator;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
 
/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2023-10-17 14:26:29
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private RedisUtils redisUtils;
 
    @Autowired
    private SysConfigService configService;
    @Autowired
    private Validator validator;
    @Autowired
    private SysUserRoleMapper userRoleMapper;
 
    @Autowired
    private ExStudentService exStudentService;
 
    @Override
    public CommonPage<SysUser> selectUserList(SysUser user) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        List<SysUser> users = new ArrayList<>();
        if (!currentUser.getUserType().equals(UserTypeEnum.WORKSHOP_USER.getCode())) {
            if (!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
                user.setCompanyId(currentUser.getCompanyId());
                Map<String, Object> paramsMap = new HashMap<>();
//                paramsMap.put("userType",currentUser.getUserType());
                user.setParams(paramsMap);
            }
            PageUtils.startPage();
            users = baseMapper.userList(user);
        }
        return CommonPage.restPage(users);
    }
 
    /**
     * 根据条件分页查询已分配用户角色列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    @DataScope(userAlias = "u")
    public List<SysUser> selectAllocatedList(SysUser user) {
        return baseMapper.selectAllocatedList(user);
    }
 
    /**
     * 根据条件分页查询未分配用户角色列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    @DataScope(userAlias = "u")
    public List<SysUser> selectUnallocatedList(SysUser user) {
        return baseMapper.selectUnallocatedList(user);
    }
 
 
    @Override
    public SysUser selectUserByUsername(String username) {
        String key = redisUtils.generateKey(CacheConstant.SYS_ADMIN_USER_NAME + username);
        SysUser sysUser = null;
        if (redisUtils.hasKey(key)) {
            sysUser = (SysUser) redisUtils.get(key);
        } else {
            sysUser = baseMapper.getUserByUsername(username);
            redisUtils.set(key, sysUser, 10, TimeUnit.MINUTES);
        }
        return sysUser;
    }
 
    public void delCacheByUsername(String username) {
        String key = redisUtils.generateKey(CacheConstant.SYS_ADMIN_USER_NAME + username);
        redisUtils.del(key);
    }
 
    @Override
    public SysUser selectUserByPhone(String phone) {
        return baseMapper.getUserByPhone(phone);
    }
 
    @Override
    public SysUser selectUserById(Long userId) {
        return baseMapper.getUserById(userId);
    }
 
    @Override
    public int deleteUserById(Long userId) {
        SysUser user = checkUserDataScope(userId);
        if (user.getId().toString().equals("1")) {
            throw new ApiException("管理员用户不能被删除");
        }
        delCacheByUsername(user.getUsername());
        return baseMapper.deleteUserById(userId);
    }
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int addUser(SysUser user) {
        ExStudent exStudent = new ExStudent();
        exStudent.setCompanyId(user.getCompanyId());
        exStudent.setName(user.getName());
        exStudent.setPassword(user.getPassword());
        exStudent.setDeptId(user.getDeptId());
        exStudent.setDuty(user.getDuty());
        exStudent.setSex(user.getSex());
        exStudent.setPhone(user.getPhone());
        checkRequestData(user);
        checkUserAllowed(user);
        user.setCreateBy(SecurityUtils.getUsername());
        user.setPassword(SecurityUtils.encryptPassword(Base64.decodeStr(user.getPassword())));
        int row = baseMapper.insert(user);
 
        batchSaveRole(user.getRoles(), user.getId(), false);
 
        exStudentService.insertStudent(exStudent);
 
        if (row < 1) {
            throw new ApiException("新增用户失败");
        }
        return row;
    }
 
    public void batchSaveRole(List<SysRole> roleIds, Long userId, boolean isUpdate) {
 
        if (isUpdate) {
            userRoleMapper.deleteUserRoleByUserId(userId);
        }
 
        List<SysUserRole> userRoleList = new ArrayList<>();
        if (roleIds != null && !roleIds.isEmpty()) {
 
            for (SysRole roleId : roleIds) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(userId);
                sysUserRole.setRoleId(roleId.getRoleId());
                userRoleList.add(sysUserRole);
            }
        } else {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(userId);
            sysUserRole.setRoleId(2L);
            userRoleList.add(sysUserRole);
        }
        userRoleMapper.batchUserRole(userRoleList);
    }
 
    @Override
    public int updateUser(SysUser user) {
        checkRequestData(user);
        checkUserAllowed(user);
        user.setUpdateBy(SecurityUtils.getUsername());
        user.setPassword(null);
        int row = baseMapper.updateById(user);
        batchSaveRole(user.getRoles(), user.getId(), true);
 
        if (row < 1) {
            throw new ApiException("更新用户信息失败");
        }
        delCacheByUsername(user.getUsername());
        return row;
    }
 
    public void checkRequestData(SysUser user) {
        if (!user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode()) && user.getCompanyId() == null) {
            throw new ApiException("所属公司不能为空");
        }
        if (!checkUsernameUnique(user)) {
            throw new ApiException("登录账号已存在");
        }
        if (StringUtils.isNotBlank(user.getPhone()) && !checkPhoneUnique(user)) {
            throw new ApiException("手机号已存在");
        }
    }
 
    @Override
    public int updateUserStatus(SysUser user) {
        SysUser existUser = checkUserDataScope(user.getId());
        SysUser su = new SysUser().setId(user.getId()).setStatus(user.getStatus());
        su.setUpdateBy(SecurityUtils.getUsername());
 
        int row = baseMapper.updateById(su);
        if (row < 1) {
            throw new ApiException("更新用户状态失败");
        }
        delCacheByUsername(existUser.getUsername());
        return row;
    }
 
    @Override
    public int updateSpecialCertificate(SysUser user) {
        SysUser existUser = checkUserDataScope(user.getId());
        SysUser su = new SysUser().setId(user.getId()).setOperationCertificate(user.getOperationCertificate());
        su.setUpdateBy(SecurityUtils.getUsername());
 
        int row = baseMapper.updateById(su);
        if (row < 1) {
            throw new ApiException("更新用户状态失败");
        }
        return row;
    }
 
 
    @Override
    public boolean resetUserPwd(SysUser user) {
        SysUser existUser = checkUserDataScope(user.getId());
        SysUser su = new SysUser().setId(user.getId()).setPassword(SecurityUtils.encryptPassword(Base64.decodeStr(user.getPassword())));
        su.setUpdateBy(SecurityUtils.getUsername());
        delCacheByUsername(existUser.getUsername());
        return updateById(su);
    }
 
    @Override
    public boolean checkUsernameUnique(SysUser user) {
        Long userId = user.getId() == null ? -1L : user.getId();
        SysUser info = baseMapper.checkLoginNameUnique(user.getUsername());
        if (info != null && info.getId().longValue() != userId.longValue()) {
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
 
    @Override
    public Map<String, Object> getUserData(Long companyId) {
        List<SysUser> sysUsers = baseMapper.selectList(new LambdaQueryWrapper<>(
                SysUser.class
        ).eq(SysUser::getCompanyId, companyId).eq(SysUser::getDelFlag, UserConstant.ENABLE));
        int totalUser = sysUsers.size();
 
        Map<String, Object> map = new HashMap<>();
        //总人数
        map.put("totalUser", totalUser);
        //专业
        map.put("post", sysUsers.stream()
                .map(SysUser::getPost)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.joining(",")));
        //学历硕士以上
        // 统计学历为硕士(4)和博士及以上(5)的人数
        long count = sysUsers.stream()
                .filter(user -> user.getQualification() != null)
                .filter(user -> user.getQualification() >= 4)  // 4: 硕士, 5: 博士及以上
                .count();
        map.put("qualification", count);
 
        long qualification1 = sysUsers.stream()
                .filter(user -> user.getQualification() != null)
                .filter(user -> user.getQualification().equals("1"))
                .count();
        map.put("qualification1", qualification1);//学历1、高中及以下
        long qualification2 = sysUsers.stream()
                .filter(user -> user.getQualification() != null)
                .filter(user -> user.getQualification().equals("2"))
                .count();
        map.put("qualification2", qualification2);//学历2、专科
        long qualification3 = sysUsers.stream()
                .filter(user -> user.getQualification() != null)
                .filter(user -> user.getQualification().equals("3"))
                .count();
        map.put("qualification3", qualification3);//学历3本科
        long qualification4 = sysUsers.stream()
                .filter(user -> user.getQualification() != null)
                .filter(user -> user.getQualification().equals("4"))
                .count();
        map.put("qualification4", qualification4);//学历4硕士
 
        long qualification5 = sysUsers.stream()
                .filter(user -> user.getQualification() != null)
                .filter(user -> user.getQualification().equals("5"))
                .count();
        map.put("qualification5", qualification5);//学历5博士及以上
 
        //高级职称人数
        long positionalCount = sysUsers.stream()
                .filter(user -> user.getPositional() != null)
                .filter(user -> user.getPositional().equals("3"))  // 4: 硕士, 5: 博士及以上
                .count();
        map.put("positional3", positionalCount);
        long positionalCount2 = sysUsers.stream()
                .filter(user -> user.getPositional() != null)
                .filter(user -> user.getPositional().equals("2"))  // 4: 硕士, 5: 博士及以上
                .count();
        map.put("positional2", positionalCount2);//中级
        long positionalCount1 = sysUsers.stream()
                .filter(user -> user.getPositional() != null)
                .filter(user -> user.getPositional().equals("1"))  // 4: 硕士, 5: 博士及以上
                .count();
        map.put("positional1", positionalCount1);//初级
 
        map.put("positional12", positionalCount2 + positionalCount1);//中级+初级
 
        long countBk = sysUsers.stream()
                .filter(user -> user.getQualification() != null)
                .filter(user -> user.getQualification() >= 3)  // 本科以上
                .count();
        //本科人数
        map.put("qualificationBK", countBk);
        double bachelorRatio = totalUser > 0 ? (double) countBk / totalUser * 100 : 0;
        //本科占比
        map.put("qualificationBKRatio", String.format("%.2f%%", bachelorRatio));
 
        long countJS = sysUsers.stream()
                .filter(user -> user.getPersonType() != null)
                .filter(user -> user.getPersonType() == 1)  // 技术类
                .count();
        map.put("personTypeJS", countJS);
        double jsRatio = totalUser > 0 ? (double) countJS / totalUser * 100 : 0;
        map.put("jsRatio", String.format("%.2f%%", jsRatio));
        double averageAge = sysUsers.stream()
                .filter(user -> user.getAge() != null)  // 过滤年龄为null的用户
                .mapToInt(SysUser::getAge)              // 提取年龄值
                .average()                              // 计算平均值
                .orElse(0.0);
        map.put("avgAge", String.format("%.1f", averageAge));//平均年龄
 
        long countAge40 = sysUsers.stream()
                .filter(user -> user.getAge() != null)
                .filter(user -> user.getAge() >= 40)  // 40以上人数
                .count();
        map.put("age40", countAge40);
        double age40Ratio = totalUser > 0 ? (double) countAge40 / totalUser * 100 : 0;
        map.put("age40Ratio", String.format("%.2f%%", age40Ratio));
        long countAge30 = totalUser - countAge40;
        map.put("age1840", countAge30);//18-40
        double age30Ratio = totalUser > 0 ? (double) countAge30 / totalUser * 100 : 0;
        map.put("age1840Ratio", String.format("%.2f%%", age30Ratio));
 
        return Collections.emptyMap();
    }
 
    @Override
    public boolean checkPhoneUnique(SysUser user) {
        Long userId = user.getId() == null ? -1L : user.getId();
        SysUser info = baseMapper.checkPhoneUnique(user.getPhone());
        if (info != null && info.getId().longValue() != userId.longValue()) {
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
 
 
    @Override
    public void checkUserAllowed(SysUser user) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        Integer currentUserType = currentUser.getUserType();
        Integer userType = user.getUserType();
        //校验权限,规则:上一级用户可以增加下一级用户类型的用户
//        if(currentUserType.equals(UserTypeEnum.SYSTEM_USER.getCode())){
//            if( !userType.equals(UserTypeEnum.SYSTEM_USER.getCode())&&!userType.equals(UserTypeEnum.OTHER_USER.getCode()) &&!userType.equals(UserTypeEnum.COMPANY_USER.getCode())){
//                throw new ApiException("管理员只能操作管理员、企业级和其他类型的用户");
//            }
//            if (user.getId().toString().equals("1")){
//                throw new ApiException("管理员用户不能被删除");
//            }
//        }else{
        if (!currentUserType.equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            if (userType.equals(UserTypeEnum.OTHER_USER.getCode())) {
                throw new ApiException("没有权限操作或者更新上级用户类型的用户");
            }
            if (currentUserType.equals(UserTypeEnum.OTHER_USER.getCode()) || currentUserType.equals(UserTypeEnum.COMPANY_ADMIN.getCode())) {
                if (userType <= UserTypeEnum.COMPANY_USER.getCode() && !currentUserType.equals(UserTypeEnum.COMPANY_ADMIN.getCode())) {
                    throw new ApiException("没有权限操作或者更新上级用户类型的用户");
                }
            } else {
                if (userType < currentUserType) {
                    throw new ApiException("没有权限操作或者更新上级用户类型的用户");
                }
            }
        }
    }
 
    @Override
    public SysUser checkUserDataScope(Long userId) {
        if (userId == null) {
            throw new ApiException("用户id为空!");
        }
        SysUser user = getById(userId);
        if (ObjectUtil.isNull(user)) {
            throw new ApiException("用户数据不存在!");
        }
        checkUserAllowed(user);
        return user;
    }
 
}