列表只返回发布键那一行;物化时若旧键行已在,就不再插入兄弟行,避免撞上唯一索引。 Co-authored-by: Cursor <cursoragent@cursor.com>
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package docgov
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"chat-agent-system/internal/workflowdef"
|
|
)
|
|
|
|
// collapseWorkflowDocuments 把同一发布键的旧键行与新键行收成一行。
|
|
// 发布键优先;都是发布键时保留已脱离模板的那条;再按 doc_id 稳定选取。
|
|
func collapseWorkflowDocuments(docs []WorkflowDocumentSummary) []WorkflowDocumentSummary {
|
|
if len(docs) < 2 {
|
|
return docs
|
|
}
|
|
best := make(map[string]WorkflowDocumentSummary, len(docs))
|
|
for _, doc := range docs {
|
|
canon := workflowdef.CanonicalTaskKey(doc.TaskKey)
|
|
if canon == "" {
|
|
continue
|
|
}
|
|
prev, ok := best[canon]
|
|
if !ok {
|
|
best[canon] = doc
|
|
continue
|
|
}
|
|
best[canon] = preferWorkflowDocument(prev, doc)
|
|
}
|
|
out := make([]WorkflowDocumentSummary, 0, len(best))
|
|
for _, doc := range best {
|
|
out = append(out, doc)
|
|
}
|
|
sort.Slice(out, func(i, j int) bool {
|
|
if out[i].TaskKey == out[j].TaskKey {
|
|
return out[i].DocID < out[j].DocID
|
|
}
|
|
return out[i].TaskKey < out[j].TaskKey
|
|
})
|
|
return out
|
|
}
|
|
|
|
func preferWorkflowDocument(current, candidate WorkflowDocumentSummary) WorkflowDocumentSummary {
|
|
currentPublished := current.TaskKey == workflowdef.CanonicalTaskKey(current.TaskKey)
|
|
candidatePublished := candidate.TaskKey == workflowdef.CanonicalTaskKey(candidate.TaskKey)
|
|
if currentPublished != candidatePublished {
|
|
if candidatePublished {
|
|
return candidate
|
|
}
|
|
return current
|
|
}
|
|
if current.IsTemplateOnly != candidate.IsTemplateOnly {
|
|
if candidate.IsTemplateOnly {
|
|
return current
|
|
}
|
|
return candidate
|
|
}
|
|
if candidate.DocID < current.DocID {
|
|
return candidate
|
|
}
|
|
return current
|
|
}
|