simple.go 6.3 KB

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