simple.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package many2many
  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/convenient/binding/response"
  7. "git.sxidc.com/go-framework/baize/framwork/domain"
  8. "git.sxidc.com/go-framework/baize/framwork/domain/entity"
  9. "github.com/iancoleman/strcase"
  10. )
  11. // Simple 关联的Bind参数
  12. // LI 为左边实体的Info类型
  13. // RI 为右边实体的Info类型
  14. type Simple[LI any, RI any] struct {
  15. // 左领域实体,注意是Entity类型
  16. Left entity.Entity
  17. // 右领域实体,注意是Entity类型
  18. Right entity.Entity
  19. // 更新左实体关联使用的请求参数
  20. LeftUpdateJsonBody request.Params
  21. // 查询左实体关联使用的请求参数,注意是Query类型
  22. LeftQueryQueryParams request.Query
  23. // 更新右实体关联使用的请求参数
  24. RightUpdateJsonBody request.Params
  25. // 查询右实体关联使用的请求参数,注意是Query类型
  26. RightQueryQueryParams request.Query
  27. // 可选配置项,通过WithXXX配置
  28. options *Options
  29. }
  30. func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
  31. options := simple.options
  32. leftDomainPath := entity.RelativeDomainPath(simple.Left)
  33. rightDomainPath := entity.RelativeDomainPath(simple.Right)
  34. leftRelationFieldName := fmt.Sprintf("%sIDs", simple.Right.DomainCamelName())
  35. rightRelationFieldName := fmt.Sprintf("%sIDs", simple.Left.DomainCamelName())
  36. middleTableName := entity.SnakeDomainName(simple.Left) + "_and_" + entity.SnakeDomainName(simple.Right)
  37. leftTableName := entity.TableName(simple.Left)
  38. leftRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(simple.Right.DomainCamelName()))
  39. rightTableName := entity.TableName(simple.Right)
  40. rightRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(simple.Left.DomainCamelName()))
  41. if !options.disableLeft {
  42. if !options.disableLeftUpdate {
  43. // 左到右更新
  44. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  45. Path: leftDomainPath + rightDomainPath + "/update",
  46. ResponseFunc: response.SendMsgResponse,
  47. RequestParams: simple.LeftUpdateJsonBody,
  48. Objects: []domain.Object{simple.Left},
  49. ServiceFunc: Update(),
  50. })
  51. }
  52. if !options.disableLeftQuery {
  53. // 左到右查询
  54. binding.GetBind(binder, &binding.SimpleBindItem[RI]{
  55. Path: leftDomainPath + rightDomainPath + "/query",
  56. ResponseFunc: response.SendInfoResponse[RI],
  57. RequestParams: simple.LeftQueryQueryParams,
  58. Objects: []domain.Object{simple.Left},
  59. ServiceFunc: Query[RI](),
  60. })
  61. }
  62. }
  63. if !options.disableRight {
  64. if !options.disableRightUpdate {
  65. // 右到左更新
  66. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  67. Path: rightDomainPath + leftDomainPath + "/update",
  68. ResponseFunc: response.SendMsgResponse,
  69. RequestParams: simple.RightUpdateJsonBody,
  70. Objects: []domain.Object{simple.Right},
  71. ServiceFunc: Update(),
  72. })
  73. }
  74. if !options.disableRightQuery {
  75. // 右到左查询
  76. binding.GetBind(binder, &binding.SimpleBindItem[RI]{
  77. Path: rightDomainPath + leftDomainPath + "/query",
  78. ResponseFunc: response.SendInfoResponse[RI],
  79. RequestParams: simple.RightQueryQueryParams,
  80. Objects: []domain.Object{simple.Right},
  81. ServiceFunc: Query[RI](),
  82. })
  83. }
  84. }
  85. }
  86. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
  87. options := new(Options)
  88. for _, opt := range opts {
  89. opt(options)
  90. }
  91. simple.options = options
  92. simple.bind(binder)
  93. }
  94. type Option func(options *Options)
  95. type Options struct {
  96. // 关闭左侧到右侧关联
  97. disableLeft bool
  98. // 关闭右侧到左侧关联
  99. disableRight bool
  100. // 关闭左侧更新
  101. disableLeftUpdate bool
  102. // 关闭左侧查询
  103. disableLeftQuery bool
  104. // 关闭右侧更新
  105. disableRightUpdate bool
  106. // 关闭右侧查询
  107. disableRightQuery bool
  108. }
  109. func WithDisableLeft() Option {
  110. return func(options *Options) {
  111. options.disableLeft = true
  112. }
  113. }
  114. func WithDisableRight() Option {
  115. return func(options *Options) {
  116. options.disableRight = true
  117. }
  118. }
  119. func WithDisableLeftUpdate() Option {
  120. return func(options *Options) {
  121. options.disableLeftUpdate = true
  122. }
  123. }
  124. func WithDisableLeftQuery() Option {
  125. return func(options *Options) {
  126. options.disableLeftQuery = true
  127. }
  128. }
  129. func WithDisableRightUpdate() Option {
  130. return func(options *Options) {
  131. options.disableRightUpdate = true
  132. }
  133. }
  134. func WithDisableRightQuery() Option {
  135. return func(options *Options) {
  136. options.disableRightQuery = true
  137. }
  138. }