Skip to content
This repository was archived by the owner on Apr 18, 2019. It is now read-only.
This repository was archived by the owner on Apr 18, 2019. It is now read-only.

在 Action 回调中如何返回执行的结果给调用方? #6

@Michael2008S

Description

@Michael2008S

这里是执行方,怎么样把 Action回调 的执行结果返回给调用方比较好?

func (this *ShopsDB) GetShopsEventFSM(o orm.DB) (*fsm.StateMachine) {
	return model.ImplShopsStatusFSM(func(fromState string, args []interface{}) {
		// OnExit
	}, func(action string, fromState string, toState string, args []interface{}) error {
		// Action
		switch action {
		case retail_shopspb.ShopsEvent_EVENT_SHOP_RECOMMIT_VERIFY.String():
			if len(args) >= 1 {
				if s, ok := args[0].(*model.Shops); ok {
					s.Status = retail_shopspb.ShopsStatus(retail_shopspb.ShopsStatus_value[toState])
					if result, err := o.Model(s).Column("status", "name", "desc").WherePK().Update(); err != nil {
						return errors.New("更新失败,未能进行状态修改。" + err.Error())
					} else {
						// FIXME 如何返回执行的结果 给调用方?
						fmt.Println(result)
						return nil
					}
				}
			}
			return errors.New("未知类型,未能进行状态修改。")
		default:
			return errors.New("未知事件,未能进行状态修改。")
		}
		return nil
	}, func(action string, fromState string, toState string, args []interface{}, err error) {
		// OnActionFailure
	}, func(toState string, args []interface{}) {
		// OnEnter
	})
}

下面的代码是调用方:

	shopsFsm := s.idb.GetShopsEventFSM(s.dbcl.WithContext(c))
	err = shopsFsm.Trigger(view_result.Status.String(),
		retail_shopspb.ShopsEvent_EVENT_SHOP_RECOMMIT_VERIFY.String(), view_result)
	if err != nil {
		return nil, twirp.NewError(twirp.InvalidArgument, "数据保存失败。")
	}
	// FIXME 返回结果处理

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions