one2many.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "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. }
  50. if !options.disableRight {
  51. if !options.disableRightUpdate {
  52. // 右到左更新
  53. builder.
  54. Url(http.MethodPost, rightDomainPath+leftDomainPath+"/update").
  55. Post(&gateway.PostRequest{
  56. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/update",
  57. }).
  58. Build()
  59. }
  60. if !options.disableRightQuery {
  61. // 右到左查询
  62. builder.
  63. Url(http.MethodGet, rightDomainPath+leftDomainPath+"/query").
  64. Get(&gateway.GetRequest{
  65. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/query",
  66. }).
  67. Build()
  68. }
  69. if !options.disableRightWithLeftQuery {
  70. // 右到左查询,携带左方信息
  71. builder.
  72. Url(http.MethodGet, rightDomainPath+leftDomainPath+"/queryWith").
  73. Get(&gateway.GetRequest{
  74. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/queryWith",
  75. QueryParamsFormFunc: gateway.FormQueryParamsWithTenantIDAndUserIDFunc("tenantId", ""),
  76. }).
  77. Build()
  78. }
  79. }
  80. }
  81. type Option func(options *Options)
  82. type Options struct {
  83. // 关闭左侧到右侧关联
  84. disableLeft bool
  85. // 关闭右侧到左侧关联
  86. disableRight bool
  87. // 关闭左侧更新
  88. disableLeftUpdate bool
  89. // 关闭左侧查询
  90. disableLeftQuery bool
  91. // 关闭右侧更新
  92. disableRightUpdate bool
  93. // 关闭右侧查询
  94. disableRightQuery bool
  95. // 关闭右实体带左实体信息查询
  96. disableRightWithLeftQuery bool
  97. }
  98. func WithDisableLeft() Option {
  99. return func(options *Options) {
  100. options.disableLeft = true
  101. }
  102. }
  103. func WithDisableRight() Option {
  104. return func(options *Options) {
  105. options.disableRight = true
  106. }
  107. }
  108. func WithDisableLeftUpdate() Option {
  109. return func(options *Options) {
  110. options.disableLeftUpdate = true
  111. }
  112. }
  113. func WithDisableLeftQuery() Option {
  114. return func(options *Options) {
  115. options.disableLeftQuery = true
  116. }
  117. }
  118. func WithDisableRightUpdate() Option {
  119. return func(options *Options) {
  120. options.disableRightUpdate = true
  121. }
  122. }
  123. func WithDisableRightQuery() Option {
  124. return func(options *Options) {
  125. options.disableRightQuery = true
  126. }
  127. }
  128. func WithDisableRightWithLeftQuery() Option {
  129. return func(options *Options) {
  130. options.disableRightWithLeftQuery = true
  131. }
  132. }