feat(api): 优化字段去重逻辑并添加标签去重处理
在exports.go中增强字段去重逻辑,确保主表字段优先于副表字段,并处理相同列名的去重,提升导出数据的准确性和一致性。同时,添加字段匹配校验以确保最终字段数量与模板一致。
This commit is contained in:
parent
3046274e24
commit
61e8cc04e7
|
|
@ -300,6 +300,44 @@ func (a *ExportsAPI) create(w http.ResponseWriter, r *http.Request) {
|
|||
filtered = deduped
|
||||
}
|
||||
}
|
||||
labels := FieldLabels()
|
||||
// 相同列名(中文标签)去重:如果多个表的字段共享同一列名,优先保留主表字段
|
||||
{
|
||||
labelIdx := map[string]int{}
|
||||
deduped := make([]string, 0, len(filtered))
|
||||
removed := []string{}
|
||||
for _, tf := range filtered {
|
||||
label := labels[tf]
|
||||
if label == "" {
|
||||
label = tf
|
||||
}
|
||||
parts := strings.Split(tf, ".")
|
||||
isMain := len(parts) == 2 && parts[0] == main
|
||||
if idx, ok := labelIdx[label]; ok {
|
||||
prev := deduped[idx]
|
||||
prevParts := strings.Split(prev, ".")
|
||||
prevMain := len(prevParts) == 2 && prevParts[0] == main
|
||||
switch {
|
||||
case prevMain:
|
||||
removed = append(removed, tf)
|
||||
continue
|
||||
case isMain:
|
||||
removed = append(removed, prev)
|
||||
deduped[idx] = tf
|
||||
continue
|
||||
default:
|
||||
removed = append(removed, tf)
|
||||
continue
|
||||
}
|
||||
}
|
||||
labelIdx[label] = len(deduped)
|
||||
deduped = append(deduped, tf)
|
||||
}
|
||||
if len(removed) > 0 {
|
||||
logging.JSON("INFO", map[string]interface{}{"event": "fields_deduplicated_by_label", "removed": removed, "reason": "同名列优先保留主表字段"})
|
||||
}
|
||||
filtered = deduped
|
||||
}
|
||||
// 字段匹配校验(数量与顺序)
|
||||
if len(filtered) != len(fs) {
|
||||
logging.JSON("ERROR", map[string]interface{}{"event": "field_count_mismatch", "template_count": len(fs), "final_count": len(filtered)})
|
||||
|
|
@ -329,7 +367,6 @@ func (a *ExportsAPI) create(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
var estimate int64
|
||||
estimate = rrepo.EstimateFastChunked(dataDB, ds, main, p.Filters)
|
||||
labels := FieldLabels()
|
||||
hdrs := make([]string, len(filtered))
|
||||
for i, tf := range filtered {
|
||||
if v, ok := labels[tf]; ok {
|
||||
|
|
@ -347,7 +384,7 @@ func (a *ExportsAPI) create(w http.ResponseWriter, r *http.Request) {
|
|||
for i := range hdrs {
|
||||
if cnt[hdrs[i]] > 1 {
|
||||
parts := strings.Split(filtered[i], ".")
|
||||
if len(parts) == 2 && parts[0] != "order" {
|
||||
if len(parts) == 2 && parts[0] != main {
|
||||
hdrs[i] = tableLabel(parts[0]) + "." + hdrs[i]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue