simple.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. if !options.disableLeftWithRightQuery {
  70. // 左到右查询,携带右方信息
  71. binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
  72. Path: simple.LeftDomainPath + simple.RightDomainPath + "/queryWith",
  73. ResponseFunc: response.SendMapResponse,
  74. RequestParams: simple.LeftQueryWithRightQueryParams,
  75. Objects: []domain.Object{simple.Left},
  76. ServiceFunc: QueryWithOtherInfo[LI, RI](simple.LeftTableName, simple.RightTableName, rightRelationColumnName),
  77. })
  78. }
  79. }
  80. if !options.disableRight {
  81. if !options.disableRightUpdate {
  82. // 右到左更新
  83. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  84. Path: simple.RightDomainPath + simple.LeftDomainPath + "/update",
  85. ResponseFunc: response.SendMsgResponse,
  86. RequestParams: simple.RightUpdateJsonBody,
  87. Objects: []domain.Object{simple.Right},
  88. ServiceFunc: Update(simple.RightTableName, rightRelationFieldName, rightRelationColumnName,
  89. simple.LeftTableName, simple.Left.DomainCNName(), leftRelationColumnName),
  90. })
  91. }
  92. if !options.disableRightQuery {
  93. // 右到左查询
  94. binding.GetBind(binder, &binding.SimpleBindItem[RI]{
  95. Path: simple.RightDomainPath + simple.LeftDomainPath + "/query",
  96. ResponseFunc: response.SendInfoResponse[RI],
  97. RequestParams: simple.RightQueryQueryParams,
  98. Objects: []domain.Object{simple.Right},
  99. ServiceFunc: Query[RI](simple.RightTableName, simple.LeftTableName, leftRelationColumnName),
  100. })
  101. }
  102. if !options.disableRightWithLeftQuery {
  103. // 右到左查询,携带左方信息
  104. binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
  105. Path: simple.RightDomainPath + simple.LeftDomainPath + "/queryWith",
  106. ResponseFunc: response.SendMapResponse,
  107. RequestParams: simple.RightQueryWithLeftQueryParams,
  108. Objects: []domain.Object{simple.Right},
  109. ServiceFunc: QueryWithOtherInfo[RI, LI](simple.RightTableName, simple.LeftTableName, leftRelationColumnName),
  110. })
  111. }
  112. }
  113. }
  114. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
  115. options := new(Options)
  116. for _, opt := range opts {
  117. opt(options)
  118. }
  119. simple.options = options
  120. simple.bind(binder)
  121. }
  122. type Option func(options *Options)
  123. type Options struct {
  124. // 关闭左侧到右侧关联
  125. disableLeft bool
  126. // 关闭右侧到左侧关联
  127. disableRight bool
  128. // 关闭左侧更新
  129. disableLeftUpdate bool
  130. // 关闭左侧查询
  131. disableLeftQuery bool
  132. // 关闭右侧更新
  133. disableRightUpdate bool
  134. // 关闭右侧查询
  135. disableRightQuery bool
  136. // 关闭左实体带右实体信息查询
  137. disableLeftWithRightQuery bool
  138. // 关闭右实体带左实体信息查询
  139. disableRightWithLeftQuery bool
  140. }
  141. func WithDisableLeft() Option {
  142. return func(options *Options) {
  143. options.disableLeft = true
  144. }
  145. }
  146. func WithDisableRight() Option {
  147. return func(options *Options) {
  148. options.disableRight = true
  149. }
  150. }
  151. func WithDisableLeftUpdate() Option {
  152. return func(options *Options) {
  153. options.disableLeftUpdate = true
  154. }
  155. }
  156. func WithDisableLeftQuery[LI any, RI any]() Option {
  157. return func(options *Options) {
  158. options.disableLeftQuery = true
  159. }
  160. }
  161. func WithDisableRightUpdate() Option {
  162. return func(options *Options) {
  163. options.disableRightUpdate = true
  164. }
  165. }
  166. func WithDisableRightQuery[LI any, RI any]() Option {
  167. return func(options *Options) {
  168. options.disableRightQuery = true
  169. }
  170. }
  171. func WithDisableLeftWithRightQuery[LI any, RI any]() Option {
  172. return func(options *Options) {
  173. options.disableLeftWithRightQuery = true
  174. }
  175. }
  176. func WithDisableRightWithLeftQuery() Option {
  177. return func(options *Options) {
  178. options.disableRightWithLeftQuery = true
  179. }
  180. }