simple.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.Left)
  40. rightTableName := domain.TableName(simple.Schema, simple.Right)
  41. rightRelationColumnName := domain.RelationColumnName(simple.Right)
  42. if !options.disableLeft {
  43. if !options.disableLeftUpdate {
  44. leftUpdateMiddlewares := append(options.globalMiddlewares, options.leftUpdateMiddlewares...)
  45. // 左到右更新
  46. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  47. Path: leftDomainPath + rightDomainPath + "/update",
  48. SendResponseFunc: response.SendMsgResponse,
  49. RequestParams: simple.LeftUpdateJsonBody,
  50. Objects: []domain.Object{simple.Left},
  51. ServiceFunc: Update(middleTableName,
  52. leftTableName, simple.Left.DomainCNName(), leftRelationFieldName, leftRelationColumnName,
  53. rightTableName, rightRelationColumnName),
  54. }, leftUpdateMiddlewares...)
  55. }
  56. if !options.disableLeftQuery {
  57. leftQueryMiddlewares := append(options.globalMiddlewares, options.leftQueryMiddlewares...)
  58. // 左到右查询
  59. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[RI]]{
  60. Path: leftDomainPath + rightDomainPath + "/query",
  61. SendResponseFunc: response.SendInfosResponse[RI],
  62. RequestParams: simple.LeftQueryQueryParams,
  63. Objects: []domain.Object{simple.Left},
  64. ServiceFunc: Query[RI](middleTableName,
  65. leftTableName, leftRelationColumnName,
  66. rightTableName, rightRelationColumnName),
  67. }, leftQueryMiddlewares...)
  68. }
  69. }
  70. if !options.disableRight {
  71. if !options.disableRightUpdate {
  72. rightUpdateMiddlewares := append(options.globalMiddlewares, options.rightUpdateMiddlewares...)
  73. // 右到左更新
  74. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  75. Path: rightDomainPath + leftDomainPath + "/update",
  76. SendResponseFunc: response.SendMsgResponse,
  77. RequestParams: simple.RightUpdateJsonBody,
  78. Objects: []domain.Object{simple.Right},
  79. ServiceFunc: Update(middleTableName,
  80. rightTableName, simple.Right.DomainCNName(), rightRelationFieldName, rightRelationColumnName,
  81. leftTableName, leftRelationColumnName),
  82. }, rightUpdateMiddlewares...)
  83. }
  84. if !options.disableRightQuery {
  85. rightQueryMiddlewares := append(options.globalMiddlewares, options.rightQueryMiddlewares...)
  86. // 右到左查询
  87. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[LI]]{
  88. Path: rightDomainPath + leftDomainPath + "/query",
  89. SendResponseFunc: response.SendInfosResponse[LI],
  90. RequestParams: simple.RightQueryQueryParams,
  91. Objects: []domain.Object{simple.Right},
  92. ServiceFunc: Query[LI](middleTableName,
  93. rightTableName, rightRelationColumnName,
  94. leftTableName, leftRelationColumnName),
  95. }, rightQueryMiddlewares...)
  96. }
  97. }
  98. }
  99. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
  100. options := new(Options)
  101. for _, opt := range opts {
  102. opt(options)
  103. }
  104. simple.options = options
  105. simple.bind(binder)
  106. }
  107. type Option func(options *Options)
  108. type Options struct {
  109. // 关闭左侧到右侧关联
  110. disableLeft bool
  111. // 关闭右侧到左侧关联
  112. disableRight bool
  113. // 关闭左侧更新
  114. disableLeftUpdate bool
  115. // 关闭左侧查询
  116. disableLeftQuery bool
  117. // 关闭右侧更新
  118. disableRightUpdate bool
  119. // 关闭右侧查询
  120. disableRightQuery bool
  121. // 全局中间件
  122. globalMiddlewares []binding.Middleware
  123. // 左侧更新中间件
  124. leftUpdateMiddlewares []binding.Middleware
  125. // 左侧查询中间件
  126. leftQueryMiddlewares []binding.Middleware
  127. // 右侧更新中间件
  128. rightUpdateMiddlewares []binding.Middleware
  129. // 右侧查询中间件
  130. rightQueryMiddlewares []binding.Middleware
  131. }
  132. func WithGlobalMiddlewares(middlewares ...binding.Middleware) Option {
  133. return func(options *Options) {
  134. options.globalMiddlewares = middlewares
  135. }
  136. }
  137. func WithDisableLeft() Option {
  138. return func(options *Options) {
  139. options.disableLeft = true
  140. }
  141. }
  142. func WithDisableRight() Option {
  143. return func(options *Options) {
  144. options.disableRight = true
  145. }
  146. }
  147. func WithDisableLeftUpdate() Option {
  148. return func(options *Options) {
  149. options.disableLeftUpdate = true
  150. }
  151. }
  152. func WithDisableLeftQuery() Option {
  153. return func(options *Options) {
  154. options.disableLeftQuery = true
  155. }
  156. }
  157. func WithDisableRightUpdate() Option {
  158. return func(options *Options) {
  159. options.disableRightUpdate = true
  160. }
  161. }
  162. func WithDisableRightQuery() Option {
  163. return func(options *Options) {
  164. options.disableRightQuery = true
  165. }
  166. }
  167. func WithLeftUpdateMiddlewares(middlewares ...binding.Middleware) Option {
  168. return func(options *Options) {
  169. options.leftUpdateMiddlewares = middlewares
  170. }
  171. }
  172. func WithLeftQueryMiddlewares(middlewares ...binding.Middleware) Option {
  173. return func(options *Options) {
  174. options.leftQueryMiddlewares = middlewares
  175. }
  176. }
  177. func WithRightUpdateMiddlewares(middlewares ...binding.Middleware) Option {
  178. return func(options *Options) {
  179. options.rightUpdateMiddlewares = middlewares
  180. }
  181. }
  182. func WithRightQueryMiddlewares(middlewares ...binding.Middleware) Option {
  183. return func(options *Options) {
  184. options.rightQueryMiddlewares = middlewares
  185. }
  186. }