91 lines
2.5 KiB
Go
91 lines
2.5 KiB
Go
package run
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type outputSchema struct {
|
|
Fields []outputField `json:"fields"`
|
|
}
|
|
type outputField struct {
|
|
Key string `json:"key"`
|
|
Name string `json:"name"`
|
|
Display string `json:"display"`
|
|
Type string `json:"type"`
|
|
Source string `json:"source"`
|
|
SourceField string `json:"source_field"`
|
|
Default interface{} `json:"default"`
|
|
}
|
|
type OutputView struct {
|
|
Key string `json:"key"`
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
Source string `json:"source"`
|
|
Value interface{} `json:"value"`
|
|
}
|
|
|
|
func buildScenarioOutputs(db *gorm.DB, run model.SOPRun) ([]OutputView, error) {
|
|
var scenario model.Scenario
|
|
if err := db.Table("scenarios sc").Select("sc.*").Joins("JOIN sops s ON s.scenario_id = sc.id").Where("s.id = ? AND sc.tenant_id = ?", run.SOPID, run.TenantID).First(&scenario).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
input := map[string]interface{}{}
|
|
derived := map[string]interface{}{}
|
|
answers := map[string]interface{}{}
|
|
_ = json.Unmarshal(run.Input, &input)
|
|
_ = json.Unmarshal(run.Derived, &derived)
|
|
_ = json.Unmarshal(run.Answers, &answers)
|
|
return buildOutputViews(scenario.OutputSchema, input, derived, answers, run.Status, run.Result)
|
|
}
|
|
|
|
func buildOutputViews(raw []byte, input, derived, answers map[string]interface{}, status, result string) ([]OutputView, error) {
|
|
var schema outputSchema
|
|
if err := json.Unmarshal(raw, &schema); err != nil {
|
|
return nil, err
|
|
}
|
|
views := make([]OutputView, 0, len(schema.Fields))
|
|
for _, field := range schema.Fields {
|
|
sourceField := field.SourceField
|
|
var value interface{}
|
|
var ok bool
|
|
switch field.Source {
|
|
case "derived":
|
|
if sourceField == "" {
|
|
sourceField = field.Key
|
|
}
|
|
value, ok = derived[sourceField]
|
|
case "form":
|
|
if sourceField == "" {
|
|
sourceField = field.Key
|
|
}
|
|
value, ok = answers[sourceField]
|
|
case "system":
|
|
if sourceField == "status" {
|
|
value, ok = status, true
|
|
} else if sourceField == "result" {
|
|
value, ok = result, true
|
|
}
|
|
default:
|
|
if sourceField == "" {
|
|
sourceField = field.Key
|
|
}
|
|
value, ok = input[sourceField]
|
|
}
|
|
if !ok {
|
|
value = field.Default
|
|
}
|
|
name := field.Name
|
|
if name == "" {
|
|
name = field.Display
|
|
}
|
|
if name == "" {
|
|
name = field.Key
|
|
}
|
|
views = append(views, OutputView{Key: field.Key, Name: name, Type: field.Type, Source: field.Source, Value: value})
|
|
}
|
|
return views, nil
|
|
}
|