SysUserServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. package com.ruoyi.system.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.stream.Collectors;
  5. import javax.validation.Validator;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import org.springframework.transaction.annotation.Transactional;
  11. import org.springframework.util.CollectionUtils;
  12. import com.ruoyi.common.annotation.DataScope;
  13. import com.ruoyi.common.constant.UserConstants;
  14. import com.ruoyi.common.core.domain.entity.SysRole;
  15. import com.ruoyi.common.core.domain.entity.SysUser;
  16. import com.ruoyi.common.exception.ServiceException;
  17. import com.ruoyi.common.utils.SecurityUtils;
  18. import com.ruoyi.common.utils.StringUtils;
  19. import com.ruoyi.common.utils.bean.BeanValidators;
  20. import com.ruoyi.common.utils.spring.SpringUtils;
  21. import com.ruoyi.system.domain.SysPost;
  22. import com.ruoyi.system.domain.SysUserPost;
  23. import com.ruoyi.system.domain.SysUserRole;
  24. import com.ruoyi.system.mapper.SysPostMapper;
  25. import com.ruoyi.system.mapper.SysRoleMapper;
  26. import com.ruoyi.system.mapper.SysUserMapper;
  27. import com.ruoyi.system.mapper.SysUserPostMapper;
  28. import com.ruoyi.system.mapper.SysUserRoleMapper;
  29. import com.ruoyi.system.service.ISysConfigService;
  30. import com.ruoyi.system.service.ISysUserService;
  31. /**
  32. * 用户 业务层处理
  33. *
  34. * @author ruoyi
  35. */
  36. @Service
  37. public class SysUserServiceImpl implements ISysUserService
  38. {
  39. private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
  40. @Autowired
  41. private SysUserMapper userMapper;
  42. @Autowired
  43. private SysRoleMapper roleMapper;
  44. @Autowired
  45. private SysPostMapper postMapper;
  46. @Autowired
  47. private SysUserRoleMapper userRoleMapper;
  48. @Autowired
  49. private SysUserPostMapper userPostMapper;
  50. @Autowired
  51. private ISysConfigService configService;
  52. @Autowired
  53. protected Validator validator;
  54. /**
  55. * 根据条件分页查询用户列表
  56. *
  57. * @param user 用户信息
  58. * @return 用户信息集合信息
  59. */
  60. @Override
  61. @DataScope(deptAlias = "d", userAlias = "u")
  62. public List<SysUser> selectUserList(SysUser user)
  63. {
  64. return userMapper.selectUserList(user);
  65. }
  66. /**
  67. * 根据条件分页查询已分配用户角色列表
  68. *
  69. * @param user 用户信息
  70. * @return 用户信息集合信息
  71. */
  72. @Override
  73. @DataScope(deptAlias = "d", userAlias = "u")
  74. public List<SysUser> selectAllocatedList(SysUser user)
  75. {
  76. return userMapper.selectAllocatedList(user);
  77. }
  78. /**
  79. * 根据条件分页查询未分配用户角色列表
  80. *
  81. * @param user 用户信息
  82. * @return 用户信息集合信息
  83. */
  84. @Override
  85. @DataScope(deptAlias = "d", userAlias = "u")
  86. public List<SysUser> selectUnallocatedList(SysUser user)
  87. {
  88. return userMapper.selectUnallocatedList(user);
  89. }
  90. /**
  91. * 通过用户名查询用户
  92. *
  93. * @param userName 用户名
  94. * @return 用户对象信息
  95. */
  96. @Override
  97. public SysUser selectUserByUserName(String userName)
  98. {
  99. return userMapper.selectUserByUserName(userName);
  100. }
  101. /**
  102. * 通过用户ID查询用户
  103. *
  104. * @param userId 用户ID
  105. * @return 用户对象信息
  106. */
  107. @Override
  108. public SysUser selectUserById(Long userId)
  109. {
  110. return userMapper.selectUserById(userId);
  111. }
  112. /**
  113. * 查询用户所属角色组
  114. *
  115. * @param userName 用户名
  116. * @return 结果
  117. */
  118. @Override
  119. public String selectUserRoleGroup(String userName)
  120. {
  121. List<SysRole> list = roleMapper.selectRolesByUserName(userName);
  122. if (CollectionUtils.isEmpty(list))
  123. {
  124. return StringUtils.EMPTY;
  125. }
  126. return list.stream().map(SysRole::getRoleName).collect(Collectors.joining(","));
  127. }
  128. /**
  129. * 查询用户所属岗位组
  130. *
  131. * @param userName 用户名
  132. * @return 结果
  133. */
  134. @Override
  135. public String selectUserPostGroup(String userName)
  136. {
  137. List<SysPost> list = postMapper.selectPostsByUserName(userName);
  138. if (CollectionUtils.isEmpty(list))
  139. {
  140. return StringUtils.EMPTY;
  141. }
  142. return list.stream().map(SysPost::getPostName).collect(Collectors.joining(","));
  143. }
  144. /**
  145. * 校验用户名称是否唯一
  146. *
  147. * @param userName 用户名称
  148. * @return 结果
  149. */
  150. @Override
  151. public String checkUserNameUnique(String userName)
  152. {
  153. int count = userMapper.checkUserNameUnique(userName);
  154. if (count > 0)
  155. {
  156. return UserConstants.NOT_UNIQUE;
  157. }
  158. return UserConstants.UNIQUE;
  159. }
  160. /**
  161. * 校验手机号码是否唯一
  162. *
  163. * @param user 用户信息
  164. * @return
  165. */
  166. @Override
  167. public String checkPhoneUnique(SysUser user)
  168. {
  169. Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
  170. SysUser info = userMapper.checkPhoneUnique(user.getPhonenumber());
  171. if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
  172. {
  173. return UserConstants.NOT_UNIQUE;
  174. }
  175. return UserConstants.UNIQUE;
  176. }
  177. /**
  178. * 校验email是否唯一
  179. *
  180. * @param user 用户信息
  181. * @return
  182. */
  183. @Override
  184. public String checkEmailUnique(SysUser user)
  185. {
  186. Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
  187. SysUser info = userMapper.checkEmailUnique(user.getEmail());
  188. if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
  189. {
  190. return UserConstants.NOT_UNIQUE;
  191. }
  192. return UserConstants.UNIQUE;
  193. }
  194. /**
  195. * 校验用户是否允许操作
  196. *
  197. * @param user 用户信息
  198. */
  199. @Override
  200. public void checkUserAllowed(SysUser user)
  201. {
  202. if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin())
  203. {
  204. throw new ServiceException("不允许操作超级管理员用户");
  205. }
  206. }
  207. /**
  208. * 校验用户是否有数据权限
  209. *
  210. * @param userId 用户id
  211. */
  212. @Override
  213. public void checkUserDataScope(Long userId)
  214. {
  215. if (!SysUser.isAdmin(SecurityUtils.getUserId()))
  216. {
  217. SysUser user = new SysUser();
  218. user.setUserId(userId);
  219. List<SysUser> users = SpringUtils.getAopProxy(this).selectUserList(user);
  220. if (StringUtils.isEmpty(users))
  221. {
  222. throw new ServiceException("没有权限访问用户数据!");
  223. }
  224. }
  225. }
  226. /**
  227. * 新增保存用户信息
  228. *
  229. * @param user 用户信息
  230. * @return 结果
  231. */
  232. @Override
  233. @Transactional
  234. public int insertUser(SysUser user)
  235. {
  236. // 新增用户信息
  237. int rows = userMapper.insertUser(user);
  238. // 新增用户岗位关联
  239. insertUserPost(user);
  240. // 新增用户与角色管理
  241. insertUserRole(user);
  242. return rows;
  243. }
  244. /**
  245. * 注册用户信息
  246. *
  247. * @param user 用户信息
  248. * @return 结果
  249. */
  250. @Override
  251. public boolean registerUser(SysUser user)
  252. {
  253. return userMapper.insertUser(user) > 0;
  254. }
  255. /**
  256. * 修改保存用户信息
  257. *
  258. * @param user 用户信息
  259. * @return 结果
  260. */
  261. @Override
  262. @Transactional
  263. public int updateUser(SysUser user)
  264. {
  265. Long userId = user.getUserId();
  266. // 删除用户与角色关联
  267. userRoleMapper.deleteUserRoleByUserId(userId);
  268. // 新增用户与角色管理
  269. insertUserRole(user);
  270. // 删除用户与岗位关联
  271. userPostMapper.deleteUserPostByUserId(userId);
  272. // 新增用户与岗位管理
  273. insertUserPost(user);
  274. return userMapper.updateUser(user);
  275. }
  276. /**
  277. * 用户授权角色
  278. *
  279. * @param userId 用户ID
  280. * @param roleIds 角色组
  281. */
  282. @Override
  283. @Transactional
  284. public void insertUserAuth(Long userId, Long[] roleIds)
  285. {
  286. userRoleMapper.deleteUserRoleByUserId(userId);
  287. insertUserRole(userId, roleIds);
  288. }
  289. /**
  290. * 修改用户状态
  291. *
  292. * @param user 用户信息
  293. * @return 结果
  294. */
  295. @Override
  296. public int updateUserStatus(SysUser user)
  297. {
  298. return userMapper.updateUser(user);
  299. }
  300. /**
  301. * 修改用户基本信息
  302. *
  303. * @param user 用户信息
  304. * @return 结果
  305. */
  306. @Override
  307. public int updateUserProfile(SysUser user)
  308. {
  309. return userMapper.updateUser(user);
  310. }
  311. /**
  312. * 修改用户头像
  313. *
  314. * @param userName 用户名
  315. * @param avatar 头像地址
  316. * @return 结果
  317. */
  318. @Override
  319. public boolean updateUserAvatar(String userName, String avatar)
  320. {
  321. return userMapper.updateUserAvatar(userName, avatar) > 0;
  322. }
  323. /**
  324. * 重置用户密码
  325. *
  326. * @param user 用户信息
  327. * @return 结果
  328. */
  329. @Override
  330. public int resetPwd(SysUser user)
  331. {
  332. return userMapper.updateUser(user);
  333. }
  334. /**
  335. * 重置用户密码
  336. *
  337. * @param userName 用户名
  338. * @param password 密码
  339. * @return 结果
  340. */
  341. @Override
  342. public int resetUserPwd(String userName, String password)
  343. {
  344. return userMapper.resetUserPwd(userName, password);
  345. }
  346. /**
  347. * 新增用户角色信息
  348. *
  349. * @param user 用户对象
  350. */
  351. public void insertUserRole(SysUser user)
  352. {
  353. Long[] roles = user.getRoleIds();
  354. if (StringUtils.isNotNull(roles))
  355. {
  356. // 新增用户与角色管理
  357. List<SysUserRole> list = new ArrayList<SysUserRole>();
  358. for (Long roleId : roles)
  359. {
  360. SysUserRole ur = new SysUserRole();
  361. ur.setUserId(user.getUserId());
  362. ur.setRoleId(roleId);
  363. list.add(ur);
  364. }
  365. if (list.size() > 0)
  366. {
  367. userRoleMapper.batchUserRole(list);
  368. }
  369. }
  370. }
  371. /**
  372. * 新增用户岗位信息
  373. *
  374. * @param user 用户对象
  375. */
  376. public void insertUserPost(SysUser user)
  377. {
  378. Long[] posts = user.getPostIds();
  379. if (StringUtils.isNotNull(posts))
  380. {
  381. // 新增用户与岗位管理
  382. List<SysUserPost> list = new ArrayList<SysUserPost>();
  383. for (Long postId : posts)
  384. {
  385. SysUserPost up = new SysUserPost();
  386. up.setUserId(user.getUserId());
  387. up.setPostId(postId);
  388. list.add(up);
  389. }
  390. if (list.size() > 0)
  391. {
  392. userPostMapper.batchUserPost(list);
  393. }
  394. }
  395. }
  396. /**
  397. * 新增用户角色信息
  398. *
  399. * @param userId 用户ID
  400. * @param roleIds 角色组
  401. */
  402. public void insertUserRole(Long userId, Long[] roleIds)
  403. {
  404. if (StringUtils.isNotNull(roleIds))
  405. {
  406. // 新增用户与角色管理
  407. List<SysUserRole> list = new ArrayList<SysUserRole>();
  408. for (Long roleId : roleIds)
  409. {
  410. SysUserRole ur = new SysUserRole();
  411. ur.setUserId(userId);
  412. ur.setRoleId(roleId);
  413. list.add(ur);
  414. }
  415. if (list.size() > 0)
  416. {
  417. userRoleMapper.batchUserRole(list);
  418. }
  419. }
  420. }
  421. /**
  422. * 通过用户ID删除用户
  423. *
  424. * @param userId 用户ID
  425. * @return 结果
  426. */
  427. @Override
  428. @Transactional
  429. public int deleteUserById(Long userId)
  430. {
  431. // 删除用户与角色关联
  432. userRoleMapper.deleteUserRoleByUserId(userId);
  433. // 删除用户与岗位表
  434. userPostMapper.deleteUserPostByUserId(userId);
  435. return userMapper.deleteUserById(userId);
  436. }
  437. /**
  438. * 批量删除用户信息
  439. *
  440. * @param userIds 需要删除的用户ID
  441. * @return 结果
  442. */
  443. @Override
  444. @Transactional
  445. public int deleteUserByIds(Long[] userIds)
  446. {
  447. for (Long userId : userIds)
  448. {
  449. checkUserAllowed(new SysUser(userId));
  450. checkUserDataScope(userId);
  451. }
  452. // 删除用户与角色关联
  453. userRoleMapper.deleteUserRole(userIds);
  454. // 删除用户与岗位关联
  455. userPostMapper.deleteUserPost(userIds);
  456. return userMapper.deleteUserByIds(userIds);
  457. }
  458. /**
  459. * 导入用户数据
  460. *
  461. * @param userList 用户数据列表
  462. * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
  463. * @param operName 操作用户
  464. * @return 结果
  465. */
  466. @Override
  467. public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName)
  468. {
  469. if (StringUtils.isNull(userList) || userList.size() == 0)
  470. {
  471. throw new ServiceException("导入用户数据不能为空!");
  472. }
  473. int successNum = 0;
  474. int failureNum = 0;
  475. StringBuilder successMsg = new StringBuilder();
  476. StringBuilder failureMsg = new StringBuilder();
  477. String password = configService.selectConfigByKey("sys.user.initPassword");
  478. for (SysUser user : userList)
  479. {
  480. try
  481. {
  482. // 验证是否存在这个用户
  483. SysUser u = userMapper.selectUserByUserName(user.getUserName());
  484. if (StringUtils.isNull(u))
  485. {
  486. BeanValidators.validateWithException(validator, user);
  487. user.setPassword(SecurityUtils.encryptPassword(password));
  488. user.setCreateBy(operName);
  489. this.insertUser(user);
  490. successNum++;
  491. successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
  492. }
  493. else if (isUpdateSupport)
  494. {
  495. BeanValidators.validateWithException(validator, user);
  496. user.setUpdateBy(operName);
  497. this.updateUser(user);
  498. successNum++;
  499. successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
  500. }
  501. else
  502. {
  503. failureNum++;
  504. failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
  505. }
  506. }
  507. catch (Exception e)
  508. {
  509. failureNum++;
  510. String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败:";
  511. failureMsg.append(msg + e.getMessage());
  512. log.error(msg, e);
  513. }
  514. }
  515. if (failureNum > 0)
  516. {
  517. failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
  518. throw new ServiceException(failureMsg.toString());
  519. }
  520. else
  521. {
  522. successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
  523. }
  524. return successMsg.toString();
  525. }
  526. }