112 lines
5.1 KiB
Go
112 lines
5.1 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
"droboticflow/internal/domain"
|
||
"fmt"
|
||
"strings"
|
||
)
|
||
|
||
func (s *server) resolveDiscardReason(ctx context.Context, item *domain.HumanItemRecord, body sliceDispositionInput) (*domain.AnnotationLabelRecord, []validationIssue) {
|
||
issue := func(message string) (*domain.AnnotationLabelRecord, []validationIssue) {
|
||
return nil, []validationIssue{{Field: "reasonLabelId", Code: "discard_reason_dictionary_required", Message: message}}
|
||
}
|
||
if body.RestoreClassifications {
|
||
if prior := historicalDiscardReason(item, body); prior != nil {
|
||
if prior.DiscardReasonDictionary != nil {
|
||
d := prior.DiscardReasonDictionary
|
||
return &domain.AnnotationLabelRecord{ID: d.ID, Kind: d.Kind, Code: d.Code, Name: d.Name, Version: d.Version, Tags: append([]string{}, d.Tags...), OrganizationID: item.OrganizationID}, nil
|
||
}
|
||
if prior.DiscardReasonLabelID == "" {
|
||
return nil, nil
|
||
}
|
||
return &domain.AnnotationLabelRecord{ID: prior.DiscardReasonLabelID, Kind: "discard_reason", Name: prior.DiscardReasonName, Version: prior.DiscardReasonVersion, OrganizationID: item.OrganizationID}, nil
|
||
}
|
||
return issue("未找到当前任务已保存的舍弃原因,无法恢复该历史选择")
|
||
}
|
||
selection, err := s.store.GetProjectAnnotationLabels(ctx, item.ProjectID)
|
||
if err != nil {
|
||
return issue("舍弃原因字典加载失败,请重试")
|
||
}
|
||
// Only a successfully read, unconfigured project permits a free-text reason.
|
||
// A stale/foreign selection or failed lookup must never bypass the dictionary.
|
||
if selection == nil || (selection.OrganizationID == item.OrganizationID && len(selection.DiscardReasonLabelIDs) == 0) {
|
||
if body.ReasonLabelID != "" || body.ReasonVersion != 0 {
|
||
return issue("舍弃原因字典已变化,请刷新后填写原因")
|
||
}
|
||
reason := strings.TrimSpace(body.Reason)
|
||
if reason == "" || len([]rune(reason)) > 1000 {
|
||
return nil, []validationIssue{{Field: "reason", Code: "discard_reason_required", Message: "请填写舍弃原因,最多 1000 字"}}
|
||
}
|
||
return nil, nil
|
||
}
|
||
enabled := false
|
||
if selection != nil && selection.OrganizationID == item.OrganizationID {
|
||
for _, id := range selection.DiscardReasonLabelIDs {
|
||
if id == body.ReasonLabelID {
|
||
enabled = true
|
||
break
|
||
}
|
||
}
|
||
}
|
||
if !enabled {
|
||
return issue("请从当前项目舍弃原因字典选择原因")
|
||
}
|
||
label, err := s.store.GetAnnotationLabel(ctx, body.ReasonLabelID)
|
||
if err != nil {
|
||
return issue("舍弃原因读取失败,请重试")
|
||
}
|
||
if label == nil || label.OrganizationID != item.OrganizationID || dictionaryKind(label.Kind) != "discard_reason" || label.Status != domain.AnnotationLabelActive {
|
||
return issue("所选舍弃原因已不可用,请刷新字典重新选择")
|
||
}
|
||
if label.Version != body.ReasonVersion {
|
||
return issue("所选舍弃原因已更新,请刷新字典并重新确认")
|
||
}
|
||
note := strings.TrimSpace(body.ReasonNote)
|
||
if (label.RequiresNote || label.Name == "其他") && note == "" {
|
||
return nil, []validationIssue{{Field: "reasonNote", Code: "discard_reason_note_required", Message: "该舍弃原因必须填写补充说明"}}
|
||
}
|
||
if len([]rune(note)) > 1000 {
|
||
return nil, []validationIssue{{Field: "reasonNote", Message: "补充说明最多 1000 字"}}
|
||
}
|
||
return label, nil
|
||
}
|
||
|
||
func describeSliceNameIssues(issues []validationIssue, slices []domain.SliceSegment) []validationIssue {
|
||
for i := range issues {
|
||
if issues[i].Code != "slice_dictionary_name_required" {
|
||
continue
|
||
}
|
||
var index int
|
||
if _, err := fmt.Sscanf(issues[i].Field, "sliceSegments.%d.name", &index); err == nil && index >= 0 && index < len(slices) {
|
||
issues[i].Message = fmt.Sprintf("切片 %d(%s)需要从本项目切片字典重新选择名称;请返回修正,已选舍弃原因会保留", index+1, slices[index].Name)
|
||
}
|
||
}
|
||
return issues
|
||
}
|
||
|
||
func discardReasonEvidence(slice domain.SliceSegment) map[string]any {
|
||
return map[string]any{"dictionary": slice.DiscardReasonDictionary, "reason": slice.DiscardReason, "reasonLabelId": slice.DiscardReasonLabelID, "reasonVersion": slice.DiscardReasonVersion, "reasonName": slice.DiscardReasonName, "reasonNote": slice.DiscardReasonNote, "at": slice.DiscardedAt, "previousAnnotations": slice.DiscardedAnnotations}
|
||
}
|
||
|
||
// Restore only a reason/note combination actually recorded for this slice;
|
||
// administrator changes never let a client invent historical reason metadata.
|
||
func historicalDiscardReason(item *domain.HumanItemRecord, body sliceDispositionInput) *domain.SliceSegment {
|
||
history, _ := normalizeStructuredValue(item.Subject.Payload["sliceDispositionHistory"]).([]any)
|
||
for i := len(history) - 1; i >= 0; i-- {
|
||
entry := mapValue(history[i])
|
||
if stringValue(entry["sliceId"]) != body.SliceID {
|
||
continue
|
||
}
|
||
after := decodeMaterializationValue[domain.SliceSegment]([]any{entry["after"]})
|
||
if len(after) != 1 {
|
||
continue
|
||
}
|
||
saved := after[0]
|
||
if saved.IsDiscarded() && saved.DiscardReasonLabelID == body.ReasonLabelID && saved.DiscardReasonVersion == body.ReasonVersion && saved.DiscardReasonNote == strings.TrimSpace(body.ReasonNote) && (saved.DiscardReasonLabelID != "" || saved.DiscardReason == strings.TrimSpace(body.Reason)) {
|
||
return &saved
|
||
}
|
||
}
|
||
return nil
|
||
}
|