api.go 3.4 KB

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