one2one.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package gwtools
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  4. "git.sxidc.com/go-framework/baize/framework/gateway"
  5. "git.sxidc.com/go-tools/utils/template"
  6. "github.com/iancoleman/strcase"
  7. "net/http"
  8. )
  9. func One2One(builder *gateway.Builder, params *One2OneParams, opts ...One2OneOption) {
  10. options := new(One2OneOptions)
  11. for _, opt := range opts {
  12. opt(options)
  13. }
  14. params.options = options
  15. params.one2one(builder)
  16. }
  17. type One2OneParams struct {
  18. // 除去后缀的服务URL,如http://localhost:8080/example/api/v1
  19. ServiceVersionedUrl string
  20. // 左领域名称
  21. LeftDomainCamelName string
  22. // 右领域名称
  23. RightDomainCamelName string
  24. // 可选配置项,通过WithXXX配置
  25. options *One2OneOptions
  26. }
  27. func (params *One2OneParams) one2one(builder *gateway.Builder) {
  28. options := params.options
  29. leftDomainPath := "/" + strcase.ToLowerCamel(template.Id(params.LeftDomainCamelName))
  30. rightDomainPath := "/" + strcase.ToLowerCamel(template.Id(params.RightDomainCamelName))
  31. if !options.disableLeft {
  32. if !options.disableLeftUpdate {
  33. // 左到右更新
  34. builder.
  35. Url(http.MethodPost, leftDomainPath+rightDomainPath+"/update").
  36. Post(&gateway.PostRequest{
  37. Url: params.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/update",
  38. }, nil).
  39. Build()
  40. }
  41. if !options.disableLeftQuery {
  42. // 左到右查询
  43. builder.
  44. Url(http.MethodGet, leftDomainPath+rightDomainPath+"/query").
  45. Get(&gateway.GetRequest{
  46. Url: params.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/query",
  47. }, nil).
  48. Build()
  49. }
  50. if !options.disableLeftWithRightQuery {
  51. // 左到右查询,携带右方信息
  52. builder.
  53. Url(http.MethodGet, leftDomainPath+rightDomainPath+"/queryWith").
  54. Request(func(c *api.Context, historyRequests []gateway.BuilderRequest, resultMap map[string]any) (gateway.BuilderRequest, error) {
  55. newQueryParams, err := gateway.AddQueryParamsTenantIDAndUserID(c, "tenantId", "")
  56. if err != nil {
  57. return nil, err
  58. }
  59. return &gateway.GetRequest{
  60. Url: params.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/queryWith",
  61. QueryParams: newQueryParams,
  62. }, nil
  63. }, nil).
  64. Build()
  65. }
  66. }
  67. if !options.disableRight {
  68. if !options.disableRightUpdate {
  69. // 右到左更新
  70. builder.
  71. Url(http.MethodPost, rightDomainPath+leftDomainPath+"/update").
  72. Post(&gateway.PostRequest{
  73. Url: params.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/update",
  74. }, nil).
  75. Build()
  76. }
  77. if !options.disableRightQuery {
  78. // 右到左查询
  79. builder.
  80. Url(http.MethodGet, rightDomainPath+leftDomainPath+"/query").
  81. Get(&gateway.GetRequest{
  82. Url: params.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/query",
  83. }, nil).
  84. Build()
  85. }
  86. if !options.disableRightWithLeftQuery {
  87. // 右到左查询,携带左方信息
  88. builder.
  89. Url(http.MethodGet, rightDomainPath+leftDomainPath+"/queryWith").
  90. Request(func(c *api.Context, historyRequests []gateway.BuilderRequest, resultMap map[string]any) (gateway.BuilderRequest, error) {
  91. newQueryParams, err := gateway.AddQueryParamsTenantIDAndUserID(c, "tenantId", "")
  92. if err != nil {
  93. return nil, err
  94. }
  95. return &gateway.GetRequest{
  96. Url: params.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/queryWith",
  97. QueryParams: newQueryParams,
  98. }, nil
  99. }, nil).
  100. Build()
  101. }
  102. }
  103. }
  104. type One2OneOption func(options *One2OneOptions)
  105. type One2OneOptions struct {
  106. // 关闭左侧到右侧关联
  107. disableLeft bool
  108. // 关闭右侧到左侧关联
  109. disableRight bool
  110. // 关闭左侧更新
  111. disableLeftUpdate bool
  112. // 关闭左侧查询
  113. disableLeftQuery bool
  114. // 关闭右侧更新
  115. disableRightUpdate bool
  116. // 关闭右侧查询
  117. disableRightQuery bool
  118. // 关闭左实体带右实体信息查询
  119. disableLeftWithRightQuery bool
  120. // 关闭右实体带左实体信息查询
  121. disableRightWithLeftQuery bool
  122. }
  123. func WithOne2OneDisableLeft() One2OneOption {
  124. return func(options *One2OneOptions) {
  125. options.disableLeft = true
  126. }
  127. }
  128. func WithOne2OneDisableRight() One2OneOption {
  129. return func(options *One2OneOptions) {
  130. options.disableRight = true
  131. }
  132. }
  133. func WithOne2OneDisableLeftUpdate() One2OneOption {
  134. return func(options *One2OneOptions) {
  135. options.disableLeftUpdate = true
  136. }
  137. }
  138. func WithOne2OneDisableLeftQuery() One2OneOption {
  139. return func(options *One2OneOptions) {
  140. options.disableLeftQuery = true
  141. }
  142. }
  143. func WithOne2OneDisableRightUpdate() One2OneOption {
  144. return func(options *One2OneOptions) {
  145. options.disableRightUpdate = true
  146. }
  147. }
  148. func WithOne2OneDisableRightQuery() One2OneOption {
  149. return func(options *One2OneOptions) {
  150. options.disableRightQuery = true
  151. }
  152. }
  153. func WithOne2OneDisableLeftWithRightQuery() One2OneOption {
  154. return func(options *One2OneOptions) {
  155. options.disableLeftWithRightQuery = true
  156. }
  157. }
  158. func WithOne2OneDisableRightWithLeftQuery() One2OneOption {
  159. return func(options *One2OneOptions) {
  160. options.disableRightWithLeftQuery = true
  161. }
  162. }