simple.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package many2many
  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. // LI 为左边实体的Info类型
  13. // RI 为右边实体的Info类型
  14. type Simple[LI any, RI any] struct {
  15. // 左领域实体,注意是Entity类型
  16. Left entity.Entity
  17. // 右领域实体,注意是Entity类型
  18. Right entity.Entity
  19. // 数据库schema
  20. Schema string
  21. // 更新左实体关联使用的请求参数
  22. LeftUpdateJsonBody request.Params
  23. // 查询左实体关联使用的请求参数,注意是QueryWithID类型
  24. LeftQueryQueryParams request.QueryWithIDRequestParams
  25. // 更新右实体关联使用的请求参数
  26. RightUpdateJsonBody request.Params
  27. // 查询右实体关联使用的请求参数,注意是QueryWithID类型
  28. RightQueryQueryParams request.QueryWithIDRequestParams
  29. // 可选配置项,通过WithXXX配置
  30. options *Options
  31. }
  32. func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
  33. options := simple.options
  34. leftDomainPath := domain.RelativeDomainPath(simple.Left)
  35. rightDomainPath := domain.RelativeDomainPath(simple.Right)
  36. leftRelationFieldName := fmt.Sprintf("%sIDs", simple.Right.DomainCamelName())
  37. rightRelationFieldName := fmt.Sprintf("%sIDs", simple.Left.DomainCamelName())
  38. middleTableName := domain.SnakeDomainName(simple.Left) + "_and_" + domain.SnakeDomainName(simple.Right)
  39. leftTableName := domain.TableName(simple.Schema, simple.Left)
  40. leftRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(simple.Right.DomainCamelName()))
  41. rightTableName := domain.TableName(simple.Schema, simple.Right)
  42. rightRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(simple.Left.DomainCamelName()))
  43. if !options.disableLeft {
  44. if !options.disableLeftUpdate {
  45. // 左到右更新
  46. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  47. Path: leftDomainPath + rightDomainPath + "/update",
  48. ResponseFunc: response.SendMsgResponse,
  49. RequestParams: simple.LeftUpdateJsonBody,
  50. Objects: []domain.Object{simple.Left},
  51. ServiceFunc: Update(middleTableName,
  52. leftTableName, simple.Left.DomainCNName(), leftRelationFieldName, leftRelationColumnName,
  53. rightTableName, rightRelationColumnName),
  54. })
  55. }
  56. if !options.disableLeftQuery {
  57. // 左到右查询
  58. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[RI]]{
  59. Path: leftDomainPath + rightDomainPath + "/query",
  60. ResponseFunc: response.SendInfosResponse[RI],
  61. RequestParams: simple.LeftQueryQueryParams,
  62. Objects: []domain.Object{simple.Left},
  63. ServiceFunc: Query[RI](middleTableName,
  64. leftTableName, leftRelationColumnName,
  65. rightTableName, rightRelationColumnName),
  66. })
  67. }
  68. }
  69. if !options.disableRight {
  70. if !options.disableRightUpdate {
  71. // 右到左更新
  72. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  73. Path: rightDomainPath + leftDomainPath + "/update",
  74. ResponseFunc: response.SendMsgResponse,
  75. RequestParams: simple.RightUpdateJsonBody,
  76. Objects: []domain.Object{simple.Right},
  77. ServiceFunc: Update(middleTableName,
  78. rightTableName, simple.Right.DomainCNName(), rightRelationFieldName, rightRelationColumnName,
  79. leftTableName, leftRelationColumnName),
  80. })
  81. }
  82. if !options.disableRightQuery {
  83. // 右到左查询
  84. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[LI]]{
  85. Path: rightDomainPath + leftDomainPath + "/query",
  86. ResponseFunc: response.SendInfosResponse[LI],
  87. RequestParams: simple.RightQueryQueryParams,
  88. Objects: []domain.Object{simple.Right},
  89. ServiceFunc: Query[LI](middleTableName,
  90. rightTableName, rightRelationColumnName,
  91. leftTableName, leftRelationColumnName),
  92. })
  93. }
  94. }
  95. }
  96. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
  97. options := new(Options)
  98. for _, opt := range opts {
  99. opt(options)
  100. }
  101. simple.options = options
  102. simple.bind(binder)
  103. }
  104. type Option func(options *Options)
  105. type Options 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. func WithDisableLeft() Option {
  120. return func(options *Options) {
  121. options.disableLeft = true
  122. }
  123. }
  124. func WithDisableRight() Option {
  125. return func(options *Options) {
  126. options.disableRight = true
  127. }
  128. }
  129. func WithDisableLeftUpdate() Option {
  130. return func(options *Options) {
  131. options.disableLeftUpdate = true
  132. }
  133. }
  134. func WithDisableLeftQuery() Option {
  135. return func(options *Options) {
  136. options.disableLeftQuery = true
  137. }
  138. }
  139. func WithDisableRightUpdate() Option {
  140. return func(options *Options) {
  141. options.disableRightUpdate = true
  142. }
  143. }
  144. func WithDisableRightQuery() Option {
  145. return func(options *Options) {
  146. options.disableRightQuery = true
  147. }
  148. }