package settings import ( "fmt" "math" "strconv" "strings" "zerorigin/internal/action" "zerorigin/internal/config" "zerorigin/internal/trigger" ) // paramSpec 是设置界面内部统一的参数描述:把 trigger.ParamSchema 与 // action.ParamSpec 归一,便于共用同一套表单渲染与值转换逻辑。 type paramSpec struct { Name string Label string Type string // "string" | "int" | "bool" | "select" Required bool Default any Options []string Labels []string // 与 Options 对齐的中文显示名 Help string } // triggerSpecs 返回归一后的触发器参数描述。 func triggerSpecs(id string) []paramSpec { src := trigger.Schema(id) out := make([]paramSpec, 0, len(src)) for _, p := range src { out = append(out, paramSpec{ Name: p.Name, Label: p.Label, Type: p.Type, Required: p.Required, Default: p.Default, Options: p.Options, Labels: p.Labels, Help: p.Help, }) } return out } // actionSpecs 返回归一后的动作参数描述;未知动作返回 nil。 func actionSpecs(id string) []paramSpec { act, err := action.New(id) if err != nil { return nil } src := act.Schema().Params out := make([]paramSpec, 0, len(src)) for _, p := range src { out = append(out, paramSpec{ Name: p.Name, Label: p.Label, Type: p.Type, Required: p.Required, Default: p.Default, Options: p.Options, Labels: p.Labels, Help: p.Help, }) } return out } // strPtr 返回指向 s 的指针(供输入控件绑定,指针生命周期与 draft 一致)。 func strPtr(s string) *string { return &s } // ruleDraft 是规则的编辑副本:数值以字符串编辑,保存时按 Schema 转换。 type ruleDraft struct { ID string Name *string Enabled bool Notify bool Priority *string CooldownMs *string Fullscreen *string // "ignore" | "allow" TriggerType string TriggerVals map[string]*string Actions []*actionDraft } // actionDraft 是一个动作的编辑副本。 type actionDraft struct { Type string Vals map[string]*string } // newRuleDraft 由配置规则创建编辑副本。 func newRuleDraft(r *config.Rule) *ruleDraft { d := &ruleDraft{ ID: r.ID, Name: strPtr(r.Name), Enabled: r.Enabled, Notify: r.Notify, Priority: strPtr(strconv.Itoa(r.Priority)), CooldownMs: strPtr(strconv.Itoa(r.CooldownMs)), Fullscreen: strPtr(fullscreenDisplay(r.Fullscreen)), TriggerType: r.Trigger.Type, TriggerVals: valuePtrs(triggerSpecs(r.Trigger.Type), r.Trigger.Params), } for _, a := range r.Actions { d.Actions = append(d.Actions, &actionDraft{Type: a.Type, Vals: valuePtrs(actionSpecs(a.Type), a.Params)}) } return d } // newActionDraft 按动作类型创建带默认参数的编辑副本。 func newActionDraft(id string) *actionDraft { return &actionDraft{Type: id, Vals: valuePtrs(actionSpecs(id), nil)} } // setTriggerType 切换触发器类型并重置参数为默认值。 func (d *ruleDraft) setTriggerType(id string) { d.TriggerType = id d.TriggerVals = valuePtrs(triggerSpecs(id), nil) } // setType 切换动作类型并重置参数为默认值。 func (a *actionDraft) setType(id string) { a.Type = id a.Vals = valuePtrs(actionSpecs(id), nil) } // fullscreenDisplay 把配置里的全屏行为转成下拉显示文本。 func fullscreenDisplay(v string) string { if v == "allow" { return fullscreenAllow } return fullscreenIgnore } // fullscreenParse 把下拉显示文本转回配置值。 func fullscreenParse(display string) string { if display == fullscreenAllow { return "allow" } return "ignore" } // valuePtrs 为每个参数建立编辑字符串:优先取已配置值,否则用默认值。 func valuePtrs(specs []paramSpec, params map[string]any) map[string]*string { out := make(map[string]*string, len(specs)) for _, p := range specs { s := "" if v, ok := params[p.Name]; ok { s = valueToDisplay(p, v) } else if p.Default != nil { s = valueToDisplay(p, p.Default) } else if p.Type == "select" && len(p.Options) > 0 { s = displayOption(p, p.Options[0]) } out[p.Name] = strPtr(s) } return out } // displayOption 把选项值转成界面显示文本(有中文标签时用标签)。 func displayOption(p paramSpec, opt string) string { if len(p.Labels) == len(p.Options) { for i, o := range p.Options { if o == opt { return p.Labels[i] } } } return opt } // valueToDisplay 把配置值转成界面编辑字符串。 func valueToDisplay(p paramSpec, v any) string { s := formatScalar(v) if p.Type == "select" { return displayOption(p, s) } return s } // formatScalar 把配置标量格式化为界面文本(整数值的 float64 不带小数点)。 func formatScalar(v any) string { switch x := v.(type) { case float64: if x == math.Trunc(x) { return strconv.FormatInt(int64(x), 10) } return strconv.FormatFloat(x, 'f', -1, 64) case int: return strconv.Itoa(x) case bool: return strconv.FormatBool(x) case string: return x case nil: return "" default: return fmt.Sprint(v) } } // displayToValue 把编辑字符串转换为配置值;nil 表示该参数未设置(走默认值)。 // 数值统一写成 float64,与 JSON 往返后的类型一致,避免引擎侧类型断言失败。 func displayToValue(p paramSpec, s string) (any, error) { switch p.Type { case "int": s = strings.TrimSpace(s) if s == "" { return nil, nil } f, err := strconv.ParseFloat(s, 64) if err != nil { return nil, fmt.Errorf("必须是整数") } if f != math.Trunc(f) { return nil, fmt.Errorf("必须是整数") } return f, nil case "bool": s = strings.TrimSpace(s) if s == "" { return nil, nil } b, err := strconv.ParseBool(s) if err != nil { return nil, fmt.Errorf("必须是 true 或 false") } return b, nil case "select": if s == "" { return nil, nil } if len(p.Labels) == len(p.Options) { for i, l := range p.Labels { if l == s { // 映射回空选项(如“任意方向”)表示不限制该字段, // 不能写成空字符串,否则事件匹配永远不成立。 if p.Options[i] == "" { return nil, nil } return p.Options[i], nil } } } for _, o := range p.Options { if o == s { if o == "" { return nil, nil } return o, nil } } return s, nil default: if strings.TrimSpace(s) == "" { return nil, nil } return s, nil } } // buildParams 按参数描述把编辑字符串转换为配置参数。 func buildParams(specs []paramSpec, vals map[string]*string) (map[string]any, error) { out := make(map[string]any, len(specs)) for _, p := range specs { src := vals[p.Name] if src == nil { continue } v, err := displayToValue(p, *src) if err != nil { return nil, fmt.Errorf("%s:%w", p.Label, err) } if v == nil { continue } out[p.Name] = v } return out, nil } // build 把编辑副本转换为配置规则。 func (d *ruleDraft) build() (*config.Rule, error) { priority, err := parseIntField("优先级", d.Priority) if err != nil { return nil, err } cooldown, err := parseIntField("冷却时间", d.CooldownMs) if err != nil { return nil, err } params, err := buildParams(triggerSpecs(d.TriggerType), d.TriggerVals) if err != nil { return nil, fmt.Errorf("触发器:%w", err) } actions := make([]config.ActionConfig, 0, len(d.Actions)) for i, ad := range d.Actions { ap, err := buildParams(actionSpecs(ad.Type), ad.Vals) if err != nil { return nil, fmt.Errorf("动作 %d(%s):%w", i+1, action.DisplayName(ad.Type), err) } actions = append(actions, config.ActionConfig{Type: ad.Type, Params: ap}) } fs := fullscreenParse(*d.Fullscreen) return &config.Rule{ ID: d.ID, Name: strings.TrimSpace(*d.Name), Enabled: d.Enabled, Notify: d.Notify, Priority: priority, Trigger: config.TriggerConfig{Type: d.TriggerType, Params: params}, Actions: actions, CooldownMs: cooldown, Fullscreen: fs, }, nil } // parseIntField 解析整数配置项(空值视为 0,负数拒绝)。 func parseIntField(label string, p *string) (int, error) { s := strings.TrimSpace(*p) if s == "" { return 0, nil } n, err := strconv.Atoi(s) if err != nil { return 0, fmt.Errorf("%s 必须是整数:%q", label, s) } if n < 0 { return 0, fmt.Errorf("%s 不能为负数", label) } return n, nil }