package gwtools import ( "git.sxidc.com/go-framework/baize/framework/core/api" "git.sxidc.com/go-framework/baize/framework/gateway" "git.sxidc.com/go-tools/utils/template" "github.com/iancoleman/strcase" "net/http" ) func One2One(builder *gateway.Builder, params *One2OneParams, opts ...One2OneOption) { options := new(One2OneOptions) for _, opt := range opts { opt(options) } params.options = options params.one2one(builder) } type One2OneParams struct { // 除去后缀的服务URL,如http://localhost:8080/example/api/v1 ServiceVersionedUrl string // 左领域名称 LeftDomainCamelName string // 右领域名称 RightDomainCamelName string // 可选配置项,通过WithXXX配置 options *One2OneOptions } func (params *One2OneParams) one2one(builder *gateway.Builder) { options := params.options leftDomainPath := "/" + strcase.ToLowerCamel(template.Id(params.LeftDomainCamelName)) rightDomainPath := "/" + strcase.ToLowerCamel(template.Id(params.RightDomainCamelName)) if !options.disableLeft { if !options.disableLeftUpdate { // 左到右更新 builder. Url(http.MethodPost, leftDomainPath+rightDomainPath+"/update"). Post(&gateway.PostRequest{ Url: params.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/update", }, nil). Build() } if !options.disableLeftQuery { // 左到右查询 builder. Url(http.MethodGet, leftDomainPath+rightDomainPath+"/query"). Get(&gateway.GetRequest{ Url: params.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/query", }, nil). Build() } if !options.disableLeftWithRightQuery { // 左到右查询,携带右方信息 builder. Url(http.MethodGet, leftDomainPath+rightDomainPath+"/queryWith"). Request(func(c *api.Context, historyRequests []gateway.BuilderRequest, resultMap map[string]any) (gateway.BuilderRequest, error) { newQueryParams, err := gateway.AddQueryParamsTenantIDAndUserID(c, "tenantId", "") if err != nil { return nil, err } return &gateway.GetRequest{ Url: params.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/queryWith", QueryParams: newQueryParams, }, nil }, nil). Build() } } if !options.disableRight { if !options.disableRightUpdate { // 右到左更新 builder. Url(http.MethodPost, rightDomainPath+leftDomainPath+"/update"). Post(&gateway.PostRequest{ Url: params.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/update", }, nil). Build() } if !options.disableRightQuery { // 右到左查询 builder. Url(http.MethodGet, rightDomainPath+leftDomainPath+"/query"). Get(&gateway.GetRequest{ Url: params.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/query", }, nil). Build() } if !options.disableRightWithLeftQuery { // 右到左查询,携带左方信息 builder. Url(http.MethodGet, rightDomainPath+leftDomainPath+"/queryWith"). Request(func(c *api.Context, historyRequests []gateway.BuilderRequest, resultMap map[string]any) (gateway.BuilderRequest, error) { newQueryParams, err := gateway.AddQueryParamsTenantIDAndUserID(c, "tenantId", "") if err != nil { return nil, err } return &gateway.GetRequest{ Url: params.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/queryWith", QueryParams: newQueryParams, }, nil }, nil). Build() } } } type One2OneOption func(options *One2OneOptions) type One2OneOptions struct { // 关闭左侧到右侧关联 disableLeft bool // 关闭右侧到左侧关联 disableRight bool // 关闭左侧更新 disableLeftUpdate bool // 关闭左侧查询 disableLeftQuery bool // 关闭右侧更新 disableRightUpdate bool // 关闭右侧查询 disableRightQuery bool // 关闭左实体带右实体信息查询 disableLeftWithRightQuery bool // 关闭右实体带左实体信息查询 disableRightWithLeftQuery bool } func WithOne2OneDisableLeft() One2OneOption { return func(options *One2OneOptions) { options.disableLeft = true } } func WithOne2OneDisableRight() One2OneOption { return func(options *One2OneOptions) { options.disableRight = true } } func WithOne2OneDisableLeftUpdate() One2OneOption { return func(options *One2OneOptions) { options.disableLeftUpdate = true } } func WithOne2OneDisableLeftQuery() One2OneOption { return func(options *One2OneOptions) { options.disableLeftQuery = true } } func WithOne2OneDisableRightUpdate() One2OneOption { return func(options *One2OneOptions) { options.disableRightUpdate = true } } func WithOne2OneDisableRightQuery() One2OneOption { return func(options *One2OneOptions) { options.disableRightQuery = true } } func WithOne2OneDisableLeftWithRightQuery() One2OneOption { return func(options *One2OneOptions) { options.disableLeftWithRightQuery = true } } func WithOne2OneDisableRightWithLeftQuery() One2OneOption { return func(options *One2OneOptions) { options.disableRightWithLeftQuery = true } }