simple.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // 更新左实体关联使用的请求参数
  20. LeftUpdateJsonBody request.Params
  21. // 查询左实体关联使用的请求参数,注意是Query类型
  22. LeftQueryQueryParams request.Query
  23. // 更新右实体关联使用的请求参数
  24. RightUpdateJsonBody request.Params
  25. // 查询右实体关联使用的请求参数,注意是Query类型
  26. RightQueryQueryParams request.Query
  27. // 可选配置项,通过WithXXX配置
  28. options *Options
  29. }
  30. func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
  31. options := simple.options
  32. leftDomainPath := entity.RelativeDomainPath(simple.Left)
  33. rightDomainPath := entity.RelativeDomainPath(simple.Right)
  34. leftRelationFieldName := fmt.Sprintf("%sID", simple.Right.DomainCamelName())
  35. rightRelationFieldName := fmt.Sprintf("%sID", simple.Left.DomainCamelName())
  36. leftRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(simple.Right.DomainCamelName()))
  37. rightRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(simple.Left.DomainCamelName()))
  38. middleTableName := entity.SnakeDomainName(simple.Left) + "_and_" + entity.SnakeDomainName(simple.Right)
  39. if !options.disableLeft {
  40. if !options.disableLeftUpdate {
  41. // 左到右更新
  42. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  43. Path: leftDomainPath + rightDomainPath + "/update",
  44. ResponseFunc: response.SendMsgResponse,
  45. RequestParams: simple.LeftUpdateJsonBody,
  46. Objects: []domain.Object{simple.Left},
  47. ServiceFunc: Update(),
  48. })
  49. }
  50. if !options.disableLeftQuery {
  51. // 左到右查询
  52. binding.GetBind(binder, &binding.SimpleBindItem[RI]{
  53. Path: leftDomainPath + rightDomainPath + "/query",
  54. ResponseFunc: response.SendInfoResponse[RI],
  55. RequestParams: simple.LeftQueryQueryParams,
  56. Objects: []domain.Object{simple.Left},
  57. ServiceFunc: Query[RI](),
  58. })
  59. }
  60. }
  61. if !options.disableRight {
  62. if !options.disableRightUpdate {
  63. // 右到左更新
  64. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  65. Path: rightDomainPath + leftDomainPath + "/update",
  66. ResponseFunc: response.SendMsgResponse,
  67. RequestParams: simple.RightUpdateJsonBody,
  68. Objects: []domain.Object{simple.Right},
  69. ServiceFunc: Update(),
  70. })
  71. }
  72. if !options.disableRightQuery {
  73. // 右到左查询
  74. binding.GetBind(binder, &binding.SimpleBindItem[RI]{
  75. Path: rightDomainPath + leftDomainPath + "/query",
  76. ResponseFunc: response.SendInfoResponse[RI],
  77. RequestParams: simple.RightQueryQueryParams,
  78. Objects: []domain.Object{simple.Right},
  79. ServiceFunc: Query[RI](),
  80. })
  81. }
  82. }
  83. }
  84. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
  85. options := new(Options)
  86. for _, opt := range opts {
  87. opt(options)
  88. }
  89. simple.options = options
  90. simple.bind(binder)
  91. }
  92. type Option func(options *Options)
  93. type Options struct {
  94. // 关闭左侧到右侧关联
  95. disableLeft bool
  96. // 关闭右侧到左侧关联
  97. disableRight bool
  98. // 关闭左侧更新
  99. disableLeftUpdate bool
  100. // 关闭左侧查询
  101. disableLeftQuery bool
  102. // 关闭右侧更新
  103. disableRightUpdate bool
  104. // 关闭右侧查询
  105. disableRightQuery bool
  106. }
  107. func WithDisableLeft() Option {
  108. return func(options *Options) {
  109. options.disableLeft = true
  110. }
  111. }
  112. func WithDisableRight() Option {
  113. return func(options *Options) {
  114. options.disableRight = true
  115. }
  116. }
  117. func WithDisableLeftUpdate() Option {
  118. return func(options *Options) {
  119. options.disableLeftUpdate = true
  120. }
  121. }
  122. func WithDisableLeftQuery() Option {
  123. return func(options *Options) {
  124. options.disableLeftQuery = true
  125. }
  126. }
  127. func WithDisableRightUpdate() Option {
  128. return func(options *Options) {
  129. options.disableRightUpdate = true
  130. }
  131. }
  132. func WithDisableRightQuery() Option {
  133. return func(options *Options) {
  134. options.disableRightQuery = true
  135. }
  136. }