package api import ( "context" "net/http" "strings" "time" "droboticflow/internal/domain" "github.com/go-chi/chi/v5" ) func acceptancePolicyAt(policy *domain.WorkflowRuntimePolicyRecord, submittedAt string) (float64, int) { if policy == nil { return domain.DefaultAcceptanceSampleProbability, 0 } at, err := time.Parse(time.RFC3339Nano, submittedAt) if err != nil { return policy.AcceptanceSampleProbability, policy.Revision } if updated, err := time.Parse(time.RFC3339Nano, policy.UpdatedAt); err == nil && !updated.After(at) { return policy.AcceptanceSampleProbability, policy.Revision } probability, revision := domain.DefaultAcceptanceSampleProbability, 0 for _, entry := range policy.History { updated, err := time.Parse(time.RFC3339Nano, entry.UpdatedAt) if err == nil && !updated.After(at) && entry.Revision > revision { probability, revision = entry.AcceptanceSampleProbability, entry.Revision } } return probability, revision } func (server *server) freezeSubmissionAcceptancePolicy(ctx context.Context, item *domain.HumanItemRecord) error { if item.AcceptanceSampleValue != nil && item.AcceptanceSampleProbability != nil { return nil } policy, err := server.store.GetWorkflowRuntimePolicy(ctx, item.WorkflowID) if err != nil { return err } probability, revision := acceptancePolicyAt(policy, item.SubmittedAt) if item.AcceptanceSampleProbability != nil { probability, revision = *item.AcceptanceSampleProbability, item.AcceptancePolicyRevision } value := 0.0 if probability <= 0 { value = 1 } else if probability < 1 { value, err = secureCaptureSamplingValue() if err != nil { return err } } item.AcceptanceSampleProbability, item.AcceptancePolicyRevision = &probability, revision item.AcceptanceSampleValue = &value return nil } type workflowRuntimePolicyPatch struct { Revision *int `json:"revision"` MaxConcurrentCaptureRuns *int `json:"maxConcurrentCaptureRuns"` AcceptanceSampleProbability *float64 `json:"acceptanceSampleProbability"` } func defaultWorkflowRuntimePolicy(workflow domain.WorkflowRecord) domain.WorkflowRuntimePolicyRecord { return domain.WorkflowRuntimePolicyRecord{ ID: "workflow-runtime-policy-" + workflow.ID, OrganizationID: workflow.OrganizationID, ProjectID: workflow.ProjectID, WorkflowID: workflow.ID, Revision: 0, MaxConcurrentCaptureRuns: domain.DefaultMaxConcurrentCaptureRuns, AcceptanceSampleProbability: domain.DefaultAcceptanceSampleProbability, } } func (server *server) workflowRuntimePolicy(writer http.ResponseWriter, request *http.Request) (*domain.WorkflowRecord, *domain.WorkflowRuntimePolicyRecord, bool) { workflow, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id")) if err != nil { renderError(writer, http.StatusInternalServerError, err) return nil, nil, false } if workflow == nil { renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"}) return nil, nil, false } if !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow") { return nil, nil, false } principal, _ := currentPrincipal(request) if principal == nil || (!annotationManager(principal) && principal.User.Role != domain.UserRoleSupplierManager) { renderJSON(writer, http.StatusForbidden, map[string]string{"error": "administrator or supplier manager role is required"}) return nil, nil, false } if !server.ensureProjectVisibility(writer, request, workflow.OrganizationID, workflow.ProjectID) { return nil, nil, false } policy, err := server.store.GetWorkflowRuntimePolicy(request.Context(), workflow.ID) if err != nil { renderError(writer, http.StatusInternalServerError, err) return nil, nil, false } if policy == nil { fallback := defaultWorkflowRuntimePolicy(*workflow) policy = &fallback } return workflow, policy, true } func (server *server) handleWorkflowRuntimePolicyGet(writer http.ResponseWriter, request *http.Request) { _, policy, ok := server.workflowRuntimePolicy(writer, request) if !ok { return } response := *policy response.History = nil renderJSON(writer, http.StatusOK, response) } func (server *server) handleWorkflowRuntimePolicyHistory(writer http.ResponseWriter, request *http.Request) { _, policy, ok := server.workflowRuntimePolicy(writer, request) if !ok { return } renderJSON(writer, http.StatusOK, map[string]any{"workflowId": policy.WorkflowID, "revisions": policy.History}) } func (server *server) handleWorkflowRuntimePolicyPatch(writer http.ResponseWriter, request *http.Request) { principal, _ := currentPrincipal(request) if !annotationManager(principal) { renderJSON(writer, http.StatusForbidden, map[string]string{"error": "organization administrator role is required"}) return } workflow, current, ok := server.workflowRuntimePolicy(writer, request) if !ok { return } var payload workflowRuntimePolicyPatch if !decodeJSON(writer, request, &payload) { return } if payload.Revision != nil && *payload.Revision != current.Revision { renderJSON(writer, http.StatusConflict, map[string]string{"error": "运行设置已被其他管理员修改,请重新加载后再保存", "code": "runtime_policy_version_conflict"}) return } if payload.MaxConcurrentCaptureRuns == nil && payload.AcceptanceSampleProbability == nil { renderValidationError(writer, validationIssue{Field: "runtimePolicy", Message: "至少修改一项运行策略"}) return } next := *current if payload.MaxConcurrentCaptureRuns != nil { if *payload.MaxConcurrentCaptureRuns <= 0 { renderValidationError(writer, validationIssue{Field: "maxConcurrentCaptureRuns", Message: "Capture 并发数必须是正整数"}) return } next.MaxConcurrentCaptureRuns = *payload.MaxConcurrentCaptureRuns } if payload.AcceptanceSampleProbability != nil { if *payload.AcceptanceSampleProbability < 0 || *payload.AcceptanceSampleProbability > 1 { renderValidationError(writer, validationIssue{Field: "acceptanceSampleProbability", Message: "最终验收抽样概率必须在 0 到 1 之间"}) return } next.AcceptanceSampleProbability = *payload.AcceptanceSampleProbability } actorID := "system" if principal != nil && strings.TrimSpace(principal.User.ID) != "" { actorID = principal.User.ID } now := nowISO() next.ID = "workflow-runtime-policy-" + workflow.ID next.OrganizationID = workflow.OrganizationID next.ProjectID = workflow.ProjectID next.WorkflowID = workflow.ID next.Revision = current.Revision + 1 next.UpdatedByUserID = actorID next.UpdatedAt = now next.History = append(append([]domain.WorkflowRuntimePolicyRevision(nil), current.History...), domain.WorkflowRuntimePolicyRevision{ PreviousAcceptanceSampleProbability: ¤t.AcceptanceSampleProbability, UpdatedByUserName: principal.User.Name, Revision: next.Revision, MaxConcurrentCaptureRuns: next.MaxConcurrentCaptureRuns, AcceptanceSampleProbability: next.AcceptanceSampleProbability, UpdatedByUserID: actorID, UpdatedAt: now, }) if len(next.History) > 100 { next.History = append([]domain.WorkflowRuntimePolicyRevision(nil), next.History[len(next.History)-100:]...) } updated, err := server.store.UpdateWorkflowRuntimePolicyVersioned(request.Context(), next, current.Revision) if err != nil { renderError(writer, http.StatusInternalServerError, err) return } if !updated { renderJSON(writer, http.StatusConflict, map[string]string{"error": "运行策略已被其他管理员修改,请刷新后重试", "code": "runtime_policy_version_conflict"}) return } next.History = nil renderJSON(writer, http.StatusOK, next) }