| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package configuration
- import (
- "git.sxidc.com/go-framework/baize/convenient/value_object_crud"
- "git.sxidc.com/go-framework/baize/framework/binding"
- "git.sxidc.com/go-framework/baize/framework/core/api"
- "git.sxidc.com/go-framework/baize/framework/core/api/request"
- "git.sxidc.com/go-framework/baize/framework/core/api/response"
- "git.sxidc.com/go-framework/baize/framework/core/domain"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
- "git.sxidc.com/go-framework/baize/framework/core/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 := []any{value_object_crud.WithDisableQuery[any]()}
- if options.disableCreate {
- valueObjectOptions = append(valueObjectOptions, value_object_crud.WithDisableCreate())
- }
- if options.disableDelete {
- valueObjectOptions = append(valueObjectOptions, value_object_crud.WithDisableDelete())
- }
- value_object_crud.BindSimple(binder, &value_object_crud.Simple[any]{
- ValueObject: &Entity{},
- Schema: simple.Schema,
- CreateJsonBody: &AddConfigurationJsonBody{},
- DeleteJsonBody: &RemoveConfigurationJsonBody{},
- }, valueObjectOptions...)
- if !options.disableQuery {
- binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
- Path: "/configuration/values",
- SendResponseFunc: 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
- }
- }
|