IdentityAuthService.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.domain.IdentityAuth;
  3. import com.izouma.nineth.domain.User;
  4. import com.izouma.nineth.dto.PageQuery;
  5. import com.izouma.nineth.enums.AuthStatus;
  6. import com.izouma.nineth.exception.BusinessException;
  7. import com.izouma.nineth.repo.IdentityAuthRepo;
  8. import com.izouma.nineth.repo.UserRepo;
  9. import com.izouma.nineth.utils.JpaUtils;
  10. import lombok.AllArgsConstructor;
  11. import org.springframework.data.domain.Page;
  12. import org.springframework.stereotype.Service;
  13. import java.util.List;
  14. import java.util.stream.Collectors;
  15. @Service
  16. @AllArgsConstructor
  17. public class IdentityAuthService {
  18. private IdentityAuthRepo identityAuthRepo;
  19. private UserRepo userRepo;
  20. private AdapayService adapayService;
  21. public Page<IdentityAuth> all(PageQuery pageQuery) {
  22. return identityAuthRepo.findAll(JpaUtils.toSpecification(pageQuery, IdentityAuth.class), JpaUtils.toPageRequest(pageQuery));
  23. }
  24. public void apply(IdentityAuth identityAuth) {
  25. if (identityAuth.getUserId() == null) {
  26. throw new BusinessException("用户不存在");
  27. }
  28. User user = userRepo.findByIdAndDelFalse(identityAuth.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  29. List<IdentityAuth> auths = identityAuthRepo.findByUserIdAndDelFalse(identityAuth.getUserId());
  30. auths.stream().filter(auth -> auth.getStatus() == AuthStatus.PENDING).findAny().ifPresent(a -> {
  31. throw new BusinessException("正在审核中,请勿重复提交");
  32. });
  33. auths.stream().filter(auth -> auth.getStatus() == AuthStatus.SUCCESS).findAny().ifPresent(a -> {
  34. throw new BusinessException("已认证,请勿重复提交");
  35. });
  36. identityAuth.setStatus(AuthStatus.PENDING);
  37. identityAuthRepo.save(identityAuth);
  38. user.setAuthStatus(AuthStatus.PENDING);
  39. userRepo.save(user);
  40. }
  41. public void audit(Long id, AuthStatus status) {
  42. IdentityAuth auth = identityAuthRepo.findByIdAndDelFalse(id).orElseThrow(new BusinessException("申请不存在"));
  43. if (auth.getStatus() != AuthStatus.PENDING) {
  44. throw new BusinessException("已经审核过");
  45. }
  46. User user = userRepo.findByIdAndDelFalse(auth.getUserId()).orElseThrow(new BusinessException("用户不存在"));
  47. if (status == AuthStatus.SUCCESS) {
  48. user.setAuthId(auth.getId());
  49. }
  50. auth.setStatus(status);
  51. identityAuthRepo.save(auth);
  52. user.setAuthStatus(status);
  53. userRepo.save(user);
  54. }
  55. public List<User> repeat(String idNo, Long userId) {
  56. List<IdentityAuth> auths = identityAuthRepo.findAllByIdNoAndUserIdIsNotAndDelFalse(idNo, userId);
  57. if (auths.isEmpty()) {
  58. return null;
  59. }
  60. List<Long> userIds = auths.stream().map(IdentityAuth::getUserId).distinct().collect(Collectors.toList());
  61. return userRepo.findByIdInAndDelFalse(userIds);
  62. }
  63. }