UserServiceImpl.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.qmth.exam.reserve.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.qmth.boot.core.collection.PageResult;
  8. import com.qmth.boot.core.exception.StatusException;
  9. import com.qmth.exam.reserve.bean.login.LoginUser;
  10. import com.qmth.exam.reserve.bean.user.UserReq;
  11. import com.qmth.exam.reserve.bean.user.UserSaveReq;
  12. import com.qmth.exam.reserve.bean.user.UserVO;
  13. import com.qmth.exam.reserve.dao.UserDao;
  14. import com.qmth.exam.reserve.entity.UserEntity;
  15. import com.qmth.exam.reserve.enums.Role;
  16. import com.qmth.exam.reserve.service.UserService;
  17. import com.qmth.exam.reserve.util.PageUtil;
  18. import org.apache.commons.codec.digest.DigestUtils;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import org.springframework.beans.BeanUtils;
  23. import org.springframework.stereotype.Service;
  24. @Service
  25. public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
  26. private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
  27. @Override
  28. public UserEntity findUserByLoginName(Long orgId, String loginName) {
  29. LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
  30. if (orgId != null) {
  31. wrapper.eq(UserEntity::getOrgId, orgId);
  32. }
  33. wrapper.eq(UserEntity::getLoginName, loginName);
  34. wrapper.eq(UserEntity::getEnable, true);
  35. return baseMapper.selectOne(wrapper);
  36. }
  37. @Override
  38. public PageResult<UserVO> pageUser(UserReq req) {
  39. IPage<UserVO> iPage = baseMapper.pageUser(new Page<>(req.getPageNumber(), req.getPageSize()), req);
  40. return PageUtil.of(iPage);
  41. }
  42. @Override
  43. public void saveUser(LoginUser user, UserSaveReq req) {
  44. checkUser(req);
  45. UserEntity userEntity = new UserEntity();
  46. userEntity.setOrgId(user.getOrgId());
  47. BeanUtils.copyProperties(req, userEntity);
  48. if(req.getId() == null) {
  49. userEntity.setEnable(Boolean.TRUE);
  50. userEntity.setPassword(DigestUtils.sha256Hex("123456").toUpperCase());
  51. this.save(userEntity);
  52. } else {
  53. this.updateById(userEntity);
  54. }
  55. }
  56. @Override
  57. public void resetPassword(Long id) {
  58. UserEntity userEntity = getById(id);
  59. if (userEntity == null) {
  60. log.error("[重置密码] 找不到用户记录:id:{}", id);
  61. throw new StatusException("重置失败");
  62. }
  63. LambdaUpdateWrapper<UserEntity> updateWrapper = new LambdaUpdateWrapper<>();
  64. updateWrapper.set(UserEntity::getPassword, DigestUtils.sha256Hex("123456").toUpperCase()).eq(UserEntity::getId, id);
  65. this.update(updateWrapper);
  66. }
  67. @Override
  68. public void enable(Long id, Boolean enable) {
  69. UserEntity userEntity = getById(id);
  70. if (userEntity == null) {
  71. log.error("[启用/禁用] 找不到用户记录:id:{}", id);
  72. throw new StatusException("启用/禁用失败");
  73. }
  74. LambdaUpdateWrapper<UserEntity> updateWrapper = new LambdaUpdateWrapper<>();
  75. updateWrapper.set(UserEntity::getEnable, enable).eq(UserEntity::getId, id);
  76. this.update(updateWrapper);
  77. }
  78. private void checkUser(UserSaveReq req) {
  79. if (req.getRole() == null) {
  80. throw new StatusException("请选择角色");
  81. }
  82. if (StringUtils.isBlank(req.getLoginName())) {
  83. throw new StatusException("登录账号不能为空");
  84. }
  85. if (StringUtils.isBlank(req.getName())) {
  86. throw new StatusException("用户名称不能为空");
  87. }
  88. if (req.getRole().equals(Role.TEACHING) && req.getCategoryId() == null) {
  89. throw new StatusException("请选择所属教学点");
  90. }
  91. }
  92. }