simple.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package one2one
  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领域相对路径,如/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. // 可选配置项,通过WithXXX配置
  32. options *Options[LI, RI]
  33. }
  34. func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
  35. //options := simple.options
  36. }
  37. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option[LI, RI]) {
  38. options := new(Options[LI, RI])
  39. for _, opt := range opts {
  40. opt(options)
  41. }
  42. simple.options = options
  43. simple.bind(binder)
  44. }
  45. type Option[LI any, RI any] func(options *Options[LI, RI])
  46. type Options[LI any, RI any] struct {
  47. // 左实体中指向右实体的字段
  48. leftRelationField string
  49. // 右实体中指向左实体的字段
  50. rightRelationField string
  51. // 左实体中指向右实体的列名
  52. leftRelationColumn string
  53. // 右实体中指向左实体的列名
  54. rightRelationColumn string
  55. // 关闭左侧到右侧关联
  56. disableLeft bool
  57. // 关闭右侧到左侧关联
  58. disableRight bool
  59. // 关闭左侧更新
  60. disableLeftUpdate bool
  61. // 关闭左侧查询
  62. disableLeftQuery bool
  63. // 关闭右侧更新
  64. disableRightUpdate bool
  65. // 关闭右侧查询
  66. disableRightQuery bool
  67. }
  68. func WithLeftRelationField[LI any, RI any](leftRelationField string) Option[LI, RI] {
  69. return func(options *Options[LI, RI]) {
  70. options.leftRelationField = leftRelationField
  71. }
  72. }
  73. func WithRightRelationField[LI any, RI any](rightRelationField string) Option[LI, RI] {
  74. return func(options *Options[LI, RI]) {
  75. options.rightRelationField = rightRelationField
  76. }
  77. }
  78. func WithLeftRelationColumn[LI any, RI any](leftRelationColumn string) Option[LI, RI] {
  79. return func(options *Options[LI, RI]) {
  80. options.leftRelationColumn = leftRelationColumn
  81. }
  82. }
  83. func WithRightRelationColumn[LI any, RI any](rightRelationColumn string) Option[LI, RI] {
  84. return func(options *Options[LI, RI]) {
  85. options.rightRelationColumn = rightRelationColumn
  86. }
  87. }
  88. func WithDisableLeft[LI any, RI any]() Option[LI, RI] {
  89. return func(options *Options[LI, RI]) {
  90. options.disableLeft = true
  91. }
  92. }
  93. func WithDisableRight[LI any, RI any]() Option[LI, RI] {
  94. return func(options *Options[LI, RI]) {
  95. options.disableRight = true
  96. }
  97. }
  98. func WithDisableLeftUpdate[LI any, RI any]() Option[LI, RI] {
  99. return func(options *Options[LI, RI]) {
  100. options.disableLeftUpdate = true
  101. }
  102. }
  103. func WithDisableLeftQuery[LI any, RI any]() Option[LI, RI] {
  104. return func(options *Options[LI, RI]) {
  105. options.disableLeftQuery = true
  106. }
  107. }
  108. func WithDisableRightUpdate[LI any, RI any]() Option[LI, RI] {
  109. return func(options *Options[LI, RI]) {
  110. options.disableRightUpdate = true
  111. }
  112. }
  113. func WithDisableRightQuery[LI any, RI any]() Option[LI, RI] {
  114. return func(options *Options[LI, RI]) {
  115. options.disableRightQuery = true
  116. }
  117. }