simple.go 8.0 KB

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