| | |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | /** |
| | | * 企业名称脱敏:把企业名称替换为 Company1、Company2… 后发给大模型,回答返回前再还原。 |
| | | * 仅保存在内存中,不落库。 |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class DesensitizeService { |
| | |
| | | private final AtomicInteger counter = new AtomicInteger(1); |
| | | |
| | | public String maskEnterprise(String name) { |
| | | if (name == null) { |
| | | return null; |
| | | } |
| | | return enterpriseMap.computeIfAbsent(name, k -> { |
| | | String alias = "Company" + counter.getAndIncrement(); |
| | | reverseMap.put(alias, name); |
| | |
| | | return reverseMap.getOrDefault(alias, alias); |
| | | } |
| | | |
| | | /** 把整段文本里出现过的脱敏别名还原为真实企业名称(回答返回给前端前调用) */ |
| | | public String unmaskAll(String text) { |
| | | if (text == null || text.isEmpty() || reverseMap.isEmpty()) { |
| | | return text; |
| | | } |
| | | String out = text; |
| | | for (Map.Entry<String, String> e : reverseMap.entrySet()) { |
| | | if (out.contains(e.getKey())) { |
| | | out = out.replace(e.getKey(), e.getValue()); |
| | | } |
| | | } |
| | | return out; |
| | | } |
| | | |
| | | public void clear() { |
| | | enterpriseMap.clear(); |
| | | reverseMap.clear(); |
| | | counter.set(1); |
| | | } |
| | | } |
| | | } |