simple.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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, options.leftQueryOrderBy),
  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. // 左侧查询OrderBy
  137. leftQueryOrderBy string
  138. // 右侧更新中间件
  139. rightUpdateMiddlewares []binding.Middleware
  140. // 右侧查询中间件
  141. rightQueryMiddlewares []binding.Middleware
  142. // 右实体带左实体查询中间件
  143. rightWithLeftQueryMiddlewares []binding.Middleware
  144. }
  145. func WithGlobalMiddlewares(middlewares ...binding.Middleware) Option {
  146. return func(options *Options) {
  147. options.globalMiddlewares = middlewares
  148. }
  149. }
  150. func WithDisableLeft() Option {
  151. return func(options *Options) {
  152. options.disableLeft = true
  153. }
  154. }
  155. func WithDisableRight() Option {
  156. return func(options *Options) {
  157. options.disableRight = true
  158. }
  159. }
  160. func WithDisableLeftUpdate() Option {
  161. return func(options *Options) {
  162. options.disableLeftUpdate = true
  163. }
  164. }
  165. func WithDisableLeftQuery() Option {
  166. return func(options *Options) {
  167. options.disableLeftQuery = true
  168. }
  169. }
  170. func WithLeftQueryOrderBy(orderBy string) Option {
  171. return func(options *Options) {
  172. options.leftQueryOrderBy = orderBy
  173. }
  174. }
  175. func WithDisableRightUpdate() Option {
  176. return func(options *Options) {
  177. options.disableRightUpdate = true
  178. }
  179. }
  180. func WithDisableRightQuery() Option {
  181. return func(options *Options) {
  182. options.disableRightQuery = true
  183. }
  184. }
  185. func WithDisableRightWithLeftQuery() Option {
  186. return func(options *Options) {
  187. options.disableRightWithLeftQuery = true
  188. }
  189. }
  190. func WithRightQueryWithConditionFieldCallback(callbacks ConditionFieldCallback) Option {
  191. return func(options *Options) {
  192. options.rightQueryWithConditionFieldCallback = callbacks
  193. }
  194. }
  195. func WithLeftUpdateMiddlewares(middlewares ...binding.Middleware) Option {
  196. return func(options *Options) {
  197. options.leftUpdateMiddlewares = middlewares
  198. }
  199. }
  200. func WithLeftQueryMiddlewares(middlewares ...binding.Middleware) Option {
  201. return func(options *Options) {
  202. options.leftQueryMiddlewares = middlewares
  203. }
  204. }
  205. func WithRightUpdateMiddlewares(middlewares ...binding.Middleware) Option {
  206. return func(options *Options) {
  207. options.rightUpdateMiddlewares = middlewares
  208. }
  209. }
  210. func WithRightQueryMiddlewares(middlewares ...binding.Middleware) Option {
  211. return func(options *Options) {
  212. options.rightQueryMiddlewares = middlewares
  213. }
  214. }
  215. func WithRightWithLeftQueryMiddlewares(middlewares ...binding.Middleware) Option {
  216. return func(options *Options) {
  217. options.rightWithLeftQueryMiddlewares = middlewares
  218. }
  219. }