simple.go 5.7 KB

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