one2many.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package one2many
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/gateway"
  4. "git.sxidc.com/go-tools/utils/template"
  5. "github.com/iancoleman/strcase"
  6. )
  7. func BindSimple(builder *gateway.Builder, params *Simple, opts ...Option) {
  8. options := new(Options)
  9. for _, opt := range opts {
  10. opt(options)
  11. }
  12. params.options = options
  13. params.bind(builder)
  14. }
  15. type Simple struct {
  16. // 除去后缀的服务URL,如http://localhost:8080/example/api/v1
  17. ServiceVersionedUrl string
  18. // 左领域名称
  19. LeftDomainCamelName string
  20. // 右领域名称
  21. RightDomainCamelName string
  22. // 可选配置项,通过WithXXX配置
  23. options *Options
  24. }
  25. func (simple *Simple) bind(builder *gateway.Builder) {
  26. options := simple.options
  27. leftDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.LeftDomainCamelName))
  28. rightDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.RightDomainCamelName))
  29. if !options.disableLeft {
  30. if !options.disableLeftUpdate {
  31. // 左到右更新
  32. builder.
  33. PostRoute(leftDomainPath+rightDomainPath+"/update",
  34. func(requestBuilder *gateway.RequestBuilder) {
  35. requestBuilder.
  36. Post(&gateway.PostRequest{
  37. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/update",
  38. }).
  39. Request()
  40. })
  41. }
  42. if !options.disableLeftQuery {
  43. // 左到右查询
  44. builder.
  45. GetRoute(leftDomainPath+rightDomainPath+"/query",
  46. func(requestBuilder *gateway.RequestBuilder) {
  47. requestBuilder.
  48. Get(&gateway.GetRequest{
  49. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/query",
  50. }).
  51. Request()
  52. })
  53. }
  54. }
  55. if !options.disableRight {
  56. if !options.disableRightUpdate {
  57. // 右到左更新
  58. builder.
  59. PostRoute(rightDomainPath+leftDomainPath+"/update",
  60. func(requestBuilder *gateway.RequestBuilder) {
  61. requestBuilder.
  62. Post(&gateway.PostRequest{
  63. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/update",
  64. }).
  65. Request()
  66. })
  67. }
  68. if !options.disableRightQuery {
  69. builder.
  70. GetRoute(rightDomainPath+leftDomainPath+"/query",
  71. func(requestBuilder *gateway.RequestBuilder) {
  72. requestBuilder.
  73. Get(&gateway.GetRequest{
  74. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/query",
  75. }).
  76. Request()
  77. })
  78. }
  79. if !options.disableRightWithLeftQuery {
  80. // 右到左查询,携带左方信息
  81. builder.
  82. GetRouteWithTenantID(rightDomainPath+leftDomainPath+"/queryWith",
  83. func(requestBuilder *gateway.RequestBuilder) {
  84. requestBuilder.
  85. Get(&gateway.GetRequest{
  86. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/queryWith",
  87. }).
  88. Request()
  89. })
  90. }
  91. }
  92. }
  93. type Option func(options *Options)
  94. type Options struct {
  95. // 关闭左侧到右侧关联
  96. disableLeft bool
  97. // 关闭右侧到左侧关联
  98. disableRight bool
  99. // 关闭左侧更新
  100. disableLeftUpdate bool
  101. // 关闭左侧查询
  102. disableLeftQuery bool
  103. // 关闭右侧更新
  104. disableRightUpdate bool
  105. // 关闭右侧查询
  106. disableRightQuery bool
  107. // 关闭右实体带左实体信息查询
  108. disableRightWithLeftQuery bool
  109. }
  110. func WithDisableLeft() Option {
  111. return func(options *Options) {
  112. options.disableLeft = true
  113. }
  114. }
  115. func WithDisableRight() Option {
  116. return func(options *Options) {
  117. options.disableRight = true
  118. }
  119. }
  120. func WithDisableLeftUpdate() Option {
  121. return func(options *Options) {
  122. options.disableLeftUpdate = true
  123. }
  124. }
  125. func WithDisableLeftQuery() Option {
  126. return func(options *Options) {
  127. options.disableLeftQuery = true
  128. }
  129. }
  130. func WithDisableRightUpdate() Option {
  131. return func(options *Options) {
  132. options.disableRightUpdate = true
  133. }
  134. }
  135. func WithDisableRightQuery() Option {
  136. return func(options *Options) {
  137. options.disableRightQuery = true
  138. }
  139. }
  140. func WithDisableRightWithLeftQuery() Option {
  141. return func(options *Options) {
  142. options.disableRightWithLeftQuery = true
  143. }
  144. }