package condflow import ( "strings" "git.fsdpf.net/go/condition" "git.fsdpf.net/go/reflux" "git.fsdpf.net/go/reflux/fieldx" "git.fsdpf.net/go/reflux/valuex" ) // CondCase 条件分支 type CondCase struct { // name 分支名称 name string // priority 优先级(数值越大优先级越高) priority int // action 执行动作标识 // DeviceExecuter/LX2003C@Up // DeviceExecuter/LX2003C@Down action string // 执行参数 actParams fieldx.Schema // 执行配置值 actConfig fieldx.Schema // 条件判断 cond *condition.Condition // 继续下一个 case 匹配 nextCaseOnSuccess bool // 下一个 flow 匹配流 nextFlowOnSuccess *CondFlow // 下一个 flow 匹配流 参数 nextFlowParams fieldx.Schema } func (cc *CondCase) Execute(ctx *flowContext) (err error) { index := strings.SplitN(cc.action, "@", 2) invoker, err := ctx.GetActionInvoker(index[0]) if err != nil { return err } actCtx := ctx if len(cc.actParams) > 0 { data, err := cc.actParams.Generate(ctx) if err != nil { return err } actCtx = ctx.Fork(reflux.New(data)) } var actCfg valuex.Accessor = valuex.Nil if len(cc.actConfig) > 0 { if data, err := cc.actConfig.Generate(ctx); err == nil { actCfg = reflux.New(data) } } result, err := invoker.Call(actCtx, index[1], actCfg) if err != nil { return err } ctx.PushResult(invoker.Res().GetCode(), result) if cc.nextFlowOnSuccess != nil { if len(cc.nextFlowParams) > 0 { data, err := cc.nextFlowParams.Generate(ctx) if err != nil { return err } ctx = ctx.Fork(reflux.New(data)) } err = cc.nextFlowOnSuccess.Execute(ctx) } return err } // NewCondCase 创建新的条件分支 func NewCondCase(name string, cond *condition.Condition, action string) *CondCase { return &CondCase{ name: name, priority: 0, cond: cond, action: action, } } // WithPriority 设置优先级 func (c *CondCase) WithPriority(priority int) *CondCase { c.priority = priority return c } // WithNextCaseOnSuccess 设置成功后继续执行下一个 case func (c *CondCase) WithNextCaseOnSuccess(next bool) *CondCase { c.nextCaseOnSuccess = next return c } // WithActionParams 设置 Action 执行参数 func (c *CondCase) WithActionParams(params fieldx.Schema) *CondCase { c.actParams = params return c } // WithActionConfig 设置 Action 执行配置 func (c *CondCase) WithActionConfig(config fieldx.Schema) *CondCase { c.actConfig = config return c } // WithNextFlowOnSuccess 设置成功后执行的下一个流 func (c *CondCase) WithNextFlowOnSuccess(flow *CondFlow, params fieldx.Schema) *CondCase { c.nextFlowOnSuccess = flow c.nextFlowParams = params return c } // Name 获取分支名称 func (c *CondCase) Name() string { return c.name } // Priority 获取优先级 func (c *CondCase) Priority() int { return c.priority } // Condition 获取条件 func (c *CondCase) Condition() *condition.Condition { return c.cond } // NextCaseOnSuccess 获取是否继续下一个 case func (c *CondCase) NextCaseOnSuccess() bool { return c.nextCaseOnSuccess } // NextFlowOnSuccess 获取下一个流 func (c *CondCase) NextFlowOnSuccess() *CondFlow { return c.nextFlowOnSuccess }