package run import ( "net/http" "strconv" "git.iwork-ai.com/xdc/iqudo-top1/internal/access" "git.iwork-ai.com/xdc/iqudo-top1/internal/auth" "git.iwork-ai.com/xdc/iqudo-top1/internal/model" "git.iwork-ai.com/xdc/iqudo-top1/internal/response" "github.com/gin-gonic/gin" ) func (h *Handler) PreviewScenario(c *gin.Context) { p, _ := auth.PrincipalFromContext(c) scenarioID, err := strconv.ParseUint(c.Param("id"), 10, 64) if err != nil || scenarioID == 0 || !access.CanViewScenario(h.db, p, scenarioID) { response.Error(c, http.StatusNotFound, "NOT_FOUND", "场景不存在") return } var body struct { Input map[string]interface{} `json:"input"` InitialValues map[string]interface{} `json:"initial_values"` Selector knowledgeSelector `json:"knowledge_selector"` } if err := c.ShouldBindJSON(&body); err != nil { response.Error(c, http.StatusBadRequest, "INVALID_ARGUMENT", "预览参数格式不正确") return } var scenario model.Scenario if err := h.db.Where("id = ? AND tenant_id = ?", scenarioID, p.TenantID).First(&scenario).Error; err != nil { response.Error(c, http.StatusNotFound, "NOT_FOUND", "场景不存在") return } var fields []model.ScenarioField if err := h.db.Where("scenario_id = ? AND tenant_id = ?", scenarioID, p.TenantID).Order("sort_order,id").Find(&fields).Error; err != nil { response.Error(c, http.StatusInternalServerError, "QUERY_FAILED", "查询场景输入失败") return } mapped := mergeValues(mapScenarioInput(fields, body.Input), body.InitialValues) if err := validateInitialAnswers(fields, mapped); err != nil { response.Error(c, http.StatusUnprocessableEntity, "INVALID_INPUT", err.Error()) return } derived, matchedRules, err := deriveForScenario(h.db, p.TenantID, scenarioID, mapped) if err != nil { response.Error(c, http.StatusUnprocessableEntity, "RULE_EVALUATION_FAILED", err.Error()) return } context := runtimeContext(mapped, derived, nil) knowledge, err := loadKnowledgeOutputsForScenario(h.db, scenarioID, p.TenantID, context, body.Selector) if err != nil { response.Error(c, http.StatusInternalServerError, "OUTPUT_FAILED", "生成知识预览失败") return } outputs, err := buildOutputViews(scenario.OutputSchema, mapped, derived, map[string]interface{}{}, knowledge, "preview", "") if err != nil { response.Error(c, http.StatusInternalServerError, "OUTPUT_FAILED", "生成场景输出失败") return } response.OK(c, gin.H{"input": mapped, "derived": derived, "matched_rules": matchedRules, "knowledge": knowledge, "outputs": outputs}) }