UserServiceImpl.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package nju.seec.helper.service.impl;
  2. import nju.seec.helper.dao.UserDAO;
  3. import nju.seec.helper.entity.User;
  4. import nju.seec.helper.service.UserService;
  5. import nju.seec.helper.vo.UserVO;
  6. import org.springframework.data.util.Optionals;
  7. import org.springframework.stereotype.Service;
  8. import org.springframework.transaction.annotation.Transactional;
  9. import java.util.Optional;
  10. /**
  11. * @author cst
  12. */
  13. @Service
  14. public class UserServiceImpl implements UserService {
  15. // private final RedisCacheUtils cacheUtils;
  16. private final UserDAO userDAO;
  17. public UserServiceImpl(UserDAO userDAO
  18. // , RedisCacheUtils cacheUtils
  19. ) {
  20. this.userDAO = userDAO;
  21. // this.cacheUtils = cacheUtils;
  22. }
  23. // @Transactional(rollbackFor = Exception.class)
  24. // @Override
  25. // public UserVO createUser(UserDTO userDTO) {
  26. // checkSameEmail(userDTO.getEmail());
  27. // checkSamePhone(userDTO.getPhone());
  28. //
  29. // String email = userDTO.getEmail();
  30. // if (!userDTO.getEmailCode().equals(cacheUtils.get(Consts.EMAIL_CACHE_NAME, email))) {
  31. // throw HelperException.of(ExceptionType.NOT_FOUND, "邮箱或验证码错误");
  32. // }
  33. //
  34. // String phone = userDTO.getPhone();
  35. // if (!userDTO.getPhoneCode().equals(cacheUtils.get(Consts.PHONE_CACHE_NAME, phone))) {
  36. // throw HelperException.of(ExceptionType.NOT_FOUND, "手机号或验证码错误");
  37. // }
  38. //
  39. // User user = userDAO.save(new User()
  40. // .setName(userDTO.getName())
  41. // .setEmail(userDTO.getEmail())
  42. // .setPhone(userDTO.getPhone())
  43. // .setPassword(SecureUtil.sha256(userDTO.getPassword()))
  44. // .setType(userDTO.getType())
  45. // .setState(userDTO.getType() == UserRole.TEACHER ? UserState.CHECKING : UserState.NORMAL));
  46. //
  47. // cacheUtils.remove(Consts.EMAIL_CACHE_NAME, email);
  48. // cacheUtils.remove(Consts.PHONE_CACHE_NAME, phone);
  49. // return new UserVO(user);
  50. // }
  51. //
  52. // @Transactional(rollbackFor = Exception.class)
  53. // @Override
  54. // public UserVO modifyUser(LoginUser loginUser, UserDTO userDTO) {
  55. // User user = userDAO.findUserById(loginUser.getId());
  56. // user.setName(userDTO.getName());
  57. // user = userDAO.save(user);
  58. // return new UserVO(user);
  59. // }
  60. //
  61. // @Transactional(rollbackFor = Exception.class)
  62. // @Override
  63. // public void resetPasswordByEmail(ResetPasswordDTO resetPasswordDTO) {
  64. // User user = userDAO.findByEmail(resetPasswordDTO.getUsername())
  65. // .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "该邮箱未在系统中注册"));
  66. //
  67. // if (!resetPasswordDTO.getCode().equals(cacheUtils.get(Consts.EMAIL_CACHE_NAME, resetPasswordDTO.getUsername()))) {
  68. // throw HelperException.of(ExceptionType.NOT_FOUND, "邮箱或验证码错误");
  69. // }
  70. //
  71. // user.setPassword(SecureUtil.sha256(resetPasswordDTO.getPassword()));
  72. // userDAO.save(user);
  73. // cacheUtils.remove(Consts.EMAIL_CACHE_NAME, resetPasswordDTO.getUsername());
  74. // }
  75. //
  76. // @Transactional(readOnly = true)
  77. // @Override
  78. // public UserVO getUserByEmailAndPassword(String email, String password) {
  79. // return userDAO
  80. // .findByEmailAndPassword(email, SecureUtil.sha256(password))
  81. // .map(UserVO::new)
  82. // .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "邮箱或密码错误"));
  83. // }
  84. @Transactional(readOnly = true)
  85. @Override
  86. public UserVO getUserById(Long userId) {
  87. return new UserVO(userDAO.findUserById(userId));
  88. }
  89. @Transactional(readOnly = true)
  90. @Override
  91. public Optional<User> getUserByPhoneOrPid(String phone, Long pid) {
  92. return Optionals.firstNonEmpty(() -> userDAO.findByPhone(phone), () -> userDAO.findByPid(pid));
  93. }
  94. @Transactional(rollbackFor = Exception.class)
  95. @Override
  96. public User createOrUpdateUser(User user) {
  97. return userDAO.save(user);
  98. }
  99. // private void checkSameEmail(String email) {
  100. // if (userDAO.existsByEmail(email)) {
  101. // throw HelperException.of(ExceptionType.CONFLICT, "该邮箱已注册");
  102. // }
  103. // }
  104. //
  105. // private void checkSamePhone(String phone) {
  106. // if (userDAO.existsByPhone(phone)) {
  107. // throw HelperException.of(ExceptionType.CONFLICT, "该手机号已注册");
  108. // }
  109. // }
  110. }