|
@@ -4,6 +4,8 @@ import java.util.ArrayList;
|
4
|
4
|
import java.util.List;
|
5
|
5
|
import java.util.stream.Collectors;
|
6
|
6
|
import javax.validation.Validator;
|
|
7
|
+
|
|
8
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
7
|
9
|
import org.slf4j.Logger;
|
8
|
10
|
import org.slf4j.LoggerFactory;
|
9
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -36,8 +38,7 @@ import com.ruoyi.system.service.ISysUserService;
|
36
|
38
|
* @author ruoyi
|
37
|
39
|
*/
|
38
|
40
|
@Service
|
39
|
|
-public class SysUserServiceImpl implements ISysUserService
|
40
|
|
-{
|
|
41
|
+public class SysUserServiceImpl implements ISysUserService {
|
41
|
42
|
private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
|
42
|
43
|
|
43
|
44
|
@Autowired
|
|
@@ -69,8 +70,7 @@ public class SysUserServiceImpl implements ISysUserService
|
69
|
70
|
*/
|
70
|
71
|
@Override
|
71
|
72
|
@DataScope(deptAlias = "d", userAlias = "u")
|
72
|
|
- public List<SysUser> selectUserList(SysUser user)
|
73
|
|
- {
|
|
73
|
+ public List<SysUser> selectUserList(SysUser user) {
|
74
|
74
|
return userMapper.selectUserList(user);
|
75
|
75
|
}
|
76
|
76
|
|
|
@@ -82,8 +82,7 @@ public class SysUserServiceImpl implements ISysUserService
|
82
|
82
|
*/
|
83
|
83
|
@Override
|
84
|
84
|
@DataScope(deptAlias = "d", userAlias = "u")
|
85
|
|
- public List<SysUser> selectAllocatedList(SysUser user)
|
86
|
|
- {
|
|
85
|
+ public List<SysUser> selectAllocatedList(SysUser user) {
|
87
|
86
|
return userMapper.selectAllocatedList(user);
|
88
|
87
|
}
|
89
|
88
|
|
|
@@ -95,8 +94,7 @@ public class SysUserServiceImpl implements ISysUserService
|
95
|
94
|
*/
|
96
|
95
|
@Override
|
97
|
96
|
@DataScope(deptAlias = "d", userAlias = "u")
|
98
|
|
- public List<SysUser> selectUnallocatedList(SysUser user)
|
99
|
|
- {
|
|
97
|
+ public List<SysUser> selectUnallocatedList(SysUser user) {
|
100
|
98
|
return userMapper.selectUnallocatedList(user);
|
101
|
99
|
}
|
102
|
100
|
|
|
@@ -107,8 +105,7 @@ public class SysUserServiceImpl implements ISysUserService
|
107
|
105
|
* @return 用户对象信息
|
108
|
106
|
*/
|
109
|
107
|
@Override
|
110
|
|
- public SysUser selectUserByUserName(String userName)
|
111
|
|
- {
|
|
108
|
+ public SysUser selectUserByUserName(String userName) {
|
112
|
109
|
return userMapper.selectUserByUserName(userName);
|
113
|
110
|
}
|
114
|
111
|
|
|
@@ -119,8 +116,7 @@ public class SysUserServiceImpl implements ISysUserService
|
119
|
116
|
* @return 用户对象信息
|
120
|
117
|
*/
|
121
|
118
|
@Override
|
122
|
|
- public SysUser selectUserById(Long userId)
|
123
|
|
- {
|
|
119
|
+ public SysUser selectUserById(Long userId) {
|
124
|
120
|
return userMapper.selectUserById(userId);
|
125
|
121
|
}
|
126
|
122
|
|
|
@@ -131,11 +127,9 @@ public class SysUserServiceImpl implements ISysUserService
|
131
|
127
|
* @return 结果
|
132
|
128
|
*/
|
133
|
129
|
@Override
|
134
|
|
- public String selectUserRoleGroup(String userName)
|
135
|
|
- {
|
|
130
|
+ public String selectUserRoleGroup(String userName) {
|
136
|
131
|
List<SysRole> list = roleMapper.selectRolesByUserName(userName);
|
137
|
|
- if (CollectionUtils.isEmpty(list))
|
138
|
|
- {
|
|
132
|
+ if (CollectionUtils.isEmpty(list)) {
|
139
|
133
|
return StringUtils.EMPTY;
|
140
|
134
|
}
|
141
|
135
|
return list.stream().map(SysRole::getRoleName).collect(Collectors.joining(","));
|
|
@@ -148,11 +142,9 @@ public class SysUserServiceImpl implements ISysUserService
|
148
|
142
|
* @return 结果
|
149
|
143
|
*/
|
150
|
144
|
@Override
|
151
|
|
- public String selectUserPostGroup(String userName)
|
152
|
|
- {
|
|
145
|
+ public String selectUserPostGroup(String userName) {
|
153
|
146
|
List<SysPost> list = postMapper.selectPostsByUserName(userName);
|
154
|
|
- if (CollectionUtils.isEmpty(list))
|
155
|
|
- {
|
|
147
|
+ if (CollectionUtils.isEmpty(list)) {
|
156
|
148
|
return StringUtils.EMPTY;
|
157
|
149
|
}
|
158
|
150
|
return list.stream().map(SysPost::getPostName).collect(Collectors.joining(","));
|
|
@@ -165,12 +157,10 @@ public class SysUserServiceImpl implements ISysUserService
|
165
|
157
|
* @return 结果
|
166
|
158
|
*/
|
167
|
159
|
@Override
|
168
|
|
- public boolean checkUserNameUnique(SysUser user)
|
169
|
|
- {
|
|
160
|
+ public boolean checkUserNameUnique(SysUser user) {
|
170
|
161
|
Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
|
171
|
162
|
SysUser info = userMapper.checkUserNameUnique(user.getUserName());
|
172
|
|
- if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
|
173
|
|
- {
|
|
163
|
+ if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue()) {
|
174
|
164
|
return UserConstants.NOT_UNIQUE;
|
175
|
165
|
}
|
176
|
166
|
return UserConstants.UNIQUE;
|
|
@@ -183,12 +173,10 @@ public class SysUserServiceImpl implements ISysUserService
|
183
|
173
|
* @return
|
184
|
174
|
*/
|
185
|
175
|
@Override
|
186
|
|
- public boolean checkPhoneUnique(SysUser user)
|
187
|
|
- {
|
|
176
|
+ public boolean checkPhoneUnique(SysUser user) {
|
188
|
177
|
Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
|
189
|
178
|
SysUser info = userMapper.checkPhoneUnique(user.getPhonenumber());
|
190
|
|
- if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
|
191
|
|
- {
|
|
179
|
+ if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue()) {
|
192
|
180
|
return UserConstants.NOT_UNIQUE;
|
193
|
181
|
}
|
194
|
182
|
return UserConstants.UNIQUE;
|
|
@@ -201,12 +189,10 @@ public class SysUserServiceImpl implements ISysUserService
|
201
|
189
|
* @return
|
202
|
190
|
*/
|
203
|
191
|
@Override
|
204
|
|
- public boolean checkEmailUnique(SysUser user)
|
205
|
|
- {
|
|
192
|
+ public boolean checkEmailUnique(SysUser user) {
|
206
|
193
|
Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
|
207
|
194
|
SysUser info = userMapper.checkEmailUnique(user.getEmail());
|
208
|
|
- if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
|
209
|
|
- {
|
|
195
|
+ if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue()) {
|
210
|
196
|
return UserConstants.NOT_UNIQUE;
|
211
|
197
|
}
|
212
|
198
|
return UserConstants.UNIQUE;
|
|
@@ -218,10 +204,8 @@ public class SysUserServiceImpl implements ISysUserService
|
218
|
204
|
* @param user 用户信息
|
219
|
205
|
*/
|
220
|
206
|
@Override
|
221
|
|
- public void checkUserAllowed(SysUser user)
|
222
|
|
- {
|
223
|
|
- if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin())
|
224
|
|
- {
|
|
207
|
+ public void checkUserAllowed(SysUser user) {
|
|
208
|
+ if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin()) {
|
225
|
209
|
throw new ServiceException("不允许操作超级管理员用户");
|
226
|
210
|
}
|
227
|
211
|
}
|
|
@@ -232,15 +216,12 @@ public class SysUserServiceImpl implements ISysUserService
|
232
|
216
|
* @param userId 用户id
|
233
|
217
|
*/
|
234
|
218
|
@Override
|
235
|
|
- public void checkUserDataScope(Long userId)
|
236
|
|
- {
|
237
|
|
- if (!SysUser.isAdmin(SecurityUtils.getUserId()))
|
238
|
|
- {
|
|
219
|
+ public void checkUserDataScope(Long userId) {
|
|
220
|
+ if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
|
239
|
221
|
SysUser user = new SysUser();
|
240
|
222
|
user.setUserId(userId);
|
241
|
223
|
List<SysUser> users = SpringUtils.getAopProxy(this).selectUserList(user);
|
242
|
|
- if (StringUtils.isEmpty(users))
|
243
|
|
- {
|
|
224
|
+ if (StringUtils.isEmpty(users)) {
|
244
|
225
|
throw new ServiceException("没有权限访问用户数据!");
|
245
|
226
|
}
|
246
|
227
|
}
|
|
@@ -254,8 +235,8 @@ public class SysUserServiceImpl implements ISysUserService
|
254
|
235
|
*/
|
255
|
236
|
@Override
|
256
|
237
|
@Transactional
|
257
|
|
- public int insertUser(SysUser user)
|
258
|
|
- {
|
|
238
|
+ public int insertUser(SysUser user) {
|
|
239
|
+ user.setUserId(Long.parseLong(IdWorker.getIdStr().substring(4)));
|
259
|
240
|
// 新增用户信息
|
260
|
241
|
int rows = userMapper.insertUser(user);
|
261
|
242
|
// 新增用户岗位关联
|
|
@@ -272,8 +253,7 @@ public class SysUserServiceImpl implements ISysUserService
|
272
|
253
|
* @return 结果
|
273
|
254
|
*/
|
274
|
255
|
@Override
|
275
|
|
- public boolean registerUser(SysUser user)
|
276
|
|
- {
|
|
256
|
+ public boolean registerUser(SysUser user) {
|
277
|
257
|
return userMapper.insertUser(user) > 0;
|
278
|
258
|
}
|
279
|
259
|
|
|
@@ -285,8 +265,7 @@ public class SysUserServiceImpl implements ISysUserService
|
285
|
265
|
*/
|
286
|
266
|
@Override
|
287
|
267
|
@Transactional
|
288
|
|
- public int updateUser(SysUser user)
|
289
|
|
- {
|
|
268
|
+ public int updateUser(SysUser user) {
|
290
|
269
|
Long userId = user.getUserId();
|
291
|
270
|
// 删除用户与角色关联
|
292
|
271
|
userRoleMapper.deleteUserRoleByUserId(userId);
|
|
@@ -302,13 +281,12 @@ public class SysUserServiceImpl implements ISysUserService
|
302
|
281
|
/**
|
303
|
282
|
* 用户授权角色
|
304
|
283
|
*
|
305
|
|
- * @param userId 用户ID
|
|
284
|
+ * @param userId 用户ID
|
306
|
285
|
* @param roleIds 角色组
|
307
|
286
|
*/
|
308
|
287
|
@Override
|
309
|
288
|
@Transactional
|
310
|
|
- public void insertUserAuth(Long userId, Long[] roleIds)
|
311
|
|
- {
|
|
289
|
+ public void insertUserAuth(Long userId, Long[] roleIds) {
|
312
|
290
|
userRoleMapper.deleteUserRoleByUserId(userId);
|
313
|
291
|
insertUserRole(userId, roleIds);
|
314
|
292
|
}
|
|
@@ -320,8 +298,7 @@ public class SysUserServiceImpl implements ISysUserService
|
320
|
298
|
* @return 结果
|
321
|
299
|
*/
|
322
|
300
|
@Override
|
323
|
|
- public int updateUserStatus(SysUser user)
|
324
|
|
- {
|
|
301
|
+ public int updateUserStatus(SysUser user) {
|
325
|
302
|
return userMapper.updateUser(user);
|
326
|
303
|
}
|
327
|
304
|
|
|
@@ -332,8 +309,7 @@ public class SysUserServiceImpl implements ISysUserService
|
332
|
309
|
* @return 结果
|
333
|
310
|
*/
|
334
|
311
|
@Override
|
335
|
|
- public int updateUserProfile(SysUser user)
|
336
|
|
- {
|
|
312
|
+ public int updateUserProfile(SysUser user) {
|
337
|
313
|
return userMapper.updateUser(user);
|
338
|
314
|
}
|
339
|
315
|
|
|
@@ -341,12 +317,11 @@ public class SysUserServiceImpl implements ISysUserService
|
341
|
317
|
* 修改用户头像
|
342
|
318
|
*
|
343
|
319
|
* @param userName 用户名
|
344
|
|
- * @param avatar 头像地址
|
|
320
|
+ * @param avatar 头像地址
|
345
|
321
|
* @return 结果
|
346
|
322
|
*/
|
347
|
323
|
@Override
|
348
|
|
- public boolean updateUserAvatar(String userName, String avatar)
|
349
|
|
- {
|
|
324
|
+ public boolean updateUserAvatar(String userName, String avatar) {
|
350
|
325
|
return userMapper.updateUserAvatar(userName, avatar) > 0;
|
351
|
326
|
}
|
352
|
327
|
|
|
@@ -357,8 +332,7 @@ public class SysUserServiceImpl implements ISysUserService
|
357
|
332
|
* @return 结果
|
358
|
333
|
*/
|
359
|
334
|
@Override
|
360
|
|
- public int resetPwd(SysUser user)
|
361
|
|
- {
|
|
335
|
+ public int resetPwd(SysUser user) {
|
362
|
336
|
return userMapper.updateUser(user);
|
363
|
337
|
}
|
364
|
338
|
|
|
@@ -370,8 +344,7 @@ public class SysUserServiceImpl implements ISysUserService
|
370
|
344
|
* @return 结果
|
371
|
345
|
*/
|
372
|
346
|
@Override
|
373
|
|
- public int resetUserPwd(String userName, String password)
|
374
|
|
- {
|
|
347
|
+ public int resetUserPwd(String userName, String password) {
|
375
|
348
|
return userMapper.resetUserPwd(userName, password);
|
376
|
349
|
}
|
377
|
350
|
|
|
@@ -380,8 +353,7 @@ public class SysUserServiceImpl implements ISysUserService
|
380
|
353
|
*
|
381
|
354
|
* @param user 用户对象
|
382
|
355
|
*/
|
383
|
|
- public void insertUserRole(SysUser user)
|
384
|
|
- {
|
|
356
|
+ public void insertUserRole(SysUser user) {
|
385
|
357
|
this.insertUserRole(user.getUserId(), user.getRoleIds());
|
386
|
358
|
}
|
387
|
359
|
|
|
@@ -390,15 +362,12 @@ public class SysUserServiceImpl implements ISysUserService
|
390
|
362
|
*
|
391
|
363
|
* @param user 用户对象
|
392
|
364
|
*/
|
393
|
|
- public void insertUserPost(SysUser user)
|
394
|
|
- {
|
|
365
|
+ public void insertUserPost(SysUser user) {
|
395
|
366
|
Long[] posts = user.getPostIds();
|
396
|
|
- if (StringUtils.isNotEmpty(posts))
|
397
|
|
- {
|
|
367
|
+ if (StringUtils.isNotEmpty(posts)) {
|
398
|
368
|
// 新增用户与岗位管理
|
399
|
|
- List<SysUserPost> list = new ArrayList<SysUserPost>(posts.length);
|
400
|
|
- for (Long postId : posts)
|
401
|
|
- {
|
|
369
|
+ List<SysUserPost> list = new ArrayList<>(posts.length);
|
|
370
|
+ for (Long postId : posts) {
|
402
|
371
|
SysUserPost up = new SysUserPost();
|
403
|
372
|
up.setUserId(user.getUserId());
|
404
|
373
|
up.setPostId(postId);
|
|
@@ -411,17 +380,14 @@ public class SysUserServiceImpl implements ISysUserService
|
411
|
380
|
/**
|
412
|
381
|
* 新增用户角色信息
|
413
|
382
|
*
|
414
|
|
- * @param userId 用户ID
|
|
383
|
+ * @param userId 用户ID
|
415
|
384
|
* @param roleIds 角色组
|
416
|
385
|
*/
|
417
|
|
- public void insertUserRole(Long userId, Long[] roleIds)
|
418
|
|
- {
|
419
|
|
- if (StringUtils.isNotEmpty(roleIds))
|
420
|
|
- {
|
|
386
|
+ public void insertUserRole(Long userId, Long[] roleIds) {
|
|
387
|
+ if (StringUtils.isNotEmpty(roleIds)) {
|
421
|
388
|
// 新增用户与角色管理
|
422
|
389
|
List<SysUserRole> list = new ArrayList<SysUserRole>(roleIds.length);
|
423
|
|
- for (Long roleId : roleIds)
|
424
|
|
- {
|
|
390
|
+ for (Long roleId : roleIds) {
|
425
|
391
|
SysUserRole ur = new SysUserRole();
|
426
|
392
|
ur.setUserId(userId);
|
427
|
393
|
ur.setRoleId(roleId);
|
|
@@ -439,8 +405,7 @@ public class SysUserServiceImpl implements ISysUserService
|
439
|
405
|
*/
|
440
|
406
|
@Override
|
441
|
407
|
@Transactional
|
442
|
|
- public int deleteUserById(Long userId)
|
443
|
|
- {
|
|
408
|
+ public int deleteUserById(Long userId) {
|
444
|
409
|
// 删除用户与角色关联
|
445
|
410
|
userRoleMapper.deleteUserRoleByUserId(userId);
|
446
|
411
|
// 删除用户与岗位表
|
|
@@ -456,10 +421,8 @@ public class SysUserServiceImpl implements ISysUserService
|
456
|
421
|
*/
|
457
|
422
|
@Override
|
458
|
423
|
@Transactional
|
459
|
|
- public int deleteUserByIds(Long[] userIds)
|
460
|
|
- {
|
461
|
|
- for (Long userId : userIds)
|
462
|
|
- {
|
|
424
|
+ public int deleteUserByIds(Long[] userIds) {
|
|
425
|
+ for (Long userId : userIds) {
|
463
|
426
|
checkUserAllowed(new SysUser(userId));
|
464
|
427
|
checkUserDataScope(userId);
|
465
|
428
|
}
|
|
@@ -473,16 +436,14 @@ public class SysUserServiceImpl implements ISysUserService
|
473
|
436
|
/**
|
474
|
437
|
* 导入用户数据
|
475
|
438
|
*
|
476
|
|
- * @param userList 用户数据列表
|
|
439
|
+ * @param userList 用户数据列表
|
477
|
440
|
* @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
|
478
|
|
- * @param operName 操作用户
|
|
441
|
+ * @param operName 操作用户
|
479
|
442
|
* @return 结果
|
480
|
443
|
*/
|
481
|
444
|
@Override
|
482
|
|
- public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName)
|
483
|
|
- {
|
484
|
|
- if (StringUtils.isNull(userList) || userList.size() == 0)
|
485
|
|
- {
|
|
445
|
+ public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName) {
|
|
446
|
+ if (StringUtils.isNull(userList) || userList.size() == 0) {
|
486
|
447
|
throw new ServiceException("导入用户数据不能为空!");
|
487
|
448
|
}
|
488
|
449
|
int successNum = 0;
|
|
@@ -490,23 +451,18 @@ public class SysUserServiceImpl implements ISysUserService
|
490
|
451
|
StringBuilder successMsg = new StringBuilder();
|
491
|
452
|
StringBuilder failureMsg = new StringBuilder();
|
492
|
453
|
String password = configService.selectConfigByKey("sys.user.initPassword");
|
493
|
|
- for (SysUser user : userList)
|
494
|
|
- {
|
495
|
|
- try
|
496
|
|
- {
|
|
454
|
+ for (SysUser user : userList) {
|
|
455
|
+ try {
|
497
|
456
|
// 验证是否存在这个用户
|
498
|
457
|
SysUser u = userMapper.selectUserByUserName(user.getUserName());
|
499
|
|
- if (StringUtils.isNull(u))
|
500
|
|
- {
|
|
458
|
+ if (StringUtils.isNull(u)) {
|
501
|
459
|
BeanValidators.validateWithException(validator, user);
|
502
|
460
|
user.setPassword(SecurityUtils.encryptPassword(password));
|
503
|
461
|
user.setCreateBy(operName);
|
504
|
462
|
userMapper.insertUser(user);
|
505
|
463
|
successNum++;
|
506
|
464
|
successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
|
507
|
|
- }
|
508
|
|
- else if (isUpdateSupport)
|
509
|
|
- {
|
|
465
|
+ } else if (isUpdateSupport) {
|
510
|
466
|
BeanValidators.validateWithException(validator, user);
|
511
|
467
|
checkUserAllowed(u);
|
512
|
468
|
checkUserDataScope(u.getUserId());
|
|
@@ -515,28 +471,21 @@ public class SysUserServiceImpl implements ISysUserService
|
515
|
471
|
userMapper.updateUser(user);
|
516
|
472
|
successNum++;
|
517
|
473
|
successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
|
518
|
|
- }
|
519
|
|
- else
|
520
|
|
- {
|
|
474
|
+ } else {
|
521
|
475
|
failureNum++;
|
522
|
476
|
failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
|
523
|
477
|
}
|
524
|
|
- }
|
525
|
|
- catch (Exception e)
|
526
|
|
- {
|
|
478
|
+ } catch (Exception e) {
|
527
|
479
|
failureNum++;
|
528
|
480
|
String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败:";
|
529
|
481
|
failureMsg.append(msg + e.getMessage());
|
530
|
482
|
log.error(msg, e);
|
531
|
483
|
}
|
532
|
484
|
}
|
533
|
|
- if (failureNum > 0)
|
534
|
|
- {
|
|
485
|
+ if (failureNum > 0) {
|
535
|
486
|
failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
|
536
|
487
|
throw new ServiceException(failureMsg.toString());
|
537
|
|
- }
|
538
|
|
- else
|
539
|
|
- {
|
|
488
|
+ } else {
|
540
|
489
|
successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
|
541
|
490
|
}
|
542
|
491
|
return successMsg.toString();
|
|
@@ -553,6 +502,6 @@ public class SysUserServiceImpl implements ISysUserService
|
553
|
502
|
@Override
|
554
|
503
|
public List<SysUser> postUserList(Long deptId, String postName) {
|
555
|
504
|
|
556
|
|
- return userMapper.postUserList(deptId,postName);
|
|
505
|
+ return userMapper.postUserList(deptId, postName);
|
557
|
506
|
}
|
558
|
507
|
}
|