simple.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package remote
  2. import (
  3. "fmt"
  4. "git.sxidc.com/go-framework/baize/convenient/binding"
  5. "git.sxidc.com/go-framework/baize/convenient/binding/request"
  6. "git.sxidc.com/go-framework/baize/convenient/binding/response"
  7. "git.sxidc.com/go-framework/baize/framwork/domain"
  8. "git.sxidc.com/go-framework/baize/framwork/domain/entity"
  9. "github.com/iancoleman/strcase"
  10. )
  11. // Simple 关联的Bind参数
  12. // I 为本地实体的Info类型
  13. type Simple[I any] struct {
  14. // 左领域实体,注意是Entity类型
  15. Left entity.Entity
  16. // 右领域实体,注意是Entity类型
  17. Right entity.Entity
  18. // 左领域实体不是本地的
  19. LeftRemote bool
  20. // 右领域实体不是本地的
  21. RightRemote bool
  22. // 数据库Schema
  23. Schema string
  24. // 更新左实体关联使用的请求参数
  25. LeftUpdateJsonBody request.Params
  26. // 查询左实体关联使用的请求参数,注意是Query类型
  27. LeftQueryQueryParams request.QueryWithIDRequestParams
  28. // 更新右实体关联使用的请求参数
  29. RightUpdateJsonBody request.Params
  30. // 查询右实体关联使用的请求参数,注意是Query类型
  31. RightQueryQueryParams request.QueryWithIDRequestParams
  32. // 可选配置项,通过WithXXX配置
  33. options *Options
  34. }
  35. func (simple *Simple[I]) bind(binder *binding.Binder) {
  36. options := simple.options
  37. leftDomainPath := domain.RelativeDomainPath(simple.Left)
  38. rightDomainPath := domain.RelativeDomainPath(simple.Right)
  39. leftRelationFieldName := fmt.Sprintf("%sIDs", simple.Right.DomainCamelName())
  40. rightRelationFieldName := fmt.Sprintf("%sIDs", simple.Left.DomainCamelName())
  41. middleTableName := domain.SnakeDomainName(simple.Left) + "_and_" + domain.SnakeDomainName(simple.Right)
  42. leftTableName := domain.TableName(simple.Schema, simple.Left)
  43. leftRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(simple.Right.DomainCamelName()))
  44. rightTableName := domain.TableName(simple.Schema, simple.Right)
  45. rightRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(simple.Left.DomainCamelName()))
  46. if !options.disableLeft {
  47. if !options.disableLeftUpdate {
  48. // 左到右更新
  49. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  50. Path: leftDomainPath + rightDomainPath + "/update",
  51. ResponseFunc: response.SendMsgResponse,
  52. RequestParams: simple.LeftUpdateJsonBody,
  53. Objects: []domain.Object{simple.Left},
  54. ServiceFunc: Update(middleTableName,
  55. simple.LeftRemote, leftTableName, simple.Left.DomainCNName(), leftRelationFieldName, leftRelationColumnName,
  56. simple.RightRemote, rightTableName, rightRelationColumnName),
  57. })
  58. }
  59. if !options.disableLeftQuery {
  60. // 左到右查询
  61. if simple.RightRemote {
  62. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[string]]{
  63. Path: leftDomainPath + rightDomainPath + "/query",
  64. ResponseFunc: response.SendInfosResponse[string],
  65. RequestParams: simple.LeftQueryQueryParams,
  66. Objects: []domain.Object{simple.Left},
  67. ServiceFunc: QueryToRemote(middleTableName, simple.LeftRemote, leftTableName, leftRelationColumnName, rightRelationColumnName),
  68. })
  69. } else {
  70. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{
  71. Path: leftDomainPath + rightDomainPath + "/query",
  72. ResponseFunc: response.SendInfosResponse[I],
  73. RequestParams: simple.LeftQueryQueryParams,
  74. Objects: []domain.Object{simple.Left},
  75. ServiceFunc: QueryToExist[I](middleTableName,
  76. simple.LeftRemote, leftTableName, leftRelationColumnName,
  77. rightTableName, rightRelationColumnName),
  78. })
  79. }
  80. }
  81. }
  82. if !options.disableRight {
  83. if !options.disableRightUpdate {
  84. // 右到左更新
  85. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  86. Path: rightDomainPath + leftDomainPath + "/update",
  87. ResponseFunc: response.SendMsgResponse,
  88. RequestParams: simple.RightUpdateJsonBody,
  89. Objects: []domain.Object{simple.Right},
  90. ServiceFunc: Update(middleTableName,
  91. simple.RightRemote, rightTableName, simple.Right.DomainCNName(), rightRelationFieldName, rightRelationColumnName,
  92. simple.LeftRemote, leftTableName, leftRelationColumnName),
  93. })
  94. }
  95. if !options.disableRightQuery {
  96. // 右到左查询
  97. if simple.LeftRemote {
  98. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[string]]{
  99. Path: rightDomainPath + leftDomainPath + "/query",
  100. ResponseFunc: response.SendInfosResponse[string],
  101. RequestParams: simple.RightQueryQueryParams,
  102. Objects: []domain.Object{simple.Right},
  103. ServiceFunc: QueryToRemote(middleTableName, simple.RightRemote, rightTableName, rightRelationColumnName, leftRelationColumnName),
  104. })
  105. } else {
  106. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{
  107. Path: rightDomainPath + leftDomainPath + "/query",
  108. ResponseFunc: response.SendInfosResponse[I],
  109. RequestParams: simple.RightQueryQueryParams,
  110. Objects: []domain.Object{simple.Right},
  111. ServiceFunc: QueryToExist[I](middleTableName,
  112. simple.RightRemote, rightTableName, rightRelationColumnName,
  113. leftTableName, leftRelationColumnName),
  114. })
  115. }
  116. }
  117. }
  118. }
  119. func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...Option) {
  120. options := new(Options)
  121. for _, opt := range opts {
  122. opt(options)
  123. }
  124. simple.options = options
  125. simple.bind(binder)
  126. }
  127. type Option func(options *Options)
  128. type Options struct {
  129. // 关闭左侧到右侧关联
  130. disableLeft bool
  131. // 关闭右侧到左侧关联
  132. disableRight bool
  133. // 关闭左侧更新
  134. disableLeftUpdate bool
  135. // 关闭左侧查询
  136. disableLeftQuery bool
  137. // 关闭右侧更新
  138. disableRightUpdate bool
  139. // 关闭右侧查询
  140. disableRightQuery bool
  141. }
  142. func WithDisableLeft() Option {
  143. return func(options *Options) {
  144. options.disableLeft = true
  145. }
  146. }
  147. func WithDisableRight() Option {
  148. return func(options *Options) {
  149. options.disableRight = true
  150. }
  151. }
  152. func WithDisableLeftUpdate() Option {
  153. return func(options *Options) {
  154. options.disableLeftUpdate = true
  155. }
  156. }
  157. func WithDisableLeftQuery() Option {
  158. return func(options *Options) {
  159. options.disableLeftQuery = true
  160. }
  161. }
  162. func WithDisableRightUpdate() Option {
  163. return func(options *Options) {
  164. options.disableRightUpdate = true
  165. }
  166. }
  167. func WithDisableRightQuery() Option {
  168. return func(options *Options) {
  169. options.disableRightQuery = true
  170. }
  171. }