simple.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package one2many
  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. // 查询左实体关联使用的请求参数,注意是Query类型
  24. LeftQueryQueryParams request.QueryWithIDRequestParams
  25. // 更新左实体关联使用的请求参数
  26. RightUpdateJsonBody request.Params
  27. // 查询右实体关联使用的请求参数,注意是QueryWithID类型
  28. RightQueryQueryParams request.QueryWithIDRequestParams
  29. // 查询右实体带左实体信息使用的请求参数,注意是Query类型
  30. RightQueryWithLeftQueryParams request.QueryRequestParams
  31. // 可选配置项,通过WithXXX配置
  32. options *Options
  33. }
  34. func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
  35. options := simple.options
  36. leftDomainPath := domain.RelativeDomainPath(simple.Left)
  37. rightDomainPath := domain.RelativeDomainPath(simple.Right)
  38. leftRelationFieldName := fmt.Sprintf("%sIDs", simple.Right.DomainCamelName())
  39. rightRelationFieldName := fmt.Sprintf("%sID", simple.Left.DomainCamelName())
  40. leftTableName := domain.TableName(simple.Schema, simple.Left)
  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: UpdateLeft(leftTableName, simple.Left.DomainCNName(), leftRelationFieldName,
  52. rightTableName, simple.Right.DomainCNName(), rightRelationColumnName),
  53. })
  54. }
  55. if !options.disableLeftQuery {
  56. // 左到右查询
  57. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[RI]]{
  58. Path: leftDomainPath + rightDomainPath + "/query",
  59. ResponseFunc: response.SendInfosResponse[RI],
  60. RequestParams: simple.LeftQueryQueryParams,
  61. Objects: []domain.Object{simple.Left},
  62. ServiceFunc: QueryLeft[RI](leftTableName, rightTableName, rightRelationColumnName),
  63. })
  64. }
  65. }
  66. if !options.disableRight {
  67. if !options.disableRightUpdate {
  68. // 右到左更新
  69. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  70. Path: rightDomainPath + leftDomainPath + "/update",
  71. ResponseFunc: response.SendMsgResponse,
  72. RequestParams: simple.RightUpdateJsonBody,
  73. Objects: []domain.Object{simple.Right},
  74. ServiceFunc: UpdateRight(rightTableName, rightRelationFieldName, rightRelationColumnName,
  75. leftTableName, simple.Left.DomainCNName()),
  76. })
  77. }
  78. if !options.disableRightQuery {
  79. // 右到左查询
  80. binding.GetBind(binder, &binding.SimpleBindItem[LI]{
  81. Path: rightDomainPath + leftDomainPath + "/query",
  82. ResponseFunc: response.SendInfoResponse[LI],
  83. RequestParams: simple.RightQueryQueryParams,
  84. Objects: []domain.Object{simple.Right},
  85. ServiceFunc: QueryRight[LI](rightTableName, rightRelationFieldName, leftTableName),
  86. })
  87. }
  88. if !options.disableRightWithLeftQuery {
  89. // 右到左查询,携带左方信息
  90. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[map[string]any]]{
  91. Path: rightDomainPath + leftDomainPath + "/queryWith",
  92. ResponseFunc: response.SendInfosResponse[map[string]any],
  93. RequestParams: simple.RightQueryWithLeftQueryParams,
  94. Objects: []domain.Object{simple.Right},
  95. ServiceFunc: QueryRightWithLeftInfo[RI, LI](rightTableName, rightRelationColumnName, options.rightQueryWithConditionFieldCallback, leftTableName),
  96. })
  97. }
  98. }
  99. }
  100. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
  101. options := new(Options)
  102. for _, opt := range opts {
  103. opt(options)
  104. }
  105. simple.options = options
  106. simple.bind(binder)
  107. }
  108. type Option func(options *Options)
  109. type Options struct {
  110. // 关闭左侧到右侧关联
  111. disableLeft bool
  112. // 关闭右侧到左侧关联
  113. disableRight bool
  114. // 关闭左侧更新
  115. disableLeftUpdate bool
  116. // 关闭左侧查询
  117. disableLeftQuery bool
  118. // 关闭右侧更新
  119. disableRightUpdate bool
  120. // 关闭右侧查询
  121. disableRightQuery bool
  122. // 关闭右实体带左实体信息查询
  123. disableRightWithLeftQuery bool
  124. // 右查询条件构造回调
  125. rightQueryWithConditionFieldCallback ConditionFieldCallback
  126. }
  127. func WithDisableLeft() Option {
  128. return func(options *Options) {
  129. options.disableLeft = true
  130. }
  131. }
  132. func WithDisableRight() Option {
  133. return func(options *Options) {
  134. options.disableRight = true
  135. }
  136. }
  137. func WithDisableLeftUpdate() Option {
  138. return func(options *Options) {
  139. options.disableLeftUpdate = true
  140. }
  141. }
  142. func WithDisableLeftQuery() Option {
  143. return func(options *Options) {
  144. options.disableLeftQuery = true
  145. }
  146. }
  147. func WithDisableRightUpdate() Option {
  148. return func(options *Options) {
  149. options.disableRightUpdate = true
  150. }
  151. }
  152. func WithDisableRightQuery() Option {
  153. return func(options *Options) {
  154. options.disableRightQuery = true
  155. }
  156. }
  157. func WithDisableRightWithLeftQuery() Option {
  158. return func(options *Options) {
  159. options.disableRightWithLeftQuery = true
  160. }
  161. }
  162. func WithRightQueryWithConditionFieldCallback(callbacks ConditionFieldCallback) Option {
  163. return func(options *Options) {
  164. options.rightQueryWithConditionFieldCallback = callbacks
  165. }
  166. }