|
|
@@ -0,0 +1,173 @@
|
|
|
+package gwtools
|
|
|
+
|
|
|
+import (
|
|
|
+ "git.sxidc.com/go-framework/baize/framework/gateway"
|
|
|
+ "git.sxidc.com/go-tools/utils/template"
|
|
|
+ "github.com/iancoleman/strcase"
|
|
|
+ "net/http"
|
|
|
+)
|
|
|
+
|
|
|
+func One2Many(builder *gateway.Builder, params *One2ManyParams, opts ...One2ManyOption) {
|
|
|
+ options := new(One2ManyOptions)
|
|
|
+
|
|
|
+ for _, opt := range opts {
|
|
|
+ opt(options)
|
|
|
+ }
|
|
|
+
|
|
|
+ params.options = options
|
|
|
+
|
|
|
+ params.one2many(builder)
|
|
|
+}
|
|
|
+
|
|
|
+type One2ManyParams struct {
|
|
|
+ // 除去后缀的服务URL,如http://localhost:8080/example/api/v1
|
|
|
+ ServiceVersionedUrl string
|
|
|
+
|
|
|
+ // 左领域名称
|
|
|
+ LeftDomainCamelName string
|
|
|
+
|
|
|
+ // 右领域名称
|
|
|
+ RightDomainCamelName string
|
|
|
+
|
|
|
+ // 可选配置项,通过WithXXX配置
|
|
|
+ options *One2ManyOptions
|
|
|
+}
|
|
|
+
|
|
|
+func (params *One2ManyParams) one2many(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.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").
|
|
|
+ Get(&gateway.GetRequest{
|
|
|
+ Url: params.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/queryWith",
|
|
|
+ QueryParams: AddQueryParamsTenantIDAndUserID("tenantId", "",
|
|
|
+ params.options.rightWithLeftQueryGetTenantIDFunc, nil),
|
|
|
+ }, nil).
|
|
|
+ Build()
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+type One2ManyOption func(options *One2ManyOptions)
|
|
|
+
|
|
|
+type One2ManyOptions struct {
|
|
|
+ // 关闭左侧到右侧关联
|
|
|
+ disableLeft bool
|
|
|
+
|
|
|
+ // 关闭右侧到左侧关联
|
|
|
+ disableRight bool
|
|
|
+
|
|
|
+ // 关闭左侧更新
|
|
|
+ disableLeftUpdate bool
|
|
|
+
|
|
|
+ // 关闭左侧查询
|
|
|
+ disableLeftQuery bool
|
|
|
+
|
|
|
+ // 关闭右侧更新
|
|
|
+ disableRightUpdate bool
|
|
|
+
|
|
|
+ // 关闭右侧查询
|
|
|
+ disableRightQuery bool
|
|
|
+
|
|
|
+ // 关闭右实体带左实体信息查询
|
|
|
+ disableRightWithLeftQuery bool
|
|
|
+
|
|
|
+ // 获取租户ID的接口
|
|
|
+ rightWithLeftQueryGetTenantIDFunc GetTenantIDFunc
|
|
|
+}
|
|
|
+
|
|
|
+func WithOne2ManyDisableLeft() One2ManyOption {
|
|
|
+ return func(options *One2ManyOptions) {
|
|
|
+ options.disableLeft = true
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithOne2ManyDisableRight() One2ManyOption {
|
|
|
+ return func(options *One2ManyOptions) {
|
|
|
+ options.disableRight = true
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithOne2ManyDisableLeftUpdate() One2ManyOption {
|
|
|
+ return func(options *One2ManyOptions) {
|
|
|
+ options.disableLeftUpdate = true
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithOne2ManyDisableLeftQuery() One2ManyOption {
|
|
|
+ return func(options *One2ManyOptions) {
|
|
|
+ options.disableLeftQuery = true
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithOne2ManyDisableRightUpdate() One2ManyOption {
|
|
|
+ return func(options *One2ManyOptions) {
|
|
|
+ options.disableRightUpdate = true
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithOne2ManyDisableRightQuery() One2ManyOption {
|
|
|
+ return func(options *One2ManyOptions) {
|
|
|
+ options.disableRightQuery = true
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithOne2ManyDisableRightWithLeftQuery() One2ManyOption {
|
|
|
+ return func(options *One2ManyOptions) {
|
|
|
+ options.disableRightWithLeftQuery = true
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func WithOne2ManyRightWithLeftQueryGetTenantIDFunc(getTenantIDFunc GetTenantIDFunc) One2OneOption {
|
|
|
+ return func(options *One2OneOptions) {
|
|
|
+ options.rightWithLeftQueryGetTenantIDFunc = getTenantIDFunc
|
|
|
+ }
|
|
|
+}
|