droboticflow/internal/api/acceptance_misreports.go

105 lines
3.6 KiB
Go

package api
import (
"droboticflow/internal/domain"
"net/http"
"strings"
)
func (s *server) handleAcceptanceMisreports(w http.ResponseWriter, r *http.Request) {
item, ok := s.loadHumanItemForRequest(w, r)
if !ok {
return
}
principal, _ := currentPrincipal(r)
if principal == nil || (principal.User.Role != domain.UserRoleQualityReviewer && !annotationManager(principal)) || item.AssignedUserID != principal.User.ID {
renderJSON(w, 403, map[string]string{"error": "只有当前领取的审核员可以处理验收意见"})
return
}
if item.CurrentStageID != domain.FanyuV58StageQualityReview || item.PendingCombinedSubmissionID != "" || (item.Status != domain.HumanItemClaimed && item.Status != domain.HumanItemInProgress) {
renderJSON(w, 409, map[string]string{"error": "当前任务不可处理验收意见"})
return
}
operation, err := s.acceptanceReturnOperation(r.Context(), *item)
if err != nil {
renderError(w, 500, err)
return
}
problems := []domain.ReviewProblem{}
if operation == nil {
if r.Method != http.MethodGet {
renderJSON(w, 409, map[string]string{"error": "本轮没有可处理的验收意见"})
return
}
renderJSON(w, 200, map[string]any{"problems": problems, "handling": nil, "operationId": "", "itemVersion": item.Version})
return
}
for _, p := range operation.Draft.Problems {
if p.SourceItemID == item.ID && p.Scope != "task" {
problems = append(problems, p)
}
}
id := item.ID + ":acceptance-handling:" + operation.ID
if r.Method == http.MethodPut {
var input struct {
OperationID string `json:"operationId"`
ItemVersion int `json:"itemVersion"`
Version int `json:"version"`
SkippedOpinionIDs []string `json:"skippedOpinionIds"`
}
if !decodeJSON(w, r, &input) {
return
}
if input.OperationID != operation.ID || input.ItemVersion != item.Version {
renderJSON(w, 409, map[string]string{"error": "任务已更新,请刷新后重试"})
return
}
current, e := s.store.GetReworkHandling(r.Context(), id)
if e != nil {
renderError(w, 500, e)
return
}
if current != nil && current.DecisionOperationID != "" {
decision, e := s.store.GetReviewOperation(r.Context(), current.DecisionOperationID)
if e != nil {
renderError(w, 500, e)
return
}
if decision == nil || decision.Status != "failed_before_effects" {
renderJSON(w, 409, map[string]string{"error": "正式决定正在处理或已完成,请先核对提交结果"})
return
}
}
known := map[string]bool{}
for _, p := range problems {
known[p.ID] = true
}
next := domain.ReworkHandlingRecord{ID: id, OrganizationID: item.OrganizationID, ItemID: item.ID, Round: item.ReviewRound, OperationID: operation.ID, ActorUserID: principal.User.ID, UpdatedAt: nowISO(), SkippedOpinionIDs: []string{}}
seen := map[string]bool{}
for _, raw := range input.SkippedOpinionIDs {
id := strings.TrimSpace(raw)
if !known[id] || seen[id] {
renderJSON(w, 400, map[string]string{"error": "意见标识无效或重复"})
return
}
seen[id] = true
next.SkippedOpinionIDs = append(next.SkippedOpinionIDs, id)
}
saved, e := s.store.CompareAndSwapReworkHandling(r.Context(), next, input.Version)
if e != nil {
renderError(w, 500, e)
return
}
if !saved {
renderJSON(w, 409, map[string]string{"error": "意见状态已更新,请刷新后重试"})
return
}
}
handling, err := s.store.GetReworkHandling(r.Context(), id)
if err != nil {
renderError(w, 500, err)
return
}
renderJSON(w, 200, map[string]any{"problems": problems, "handling": handling, "operationId": operation.ID, "itemVersion": item.Version})
}