feat: 站点信息提交时自动匹配已有站点(URL/名称),匹配则关联否则新建
This commit is contained in:
@@ -33,9 +33,11 @@ public interface SiteService {
|
|||||||
String iconUrl, String siteType, Long submitterId);
|
String iconUrl, String siteType, Long submitterId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 提交站点信息反馈 → 写入 site_submissions 审核表
|
* 提交站点信息反馈 → 写入 site_changes 审核表。
|
||||||
|
* 自动匹配已有站点(按名称/URL),匹配到则关联,否则新建。
|
||||||
|
* @param suggestedSiteId 建议的 siteId,为 null 时自动从名称生成
|
||||||
*/
|
*/
|
||||||
void submitSiteInfo(String siteId, String status, List<String> names,
|
void submitSiteInfo(String suggestedSiteId, String status, List<String> names,
|
||||||
List<String> urls, String notes, Long submitterId);
|
List<String> urls, String notes, Long submitterId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -124,8 +124,11 @@ public class SiteServiceImpl implements SiteService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void submitSiteInfo(String siteId, String status, List<String> names,
|
public void submitSiteInfo(String suggestedSiteId, String status, List<String> names,
|
||||||
List<String> urls, String notes, Long submitterId) {
|
List<String> urls, String notes, Long submitterId) {
|
||||||
|
// 尝试匹配已有站点
|
||||||
|
String resolvedSiteId = resolveSiteId(suggestedSiteId, names, urls);
|
||||||
|
|
||||||
Map<String, Object> raw = new LinkedHashMap<>();
|
Map<String, Object> raw = new LinkedHashMap<>();
|
||||||
raw.put("names", names != null ? names : List.of());
|
raw.put("names", names != null ? names : List.of());
|
||||||
raw.put("urls", urls != null ? urls : List.of());
|
raw.put("urls", urls != null ? urls : List.of());
|
||||||
@@ -133,7 +136,7 @@ public class SiteServiceImpl implements SiteService {
|
|||||||
raw.put("siteStatus", status != null ? status : "正常");
|
raw.put("siteStatus", status != null ? status : "正常");
|
||||||
|
|
||||||
SiteChange entry = new SiteChange();
|
SiteChange entry = new SiteChange();
|
||||||
entry.setSiteId(siteId);
|
entry.setSiteId(resolvedSiteId);
|
||||||
entry.setSubmitterId(submitterId);
|
entry.setSubmitterId(submitterId);
|
||||||
entry.setStatus("pending");
|
entry.setStatus("pending");
|
||||||
try {
|
try {
|
||||||
@@ -142,7 +145,52 @@ public class SiteServiceImpl implements SiteService {
|
|||||||
throw new RuntimeException("序列化变更数据失败", e);
|
throw new RuntimeException("序列化变更数据失败", e);
|
||||||
}
|
}
|
||||||
changeMapper.insert(entry);
|
changeMapper.insert(entry);
|
||||||
log.info("站点变更提交: siteId={} changeId={}", siteId, entry.getId());
|
log.info("站点变更提交: siteId={} changeId={} matched={}",
|
||||||
|
resolvedSiteId, entry.getId(), !resolvedSiteId.equals(suggestedSiteId == null ? "" : suggestedSiteId));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 匹配已有站点:先按 URL 域名匹配 → 再按名称匹配 → 都不匹配则用建议的 siteId 或自动生成。
|
||||||
|
*/
|
||||||
|
private String resolveSiteId(String suggested, List<String> names, List<String> urls) {
|
||||||
|
// 1. URL 匹配:提取域名,去重后对比
|
||||||
|
if (urls != null) {
|
||||||
|
for (String u : urls) {
|
||||||
|
try {
|
||||||
|
String host = new java.net.URI(u).getHost();
|
||||||
|
if (host != null) {
|
||||||
|
Site match = siteMapper.selectOne(
|
||||||
|
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Site>()
|
||||||
|
.like(Site::getUrlsJson, host)
|
||||||
|
.eq(Site::getDeleted, 0));
|
||||||
|
if (match != null) {
|
||||||
|
log.info("URL 匹配到已有站点: {} → {}", u, match.getSiteId());
|
||||||
|
return match.getSiteId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 名称匹配(不区分大小写)
|
||||||
|
if (names != null) {
|
||||||
|
for (String n : names) {
|
||||||
|
Site match = siteMapper.selectOne(
|
||||||
|
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Site>()
|
||||||
|
.like(Site::getNamesJson, n)
|
||||||
|
.eq(Site::getDeleted, 0));
|
||||||
|
if (match != null) {
|
||||||
|
log.info("名称匹配到已有站点: {} → {}", n, match.getSiteId());
|
||||||
|
return match.getSiteId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 用建议值或从名称自动生成
|
||||||
|
if (suggested != null && !suggested.isBlank()) return suggested;
|
||||||
|
|
||||||
|
String firstName = (names != null && !names.isEmpty()) ? names.get(0) : "unknown";
|
||||||
|
return firstName.toLowerCase().replaceAll("[^a-z0-9.-]", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user