package nju.seec.helper.service.impl; import nju.seec.helper.dao.UserDAO; import nju.seec.helper.entity.User; import nju.seec.helper.service.UserService; import nju.seec.helper.vo.UserVO; import org.springframework.data.util.Optionals; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Optional; /** * @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(SecureUtil.sha256(userDTO.getPassword())) // .setType(userDTO.getType()) // .setState(userDTO.getType() == UserRole.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(SecureUtil.sha256(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, SecureUtil.sha256(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)); } @Transactional(readOnly = true) @Override public Optional getUserByPhoneOrPid(String phone, Long pid) { return Optionals.firstNonEmpty(() -> userDAO.findByPhone(phone), () -> userDAO.findByPid(pid)); } @Transactional(rollbackFor = Exception.class) @Override public User createOrUpdateUser(User user) { return userDAO.save(user); } // 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, "该手机号已注册"); // } // } }