one2many.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. ServiceShortName string
  20. // 左领域名称
  21. LeftDomainCamelName string
  22. // 右领域名称
  23. RightDomainCamelName string
  24. // 可选配置项,通过WithXXX配置
  25. options *Options
  26. }
  27. func (simple *Simple) bind(builder *gateway.Builder) {
  28. options := simple.options
  29. leftDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.LeftDomainCamelName))
  30. rightDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.RightDomainCamelName))
  31. if !options.disableLeft {
  32. if !options.disableLeftUpdate {
  33. // 左到右更新
  34. builder.
  35. PostRoute("/"+simple.ServiceShortName+leftDomainPath+rightDomainPath+"/update",
  36. func(requestBuilder *gateway.RequestBuilder) {
  37. requestBuilder.
  38. Post(&gateway.PostRequest{
  39. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/update",
  40. }).
  41. Request()
  42. })
  43. }
  44. if !options.disableLeftQuery {
  45. // 左到右查询
  46. builder.
  47. GetRoute("/"+simple.ServiceShortName+leftDomainPath+rightDomainPath+"/query",
  48. func(requestBuilder *gateway.RequestBuilder) {
  49. requestBuilder.
  50. Get(&gateway.GetRequest{
  51. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/query",
  52. }).
  53. Request()
  54. })
  55. }
  56. }
  57. if !options.disableRight {
  58. if !options.disableRightUpdate {
  59. // 右到左更新
  60. builder.
  61. PostRoute("/"+simple.ServiceShortName+rightDomainPath+leftDomainPath+"/update",
  62. func(requestBuilder *gateway.RequestBuilder) {
  63. requestBuilder.
  64. Post(&gateway.PostRequest{
  65. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/update",
  66. }).
  67. Request()
  68. })
  69. }
  70. if !options.disableRightQuery {
  71. builder.
  72. GetRoute("/"+simple.ServiceShortName+rightDomainPath+leftDomainPath+"/query",
  73. func(requestBuilder *gateway.RequestBuilder) {
  74. requestBuilder.
  75. Get(&gateway.GetRequest{
  76. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/query",
  77. }).
  78. Request()
  79. })
  80. }
  81. if !options.disableRightWithLeftQuery {
  82. // 右到左查询,携带左方信息
  83. builder.
  84. GetRouteWithTenantID("/"+simple.ServiceShortName+rightDomainPath+leftDomainPath+"/queryWith",
  85. func(requestBuilder *gateway.RequestBuilder) {
  86. requestBuilder.
  87. Get(&gateway.GetRequest{
  88. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/queryWith",
  89. }).
  90. Request()
  91. })
  92. }
  93. }
  94. }
  95. type Option func(options *Options)
  96. type Options struct {
  97. // 关闭左侧到右侧关联
  98. disableLeft bool
  99. // 关闭右侧到左侧关联
  100. disableRight bool
  101. // 关闭左侧更新
  102. disableLeftUpdate bool
  103. // 关闭左侧查询
  104. disableLeftQuery bool
  105. // 关闭右侧更新
  106. disableRightUpdate bool
  107. // 关闭右侧查询
  108. disableRightQuery bool
  109. // 关闭右实体带左实体信息查询
  110. disableRightWithLeftQuery bool
  111. }
  112. func WithDisableLeft() Option {
  113. return func(options *Options) {
  114. options.disableLeft = true
  115. }
  116. }
  117. func WithDisableRight() Option {
  118. return func(options *Options) {
  119. options.disableRight = true
  120. }
  121. }
  122. func WithDisableLeftUpdate() Option {
  123. return func(options *Options) {
  124. options.disableLeftUpdate = true
  125. }
  126. }
  127. func WithDisableLeftQuery() Option {
  128. return func(options *Options) {
  129. options.disableLeftQuery = true
  130. }
  131. }
  132. func WithDisableRightUpdate() Option {
  133. return func(options *Options) {
  134. options.disableRightUpdate = true
  135. }
  136. }
  137. func WithDisableRightQuery() Option {
  138. return func(options *Options) {
  139. options.disableRightQuery = true
  140. }
  141. }
  142. func WithDisableRightWithLeftQuery() Option {
  143. return func(options *Options) {
  144. options.disableRightWithLeftQuery = true
  145. }
  146. }