forked from longtao.wu/droboticflow
134 lines
5.2 KiB
Go
134 lines
5.2 KiB
Go
package api
|
||
|
||
import (
|
||
"droboticflow/internal/domain"
|
||
"droboticflow/internal/store"
|
||
"net/http"
|
||
"strconv"
|
||
"strings"
|
||
)
|
||
|
||
// 返工处理状态读写契约(返工界面重设计):标注员零文字输入——意见的"已处理"由界面按
|
||
// "意见目标相对退回基线已改动"自动消红,不落库;这里只持久化显式留痕的一键"不改了"
|
||
// (误报/不适用),复审侧可见。原意见本体只读(仅存在于退回基线),状态独立 CAS 保存。
|
||
|
||
type reworkHandlingInput struct {
|
||
Round int `json:"round,omitempty"`
|
||
Version int `json:"version"`
|
||
SkippedOpinionIDs []string `json:"skippedOpinionIds"`
|
||
}
|
||
|
||
func (s *server) handleReworkBaseline(w http.ResponseWriter, r *http.Request) {
|
||
item, ok := s.loadHumanItemForRequest(w, r)
|
||
if !ok {
|
||
return
|
||
}
|
||
round := item.ReviewRound
|
||
if raw := strings.TrimSpace(r.URL.Query().Get("round")); raw != "" {
|
||
if parsed, e := strconv.Atoi(raw); e == nil {
|
||
round = parsed
|
||
}
|
||
}
|
||
// R1-Q8(ADR 0008):任何轮次的基线查询都收窄为"未解决意见"视图——历史
|
||
// 轮次已判解决的意见彻底不返回(不折叠);完整轨迹走正式审查记录。
|
||
// 缺快照时返回空基线(旧数据/早期轮次),调用方降级读旧基线,不阻塞流程。
|
||
baseline, e := s.visibleReworkBaseline(r.Context(), *item, round)
|
||
if e != nil {
|
||
renderError(w, http.StatusInternalServerError, e)
|
||
return
|
||
}
|
||
renderJSON(w, http.StatusOK, map[string]any{"baseline": baseline, "round": round})
|
||
}
|
||
|
||
func (s *server) handleReworkHandling(w http.ResponseWriter, r *http.Request) {
|
||
item, ok := s.loadHumanItemForRequest(w, r)
|
||
if !ok {
|
||
return
|
||
}
|
||
principal, _ := currentPrincipal(r)
|
||
round := item.ReviewRound
|
||
if r.Method == http.MethodGet {
|
||
if parsed, e := strconv.Atoi(strings.TrimSpace(r.URL.Query().Get("round"))); e == nil {
|
||
round = parsed
|
||
}
|
||
handling, e := s.store.GetReworkHandling(r.Context(), store.ReworkHandlingID(item.ID, round))
|
||
if e != nil {
|
||
renderError(w, http.StatusInternalServerError, e)
|
||
return
|
||
}
|
||
renderJSON(w, http.StatusOK, map[string]any{"handling": handling, "round": round})
|
||
return
|
||
}
|
||
var input reworkHandlingInput
|
||
if !decodeJSON(w, r, &input) {
|
||
return
|
||
}
|
||
// 归属身份不能替代标注角色。保留管理员代管,但不豁免下面的阶段和轮次校验。
|
||
if principal == nil || (!annotationManager(principal) &&
|
||
(principal.User.Role != domain.UserRoleAnnotator || item.AssignedUserID != principal.User.ID)) {
|
||
renderJSON(w, http.StatusForbidden, map[string]string{"error": "只有当前返工归属标注员或管理员可以更新处理状态"})
|
||
return
|
||
}
|
||
requestedRound := round
|
||
if raw := strings.TrimSpace(r.URL.Query().Get("round")); raw != "" {
|
||
parsed, err := strconv.Atoi(raw)
|
||
if err != nil || parsed < 1 {
|
||
renderValidationError(w, validationIssue{Field: "round", Message: "返工轮次必须为正整数"})
|
||
return
|
||
}
|
||
requestedRound = parsed
|
||
}
|
||
if round < 1 || requestedRound != round || item.CurrentStageID != domain.FanyuV58StageAnnotationCombined || item.PendingCombinedSubmissionID != "" ||
|
||
(item.Status != domain.HumanItemClaimed && item.Status != domain.HumanItemRework && item.Status != domain.HumanItemInProgress) ||
|
||
(input.Round != 0 && input.Round != round) {
|
||
renderJSON(w, http.StatusConflict, map[string]string{"code": "rework_handling_locked", "error": "当前返工已提交、正在送检或轮次已变化,不能修改意见处理状态"})
|
||
return
|
||
}
|
||
id := store.ReworkHandlingID(item.ID, round)
|
||
baseline, e := s.store.GetReworkBaseline(r.Context(), store.ReworkBaselineID(item.ID, round))
|
||
if e != nil {
|
||
renderError(w, http.StatusInternalServerError, e)
|
||
return
|
||
}
|
||
if baseline == nil || len(baseline.Problems) == 0 {
|
||
renderJSON(w, http.StatusConflict, map[string]string{"code": "rework_baseline_missing", "error": "本轮没有退回意见基线,无需处理状态"})
|
||
return
|
||
}
|
||
known := map[string]bool{}
|
||
for _, p := range baseline.Problems {
|
||
known[p.ID] = true
|
||
}
|
||
seen := map[string]bool{}
|
||
record := domain.ReworkHandlingRecord{ID: id, OrganizationID: item.OrganizationID, ItemID: item.ID, Round: round, UpdatedAt: nowISO()}
|
||
for _, rawID := range input.SkippedOpinionIDs {
|
||
opinionID := strings.TrimSpace(rawID)
|
||
if opinionID == "" {
|
||
continue
|
||
}
|
||
if !known[opinionID] {
|
||
renderValidationError(w, validationIssue{Field: "skippedOpinionIds", Message: "处理状态只能引用本轮退回的原意见"})
|
||
return
|
||
}
|
||
if seen[opinionID] {
|
||
renderValidationError(w, validationIssue{Field: "skippedOpinionIds", Message: "同一原意见只能标记一次"})
|
||
return
|
||
}
|
||
seen[opinionID] = true
|
||
record.SkippedOpinionIDs = append(record.SkippedOpinionIDs, opinionID)
|
||
}
|
||
if record.SkippedOpinionIDs == nil {
|
||
record.SkippedOpinionIDs = []string{}
|
||
}
|
||
saved, e := s.store.CompareAndSwapReworkHandling(r.Context(), record, input.Version)
|
||
if e != nil {
|
||
renderError(w, http.StatusInternalServerError, e)
|
||
return
|
||
}
|
||
if !saved {
|
||
renderJSON(w, http.StatusConflict, map[string]string{"code": "rework_handling_version_conflict", "error": "处理状态已被更新,请刷新后重试"})
|
||
return
|
||
}
|
||
stored, _ := s.store.GetReworkHandling(r.Context(), id)
|
||
renderJSON(w, http.StatusOK, map[string]any{"handling": stored})
|
||
}
|