simple.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package one2one
  2. import (
  3. "fmt"
  4. "git.sxidc.com/go-framework/baize/framework/binding"
  5. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  6. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  7. "git.sxidc.com/go-framework/baize/framework/core/domain"
  8. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  9. )
  10. // Simple 关联的Bind参数
  11. // LI 为左边实体的Info类型
  12. // RI 为右边实体的Info类型
  13. type Simple[LI any, RI any] struct {
  14. // 左领域实体,注意是Entity类型
  15. Left entity.Entity
  16. // 右领域实体,注意是Entity类型
  17. Right entity.Entity
  18. // 数据库Schema
  19. Schema string
  20. // 更新左实体关联使用的请求参数
  21. LeftUpdateJsonBody request.IDRequestParam
  22. // 查询左实体关联使用的请求参数,注意是WithID类型
  23. LeftQueryQueryParams request.IDRequestParam
  24. // 查询左实体带右实体信息使用的请求参数,注意是Query类型
  25. LeftQueryWithRightQueryParams request.QueryRequestParams
  26. // 更新右实体关联使用的请求参数,注意是WithID类型
  27. RightUpdateJsonBody request.IDRequestParam
  28. // 查询右实体关联使用的请求参数,注意是WithID类型
  29. RightQueryQueryParams request.IDRequestParam
  30. // 查询右实体带左实体信息使用的请求参数,注意是WithID类型
  31. RightQueryWithLeftQueryParams request.QueryRequestParams
  32. // 可选配置项,通过WithXXX配置
  33. options *Options
  34. }
  35. func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
  36. options := simple.options
  37. leftDomainPath := domain.RelativeDomainPath(simple.Left)
  38. rightDomainPath := domain.RelativeDomainPath(simple.Right)
  39. leftRelationFieldName := fmt.Sprintf("%sID", simple.Right.DomainCamelName())
  40. rightRelationFieldName := fmt.Sprintf("%sID", simple.Left.DomainCamelName())
  41. leftTableName := domain.TableName(simple.Schema, simple.Left)
  42. leftRelationColumnName := domain.RelationColumnName(simple.Left)
  43. rightTableName := domain.TableName(simple.Schema, simple.Right)
  44. rightRelationColumnName := domain.RelationColumnName(simple.Right)
  45. if !options.disableLeft {
  46. if !options.disableLeftUpdate {
  47. leftUpdateMiddlewares := append(options.globalMiddlewares, options.leftUpdateMiddlewares...)
  48. // 左到右更新
  49. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  50. Path: leftDomainPath + rightDomainPath + "/update",
  51. SendResponseFunc: response.SendMsgResponse,
  52. RequestParams: simple.LeftUpdateJsonBody,
  53. Objects: []domain.Object{simple.Left},
  54. ServiceFunc: Update(leftTableName, leftRelationFieldName, leftRelationColumnName,
  55. rightTableName, simple.Right.DomainCNName(), rightRelationColumnName),
  56. }, leftUpdateMiddlewares...)
  57. }
  58. if !options.disableLeftQuery {
  59. leftQueryMiddlewares := append(options.globalMiddlewares, options.leftQueryMiddlewares...)
  60. // 左到右查询
  61. binding.GetBind(binder, &binding.SimpleBindItem[RI]{
  62. Path: leftDomainPath + rightDomainPath + "/query",
  63. SendResponseFunc: response.SendInfoResponse[RI],
  64. RequestParams: simple.LeftQueryQueryParams,
  65. Objects: []domain.Object{simple.Left},
  66. ServiceFunc: Query[RI](leftTableName, leftRelationFieldName, rightTableName),
  67. }, leftQueryMiddlewares...)
  68. }
  69. if !options.disableLeftWithRightQuery {
  70. leftWithRightQueryMiddlewares := append(options.globalMiddlewares, options.leftWithRightQueryMiddlewares...)
  71. // 左到右查询,携带右方信息
  72. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[map[string]any]]{
  73. Path: leftDomainPath + rightDomainPath + "/queryWith",
  74. SendResponseFunc: response.SendInfosResponse[map[string]any],
  75. RequestParams: simple.LeftQueryWithRightQueryParams,
  76. Objects: []domain.Object{simple.Left},
  77. ServiceFunc: QueryWithOtherInfo[LI, RI](leftTableName, options.leftQueryWithConditionFieldCallback, rightTableName, rightRelationColumnName),
  78. }, leftWithRightQueryMiddlewares...)
  79. }
  80. }
  81. if !options.disableRight {
  82. if !options.disableRightUpdate {
  83. rightUpdateMiddlewares := append(options.globalMiddlewares, options.rightUpdateMiddlewares...)
  84. // 右到左更新
  85. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  86. Path: rightDomainPath + leftDomainPath + "/update",
  87. SendResponseFunc: response.SendMsgResponse,
  88. RequestParams: simple.RightUpdateJsonBody,
  89. Objects: []domain.Object{simple.Right},
  90. ServiceFunc: Update(rightTableName, rightRelationFieldName, rightRelationColumnName,
  91. leftTableName, simple.Left.DomainCNName(), leftRelationColumnName),
  92. }, rightUpdateMiddlewares...)
  93. }
  94. if !options.disableRightQuery {
  95. rightQueryMiddlewares := append(options.globalMiddlewares, options.rightQueryMiddlewares...)
  96. // 右到左查询
  97. binding.GetBind(binder, &binding.SimpleBindItem[LI]{
  98. Path: rightDomainPath + leftDomainPath + "/query",
  99. SendResponseFunc: response.SendInfoResponse[LI],
  100. RequestParams: simple.RightQueryQueryParams,
  101. Objects: []domain.Object{simple.Right},
  102. ServiceFunc: Query[LI](rightTableName, rightRelationFieldName, leftTableName),
  103. }, rightQueryMiddlewares...)
  104. }
  105. if !options.disableRightWithLeftQuery {
  106. rightWithLeftQueryMiddlewares := append(options.globalMiddlewares, options.rightWithLeftQueryMiddlewares...)
  107. // 右到左查询,携带左方信息
  108. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[map[string]any]]{
  109. Path: rightDomainPath + leftDomainPath + "/queryWith",
  110. SendResponseFunc: response.SendInfosResponse[map[string]any],
  111. RequestParams: simple.RightQueryWithLeftQueryParams,
  112. Objects: []domain.Object{simple.Right},
  113. ServiceFunc: QueryWithOtherInfo[RI, LI](rightTableName, options.rightQueryWithConditionFieldCallback, leftTableName, leftRelationColumnName),
  114. }, rightWithLeftQueryMiddlewares...)
  115. }
  116. }
  117. }
  118. func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
  119. options := new(Options)
  120. for _, opt := range opts {
  121. opt(options)
  122. }
  123. simple.options = options
  124. simple.bind(binder)
  125. }
  126. type Option func(options *Options)
  127. type Options struct {
  128. // 关闭左侧到右侧关联
  129. disableLeft bool
  130. // 关闭右侧到左侧关联
  131. disableRight bool
  132. // 关闭左侧更新
  133. disableLeftUpdate bool
  134. // 关闭左侧查询
  135. disableLeftQuery bool
  136. // 关闭右侧更新
  137. disableRightUpdate bool
  138. // 关闭右侧查询
  139. disableRightQuery bool
  140. // 关闭左实体带右实体信息查询
  141. disableLeftWithRightQuery bool
  142. // 关闭右实体带左实体信息查询
  143. disableRightWithLeftQuery bool
  144. // 左查询条件构造回调
  145. leftQueryWithConditionFieldCallback ConditionFieldCallback
  146. // 右查询条件构造回调
  147. rightQueryWithConditionFieldCallback ConditionFieldCallback
  148. // 全局中间件
  149. globalMiddlewares []binding.Middleware
  150. // 左侧更新中间件
  151. leftUpdateMiddlewares []binding.Middleware
  152. // 左侧查询中间件
  153. leftQueryMiddlewares []binding.Middleware
  154. // 左实体带右实体查询中间件
  155. leftWithRightQueryMiddlewares []binding.Middleware
  156. // 右侧更新中间件
  157. rightUpdateMiddlewares []binding.Middleware
  158. // 右侧查询中间件
  159. rightQueryMiddlewares []binding.Middleware
  160. // 右实体带左实体查询中间件
  161. rightWithLeftQueryMiddlewares []binding.Middleware
  162. }
  163. func WithGlobalMiddlewares(middlewares ...binding.Middleware) Option {
  164. return func(options *Options) {
  165. options.globalMiddlewares = middlewares
  166. }
  167. }
  168. func WithDisableLeft() Option {
  169. return func(options *Options) {
  170. options.disableLeft = true
  171. }
  172. }
  173. func WithDisableRight() Option {
  174. return func(options *Options) {
  175. options.disableRight = true
  176. }
  177. }
  178. func WithDisableLeftUpdate() Option {
  179. return func(options *Options) {
  180. options.disableLeftUpdate = true
  181. }
  182. }
  183. func WithDisableLeftQuery() Option {
  184. return func(options *Options) {
  185. options.disableLeftQuery = true
  186. }
  187. }
  188. func WithDisableRightUpdate() Option {
  189. return func(options *Options) {
  190. options.disableRightUpdate = true
  191. }
  192. }
  193. func WithDisableRightQuery() Option {
  194. return func(options *Options) {
  195. options.disableRightQuery = true
  196. }
  197. }
  198. func WithDisableLeftWithRightQuery() Option {
  199. return func(options *Options) {
  200. options.disableLeftWithRightQuery = true
  201. }
  202. }
  203. func WithDisableRightWithLeftQuery() Option {
  204. return func(options *Options) {
  205. options.disableRightWithLeftQuery = true
  206. }
  207. }
  208. func WithLeftQueryWithConditionFieldCallback(callbacks ConditionFieldCallback) Option {
  209. return func(options *Options) {
  210. options.leftQueryWithConditionFieldCallback = callbacks
  211. }
  212. }
  213. func WithRightQueryWithConditionFieldCallback(callbacks ConditionFieldCallback) Option {
  214. return func(options *Options) {
  215. options.rightQueryWithConditionFieldCallback = callbacks
  216. }
  217. }
  218. func WithLeftUpdateMiddlewares(middlewares ...binding.Middleware) Option {
  219. return func(options *Options) {
  220. options.leftUpdateMiddlewares = middlewares
  221. }
  222. }
  223. func WithLeftQueryMiddlewares(middlewares ...binding.Middleware) Option {
  224. return func(options *Options) {
  225. options.leftQueryMiddlewares = middlewares
  226. }
  227. }
  228. func WithLeftWithRightQueryMiddlewares(middlewares ...binding.Middleware) Option {
  229. return func(options *Options) {
  230. options.leftWithRightQueryMiddlewares = middlewares
  231. }
  232. }
  233. func WithRightUpdateMiddlewares(middlewares ...binding.Middleware) Option {
  234. return func(options *Options) {
  235. options.rightUpdateMiddlewares = middlewares
  236. }
  237. }
  238. func WithRightQueryMiddlewares(middlewares ...binding.Middleware) Option {
  239. return func(options *Options) {
  240. options.rightQueryMiddlewares = middlewares
  241. }
  242. }
  243. func WithRightWithLeftQueryMiddlewares(middlewares ...binding.Middleware) Option {
  244. return func(options *Options) {
  245. options.rightWithLeftQueryMiddlewares = middlewares
  246. }
  247. }