one2one.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package one2one
  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. "net/http"
  7. )
  8. func BindSimple(builder *gateway.Builder, params *Simple, opts ...Option) {
  9. options := new(Options)
  10. for _, opt := range opts {
  11. opt(options)
  12. }
  13. params.options = options
  14. params.bind(builder)
  15. }
  16. type Simple struct {
  17. // 除去后缀的服务URL,如http://localhost:8080/example/api/v1
  18. ServiceVersionedUrl string
  19. // 左领域名称
  20. LeftDomainCamelName string
  21. // 右领域名称
  22. RightDomainCamelName string
  23. // 可选配置项,通过WithXXX配置
  24. options *Options
  25. }
  26. func (simple *Simple) bind(builder *gateway.Builder) {
  27. options := simple.options
  28. leftDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.LeftDomainCamelName))
  29. rightDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.RightDomainCamelName))
  30. if !options.disableLeft {
  31. if !options.disableLeftUpdate {
  32. // 左到右更新
  33. builder.
  34. Url(http.MethodPost, leftDomainPath+rightDomainPath+"/update").
  35. Post(&gateway.PostRequest{
  36. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/update",
  37. }).
  38. Build()
  39. }
  40. if !options.disableLeftQuery {
  41. // 左到右查询
  42. builder.
  43. Url(http.MethodGet, leftDomainPath+rightDomainPath+"/query").
  44. Get(&gateway.GetRequest{
  45. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/query",
  46. }).
  47. Build()
  48. }
  49. if !options.disableLeftWithRightQuery {
  50. // 左到右查询,携带右方信息
  51. builder.
  52. Url(http.MethodGet, leftDomainPath+rightDomainPath+"/queryWith").
  53. Get(&gateway.GetRequest{
  54. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/queryWith",
  55. QueryParamsFormFunc: gateway.FormQueryParamsWithTenantIDAndUserIDFunc("tenantId", ""),
  56. }).
  57. Build()
  58. }
  59. }
  60. if !options.disableRight {
  61. if !options.disableRightUpdate {
  62. // 右到左更新
  63. builder.
  64. Url(http.MethodPost, rightDomainPath+leftDomainPath+"/update").
  65. Post(&gateway.PostRequest{
  66. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/update",
  67. }).
  68. Build()
  69. }
  70. if !options.disableRightQuery {
  71. // 右到左查询
  72. builder.
  73. Url(http.MethodGet, rightDomainPath+leftDomainPath+"/query").
  74. Get(&gateway.GetRequest{
  75. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/query",
  76. }).
  77. Build()
  78. }
  79. if !options.disableRightWithLeftQuery {
  80. // 右到左查询,携带左方信息
  81. builder.
  82. Url(http.MethodGet, rightDomainPath+leftDomainPath+"/queryWith").
  83. Get(&gateway.GetRequest{
  84. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/queryWith",
  85. QueryParamsFormFunc: gateway.FormQueryParamsWithTenantIDAndUserIDFunc("tenantId", ""),
  86. }).
  87. Build()
  88. }
  89. }
  90. }
  91. type Option func(options *Options)
  92. type Options struct {
  93. // 关闭左侧到右侧关联
  94. disableLeft bool
  95. // 关闭右侧到左侧关联
  96. disableRight bool
  97. // 关闭左侧更新
  98. disableLeftUpdate bool
  99. // 关闭左侧查询
  100. disableLeftQuery bool
  101. // 关闭右侧更新
  102. disableRightUpdate bool
  103. // 关闭右侧查询
  104. disableRightQuery bool
  105. // 关闭左实体带右实体信息查询
  106. disableLeftWithRightQuery 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 WithDisableLeftWithRightQuery() Option {
  141. return func(options *Options) {
  142. options.disableLeftWithRightQuery = true
  143. }
  144. }
  145. func WithDisableRightWithLeftQuery() Option {
  146. return func(options *Options) {
  147. options.disableRightWithLeftQuery = true
  148. }
  149. }