inleft
2022-02-09 9bcb19959eeb9da9bde2561e7278f6d0a55eb151
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.sys.modular.user.service.impl;
26
27 import cn.hutool.core.bean.BeanUtil;
28 import cn.hutool.core.collection.CollectionUtil;
29 import cn.hutool.core.lang.Dict;
30 import cn.hutool.core.util.ObjectUtil;
31 import cn.hutool.core.util.RandomUtil;
32 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
33 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
34 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
35 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
36 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
37 import me.zhyd.oauth.enums.AuthUserGender;
38 import me.zhyd.oauth.model.AuthUser;
39 import org.springframework.stereotype.Service;
40 import org.springframework.transaction.annotation.Transactional;
41 import vip.xiaonuo.core.consts.CommonConstant;
42 import vip.xiaonuo.core.consts.SymbolConstant;
43 import vip.xiaonuo.core.context.constant.ConstantContextHolder;
44 import vip.xiaonuo.core.context.login.LoginContextHolder;
45 import vip.xiaonuo.core.enums.CommonStatusEnum;
46 import vip.xiaonuo.core.exception.PermissionException;
47 import vip.xiaonuo.core.exception.ServiceException;
48 import vip.xiaonuo.core.exception.enums.PermissionExceptionEnum;
49 import vip.xiaonuo.core.exception.enums.StatusExceptionEnum;
50 import vip.xiaonuo.core.factory.PageFactory;
51 import vip.xiaonuo.core.pojo.page.PageResult;
52 import vip.xiaonuo.core.util.CryptogramUtil;
53 import vip.xiaonuo.core.util.PoiUtil;
54 import vip.xiaonuo.sys.core.enums.AdminTypeEnum;
55 import vip.xiaonuo.sys.core.enums.OauthSexEnum;
56 import vip.xiaonuo.sys.core.enums.SexEnum;
57 import vip.xiaonuo.sys.modular.emp.param.SysEmpParam;
58 import vip.xiaonuo.sys.modular.emp.result.SysEmpInfo;
59 import vip.xiaonuo.sys.modular.emp.service.SysEmpService;
60 import vip.xiaonuo.sys.modular.file.service.SysFileInfoService;
61 import vip.xiaonuo.sys.modular.user.entity.SysUser;
62 import vip.xiaonuo.sys.modular.user.enums.SysUserExceptionEnum;
63 import vip.xiaonuo.sys.modular.user.factory.SysUserFactory;
64 import vip.xiaonuo.sys.modular.user.mapper.SysUserMapper;
65 import vip.xiaonuo.sys.modular.user.param.SysUserParam;
66 import vip.xiaonuo.sys.modular.user.result.SysUserResult;
67 import vip.xiaonuo.sys.modular.user.service.SysUserDataScopeService;
68 import vip.xiaonuo.sys.modular.user.service.SysUserRoleService;
69 import vip.xiaonuo.sys.modular.user.service.SysUserService;
70 import javax.annotation.Resource;
71 import java.util.*;
72
73 /**
74  * 系统用户service接口实现类
75  *
76  * @author xuyuxiang
77  * @date 2020/3/11 17:49
78  */
79 @Service
80 public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
81
82     @Resource
83     private SysEmpService sysEmpService;
84
85     @Resource
86     private SysUserRoleService sysUserRoleService;
87
88     @Resource
89     private SysUserDataScopeService sysUserDataScopeService;
90
91     @Resource
92     private SysFileInfoService sysFileInfoService;
93
94     @Override
95     public SysUser getUserByCount(String account) {
96         LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
97         queryWrapper.eq(SysUser::getAccount, account);
98         queryWrapper.ne(SysUser::getStatus, CommonStatusEnum.DELETED.getCode());
99         return this.getOne(queryWrapper);
100     }
101
102     @Override
103     public PageResult<SysUserResult> page(SysUserParam sysUserParam) {
104         QueryWrapper<SysUserResult> queryWrapper = new QueryWrapper<>();
105         if (ObjectUtil.isNotNull(sysUserParam)) {
106             //根据关键字模糊查询(姓名,账号,手机号)
107             if (ObjectUtil.isNotEmpty(sysUserParam.getSearchValue())) {
108                 queryWrapper.and(q -> q.like("sys_user.account", sysUserParam.getSearchValue())
109                         .or().like("sys_user.name", sysUserParam.getSearchValue()));
110             }
111             //根据员工所属机构查询
112             if (ObjectUtil.isNotEmpty(sysUserParam.getSysEmpParam())) {
113                 SysEmpParam sysEmpParam = sysUserParam.getSysEmpParam();
114                 if (ObjectUtil.isNotEmpty(sysEmpParam.getOrgId())) {
115                     //查询属于该机构的,或该机构下级所有的人员
116                     queryWrapper.and(q -> q.eq("sys_emp.org_id", sysEmpParam.getOrgId())
117                             .or().like("sys_org.pids", sysEmpParam.getOrgId()));
118                 }
119             }
120             //根据状态查询(0正常 1停用),删除的不会展示在列表
121             if (ObjectUtil.isNotEmpty(sysUserParam.getSearchStatus())) {
122                 queryWrapper.eq("sys_user.status", sysUserParam.getSearchStatus());
123             }
124         }
125         //查询非删除状态,排除超级管理员
126         queryWrapper.ne("sys_user.status", CommonStatusEnum.DELETED.getCode())
127                 .ne("sys_user.admin_type", AdminTypeEnum.SUPER_ADMIN.getCode());
128
129         //如果是超级管理员则获取所有用户,否则只获取其数据范围的用户
130         boolean superAdmin = LoginContextHolder.me().isSuperAdmin();
131         if (!superAdmin) {
132             List<Long> dataScope = sysUserParam.getDataScope();
133             if (ObjectUtil.isEmpty(dataScope)) {
134                 return new PageResult<>(new Page<>());
135             } else {
136                 Set<Long> dataScopeSet = CollectionUtil.newHashSet(dataScope);
137                 queryWrapper.in("sys_emp.org_id", dataScopeSet);
138             }
139         }
140
141         return new PageResult<>(this.baseMapper.page(PageFactory.defaultPage(), queryWrapper));
142     }
143
144     @Override
145     public List<Dict> list(SysUserParam sysUserParam) {
146         List<Dict> dictList = CollectionUtil.newArrayList();
147         LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
148         if (ObjectUtil.isNotNull(sysUserParam)) {
149             //根据账号,姓名模糊查询
150             if (ObjectUtil.isNotEmpty(sysUserParam.getAccount())) {
151                 queryWrapper.and(i -> i.like(SysUser::getAccount, sysUserParam.getAccount())
152                         .or().like(SysUser::getName, sysUserParam.getAccount()));
153             }
154         }
155         //查询正常状态,排除超级管理员
156         queryWrapper.eq(SysUser::getStatus, CommonStatusEnum.ENABLE.getCode())
157                 .ne(SysUser::getAdminType, AdminTypeEnum.SUPER_ADMIN.getCode());
158         this.list(queryWrapper).forEach(sysUser -> {
159             Dict dict = Dict.create();
160             dict.put("id", sysUser.getId().toString());
161             dict.put("firstName", sysUser.getName() + SymbolConstant.LEFT_SQUARE_BRACKETS
162                     + sysUser.getAccount() + SymbolConstant.RIGHT_SQUARE_BRACKETS);
163             dictList.add(dict);
164         });
165         return dictList;
166     }
167
168     @Transactional(rollbackFor = Exception.class)
169     @Override
170     public void add(SysUserParam sysUserParam) {
171         checkParam(sysUserParam, false);
172         boolean superAdmin = LoginContextHolder.me().isSuperAdmin();
173         //如果登录用户不是超级管理员,则进行数据权限校验
174         if (!superAdmin) {
175             List<Long> dataScope = sysUserParam.getDataScope();
176             //获取添加的用户的所属机构
177             Long orgId = sysUserParam.getSysEmpParam().getOrgId();
178             //数据范围为空
179             if (ObjectUtil.isEmpty(dataScope)) {
180                 throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
181             } else if (!dataScope.contains(orgId)) {
182                 //所添加的用户的所属机构不在自己的数据范围内
183                 throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
184             }
185         }
186         SysUser sysUser = new SysUser();
187         BeanUtil.copyProperties(sysUserParam, sysUser);
188         SysUserFactory.fillAddCommonUserInfo(sysUser);
189         if(ObjectUtil.isNotEmpty(sysUserParam.getPassword())) {
190             sysUser.setPwdHashValue(CryptogramUtil.doHashValue(sysUserParam.getPassword()));
191         }
192         // 对铭感字段(手机号进行加密保护)
193         if (ConstantContextHolder.getCryptogramConfigs().getFieldEncDec()) {
194             sysUser.setPhone(CryptogramUtil.doEncrypt(sysUserParam.getPhone()));
195         }
196         this.save(sysUser);
197         Long sysUserId = sysUser.getId();
198         //增加员工信息
199         SysEmpParam sysEmpParam = sysUserParam.getSysEmpParam();
200         sysEmpParam.setId(sysUserId);
201         sysEmpService.addOrUpdate(sysEmpParam);
202     }
203
204     @Transactional(rollbackFor = Exception.class)
205     @Override
206     public void delete(List<SysUserParam> sysUserParamList) {
207         sysUserParamList.forEach(sysUserParam -> {
208             SysUser sysUser = this.querySysUser(sysUserParam);
209             //不能删除超级管理员
210             if (AdminTypeEnum.SUPER_ADMIN.getCode().equals(sysUser.getAdminType())) {
211                 throw new ServiceException(SysUserExceptionEnum.USER_CAN_NOT_DELETE_ADMIN);
212             }
213             boolean superAdmin = LoginContextHolder.me().isSuperAdmin();
214             //如果登录用户不是超级管理员,则进行数据权限校验
215             if (!superAdmin) {
216                 List<Long> dataScope = sysUserParam.getDataScope();
217                 //获取要删除的用户的所属机构
218                 SysEmpInfo sysEmpInfo = sysEmpService.getSysEmpInfo(sysUser.getId());
219                 Long orgId = sysEmpInfo.getOrgId();
220                 //数据范围为空
221                 if (ObjectUtil.isEmpty(dataScope)) {
222                     throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
223                 } else if (!dataScope.contains(orgId)) {
224                     //所要删除的用户的所属机构不在自己的数据范围内
225                     throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
226                 }
227             }
228             sysUser.setStatus(CommonStatusEnum.DELETED.getCode());
229             this.updateById(sysUser);
230             Long id = sysUser.getId();
231             //删除该用户对应的员工表信息
232             sysEmpService.deleteEmpInfoByUserId(id);
233
234             //删除该用户对应的用户-角色表关联信息
235             sysUserRoleService.deleteUserRoleListByUserId(id);
236
237             //删除该用户对应的用户-数据范围表关联信息
238             sysUserDataScopeService.deleteUserDataScopeListByUserId(id);
239         });
240     }
241
242     @Transactional(rollbackFor = Exception.class)
243     @Override
244     public void edit(SysUserParam sysUserParam) {
245         SysUser sysUser = this.querySysUser(sysUserParam);
246         checkParam(sysUserParam, true);
247         boolean superAdmin = LoginContextHolder.me().isSuperAdmin();
248         //如果登录用户不是超级管理员,则进行数据权限校验
249         if (!superAdmin) {
250             List<Long> dataScope = sysUserParam.getDataScope();
251             //获取要编辑的用户的所属机构
252             Long orgId = sysUserParam.getSysEmpParam().getOrgId();
253             //数据范围为空
254             if (ObjectUtil.isEmpty(dataScope)) {
255                 throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
256             } else if (!dataScope.contains(orgId)) {
257                 //所要编辑的用户的所属机构不在自己的数据范围内
258                 throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
259             }
260         }
261
262         BeanUtil.copyProperties(sysUserParam, sysUser);
263         //不能修改状态,用修改状态接口修改状态
264         sysUser.setStatus(null);
265         //设置密码
266         SysUserFactory.fillBaseUserInfo(sysUser);
267         if(ObjectUtil.isNotEmpty(sysUserParam.getPassword())) {
268             // 将其哈希掉,存进去
269             sysUser.setPwdHashValue(CryptogramUtil.doHashValue(sysUserParam.getPassword()));
270         }
271
272         // 对铭感字段(手机号进行加密保护)
273         if (ConstantContextHolder.getCryptogramConfigs().getFieldEncDec()) {
274             sysUser.setPhone(CryptogramUtil.doEncrypt(sysUserParam.getPhone()));
275         }
276
277         this.updateById(sysUser);
278         Long sysUserId = sysUser.getId();
279         //编辑员工信息
280         SysEmpParam sysEmpParam = sysUserParam.getSysEmpParam();
281         BeanUtil.copyProperties(sysUserParam, sysEmpParam);
282         sysEmpParam.setId(sysUserId);
283         sysEmpService.addOrUpdate(sysEmpParam);
284     }
285
286     @Override
287     public SysUserResult detail(SysUserParam sysUserParam) {
288         SysUserResult sysUserResult = new SysUserResult();
289         //获取系统用户
290         SysUser sysUser = this.querySysUser(sysUserParam);
291         BeanUtil.copyProperties(sysUser, sysUserResult);
292         //获取对应员工信息
293         SysEmpInfo sysEmpInfo = sysEmpService.getSysEmpInfo(sysUser.getId());
294         //设置员工信息
295         sysUserResult.setSysEmpInfo(sysEmpInfo);
296         return sysUserResult;
297     }
298
299     @Override
300     public void changeStatus(SysUserParam sysUserParam) {
301         SysUser sysUser = this.querySysUser(sysUserParam);
302         //不能修改超级管理员状态
303         if (AdminTypeEnum.SUPER_ADMIN.getCode().equals(sysUser.getAdminType())) {
304             throw new ServiceException(SysUserExceptionEnum.USER_CAN_NOT_UPDATE_ADMIN);
305         }
306
307         Long id = sysUser.getId();
308
309         Integer status = sysUserParam.getStatus();
310         //校验状态在不在枚举值里
311         CommonStatusEnum.validateStatus(status);
312
313         //更新枚举,更新只能更新未删除状态的
314         LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
315         updateWrapper.eq(SysUser::getId, id)
316                 .and(i -> i.ne(SysUser::getStatus, CommonStatusEnum.DELETED.getCode()))
317                 .set(SysUser::getStatus, status);
318         boolean update = this.update(updateWrapper);
319         if (!update) {
320             throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
321         }
322     }
323
324     @Transactional(rollbackFor = Exception.class)
325     @Override
326     public void grantRole(SysUserParam sysUserParam) {
327         SysUser sysUser = this.querySysUser(sysUserParam);
328         boolean superAdmin = LoginContextHolder.me().isSuperAdmin();
329         //如果登录用户不是超级管理员,则进行数据权限校验
330         if (!superAdmin) {
331             List<Long> dataScope = sysUserParam.getDataScope();
332             //获取要授权角色的用户的所属机构
333             SysEmpInfo sysEmpInfo = sysEmpService.getSysEmpInfo(sysUser.getId());
334             Long orgId = sysEmpInfo.getOrgId();
335             //数据范围为空
336             if (ObjectUtil.isEmpty(dataScope)) {
337                 throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
338             } else if (!dataScope.contains(orgId)) {
339                 //所要授权角色的用户的所属机构不在自己的数据范围内
340                 throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
341             }
342         }
343         sysUserRoleService.grantRole(sysUserParam);
344     }
345
346     @Transactional(rollbackFor = Exception.class)
347     @Override
348     public void grantData(SysUserParam sysUserParam) {
349         SysUser sysUser = this.querySysUser(sysUserParam);
350         boolean superAdmin = LoginContextHolder.me().isSuperAdmin();
351         //如果登录用户不是超级管理员,则进行数据权限校验
352         if (!superAdmin) {
353             List<Long> dataScope = sysUserParam.getDataScope();
354             //获取要授权数据的用户的所属机构
355             SysEmpInfo sysEmpInfo = sysEmpService.getSysEmpInfo(sysUser.getId());
356             Long orgId = sysEmpInfo.getOrgId();
357             //数据范围为空
358             if (ObjectUtil.isEmpty(dataScope)) {
359                 throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
360             } else if (!dataScope.contains(orgId)) {
361                 //所要授权数据的用户的所属机构不在自己的数据范围内
362                 throw new PermissionException(PermissionExceptionEnum.NO_PERMISSION_OPERATE);
363             }
364         }
365         sysUserDataScopeService.grantData(sysUserParam);
366     }
367
368     @Override
369     public void updateInfo(SysUserParam sysUserParam) {
370         SysUser sysUser = this.querySysUser(sysUserParam);
371         BeanUtil.copyProperties(sysUserParam, sysUser);
372         this.updateById(sysUser);
373     }
374
375     @Override
376     public void updatePwd(SysUserParam sysUserParam) {
377         SysUser sysUser = this.querySysUser(sysUserParam);
378         //新密码与原密码相同
379         if (sysUserParam.getNewPassword().equals(sysUserParam.getPassword())) {
380             throw new ServiceException(SysUserExceptionEnum.USER_PWD_REPEAT);
381         }
382         //原密码错误
383         if (!sysUser.getPwdHashValue().equals(CryptogramUtil.doHashValue(sysUserParam.getPassword()))) {
384             throw new ServiceException(SysUserExceptionEnum.USER_PWD_ERROR);
385         }
386         sysUser.setPwdHashValue(CryptogramUtil.doHashValue(sysUserParam.getNewPassword()));
387         this.updateById(sysUser);
388     }
389
390     @Override
391     public List<Long> getUserDataScopeIdList(Long userId, Long orgId) {
392         Set<Long> userDataScopeIdSet = CollectionUtil.newHashSet();
393         if (ObjectUtil.isAllNotEmpty(userId, orgId)) {
394
395             //获取该用户对应的数据范围集合
396             List<Long> userDataScopeIdListForUser = sysUserDataScopeService.getUserDataScopeIdList(userId);
397
398             //获取该用户的角色对应的数据范围集合
399             List<Long> userDataScopeIdListForRole = sysUserRoleService.getUserRoleDataScopeIdList(userId, orgId);
400
401             userDataScopeIdSet.addAll(userDataScopeIdListForUser);
402             userDataScopeIdSet.addAll(userDataScopeIdListForRole);
403         }
404         return CollectionUtil.newArrayList(userDataScopeIdSet);
405     }
406
407     @Override
408     public String getNameByUserId(Long userId) {
409         SysUser sysUser = this.getById(userId);
410         if (ObjectUtil.isNull(sysUser)) {
411             throw new ServiceException(SysUserExceptionEnum.USER_NOT_EXIST);
412         }
413         return sysUser.getName();
414     }
415
416     @Override
417     public List<Long> ownRole(SysUserParam sysUserParam) {
418         SysUser sysUser = this.querySysUser(sysUserParam);
419         return sysUserRoleService.getUserRoleIdList(sysUser.getId());
420     }
421
422     @Override
423     public List<Long> ownData(SysUserParam sysUserParam) {
424         SysUser sysUser = this.querySysUser(sysUserParam);
425         return sysUserDataScopeService.getUserDataScopeIdList(sysUser.getId());
426     }
427
428     @Override
429     public void resetPwd(SysUserParam sysUserParam) {
430         SysUser sysUser = this.querySysUser(sysUserParam);
431         String password = ConstantContextHolder.getDefaultPassWord();
432         sysUser.setPwdHashValue(CryptogramUtil.doHashValue(password));
433         this.updateById(sysUser);
434     }
435
436     @Override
437     public void updateAvatar(SysUserParam sysUserParam) {
438         SysUser sysUser = this.querySysUser(sysUserParam);
439         Long avatar = sysUserParam.getAvatar();
440         sysFileInfoService.assertFile(avatar);
441         sysUser.setAvatar(avatar);
442         this.updateById(sysUser);
443     }
444
445     @Override
446     public void export(SysUserParam sysUserParam) {
447         // 只导出正常的
448         LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
449         queryWrapper.eq(SysUser::getStatus, CommonStatusEnum.ENABLE.getCode());
450         // 其他的条件正常来说导出也只能是自己权限范围内看到的用户,改天我们再优化
451         List<SysUser> list = this.list(queryWrapper);
452         PoiUtil.exportExcelWithStream("SonwyUsers.xls", SysUser.class, list);
453     }
454
455     @Override
456     public List<Dict> selector(SysUserParam sysUserParam) {
457         List<Dict> dictList = CollectionUtil.newArrayList();
458         LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
459         if (ObjectUtil.isNotNull(sysUserParam)) {
460             //根据姓名模糊查询
461             if (ObjectUtil.isNotEmpty(sysUserParam.getName())) {
462                 queryWrapper.like(SysUser::getName, sysUserParam.getName());
463             }
464         }
465         //查询非删除状态,排除超级管理员
466         queryWrapper.ne(SysUser::getStatus, CommonStatusEnum.DELETED.getCode())
467                 .ne(SysUser::getAdminType, AdminTypeEnum.SUPER_ADMIN.getCode());
468         this.list(queryWrapper).forEach(sysUser -> {
469             Dict dict  = Dict.create();
470             dict.put(CommonConstant.ID, sysUser.getId());
471             dict.put(CommonConstant.NAME, sysUser.getName());
472             dictList.add(dict);
473         });
474         return dictList;
475     }
476
477     @Override
478     public SysUser getUserById(Long userId) {
479         SysUser sysUser = this.getById(userId);
480         if (ObjectUtil.isNull(sysUser)) {
481             throw new ServiceException(SysUserExceptionEnum.USER_NOT_EXIST);
482         }
483         return sysUser;
484     }
485
486     @Override
487     public void saveAuthUserToUser(AuthUser authUser, SysUser sysUser) {
488         SysUserFactory.fillAddCommonUserInfo(sysUser);
489         //获取oauth用户的账号
490         String username = authUser.getUsername();
491         //判断账号是否在用户表中有重复的
492         LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
493         queryWrapper.eq(SysUser::getAccount, username);
494         SysUser existUser = this.getOne(queryWrapper);
495         if (ObjectUtil.isNotNull(existUser)) {
496             //如果该账号已经存在了,则将oauth用户的账号后缀拼接随机数
497             username = username + RandomUtil.randomString(5);
498         }
499         sysUser.setAccount(username);
500         sysUser.setName(authUser.getNickname());
501         sysUser.setNickName(authUser.getNickname());
502         sysUser.setEmail(authUser.getEmail());
503         if (ObjectUtil.isNotNull(authUser.getGender())) {
504             AuthUserGender gender = authUser.getGender();
505             //性别转换
506             if (OauthSexEnum.MAN.getCode().equals(gender.getCode())) {
507                 sysUser.setSex(SexEnum.MAN.getCode());
508             } else if (OauthSexEnum.WOMAN.getCode().equals(gender.getCode())) {
509                 sysUser.setSex(SexEnum.WOMAN.getCode());
510             } else {
511                 sysUser.setSex(SexEnum.NONE.getCode());
512             }
513         }
514         this.save(sysUser);
515     }
516
517     @Override
518     public List<Long> getAllUserIdList() {
519         List<Long> resultList = CollectionUtil.newArrayList();
520         LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
521         queryWrapper.ne(SysUser::getAdminType, AdminTypeEnum.SUPER_ADMIN);
522         this.list(queryWrapper).forEach(sysUser -> {
523             resultList.add(sysUser.getId());
524         });
525         return resultList;
526     }
527
528     @Override
529     public boolean hasAllDeletedUser(Set<Long> userIdSet) {
530         //查询id在此集合内,且状态为删除的用户,判断其数量是否大于等于集合数量,大于是为了容错
531         LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<SysUser>()
532                 .eq(SysUser::getStatus, CommonStatusEnum.DELETED).in(SysUser::getId, userIdSet);
533         return this.count(lambdaQueryWrapper) >= userIdSet.size();
534     }
535
536     /**
537      * 校验参数,检查是否存在相同的账号
538      *
539      * @author xuyuxiang
540      * @date 2020/3/27 16:04
541      */
542     private void checkParam(SysUserParam sysUserParam, boolean isExcludeSelf) {
543         Long id = sysUserParam.getId();
544         String account = sysUserParam.getAccount();
545         LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
546         queryWrapper.eq(SysUser::getAccount, account)
547                 .ne(SysUser::getStatus, CommonStatusEnum.DELETED.getCode());
548         //是否排除自己,如果是则查询条件排除自己id
549         if (isExcludeSelf) {
550             queryWrapper.ne(SysUser::getId, id);
551         }
552         int countByAccount = this.count(queryWrapper);
553         //大于等于1个则表示重复
554         if (countByAccount >= 1) {
555             throw new ServiceException(SysUserExceptionEnum.USER_ACCOUNT_REPEAT);
556         }
557     }
558
559     /**
560      * 获取系统用户
561      *
562      * @author xuyuxiang
563      * @date 2020/3/26 9:54
564      */
565     private SysUser querySysUser(SysUserParam sysUserParam) {
566         SysUser sysUser = this.getById(sysUserParam.getId());
567         if (ObjectUtil.isNull(sysUser)) {
568             throw new ServiceException(SysUserExceptionEnum.USER_NOT_EXIST);
569         }
570         return sysUser;
571     }
572 }