simple.go 5.1 KB

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