api.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package configuration
  2. import (
  3. "git.sxidc.com/go-framework/baize/api"
  4. "git.sxidc.com/go-framework/baize/convenient/binding"
  5. "git.sxidc.com/go-framework/baize/convenient/binding/request"
  6. "git.sxidc.com/go-framework/baize/convenient/binding/response"
  7. "git.sxidc.com/go-framework/baize/convenient/value_object"
  8. "git.sxidc.com/go-framework/baize/domain"
  9. "git.sxidc.com/go-framework/baize/infrastructure"
  10. "git.sxidc.com/go-framework/baize/infrastructure/database"
  11. "git.sxidc.com/go-framework/baize/infrastructure/database/sql"
  12. "git.sxidc.com/go-tools/utils/strutils"
  13. "git.sxidc.com/service-supports/fserr"
  14. )
  15. func BindConfiguration(binder *binding.Binder, opts ...Option) {
  16. options := new(Options)
  17. for _, opt := range opts {
  18. opt(options)
  19. }
  20. configurationTableName := tableName
  21. if strutils.IsStringNotEmpty(options.schema) {
  22. configurationTableName = options.schema + "." + tableName
  23. }
  24. valueObjectOptions := []value_object.Option[any]{value_object.WithDisableQuery[any]()}
  25. if options.disableCreate {
  26. valueObjectOptions = append(valueObjectOptions, value_object.WithDisableCreate[any]())
  27. }
  28. if options.disableDelete {
  29. valueObjectOptions = append(valueObjectOptions, value_object.WithDisableDelete[any]())
  30. }
  31. value_object.BindSimple(binder, &value_object.Simple[any]{
  32. ValueObject: &Entity{},
  33. TableName: configurationTableName,
  34. DomainPath: "/configuration",
  35. CreateJsonBody: &AddConfigurationJsonBody{},
  36. DeleteQueryParams: &RemoveConfigurationJsonBody{},
  37. }, valueObjectOptions...)
  38. if !options.disableQuery {
  39. binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
  40. Path: "/configuration/values",
  41. ResponseFunc: response.SendMapResponse,
  42. RequestParams: &GetConfigurationValuesQueryParams{},
  43. Objects: []domain.Object{&Entity{}},
  44. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
  45. dbExecutor := i.DBExecutor()
  46. e, ok := objects[0].(*Entity)
  47. if !ok {
  48. return map[string]any{
  49. "values": make([]string, 0),
  50. }, fserr.New("传递的实体不是该领域的实体")
  51. }
  52. conditions := sql.NewConditions()
  53. if strutils.IsStringNotEmpty(e.Scope) {
  54. conditions.Equal(ColumnScope, e.Scope)
  55. }
  56. if strutils.IsStringNotEmpty(e.Group) {
  57. conditions.Equal(ColumnGroup, e.Group)
  58. }
  59. results, _, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  60. TableName: configurationTableName,
  61. SelectColumns: []string{ColumnValue},
  62. Conditions: conditions,
  63. })
  64. if err != nil {
  65. return map[string]any{
  66. "values": make([]string, 0),
  67. }, err
  68. }
  69. values := make([]string, 0)
  70. for _, result := range results {
  71. values = append(values, result.ColumnValueString(ColumnValue))
  72. }
  73. return map[string]any{
  74. "values": values,
  75. }, nil
  76. },
  77. })
  78. }
  79. }
  80. type Option func(options *Options)
  81. type Options struct {
  82. // 表schema
  83. schema string
  84. // 创建删除
  85. disableCreate bool
  86. // 关闭删除
  87. disableDelete bool
  88. // 关闭查询
  89. disableQuery bool
  90. }
  91. func WithSchema(schema string) Option {
  92. return func(options *Options) {
  93. options.schema = schema
  94. }
  95. }
  96. func WithDisableCreate() Option {
  97. return func(options *Options) {
  98. options.disableCreate = true
  99. }
  100. }
  101. func WithDisableDelete() Option {
  102. return func(options *Options) {
  103. options.disableDelete = true
  104. }
  105. }
  106. func WithDisableQuery() Option {
  107. return func(options *Options) {
  108. options.disableQuery = true
  109. }
  110. }