simple.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package one2one
  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. "github.com/iancoleman/strcase"
  9. "reflect"
  10. )
  11. // Simple 关联的Bind参数
  12. // LI 为左边实体的Info类型
  13. // RI 为右边实体的Info类型
  14. type Simple[LI any, RI any] struct {
  15. // 左领域实体,注意是Entity类型
  16. Left domain.Entity
  17. // 右领域实体,注意是Entity类型
  18. Right domain.Entity
  19. // 左表名
  20. LeftTableName string
  21. // 右表名
  22. RightTableName string
  23. // URL领域相对路径,如/user/userInfo,后面会自动补充,如/user/userInfo/update
  24. LeftDomainPath string
  25. // URL领域相对路径,如/userInfo/user,后面会自动补充,如/userInfo/user/update
  26. RightDomainPath string
  27. // 更新左实体关联使用的请求参数
  28. LeftUpdateJsonBody request.WithID
  29. // 查询左实体关联使用的请求参数,注意是WithID类型
  30. LeftQueryQueryParams request.WithID
  31. // 查询左实体带右实体信息使用的请求参数,注意是WithID类型
  32. LeftQueryWithRightQueryParams request.WithID
  33. // 更新右实体关联使用的请求参数,注意是WithID类型
  34. RightUpdateJsonBody request.WithID
  35. // 查询右实体关联使用的请求参数,注意是WithID类型
  36. RightQueryQueryParams request.WithID
  37. // 查询右实体带左实体信息使用的请求参数,注意是WithID类型
  38. RightQueryWithLeftQueryParams request.WithID
  39. // 可选配置项,通过WithXXX配置
  40. options *Options
  41. }
  42. func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
  43. options := simple.options
  44. leftEntityType := reflect.TypeOf(simple.Left)
  45. rightEntityType := reflect.TypeOf(simple.Right)
  46. leftRelationFieldName := fmt.Sprintf("%sID", strcase.ToCamel(rightEntityType.Name()))
  47. rightRelationFieldName := fmt.Sprintf("%sID", strcase.ToCamel(leftEntityType.Name()))
  48. leftRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(rightEntityType.Name()))
  49. rightRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(leftEntityType.Name()))
  50. if !options.disableLeft {
  51. if !options.disableLeftUpdate {
  52. // 左到右更新
  53. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  54. Path: simple.LeftDomainPath + simple.RightDomainPath + "/update",
  55. ResponseFunc: response.SendMsgResponse,
  56. RequestParams: simple.LeftUpdateJsonBody,
  57. Objects: []domain.Object{simple.Left},
  58. ServiceFunc: Update(simple.LeftTableName, leftRelationFieldName, leftRelationColumnName,
  59. simple.RightTableName, simple.Right.DomainCNName()),
  60. })
  61. }
  62. }
  63. if !options.disableRight {
  64. if !options.disableRightUpdate {
  65. // 右到左更新
  66. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  67. Path: simple.RightDomainPath + simple.LeftDomainPath + "/update",
  68. ResponseFunc: response.SendMsgResponse,
  69. RequestParams: simple.RightUpdateJsonBody,
  70. Objects: []domain.Object{simple.Right},
  71. ServiceFunc: Update(simple.RightTableName, rightRelationFieldName, rightRelationColumnName,
  72. simple.LeftTableName, simple.Left.DomainCNName()),
  73. })
  74. }
  75. }
  76. }
  77. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
  78. options := new(Options)
  79. for _, opt := range opts {
  80. opt(options)
  81. }
  82. simple.options = options
  83. simple.bind(binder)
  84. }
  85. type Option func(options *Options)
  86. type Options struct {
  87. // 关闭左侧到右侧关联
  88. disableLeft bool
  89. // 关闭右侧到左侧关联
  90. disableRight bool
  91. // 关闭左侧更新
  92. disableLeftUpdate bool
  93. // 关闭左侧查询
  94. disableLeftQuery bool
  95. // 关闭右侧更新
  96. disableRightUpdate bool
  97. // 关闭右侧查询
  98. disableRightQuery bool
  99. // 关闭左实体带右实体信息查询
  100. disableLeftWithRightQuery bool
  101. // 关闭右实体带左实体信息查询
  102. disableRightWithLeftQuery bool
  103. }
  104. func WithDisableLeft() Option {
  105. return func(options *Options) {
  106. options.disableLeft = true
  107. }
  108. }
  109. func WithDisableRight() Option {
  110. return func(options *Options) {
  111. options.disableRight = true
  112. }
  113. }
  114. func WithDisableLeftUpdate() Option {
  115. return func(options *Options) {
  116. options.disableLeftUpdate = true
  117. }
  118. }
  119. func WithDisableLeftQuery[LI any, RI any]() Option {
  120. return func(options *Options) {
  121. options.disableLeftQuery = true
  122. }
  123. }
  124. func WithDisableRightUpdate() Option {
  125. return func(options *Options) {
  126. options.disableRightUpdate = true
  127. }
  128. }
  129. func WithDisableRightQuery[LI any, RI any]() Option {
  130. return func(options *Options) {
  131. options.disableRightQuery = true
  132. }
  133. }
  134. func WithDisableLeftWithRightQuery[LI any, RI any]() Option {
  135. return func(options *Options) {
  136. options.disableLeftWithRightQuery = true
  137. }
  138. }
  139. func WithDisableRightWithLeftQuery() Option {
  140. return func(options *Options) {
  141. options.disableRightWithLeftQuery = true
  142. }
  143. }