simple.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package one2many
  2. import (
  3. "fmt"
  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/framwork/domain/entity"
  7. "github.com/iancoleman/strcase"
  8. )
  9. // Simple 关联的Bind参数
  10. // LI 为左边实体的Info类型
  11. // RI 为右边实体的Info类型
  12. type Simple[LI any, RI any] struct {
  13. // 左领域实体,注意是Entity类型
  14. Left entity.Entity
  15. // 右领域实体,注意是Entity类型
  16. Right entity.Entity
  17. // 数据库Schema
  18. Schema string
  19. // 更新左实体关联使用的请求参数
  20. LeftUpdateJsonBody request.Params
  21. // 查询左实体关联使用的请求参数,注意是Query类型
  22. LeftQueryQueryParams request.Query
  23. // 更新左实体关联使用的请求参数
  24. RightUpdateJsonBody request.Params
  25. // 查询右实体关联使用的请求参数,注意是Query类型
  26. RightQueryQueryParams request.Query
  27. // 查询右实体带左实体信息使用的请求参数,注意是Query类型
  28. RightQueryWithLeftQueryParams request.Query
  29. // 可选配置项,通过WithXXX配置
  30. options *Options
  31. }
  32. func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
  33. options := simple.options
  34. leftDomainPath := entity.RelativeDomainPath(simple.Left)
  35. rightDomainPath := entity.RelativeDomainPath(simple.Right)
  36. leftRelationFieldName := fmt.Sprintf("%sID", simple.Right.DomainCamelName())
  37. rightRelationFieldName := fmt.Sprintf("%sID", simple.Left.DomainCamelName())
  38. leftTableName := entity.TableName(simple.Schema, simple.Left)
  39. leftRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(simple.Right.DomainCamelName()))
  40. rightTableName := entity.TableName(simple.Schema, simple.Right)
  41. rightRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(simple.Left.DomainCamelName()))
  42. }
  43. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
  44. options := new(Options)
  45. for _, opt := range opts {
  46. opt(options)
  47. }
  48. simple.options = options
  49. simple.bind(binder)
  50. }
  51. type Option func(options *Options)
  52. type Options struct {
  53. // 关闭左侧到右侧关联
  54. disableLeft bool
  55. // 关闭右侧到左侧关联
  56. disableRight bool
  57. // 关闭左侧更新
  58. disableLeftUpdate bool
  59. // 关闭左侧查询
  60. disableLeftQuery bool
  61. // 关闭右侧更新
  62. disableRightUpdate bool
  63. // 关闭右侧查询
  64. disableRightQuery bool
  65. // 关闭右实体带左实体信息查询
  66. disableRightWithLeftQuery bool
  67. }
  68. func WithDisableLeft() Option {
  69. return func(options *Options) {
  70. options.disableLeft = true
  71. }
  72. }
  73. func WithDisableRight() Option {
  74. return func(options *Options) {
  75. options.disableRight = true
  76. }
  77. }
  78. func WithDisableLeftUpdate() Option {
  79. return func(options *Options) {
  80. options.disableLeftUpdate = true
  81. }
  82. }
  83. func WithDisableLeftQuery() Option {
  84. return func(options *Options) {
  85. options.disableLeftQuery = true
  86. }
  87. }
  88. func WithDisableRightUpdate() Option {
  89. return func(options *Options) {
  90. options.disableRightUpdate = true
  91. }
  92. }
  93. func WithDisableRightQuery() Option {
  94. return func(options *Options) {
  95. options.disableRightQuery = true
  96. }
  97. }
  98. func WithDisableRightWithLeftQuery() Option {
  99. return func(options *Options) {
  100. options.disableRightWithLeftQuery = true
  101. }
  102. }