| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package com.etotem.cfc.service;
- import com.etotem.cfc.entity.User;
- import com.etotem.cfc.mapper.UserMapper;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- /**
- * 真实姓名回填:用户第一次输入地址/收货人时,若 users.real_name 尚未填写,
- * 用联系人姓名回填(规格:docs/superpowers/specs/2026-10-10-address-contact-backfill-realname-design.md)。
- * 幂等:回填一次后 realName 非空,后续保存不再触发。
- * 风格参照 ConsigneeService.syncUserRegion():失败静默,不阻塞主流程。
- */
- @Service
- public class RealNameBackfillService {
- private static final Logger log = LoggerFactory.getLogger(RealNameBackfillService.class);
- @Resource
- private UserMapper userMapper;
- /**
- * 若 userId 对应用户的 realName 为空白,则用 contactName(去首尾空白后)回填。
- * contactName 空白、userId 为空、用户不存在时直接跳过。
- */
- public void backfill(Long userId, String contactName) {
- backfillRealNameAndNickname(userId, contactName);
- }
- /**
- * 若 userId 对应用户的 realName 为空白,则同时回填 realName 和 nickname(nickname 仅在也为空白时补)。
- * contactName 空白、userId 为空、用户不存在时直接跳过;失败静默不阻塞主流程。
- * 幂等:realName 非空后再次调用直接跳过。
- */
- public void backfillRealNameAndNickname(Long userId, String contactName) {
- try {
- if (userId == null) return;
- if (contactName == null || contactName.trim().isEmpty()) return;
- User user = userMapper.selectById(userId);
- if (user == null) return;
- String realName = user.getRealName();
- if (realName != null && !realName.trim().isEmpty()) return;
- String name = contactName.trim();
- user.setRealName(name);
- String nickname = user.getNickname();
- if (nickname == null || nickname.trim().isEmpty()) {
- user.setNickname(name);
- }
- userMapper.updateById(user);
- } catch (Exception e) {
- log.warn("[RealNameBackfill] 回填失败 userId={} : {}", userId, e.getMessage());
- }
- }
- }
|