simple.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package one2many
  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. // 查询左实体关联使用的请求参数,注意是Query类型
  23. LeftQueryQueryParams request.QueryWithIDRequestParams
  24. // 更新左实体关联使用的请求参数
  25. RightUpdateJsonBody request.Params
  26. // 查询右实体关联使用的请求参数,注意是QueryWithID类型
  27. RightQueryQueryParams request.QueryWithIDRequestParams
  28. // 查询右实体带左实体信息使用的请求参数,注意是Query类型
  29. RightQueryWithLeftQueryParams request.QueryRequestParams
  30. // 可选配置项,通过WithXXX配置
  31. options *Options
  32. }
  33. func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
  34. options := simple.options
  35. leftDomainPath := domain.RelativeDomainPath(simple.Left)
  36. rightDomainPath := domain.RelativeDomainPath(simple.Right)
  37. leftRelationFieldName := fmt.Sprintf("%sIDs", simple.Right.DomainCamelName())
  38. rightRelationFieldName := fmt.Sprintf("%sID", simple.Left.DomainCamelName())
  39. leftTableName := domain.TableName(simple.Schema, simple.Left)
  40. leftRelationColumnName := domain.RelationColumnName(simple.Left)
  41. rightTableName := domain.TableName(simple.Schema, 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: UpdateLeft(leftTableName, simple.Left.DomainCNName(), leftRelationFieldName, leftRelationColumnName,
  52. rightTableName, simple.Right.DomainCNName()),
  53. }, leftUpdateMiddlewares...)
  54. }
  55. if !options.disableLeftQuery {
  56. leftQueryMiddlewares := append(options.globalMiddlewares, options.leftQueryMiddlewares...)
  57. // 左到右查询
  58. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[RI]]{
  59. Path: leftDomainPath + rightDomainPath + "/query",
  60. SendResponseFunc: response.SendInfosResponse[RI],
  61. RequestParams: simple.LeftQueryQueryParams,
  62. Objects: []domain.Object{simple.Left},
  63. ServiceFunc: QueryLeft[RI](leftTableName, leftRelationColumnName, rightTableName),
  64. }, leftQueryMiddlewares...)
  65. }
  66. }
  67. if !options.disableRight {
  68. if !options.disableRightUpdate {
  69. rightUpdateMiddlewares := append(options.globalMiddlewares, options.rightUpdateMiddlewares...)
  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: UpdateRight(rightTableName, rightRelationFieldName,
  77. leftTableName, simple.Left.DomainCNName(), leftRelationColumnName),
  78. }, rightUpdateMiddlewares...)
  79. }
  80. if !options.disableRightQuery {
  81. rightQueryMiddlewares := append(options.globalMiddlewares, options.rightQueryMiddlewares...)
  82. // 右到左查询
  83. binding.GetBind(binder, &binding.SimpleBindItem[LI]{
  84. Path: rightDomainPath + leftDomainPath + "/query",
  85. SendResponseFunc: response.SendInfoResponse[LI],
  86. RequestParams: simple.RightQueryQueryParams,
  87. Objects: []domain.Object{simple.Right},
  88. ServiceFunc: QueryRight[LI](rightTableName, rightRelationFieldName, leftTableName),
  89. }, rightQueryMiddlewares...)
  90. }
  91. if !options.disableRightWithLeftQuery {
  92. rightWithLeftQueryMiddlewares := append(options.globalMiddlewares, options.rightWithLeftQueryMiddlewares...)
  93. // 右到左查询,携带左方信息
  94. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[map[string]any]]{
  95. Path: rightDomainPath + leftDomainPath + "/queryWith",
  96. SendResponseFunc: response.SendInfosResponse[map[string]any],
  97. RequestParams: simple.RightQueryWithLeftQueryParams,
  98. Objects: []domain.Object{simple.Right},
  99. ServiceFunc: QueryRightWithLeftInfo[RI, LI](rightTableName, options.rightQueryWithConditionFieldCallback, leftTableName, leftRelationColumnName),
  100. }, rightWithLeftQueryMiddlewares...)
  101. }
  102. }
  103. }
  104. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
  105. options := new(Options)
  106. for _, opt := range opts {
  107. opt(options)
  108. }
  109. simple.options = options
  110. simple.bind(binder)
  111. }
  112. type Option func(options *Options)
  113. type Options struct {
  114. // 关闭左侧到右侧关联
  115. disableLeft bool
  116. // 关闭右侧到左侧关联
  117. disableRight bool
  118. // 关闭左侧更新
  119. disableLeftUpdate bool
  120. // 关闭左侧查询
  121. disableLeftQuery bool
  122. // 关闭右侧更新
  123. disableRightUpdate bool
  124. // 关闭右侧查询
  125. disableRightQuery bool
  126. // 关闭右实体带左实体信息查询
  127. disableRightWithLeftQuery bool
  128. // 右查询条件构造回调
  129. rightQueryWithConditionFieldCallback ConditionFieldCallback
  130. // 全局中间件
  131. globalMiddlewares []binding.Middleware
  132. // 左侧更新中间件
  133. leftUpdateMiddlewares []binding.Middleware
  134. // 左侧查询中间件
  135. leftQueryMiddlewares []binding.Middleware
  136. // 右侧更新中间件
  137. rightUpdateMiddlewares []binding.Middleware
  138. // 右侧查询中间件
  139. rightQueryMiddlewares []binding.Middleware
  140. // 右实体带左实体查询中间件
  141. rightWithLeftQueryMiddlewares []binding.Middleware
  142. }
  143. func WithGlobalMiddlewares(middlewares ...binding.Middleware) Option {
  144. return func(options *Options) {
  145. options.globalMiddlewares = middlewares
  146. }
  147. }
  148. func WithDisableLeft() Option {
  149. return func(options *Options) {
  150. options.disableLeft = true
  151. }
  152. }
  153. func WithDisableRight() Option {
  154. return func(options *Options) {
  155. options.disableRight = true
  156. }
  157. }
  158. func WithDisableLeftUpdate() Option {
  159. return func(options *Options) {
  160. options.disableLeftUpdate = true
  161. }
  162. }
  163. func WithDisableLeftQuery() Option {
  164. return func(options *Options) {
  165. options.disableLeftQuery = true
  166. }
  167. }
  168. func WithDisableRightUpdate() Option {
  169. return func(options *Options) {
  170. options.disableRightUpdate = true
  171. }
  172. }
  173. func WithDisableRightQuery() Option {
  174. return func(options *Options) {
  175. options.disableRightQuery = true
  176. }
  177. }
  178. func WithDisableRightWithLeftQuery() Option {
  179. return func(options *Options) {
  180. options.disableRightWithLeftQuery = true
  181. }
  182. }
  183. func WithRightQueryWithConditionFieldCallback(callbacks ConditionFieldCallback) Option {
  184. return func(options *Options) {
  185. options.rightQueryWithConditionFieldCallback = callbacks
  186. }
  187. }
  188. func WithLeftUpdateMiddlewares(middlewares ...binding.Middleware) Option {
  189. return func(options *Options) {
  190. options.leftUpdateMiddlewares = middlewares
  191. }
  192. }
  193. func WithLeftQueryMiddlewares(middlewares ...binding.Middleware) Option {
  194. return func(options *Options) {
  195. options.leftQueryMiddlewares = middlewares
  196. }
  197. }
  198. func WithRightUpdateMiddlewares(middlewares ...binding.Middleware) Option {
  199. return func(options *Options) {
  200. options.rightUpdateMiddlewares = middlewares
  201. }
  202. }
  203. func WithRightQueryMiddlewares(middlewares ...binding.Middleware) Option {
  204. return func(options *Options) {
  205. options.rightQueryMiddlewares = middlewares
  206. }
  207. }
  208. func WithRightWithLeftQueryMiddlewares(middlewares ...binding.Middleware) Option {
  209. return func(options *Options) {
  210. options.rightWithLeftQueryMiddlewares = middlewares
  211. }
  212. }