123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- package remote
- import (
- "fmt"
- "git.sxidc.com/go-framework/baize/framework/binding"
- "git.sxidc.com/go-framework/baize/framework/core/api/request"
- "git.sxidc.com/go-framework/baize/framework/core/api/response"
- "git.sxidc.com/go-framework/baize/framework/core/domain"
- "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
- "reflect"
- )
- // Simple 关联的Bind参数
- // I 为本地实体的Info类型
- type Simple[LI any, RI any] struct {
- // 左领域实体,注意是Entity类型
- Left entity.Entity
- // 右领域实体,注意是Entity类型
- Right entity.Entity
- // 数据库Schema
- Schema string
- // 更新左实体关联使用的请求参数
- LeftUpdateJsonBody request.Params
- // 查询左实体关联使用的请求参数,注意是Query类型
- LeftQueryQueryParams request.QueryWithIDRequestParams
- // 更新右实体关联使用的请求参数
- RightUpdateJsonBody request.Params
- // 查询右实体关联使用的请求参数,注意是Query类型
- RightQueryQueryParams request.QueryWithIDRequestParams
- // 可选配置项,通过WithXXX配置
- options *Options
- }
- func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
- options := simple.options
- leftDomainPath := domain.RelativeDomainPath(simple.Left)
- rightDomainPath := domain.RelativeDomainPath(simple.Right)
- leftRelationFieldName := fmt.Sprintf("%sIDs", simple.Right.DomainCamelName())
- rightRelationFieldName := fmt.Sprintf("%sIDs", simple.Left.DomainCamelName())
- middleTableName := domain.RelationTableName(simple.Schema, simple.Left, simple.Right)
- leftTableName := domain.TableName(simple.Schema, simple.Left)
- leftRelationColumnName := domain.RelationColumnName(simple.Left)
- rightTableName := domain.TableName(simple.Schema, simple.Right)
- rightRelationColumnName := domain.RelationColumnName(simple.Right)
- leftRemote := false
- if reflect.TypeOf(new(LI)).Elem().Kind() == reflect.String {
- leftRemote = true
- }
- rightRemote := false
- if reflect.TypeOf(new(RI)).Elem().Kind() == reflect.String {
- rightRemote = true
- }
- if !options.disableLeft {
- if !options.disableLeftUpdate {
- leftUpdateMiddlewares := append(options.globalMiddlewares, options.leftUpdateMiddlewares...)
- // 左到右更新
- binding.PostBind(binder, &binding.SimpleBindItem[any]{
- Path: leftDomainPath + rightDomainPath + "/update",
- SendResponseFunc: response.SendMsgResponse,
- RequestParams: simple.LeftUpdateJsonBody,
- Objects: []domain.Object{simple.Left},
- ServiceFunc: Update(middleTableName,
- leftRemote, leftTableName, simple.Left.DomainCNName(), leftRelationFieldName, leftRelationColumnName,
- rightRemote, rightTableName, rightRelationColumnName),
- }, leftUpdateMiddlewares...)
- }
- if !options.disableLeftQuery {
- leftQueryMiddlewares := append(options.globalMiddlewares, options.leftQueryMiddlewares...)
- // 左到右查询
- if rightRemote {
- binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[string]]{
- Path: leftDomainPath + rightDomainPath + "/query",
- SendResponseFunc: response.SendInfosResponse[string],
- RequestParams: simple.LeftQueryQueryParams,
- Objects: []domain.Object{simple.Left},
- ServiceFunc: QueryToRemote(middleTableName, leftRemote, leftTableName, leftRelationColumnName, rightRelationColumnName),
- }, leftQueryMiddlewares...)
- } else {
- binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[RI]]{
- Path: leftDomainPath + rightDomainPath + "/query",
- SendResponseFunc: response.SendInfosResponse[RI],
- RequestParams: simple.LeftQueryQueryParams,
- Objects: []domain.Object{simple.Left},
- ServiceFunc: QueryToExist[RI](middleTableName,
- leftRemote, leftTableName, leftRelationColumnName,
- rightTableName, rightRelationColumnName),
- }, leftQueryMiddlewares...)
- }
- }
- }
- if !options.disableRight {
- if !options.disableRightUpdate {
- rightUpdateMiddlewares := append(options.globalMiddlewares, options.rightUpdateMiddlewares...)
- // 右到左更新
- binding.PostBind(binder, &binding.SimpleBindItem[any]{
- Path: rightDomainPath + leftDomainPath + "/update",
- SendResponseFunc: response.SendMsgResponse,
- RequestParams: simple.RightUpdateJsonBody,
- Objects: []domain.Object{simple.Right},
- ServiceFunc: Update(middleTableName,
- rightRemote, rightTableName, simple.Right.DomainCNName(), rightRelationFieldName, rightRelationColumnName,
- leftRemote, leftTableName, leftRelationColumnName),
- }, rightUpdateMiddlewares...)
- }
- if !options.disableRightQuery {
- rightQueryMiddlewares := append(options.globalMiddlewares, options.rightQueryMiddlewares...)
- // 右到左查询
- if leftRemote {
- binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[string]]{
- Path: rightDomainPath + leftDomainPath + "/query",
- SendResponseFunc: response.SendInfosResponse[string],
- RequestParams: simple.RightQueryQueryParams,
- Objects: []domain.Object{simple.Right},
- ServiceFunc: QueryToRemote(middleTableName, rightRemote, rightTableName, rightRelationColumnName, leftRelationColumnName),
- }, rightQueryMiddlewares...)
- } else {
- binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[LI]]{
- Path: rightDomainPath + leftDomainPath + "/query",
- SendResponseFunc: response.SendInfosResponse[LI],
- RequestParams: simple.RightQueryQueryParams,
- Objects: []domain.Object{simple.Right},
- ServiceFunc: QueryToExist[LI](middleTableName,
- rightRemote, rightTableName, rightRelationColumnName,
- leftTableName, leftRelationColumnName),
- }, rightQueryMiddlewares...)
- }
- }
- }
- }
- func BindSimple[LI any, RI any](binder *binding.Binder, simple *Simple[LI, RI], opts ...Option) {
- options := new(Options)
- for _, opt := range opts {
- opt(options)
- }
- simple.options = options
- simple.bind(binder)
- }
- type Option func(options *Options)
- type Options struct {
- // 关闭左侧到右侧关联
- disableLeft bool
- // 关闭右侧到左侧关联
- disableRight bool
- // 关闭左侧更新
- disableLeftUpdate bool
- // 关闭左侧查询
- disableLeftQuery bool
- // 关闭右侧更新
- disableRightUpdate bool
- // 关闭右侧查询
- disableRightQuery bool
- // 全局中间件
- globalMiddlewares []binding.Middleware
- // 左侧更新中间件
- leftUpdateMiddlewares []binding.Middleware
- // 左侧查询中间件
- leftQueryMiddlewares []binding.Middleware
- // 右侧更新中间件
- rightUpdateMiddlewares []binding.Middleware
- // 右侧查询中间件
- rightQueryMiddlewares []binding.Middleware
- }
- func WithGlobalMiddlewares(middlewares ...binding.Middleware) Option {
- return func(options *Options) {
- options.globalMiddlewares = middlewares
- }
- }
- func WithDisableLeft() Option {
- return func(options *Options) {
- options.disableLeft = true
- }
- }
- func WithDisableRight() Option {
- return func(options *Options) {
- options.disableRight = true
- }
- }
- func WithDisableLeftUpdate() Option {
- return func(options *Options) {
- options.disableLeftUpdate = true
- }
- }
- func WithDisableLeftQuery() Option {
- return func(options *Options) {
- options.disableLeftQuery = true
- }
- }
- func WithDisableRightUpdate() Option {
- return func(options *Options) {
- options.disableRightUpdate = true
- }
- }
- func WithDisableRightQuery() Option {
- return func(options *Options) {
- options.disableRightQuery = true
- }
- }
- func WithLeftUpdateMiddlewares(middlewares ...binding.Middleware) Option {
- return func(options *Options) {
- options.leftUpdateMiddlewares = middlewares
- }
- }
- func WithLeftQueryMiddlewares(middlewares ...binding.Middleware) Option {
- return func(options *Options) {
- options.leftQueryMiddlewares = middlewares
- }
- }
- func WithRightUpdateMiddlewares(middlewares ...binding.Middleware) Option {
- return func(options *Options) {
- options.rightUpdateMiddlewares = middlewares
- }
- }
- func WithRightQueryMiddlewares(middlewares ...binding.Middleware) Option {
- return func(options *Options) {
- options.rightQueryMiddlewares = middlewares
- }
- }
|