111 lines
4.0 KiB
Go
111 lines
4.0 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// Binding happens before persistence, and never at delivery time. Name-only
|
|
// historical records remain name-only until a new selection is made.
|
|
func (s *server) validateProjectSliceNames(ctx context.Context, item *domain.HumanItemRecord, slices []domain.SliceSegment, mode string) []validationIssue {
|
|
if item == nil {
|
|
return nil
|
|
}
|
|
task, err := s.store.GetAnnotationTask(ctx, item.LegacyTaskID)
|
|
if err != nil {
|
|
return []validationIssue{annotationReadValidationIssue("sliceSegments", "无法读取切片字典规则", err)}
|
|
}
|
|
if task == nil || task.TemplateVersion < 6 {
|
|
// Older templates use taskLabelId/taskLabel; they cannot introduce the
|
|
// independent v6 slice dictionary field, including through undo/discard.
|
|
for i := range slices {
|
|
slices[i].Dictionary = nil
|
|
}
|
|
return nil
|
|
}
|
|
previous := decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(item)["sliceSegments"])
|
|
if len(previous) == 0 {
|
|
revisions, err := s.store.ListSliceRevisions(ctx, item.OrganizationID, item.LegacyTaskID)
|
|
if err != nil {
|
|
return []validationIssue{annotationReadValidationIssue("sliceSegments", "无法读取历史切片名称", err)}
|
|
}
|
|
for _, rev := range revisions {
|
|
if rev.Revision == item.CurrentRevision || len(previous) == 0 {
|
|
previous = rev.Segments
|
|
}
|
|
}
|
|
}
|
|
if issues := rememberDictionarySelections(item, sliceDictionarySelections(previous)...); len(issues) > 0 {
|
|
return issues
|
|
}
|
|
old := map[string]domain.SliceSegment{}
|
|
for _, p := range previous {
|
|
old[p.ID] = p
|
|
}
|
|
unconfirmed := mapValue(item.Subject.Payload["unconfirmedSliceNames"])
|
|
if unconfirmed == nil {
|
|
unconfirmed = map[string]any{}
|
|
}
|
|
var allowed map[string]domain.AnnotationLabelRecord
|
|
for i := range slices {
|
|
c := &slices[i]
|
|
p, exists := old[c.ID]
|
|
if mode == "restore" && c.Dictionary != nil {
|
|
if saved := savedDictionarySelection(item, c.Dictionary, "slice"); saved != nil && c.Name == saved.Name {
|
|
c.Dictionary = saved
|
|
delete(unconfirmed, c.ID)
|
|
continue
|
|
}
|
|
}
|
|
if c.IsDiscarded() {
|
|
c.Dictionary = nil
|
|
delete(unconfirmed, c.ID)
|
|
continue
|
|
}
|
|
unchanged := exists && p.Name == c.Name && (c.Dictionary == nil || sameDictionaryVersion(p.Dictionary, c.Dictionary))
|
|
finalizing := mode == "confirm" || mode == "submit"
|
|
pendingName := p.Dictionary == nil && c.Name != "" && stringValue(unconfirmed[c.ID]) == c.Name
|
|
needsNameCheck := (mode == "confirm" && p.Dictionary == nil) || (mode == "submit" && strings.TrimSpace(c.Name) == "") || (finalizing && pendingName)
|
|
if unchanged && !needsNameCheck {
|
|
c.Dictionary = p.Dictionary
|
|
continue
|
|
}
|
|
|
|
if (mode == "restore" && c.Dictionary == nil) || (mode == "draft" && strings.TrimSpace(c.Name) == "") {
|
|
c.Dictionary = nil
|
|
if c.Name != "" {
|
|
unconfirmed[c.ID] = c.Name
|
|
}
|
|
continue
|
|
}
|
|
if allowed == nil {
|
|
allowed, err = s.projectDictionarySelections(ctx, item, "slice")
|
|
if err != nil {
|
|
return []validationIssue{dictionaryReadValidationIssue("sliceSegments", "无法读取项目切片字典,请重试", err)}
|
|
}
|
|
}
|
|
var chosen *domain.DictionarySelection
|
|
for _, label := range allowed {
|
|
if label.Name == c.Name && (c.Dictionary == nil || (label.ID == c.Dictionary.ID && label.Version == c.Dictionary.Version)) {
|
|
chosen = label.Selection()
|
|
break
|
|
}
|
|
}
|
|
if chosen == nil {
|
|
return []validationIssue{{Field: fmt.Sprintf("sliceSegments.%d.name", i), Code: "slice_dictionary_name_required", Message: "请从本项目切片字典选择名称;词条已更新时请重新选择"}}
|
|
}
|
|
if !(exists && p.Name == c.Name && p.Dictionary == nil && c.Dictionary == nil && stringValue(unconfirmed[c.ID]) != c.Name) {
|
|
c.Dictionary = chosen
|
|
}
|
|
delete(unconfirmed, c.ID)
|
|
}
|
|
if len(unconfirmed) > 0 {
|
|
item.Subject.Payload["unconfirmedSliceNames"] = unconfirmed
|
|
} else {
|
|
delete(item.Subject.Payload, "unconfirmedSliceNames")
|
|
}
|
|
return rememberDictionarySelections(item, sliceDictionarySelections(slices)...)
|
|
}
|