simple.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package one2many
  2. import (
  3. "git.sxidc.com/go-framework/baize/binding"
  4. "git.sxidc.com/go-framework/baize/binding/request"
  5. "git.sxidc.com/go-framework/baize/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领域相对路径,如/class/students,后面会自动补充,如/class/students/update
  20. LeftDomainPath string
  21. // URL领域相对路径,如/student/classes,后面会自动补充,如/student/classes/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. RightQueryWithLeftQueryParams request.Query
  33. // 可选配置项,通过WithXXX配置
  34. options *Options
  35. }
  36. func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
  37. //options := simple.options
  38. }
  39. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
  40. options := new(Options)
  41. for _, opt := range opts {
  42. opt(options)
  43. }
  44. simple.options = options
  45. simple.bind(binder)
  46. }
  47. type Option func(options *Options)
  48. type Options struct {
  49. // 右实体中指向左实体的字段
  50. rightRelationField string
  51. // 右实体中指向左实体的列名
  52. rightRelationColumn string
  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 WithRightRelationField(rightRelationField string) Option {
  69. return func(options *Options) {
  70. options.rightRelationField = rightRelationField
  71. }
  72. }
  73. func WithRightRelationColumn(rightRelationColumn string) Option {
  74. return func(options *Options) {
  75. options.rightRelationColumn = rightRelationColumn
  76. }
  77. }
  78. func WithDisableLeft() Option {
  79. return func(options *Options) {
  80. options.disableLeft = true
  81. }
  82. }
  83. func WithDisableRight() Option {
  84. return func(options *Options) {
  85. options.disableRight = true
  86. }
  87. }
  88. func WithDisableLeftUpdate() Option {
  89. return func(options *Options) {
  90. options.disableLeftUpdate = true
  91. }
  92. }
  93. func WithDisableLeftQuery() Option {
  94. return func(options *Options) {
  95. options.disableLeftQuery = true
  96. }
  97. }
  98. func WithDisableRightUpdate() Option {
  99. return func(options *Options) {
  100. options.disableRightUpdate = true
  101. }
  102. }
  103. func WithDisableRightQuery() Option {
  104. return func(options *Options) {
  105. options.disableRightQuery = true
  106. }
  107. }
  108. func WithDisableRightWithLeftQuery() Option {
  109. return func(options *Options) {
  110. options.disableRightWithLeftQuery = true
  111. }
  112. }