simple.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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, options.leftQueryOrderBy),
  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, options.rightQueryOrderBy),
  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. // 左侧查询OrderBy
  128. leftQueryOrderBy string
  129. // 右侧更新中间件
  130. rightUpdateMiddlewares []binding.Middleware
  131. // 右侧查询中间件
  132. rightQueryMiddlewares []binding.Middleware
  133. // 右侧查询OrderBy
  134. rightQueryOrderBy string
  135. }
  136. func WithGlobalMiddlewares(middlewares ...binding.Middleware) Option {
  137. return func(options *Options) {
  138. options.globalMiddlewares = middlewares
  139. }
  140. }
  141. func WithDisableLeft() Option {
  142. return func(options *Options) {
  143. options.disableLeft = true
  144. }
  145. }
  146. func WithDisableRight() Option {
  147. return func(options *Options) {
  148. options.disableRight = true
  149. }
  150. }
  151. func WithDisableLeftUpdate() Option {
  152. return func(options *Options) {
  153. options.disableLeftUpdate = true
  154. }
  155. }
  156. func WithDisableLeftQuery() Option {
  157. return func(options *Options) {
  158. options.disableLeftQuery = true
  159. }
  160. }
  161. func WithDisableRightUpdate() Option {
  162. return func(options *Options) {
  163. options.disableRightUpdate = true
  164. }
  165. }
  166. func WithDisableRightQuery() Option {
  167. return func(options *Options) {
  168. options.disableRightQuery = true
  169. }
  170. }
  171. func WithLeftUpdateMiddlewares(middlewares ...binding.Middleware) Option {
  172. return func(options *Options) {
  173. options.leftUpdateMiddlewares = middlewares
  174. }
  175. }
  176. func WithLeftQueryMiddlewares(middlewares ...binding.Middleware) Option {
  177. return func(options *Options) {
  178. options.leftQueryMiddlewares = middlewares
  179. }
  180. }
  181. func WithLeftQueryOrderBy(orderBy string) Option {
  182. return func(options *Options) {
  183. options.leftQueryOrderBy = orderBy
  184. }
  185. }
  186. func WithRightUpdateMiddlewares(middlewares ...binding.Middleware) Option {
  187. return func(options *Options) {
  188. options.rightUpdateMiddlewares = middlewares
  189. }
  190. }
  191. func WithRightQueryMiddlewares(middlewares ...binding.Middleware) Option {
  192. return func(options *Options) {
  193. options.rightQueryMiddlewares = middlewares
  194. }
  195. }
  196. func WithRightQueryOrderBy(orderBy string) Option {
  197. return func(options *Options) {
  198. options.rightQueryOrderBy = orderBy
  199. }
  200. }