package configuration import ( "git.sxidc.com/go-framework/baize/convenient/binding" "git.sxidc.com/go-framework/baize/convenient/binding/request" "git.sxidc.com/go-framework/baize/convenient/binding/response" "git.sxidc.com/go-framework/baize/convenient/value_object" "git.sxidc.com/go-framework/baize/framwork/api" "git.sxidc.com/go-framework/baize/framwork/domain" "git.sxidc.com/go-framework/baize/framwork/infrastructure" "git.sxidc.com/go-framework/baize/framwork/infrastructure/database" "git.sxidc.com/go-framework/baize/framwork/infrastructure/database/sql" "git.sxidc.com/go-tools/utils/strutils" "git.sxidc.com/service-supports/fserr" ) // Simple Bind参数 type Simple struct { // schema Schema string // 可选配置项,通过WithXXX配置 options *Options } func (simple *Simple) bind(binder *binding.Binder) { options := simple.options configurationTableName := domain.TableName(simple.Schema, &Entity{}) valueObjectOptions := []value_object.Option[any]{value_object.WithDisableQuery[any]()} if options.disableCreate { valueObjectOptions = append(valueObjectOptions, value_object.WithDisableCreate[any]()) } if options.disableDelete { valueObjectOptions = append(valueObjectOptions, value_object.WithDisableDelete[any]()) } value_object.BindSimple(binder, &value_object.Simple[any]{ ValueObject: &Entity{}, Schema: simple.Schema, CreateJsonBody: &AddConfigurationJsonBody{}, DeleteQueryParams: &RemoveConfigurationJsonBody{}, }, valueObjectOptions...) if !options.disableQuery { binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{ Path: "/configuration/values", ResponseFunc: response.SendMapResponse, RequestParams: &GetConfigurationValuesQueryParams{}, Objects: []domain.Object{&Entity{}}, ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) { dbExecutor := i.DBExecutor() e, ok := objects[0].(*Entity) if !ok { return map[string]any{ "values": make([]string, 0), }, fserr.New("传递的实体不是该领域的实体") } conditions := sql.NewConditions() if strutils.IsStringNotEmpty(e.Scope) { conditions.Equal(ColumnScope, e.Scope) } if strutils.IsStringNotEmpty(e.Group) { conditions.Equal(ColumnGroup, e.Group) } results, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{ TableName: configurationTableName, SelectColumns: []string{ColumnValue}, Conditions: conditions, }) if err != nil { return map[string]any{ "values": make([]string, 0), }, err } values := make([]string, 0) for _, result := range results { values = append(values, result.ColumnValueString(ColumnValue)) } return map[string]any{ "values": values, }, nil }, }) } } func BindConfiguration(binder *binding.Binder, simple *Simple, opts ...Option) { options := new(Options) for _, opt := range opts { opt(options) } simple.options = options simple.bind(binder) } type Option func(options *Options) type Options struct { // 创建删除 disableCreate bool // 关闭删除 disableDelete bool // 关闭查询 disableQuery bool } func WithDisableCreate() Option { return func(options *Options) { options.disableCreate = true } } func WithDisableDelete() Option { return func(options *Options) { options.disableDelete = true } } func WithDisableQuery() Option { return func(options *Options) { options.disableQuery = true } }