RealNameBackfillService.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.etotem.cfc.service;
  2. import com.etotem.cfc.entity.User;
  3. import com.etotem.cfc.mapper.UserMapper;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.stereotype.Service;
  7. import javax.annotation.Resource;
  8. /**
  9. * 真实姓名回填:用户第一次输入地址/收货人时,若 users.real_name 尚未填写,
  10. * 用联系人姓名回填(规格:docs/superpowers/specs/2026-10-10-address-contact-backfill-realname-design.md)。
  11. * 幂等:回填一次后 realName 非空,后续保存不再触发。
  12. * 风格参照 ConsigneeService.syncUserRegion():失败静默,不阻塞主流程。
  13. */
  14. @Service
  15. public class RealNameBackfillService {
  16. private static final Logger log = LoggerFactory.getLogger(RealNameBackfillService.class);
  17. @Resource
  18. private UserMapper userMapper;
  19. /**
  20. * 若 userId 对应用户的 realName 为空白,则用 contactName(去首尾空白后)回填。
  21. * contactName 空白、userId 为空、用户不存在时直接跳过。
  22. */
  23. public void backfill(Long userId, String contactName) {
  24. backfillRealNameAndNickname(userId, contactName);
  25. }
  26. /**
  27. * 若 userId 对应用户的 realName 为空白,则同时回填 realName 和 nickname(nickname 仅在也为空白时补)。
  28. * contactName 空白、userId 为空、用户不存在时直接跳过;失败静默不阻塞主流程。
  29. * 幂等:realName 非空后再次调用直接跳过。
  30. */
  31. public void backfillRealNameAndNickname(Long userId, String contactName) {
  32. try {
  33. if (userId == null) return;
  34. if (contactName == null || contactName.trim().isEmpty()) return;
  35. User user = userMapper.selectById(userId);
  36. if (user == null) return;
  37. String realName = user.getRealName();
  38. if (realName != null && !realName.trim().isEmpty()) return;
  39. String name = contactName.trim();
  40. user.setRealName(name);
  41. String nickname = user.getNickname();
  42. if (nickname == null || nickname.trim().isEmpty()) {
  43. user.setNickname(name);
  44. }
  45. userMapper.updateById(user);
  46. } catch (Exception e) {
  47. log.warn("[RealNameBackfill] 回填失败 userId={} : {}", userId, e.getMessage());
  48. }
  49. }
  50. }