cee572f2
RuoYi
若依 3.0
|
1
|
package com.ruoyi.system.service.impl;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
2
3
4
|
import java.util.ArrayList;
import java.util.List;
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
5
|
|
b508e6f2
RuoYi
新增用户导入
|
6
7
|
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
8
9
10
|
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
|
cee572f2
RuoYi
若依 3.0
|
11
|
import com.ruoyi.common.annotation.DataScope;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
12
|
import com.ruoyi.common.constant.UserConstants;
|
cee572f2
RuoYi
若依 3.0
|
13
14
|
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
15
|
import com.ruoyi.common.exception.CustomException;
|
b508e6f2
RuoYi
新增用户导入
|
16
|
import com.ruoyi.common.utils.SecurityUtils;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
17
|
import com.ruoyi.common.utils.StringUtils;
|
cee572f2
RuoYi
若依 3.0
|
18
19
20
21
22
23
24
25
26
27
|
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysPostMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
28
29
30
|
/**
* 用户 业务层处理
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
31
|
*
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
32
33
34
|
* @author ruoyi
*/
@Service
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
35
|
public class SysUserServiceImpl implements ISysUserService {
|
b508e6f2
RuoYi
新增用户导入
|
36
|
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
37
38
|
/**
* 根据条件分页查询用户列表
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
39
|
*
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
40
41
42
43
44
|
* @param user 用户信息
* @return 用户信息集合信息
*/
@Override
@DataScope(deptAlias = "d", userAlias = "u")
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
45
|
public List<SysUser> selectUserList(SysUser user) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
46
47
48
49
|
return userMapper.selectUserList(user);
}
/**
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
50
|
* 查询用户所属角色组
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
51
|
*
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
52
53
54
55
|
* @param userName 用户名
* @return 结果
*/
@Override
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
56
|
public String selectUserRoleGroup(String userName) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
57
58
|
List<SysRole> list = roleMapper.selectRolesByUserName(userName);
StringBuffer idsStr = new StringBuffer();
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
59
|
for (SysRole role : list) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
60
61
|
idsStr.append(role.getRoleName()).append(",");
}
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
62
|
if (StringUtils.isNotEmpty(idsStr.toString())) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
63
64
65
66
67
68
69
|
return idsStr.substring(0, idsStr.length() - 1);
}
return idsStr.toString();
}
/**
* 查询用户所属岗位组
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
70
|
*
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
71
72
73
74
|
* @param userName 用户名
* @return 结果
*/
@Override
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
75
|
public String selectUserPostGroup(String userName) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
76
77
|
List<SysPost> list = postMapper.selectPostsByUserName(userName);
StringBuffer idsStr = new StringBuffer();
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
78
|
for (SysPost post : list) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
79
80
|
idsStr.append(post.getPostName()).append(",");
}
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
81
|
if (StringUtils.isNotEmpty(idsStr.toString())) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
82
83
84
85
86
87
|
return idsStr.substring(0, idsStr.length() - 1);
}
return idsStr.toString();
}
/**
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
88
89
90
91
92
93
|
* 校验email是否唯一
*
* @param user 用户信息
* @return
*/
@Override
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
94
|
public String checkEmailUnique(SysUser user) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
95
96
|
Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
SysUser info = userMapper.checkEmailUnique(user.getEmail());
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
97
|
if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue()) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
98
99
100
101
102
103
104
|
return UserConstants.NOT_UNIQUE;
}
return UserConstants.UNIQUE;
}
/**
* 校验用户是否允许操作
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
105
|
*
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
106
107
|
* @param user 用户信息
*/
|
f39be3ab
Sxile
添加缺少的@ Override
|
108
|
@Override
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
109
110
|
public void checkUserAllowed(SysUser user) {
if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin()) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
111
112
113
114
115
116
|
throw new CustomException("不允许操作超级管理员用户");
}
}
/**
* 新增保存用户信息
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
117
|
*
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
118
119
120
121
122
|
* @param user 用户信息
* @return 结果
*/
@Override
@Transactional
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
123
|
public int insertUser(SysUser user) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
124
125
126
127
128
129
130
131
132
133
134
|
// 新增用户信息
int rows = userMapper.insertUser(user);
// 新增用户岗位关联
insertUserPost(user);
// 新增用户与角色管理
insertUserRole(user);
return rows;
}
/**
* 修改保存用户信息
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
135
|
*
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
136
137
138
139
140
|
* @param user 用户信息
* @return 结果
*/
@Override
@Transactional
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
141
|
public int updateUser(SysUser user) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
142
143
144
145
146
147
148
149
150
151
152
153
154
|
Long userId = user.getUserId();
// 删除用户与角色关联
userRoleMapper.deleteUserRoleByUserId(userId);
// 新增用户与角色管理
insertUserRole(user);
// 删除用户与岗位关联
userPostMapper.deleteUserPostByUserId(userId);
// 新增用户与岗位管理
insertUserPost(user);
return userMapper.updateUser(user);
}
/**
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
155
|
* 修改用户基本信息
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
156
|
*
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
157
158
159
160
|
* @param user 用户信息
* @return 结果
*/
@Override
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
161
|
public int updateUserProfile(SysUser user) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
162
163
164
165
166
|
return userMapper.updateUser(user);
}
/**
* 修改用户头像
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
167
|
*
|
f39be3ab
Sxile
添加缺少的@ Override
|
168
|
* @param userName 用户名
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
169
|
* @param avatar 头像地址
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
170
171
|
* @return 结果
*/
|
f39be3ab
Sxile
添加缺少的@ Override
|
172
|
@Override
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
173
|
public boolean updateUserAvatar(String userName, String avatar) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
174
175
176
177
178
|
return userMapper.updateUserAvatar(userName, avatar) > 0;
}
/**
* 重置用户密码
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
179
|
*
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
180
181
182
183
|
* @param user 用户信息
* @return 结果
*/
@Override
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
184
|
public int resetPwd(SysUser user) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
185
186
187
188
189
|
return userMapper.updateUser(user);
}
/**
* 重置用户密码
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
190
|
*
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
191
192
193
194
195
|
* @param userName 用户名
* @param password 密码
* @return 结果
*/
@Override
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
196
|
public int resetUserPwd(String userName, String password) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
197
198
199
200
201
|
return userMapper.resetUserPwd(userName, password);
}
/**
* 新增用户角色信息
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
202
|
*
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
203
204
|
* @param user 用户对象
*/
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
205
|
public void insertUserRole(SysUser user) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
206
|
Long[] roles = user.getRoleIds();
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
207
|
if (StringUtils.isNotNull(roles)) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
208
209
|
// 新增用户与角色管理
List<SysUserRole> list = new ArrayList<SysUserRole>();
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
210
|
for (Long roleId : roles) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
211
212
213
214
215
|
SysUserRole ur = new SysUserRole();
ur.setUserId(user.getUserId());
ur.setRoleId(roleId);
list.add(ur);
}
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
216
|
if (list.size() > 0) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
217
218
219
220
221
222
223
|
userRoleMapper.batchUserRole(list);
}
}
}
/**
* 新增用户岗位信息
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
224
|
*
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
225
226
|
* @param user 用户对象
*/
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
227
|
public void insertUserPost(SysUser user) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
228
|
Long[] posts = user.getPostIds();
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
229
|
if (StringUtils.isNotNull(posts)) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
230
231
|
// 新增用户与岗位管理
List<SysUserPost> list = new ArrayList<SysUserPost>();
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
232
|
for (Long postId : posts) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
233
234
235
236
237
|
SysUserPost up = new SysUserPost();
up.setUserId(user.getUserId());
up.setPostId(postId);
list.add(up);
}
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
238
|
if (list.size() > 0) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
239
240
241
242
243
244
245
|
userPostMapper.batchUserPost(list);
}
}
}
/**
* 通过用户ID删除用户
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
246
|
*
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
247
248
249
250
|
* @param userId 用户ID
* @return 结果
*/
@Override
|
4d46f4c1
RuoYi
删除用户和角色解绑关联
|
251
|
@Transactional
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
252
|
public int deleteUserById(Long userId) {
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
253
254
255
256
257
258
|
// 删除用户与角色关联
userRoleMapper.deleteUserRoleByUserId(userId);
// 删除用户与岗位表
userPostMapper.deleteUserPostByUserId(userId);
return userMapper.deleteUserById(userId);
}
|
bcee37b8
RuoYi
若依 1.1
|
259
260
261
|
/**
* 批量删除用户信息
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
262
|
*
|
bcee37b8
RuoYi
若依 1.1
|
263
264
265
|
* @param userIds 需要删除的用户ID
* @return 结果
*/
|
f39be3ab
Sxile
添加缺少的@ Override
|
266
|
@Override
|
4d46f4c1
RuoYi
删除用户和角色解绑关联
|
267
|
@Transactional
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
268
269
|
public int deleteUserByIds(Long[] userIds) {
for (Long userId : userIds) {
|
bcee37b8
RuoYi
若依 1.1
|
270
271
|
checkUserAllowed(new SysUser(userId));
}
|
4d46f4c1
RuoYi
删除用户和角色解绑关联
|
272
273
274
275
|
// 删除用户与角色关联
userRoleMapper.deleteUserRole(userIds);
// 删除用户与岗位关联
userPostMapper.deleteUserPost(userIds);
|
bcee37b8
RuoYi
若依 1.1
|
276
277
|
return userMapper.deleteUserByIds(userIds);
}
|
b508e6f2
RuoYi
新增用户导入
|
278
279
280
|
/**
* 导入用户数据
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
281
282
|
*
* @param userList 用户数据列表
|
b508e6f2
RuoYi
新增用户导入
|
283
|
* @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
284
|
* @param operName 操作用户
|
b508e6f2
RuoYi
新增用户导入
|
285
286
287
|
* @return 结果
*/
@Override
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
288
289
|
public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName) {
if (StringUtils.isNull(userList) || userList.size() == 0) {
|
b508e6f2
RuoYi
新增用户导入
|
290
291
292
293
294
295
296
|
throw new CustomException("导入用户数据不能为空!");
}
int successNum = 0;
int failureNum = 0;
StringBuilder successMsg = new StringBuilder();
StringBuilder failureMsg = new StringBuilder();
String password = configService.selectConfigByKey("sys.user.initPassword");
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
297
298
|
for (SysUser user : userList) {
try {
|
b508e6f2
RuoYi
新增用户导入
|
299
300
|
// 验证是否存在这个用户
SysUser u = userMapper.selectUserByUserName(user.getUserName());
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
301
|
if (StringUtils.isNull(u)) {
|
b508e6f2
RuoYi
新增用户导入
|
302
303
304
305
306
|
user.setPassword(SecurityUtils.encryptPassword(password));
user.setCreateBy(operName);
this.insertUser(user);
successNum++;
successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
307
|
} else if (isUpdateSupport) {
|
b508e6f2
RuoYi
新增用户导入
|
308
309
310
311
|
user.setUpdateBy(operName);
this.updateUser(user);
successNum++;
successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
312
|
} else {
|
b508e6f2
RuoYi
新增用户导入
|
313
314
315
|
failureNum++;
failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
}
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
316
|
} catch (Exception e) {
|
b508e6f2
RuoYi
新增用户导入
|
317
318
319
320
321
322
|
failureNum++;
String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败:";
failureMsg.append(msg + e.getMessage());
log.error(msg, e);
}
}
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
323
|
if (failureNum > 0) {
|
b508e6f2
RuoYi
新增用户导入
|
324
325
|
failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
throw new CustomException(failureMsg.toString());
|
f5b7142a
YunaiV
完成用户列表的前后端对接
|
326
|
} else {
|
b508e6f2
RuoYi
新增用户导入
|
327
328
329
330
|
successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
}
return successMsg.toString();
}
|
46444bd0
RuoYi
RuoYi-Vue 1.0
|
331
|
}
|