simple.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package one2one
  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/framwork/domain"
  6. )
  7. // Simple 关联的Bind参数
  8. // LI 为左边实体的Info类型
  9. // RI 为右边实体的Info类型
  10. type Simple[LI any, RI any] struct {
  11. // 左领域实体,注意是Entity类型
  12. Left domain.Entity
  13. // 右领域实体,注意是Entity类型
  14. Right domain.Entity
  15. // 左表名
  16. LeftTableName string
  17. // 右表名
  18. RightTableName string
  19. // URL领域相对路径,如/user/userInfo,后面会自动补充,如/user/userInfo/update
  20. LeftDomainPath string
  21. // URL领域相对路径,如/userInfo/user,后面会自动补充,如/userInfo/user/update
  22. RightDomainPath string
  23. // 更新左实体关联使用的请求参数
  24. LeftUpdateJsonBody request.Params
  25. // 查询左实体关联使用的请求参数,注意是Query类型
  26. LeftQueryQueryParams request.Query
  27. // 更新右实体关联使用的请求参数
  28. RightUpdateJsonBody request.Params
  29. // 查询右实体关联使用的请求参数,注意是Query类型
  30. RightQueryQueryParams request.Query
  31. // 查询左实体带右实体信息使用的请求参数,注意是Query类型
  32. LeftQueryWithRightQueryParams request.Query
  33. // 查询右实体带左实体信息使用的请求参数,注意是Query类型
  34. RightQueryWithLeftQueryParams request.Query
  35. // 可选配置项,通过WithXXX配置
  36. options *Options
  37. }
  38. func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
  39. //options := simple.options
  40. }
  41. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
  42. options := new(Options)
  43. for _, opt := range opts {
  44. opt(options)
  45. }
  46. simple.options = options
  47. simple.bind(binder)
  48. }
  49. type Option func(options *Options)
  50. type Options struct {
  51. // 左实体中指向右实体的字段
  52. leftRelationField string
  53. // 右实体中指向左实体的字段
  54. rightRelationField string
  55. // 左实体中指向右实体的列名
  56. leftRelationColumn string
  57. // 右实体中指向左实体的列名
  58. rightRelationColumn string
  59. // 关闭左侧到右侧关联
  60. disableLeft bool
  61. // 关闭右侧到左侧关联
  62. disableRight bool
  63. // 关闭左侧更新
  64. disableLeftUpdate bool
  65. // 关闭左侧查询
  66. disableLeftQuery bool
  67. // 关闭右侧更新
  68. disableRightUpdate bool
  69. // 关闭右侧查询
  70. disableRightQuery bool
  71. // 关闭左实体带右实体信息查询
  72. disableLeftWithRightQuery bool
  73. // 关闭右实体带左实体信息查询
  74. disableRightWithLeftQuery bool
  75. }
  76. func WithLeftRelationField(leftRelationField string) Option {
  77. return func(options *Options) {
  78. options.leftRelationField = leftRelationField
  79. }
  80. }
  81. func WithRightRelationField(rightRelationField string) Option {
  82. return func(options *Options) {
  83. options.rightRelationField = rightRelationField
  84. }
  85. }
  86. func WithLeftRelationColumn(leftRelationColumn string) Option {
  87. return func(options *Options) {
  88. options.leftRelationColumn = leftRelationColumn
  89. }
  90. }
  91. func WithRightRelationColumn(rightRelationColumn string) Option {
  92. return func(options *Options) {
  93. options.rightRelationColumn = rightRelationColumn
  94. }
  95. }
  96. func WithDisableLeft() Option {
  97. return func(options *Options) {
  98. options.disableLeft = true
  99. }
  100. }
  101. func WithDisableRight() Option {
  102. return func(options *Options) {
  103. options.disableRight = true
  104. }
  105. }
  106. func WithDisableLeftUpdate() Option {
  107. return func(options *Options) {
  108. options.disableLeftUpdate = true
  109. }
  110. }
  111. func WithDisableLeftQuery[LI any, RI any]() Option {
  112. return func(options *Options) {
  113. options.disableLeftQuery = true
  114. }
  115. }
  116. func WithDisableRightUpdate() Option {
  117. return func(options *Options) {
  118. options.disableRightUpdate = true
  119. }
  120. }
  121. func WithDisableRightQuery[LI any, RI any]() Option {
  122. return func(options *Options) {
  123. options.disableRightQuery = true
  124. }
  125. }
  126. func WithDisableLeftWithRightQuery[LI any, RI any]() Option {
  127. return func(options *Options) {
  128. options.disableLeftWithRightQuery = true
  129. }
  130. }
  131. func WithDisableRightWithLeftQuery() Option {
  132. return func(options *Options) {
  133. options.disableRightWithLeftQuery = true
  134. }
  135. }