simple.go 7.4 KB

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