package nju.seec.helper.service.impl; import nju.seec.helper.aspect.auth.LoginUser; import nju.seec.helper.dao.UserDAO; import nju.seec.helper.dto.user.ResetPasswordDTO; import nju.seec.helper.dto.user.UserDTO; import nju.seec.helper.entity.User; import nju.seec.helper.enums.ExceptionType; import nju.seec.helper.enums.UserState; import nju.seec.helper.enums.UserType; import nju.seec.helper.exception.HelperException; import nju.seec.helper.service.UserService; import nju.seec.helper.util.Consts; import nju.seec.helper.util.EncryptUtils; import nju.seec.helper.util.cache.RedisCacheUtils; import nju.seec.helper.vo.UserVO; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * @author cst */ @Service public class UserServiceImpl implements UserService { private final RedisCacheUtils cacheUtils; private final UserDAO userDAO; public UserServiceImpl(UserDAO userDAO, RedisCacheUtils cacheUtils) { this.userDAO = userDAO; this.cacheUtils = cacheUtils; } @Transactional(rollbackFor = Exception.class) @Override public UserVO createUser(UserDTO userDTO) { checkSameEmail(userDTO.getEmail()); checkSamePhone(userDTO.getPhone()); String email = userDTO.getEmail(); if (!userDTO.getEmailCode().equals(cacheUtils.get(Consts.EMAIL_CACHE_NAME, email))) { throw HelperException.of(ExceptionType.NOT_FOUND, "邮箱或验证码错误"); } String phone = userDTO.getPhone(); if (!userDTO.getPhoneCode().equals(cacheUtils.get(Consts.PHONE_CACHE_NAME, phone))) { throw HelperException.of(ExceptionType.NOT_FOUND, "手机号或验证码错误"); } User user = userDAO.save(new User() .setName(userDTO.getName()) .setEmail(userDTO.getEmail()) .setPhone(userDTO.getPhone()) .setPassword(EncryptUtils.encode(userDTO.getPassword())) .setType(userDTO.getType()) .setState(userDTO.getType() == UserType.TEACHER ? UserState.CHECKING : UserState.NORMAL)); cacheUtils.remove(Consts.EMAIL_CACHE_NAME, email); cacheUtils.remove(Consts.PHONE_CACHE_NAME, phone); return new UserVO(user); } @Transactional(rollbackFor = Exception.class) @Override public UserVO modifyUser(LoginUser loginUser, UserDTO userDTO) { User user = userDAO.findUserById(loginUser.getId()); user.setName(userDTO.getName()); user = userDAO.save(user); return new UserVO(user); } @Transactional(rollbackFor = Exception.class) @Override public void resetPasswordByEmail(ResetPasswordDTO resetPasswordDTO) { User user = userDAO.findByEmail(resetPasswordDTO.getUsername()) .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "该邮箱未在系统中注册")); if (!resetPasswordDTO.getCode().equals(cacheUtils.get(Consts.EMAIL_CACHE_NAME, resetPasswordDTO.getUsername()))) { throw HelperException.of(ExceptionType.NOT_FOUND, "邮箱或验证码错误"); } user.setPassword(EncryptUtils.encode(resetPasswordDTO.getPassword())); userDAO.save(user); cacheUtils.remove(Consts.EMAIL_CACHE_NAME, resetPasswordDTO.getUsername()); } @Transactional(readOnly = true) @Override public UserVO getUserByEmailAndPassword(String email, String password) { return userDAO .findByEmailAndPassword(email, EncryptUtils.encode(password)) .map(UserVO::new) .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "邮箱或密码错误")); } @Transactional(readOnly = true) @Override public UserVO getUserById(Long userId) { return new UserVO(userDAO.findUserById(userId)); } private void checkSameEmail(String email) { if (userDAO.existsByEmail(email)) { throw HelperException.of(ExceptionType.CONFLICT, "该邮箱已注册"); } } private void checkSamePhone(String phone) { if (userDAO.existsByPhone(phone)) { throw HelperException.of(ExceptionType.CONFLICT, "该手机号已注册"); } } }