simple.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. if !options.disableLeft {
  39. if !options.disableLeftUpdate {
  40. // 左到右更新
  41. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  42. Path: simple.LeftDomainPath + simple.RightDomainPath + "/update",
  43. ResponseFunc: response.SendMsgResponse,
  44. RequestParams: simple.LeftUpdateJsonBody,
  45. Objects: []domain.Object{simple.Left},
  46. ServiceFunc: Update(),
  47. })
  48. }
  49. if !options.disableLeftQuery {
  50. // 左到右查询
  51. binding.GetBind(binder, &binding.SimpleBindItem[RI]{
  52. Path: simple.LeftDomainPath + simple.RightDomainPath + "/query",
  53. ResponseFunc: response.SendInfoResponse[RI],
  54. RequestParams: simple.LeftQueryQueryParams,
  55. Objects: []domain.Object{simple.Left},
  56. ServiceFunc: Query[RI](),
  57. })
  58. }
  59. }
  60. if !options.disableRight {
  61. if !options.disableRightUpdate {
  62. // 右到左更新
  63. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  64. Path: simple.RightDomainPath + simple.LeftDomainPath + "/update",
  65. ResponseFunc: response.SendMsgResponse,
  66. RequestParams: simple.RightUpdateJsonBody,
  67. Objects: []domain.Object{simple.Right},
  68. ServiceFunc: Update(),
  69. })
  70. }
  71. if !options.disableRightQuery {
  72. // 右到左查询
  73. binding.GetBind(binder, &binding.SimpleBindItem[RI]{
  74. Path: simple.RightDomainPath + simple.LeftDomainPath + "/query",
  75. ResponseFunc: response.SendInfoResponse[RI],
  76. RequestParams: simple.RightQueryQueryParams,
  77. Objects: []domain.Object{simple.Right},
  78. ServiceFunc: Query[RI](),
  79. })
  80. }
  81. }
  82. }
  83. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
  84. options := new(Options)
  85. for _, opt := range opts {
  86. opt(options)
  87. }
  88. simple.options = options
  89. simple.bind(binder)
  90. }
  91. type Option func(options *Options)
  92. type Options struct {
  93. // 关闭左侧到右侧关联
  94. disableLeft bool
  95. // 关闭右侧到左侧关联
  96. disableRight bool
  97. // 关闭左侧更新
  98. disableLeftUpdate bool
  99. // 关闭左侧查询
  100. disableLeftQuery bool
  101. // 关闭右侧更新
  102. disableRightUpdate bool
  103. // 关闭右侧查询
  104. disableRightQuery bool
  105. }
  106. func WithDisableLeft() Option {
  107. return func(options *Options) {
  108. options.disableLeft = true
  109. }
  110. }
  111. func WithDisableRight() Option {
  112. return func(options *Options) {
  113. options.disableRight = true
  114. }
  115. }
  116. func WithDisableLeftUpdate() Option {
  117. return func(options *Options) {
  118. options.disableLeftUpdate = true
  119. }
  120. }
  121. func WithDisableLeftQuery() Option {
  122. return func(options *Options) {
  123. options.disableLeftQuery = true
  124. }
  125. }
  126. func WithDisableRightUpdate() Option {
  127. return func(options *Options) {
  128. options.disableRightUpdate = true
  129. }
  130. }
  131. func WithDisableRightQuery() Option {
  132. return func(options *Options) {
  133. options.disableRightQuery = true
  134. }
  135. }