one2one.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package gwtools
  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 One2One(builder *gateway.Builder, params *One2OneParams, opts ...One2OneOption) {
  9. options := new(One2OneOptions)
  10. for _, opt := range opts {
  11. opt(options)
  12. }
  13. params.options = options
  14. params.one2one(builder)
  15. }
  16. type One2OneParams struct {
  17. // 除去后缀的服务URL,如http://localhost:8080/example/api/v1
  18. ServiceVersionedUrl string
  19. // 左领域名称
  20. LeftDomainCamelName string
  21. // 右领域名称
  22. RightDomainCamelName string
  23. // 可选配置项,通过WithXXX配置
  24. options *One2OneOptions
  25. }
  26. func (params *One2OneParams) one2one(builder *gateway.Builder) {
  27. options := params.options
  28. leftDomainPath := "/" + strcase.ToLowerCamel(template.Id(params.LeftDomainCamelName))
  29. rightDomainPath := "/" + strcase.ToLowerCamel(template.Id(params.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: params.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/update",
  37. }, nil).
  38. Build()
  39. }
  40. if !options.disableLeftQuery {
  41. // 左到右查询
  42. builder.
  43. Url(http.MethodGet, leftDomainPath+rightDomainPath+"/query").
  44. Get(&gateway.GetRequest{
  45. Url: params.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/query",
  46. }, nil).
  47. Build()
  48. }
  49. if !options.disableLeftWithRightQuery {
  50. // 左到右查询,携带右方信息
  51. builder.
  52. Url(http.MethodGet, leftDomainPath+rightDomainPath+"/queryWith").
  53. Get(&gateway.GetRequest{
  54. Url: params.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/queryWith",
  55. QueryParams: AddQueryParamsTenantIDAndUserID("tenantId", "",
  56. params.options.leftWithRightQueryGetTenantIDFunc, nil),
  57. }, nil).
  58. Build()
  59. }
  60. }
  61. if !options.disableRight {
  62. if !options.disableRightUpdate {
  63. // 右到左更新
  64. builder.
  65. Url(http.MethodPost, rightDomainPath+leftDomainPath+"/update").
  66. Post(&gateway.PostRequest{
  67. Url: params.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/update",
  68. }, nil).
  69. Build()
  70. }
  71. if !options.disableRightQuery {
  72. // 右到左查询
  73. builder.
  74. Url(http.MethodGet, rightDomainPath+leftDomainPath+"/query").
  75. Get(&gateway.GetRequest{
  76. Url: params.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/query",
  77. }, nil).
  78. Build()
  79. }
  80. if !options.disableRightWithLeftQuery {
  81. // 右到左查询,携带左方信息
  82. builder.
  83. Url(http.MethodGet, rightDomainPath+leftDomainPath+"/queryWith").
  84. Get(&gateway.GetRequest{
  85. Url: params.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/queryWith",
  86. QueryParams: AddQueryParamsTenantIDAndUserID("tenantId", "",
  87. params.options.rightWithLeftQueryGetTenantIDFunc, nil),
  88. }, nil).
  89. Build()
  90. }
  91. }
  92. }
  93. type One2OneOption func(options *One2OneOptions)
  94. type One2OneOptions 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. disableLeftWithRightQuery bool
  109. // 关闭右实体带左实体信息查询
  110. disableRightWithLeftQuery bool
  111. // 获取租户ID的接口
  112. leftWithRightQueryGetTenantIDFunc GetTenantInfoFunc
  113. // 获取租户ID的接口
  114. rightWithLeftQueryGetTenantIDFunc GetTenantInfoFunc
  115. }
  116. func WithOne2OneDisableLeft() One2OneOption {
  117. return func(options *One2OneOptions) {
  118. options.disableLeft = true
  119. }
  120. }
  121. func WithOne2OneDisableRight() One2OneOption {
  122. return func(options *One2OneOptions) {
  123. options.disableRight = true
  124. }
  125. }
  126. func WithOne2OneDisableLeftUpdate() One2OneOption {
  127. return func(options *One2OneOptions) {
  128. options.disableLeftUpdate = true
  129. }
  130. }
  131. func WithOne2OneDisableLeftQuery() One2OneOption {
  132. return func(options *One2OneOptions) {
  133. options.disableLeftQuery = true
  134. }
  135. }
  136. func WithOne2OneDisableRightUpdate() One2OneOption {
  137. return func(options *One2OneOptions) {
  138. options.disableRightUpdate = true
  139. }
  140. }
  141. func WithOne2OneDisableRightQuery() One2OneOption {
  142. return func(options *One2OneOptions) {
  143. options.disableRightQuery = true
  144. }
  145. }
  146. func WithOne2OneDisableLeftWithRightQuery() One2OneOption {
  147. return func(options *One2OneOptions) {
  148. options.disableLeftWithRightQuery = true
  149. }
  150. }
  151. func WithOne2OneDisableRightWithLeftQuery() One2OneOption {
  152. return func(options *One2OneOptions) {
  153. options.disableRightWithLeftQuery = true
  154. }
  155. }
  156. func WithOne2OneLeftWithRightQueryGetTenantIDFunc(getTenantIDFunc GetTenantInfoFunc) One2OneOption {
  157. return func(options *One2OneOptions) {
  158. options.leftWithRightQueryGetTenantIDFunc = getTenantIDFunc
  159. }
  160. }
  161. func WithOne2OneRightWithLeftQueryGetTenantIDFunc(getTenantIDFunc GetTenantInfoFunc) One2OneOption {
  162. return func(options *One2OneOptions) {
  163. options.rightWithLeftQueryGetTenantIDFunc = getTenantIDFunc
  164. }
  165. }