RealnameAuthServiceImpl.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package space.anyi.serve.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  4. import org.springframework.stereotype.Service;
  5. import org.springframework.transaction.annotation.Transactional;
  6. import space.anyi.serve.entity.profile.UserProfile;
  7. import space.anyi.serve.entity.realname.RealnameAuth;
  8. import space.anyi.serve.mapper.RealnameAuthMapper;
  9. import space.anyi.serve.mapper.UserProfileMapper;
  10. import space.anyi.serve.service.NotificationService;
  11. import space.anyi.serve.service.RealnameAuthService;
  12. import java.time.LocalDateTime;
  13. @Service
  14. public class RealnameAuthServiceImpl extends ServiceImpl<RealnameAuthMapper, RealnameAuth> implements RealnameAuthService {
  15. private final UserProfileMapper userProfileMapper;
  16. private final NotificationService notificationService;
  17. public RealnameAuthServiceImpl(UserProfileMapper userProfileMapper, NotificationService notificationService) {
  18. this.userProfileMapper = userProfileMapper;
  19. this.notificationService = notificationService;
  20. }
  21. @Override
  22. public RealnameAuth getByUserId(Long userId) {
  23. return getOne(new LambdaQueryWrapper<RealnameAuth>().eq(RealnameAuth::getUserId, userId));
  24. }
  25. @Override
  26. @Transactional
  27. public void submit(Long userId, String realName, String idCard, String idCardFront, String idCardBack) {
  28. RealnameAuth existing = getByUserId(userId);
  29. if (existing != null) throw new IllegalArgumentException("已提交过实名认证");
  30. RealnameAuth auth = new RealnameAuth();
  31. auth.setUserId(userId);
  32. auth.setRealName(realName);
  33. auth.setIdCard(idCard);
  34. auth.setIdCardFront(idCardFront);
  35. auth.setIdCardBack(idCardBack);
  36. auth.setStatus("pending");
  37. auth.setCreatedAt(LocalDateTime.now());
  38. auth.setUpdatedAt(LocalDateTime.now());
  39. save(auth);
  40. }
  41. @Override
  42. @Transactional
  43. public void review(Long id, boolean approved, String rejectReason) {
  44. RealnameAuth auth = getById(id);
  45. if (auth == null) throw new IllegalArgumentException("认证记录不存在");
  46. if (!"pending".equals(auth.getStatus())) throw new IllegalArgumentException("已审核");
  47. if (approved) {
  48. auth.setStatus("approved");
  49. UserProfile profile = userProfileMapper.selectOne(
  50. new LambdaQueryWrapper<UserProfile>().eq(UserProfile::getUserId, auth.getUserId()));
  51. if (profile != null) {
  52. profile.setIsRealname(true);
  53. profile.setUpdatedAt(LocalDateTime.now());
  54. userProfileMapper.updateById(profile);
  55. }
  56. notificationService.createNotification(auth.getUserId(), "system", "实名认证通过",
  57. "您的实名认证已通过");
  58. } else {
  59. auth.setStatus("rejected");
  60. auth.setRejectReason(rejectReason);
  61. notificationService.createNotification(auth.getUserId(), "system", "实名认证驳回",
  62. "实名认证被驳回" + (rejectReason != null ? ",原因:" + rejectReason : ""));
  63. }
  64. auth.setReviewTime(LocalDateTime.now());
  65. auth.setUpdatedAt(LocalDateTime.now());
  66. updateById(auth);
  67. }
  68. }