| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- package one2one
- import (
- "fmt"
- "git.sxidc.com/go-framework/baize/convenient/binding"
- "git.sxidc.com/go-framework/baize/convenient/binding/request"
- "git.sxidc.com/go-framework/baize/convenient/binding/response"
- "git.sxidc.com/go-framework/baize/framwork/domain"
- "github.com/iancoleman/strcase"
- )
- // Simple 关联的Bind参数
- // LI 为左边实体的Info类型
- // RI 为右边实体的Info类型
- type Simple[LI any, RI any] struct {
- // 左领域实体,注意是Entity类型
- Left domain.Entity
- // 右领域实体,注意是Entity类型
- Right domain.Entity
- // 左表名,只存在单侧表,赋值空即可
- LeftTableName string
- // 右表名,只存在单侧表,赋值空即可
- RightTableName string
- // URL领域相对路径,如/user,后面会自动补充,如/user/右领域path/update
- LeftDomainPath string
- // URL领域相对路径,如/userInfo,后面会自动补充,如/userInfo/左领域path/update
- RightDomainPath string
- // 更新左实体关联使用的请求参数
- LeftUpdateJsonBody request.WithID
- // 查询左实体关联使用的请求参数,注意是WithID类型
- LeftQueryQueryParams request.WithID
- // 查询左实体带右实体信息使用的请求参数,注意是WithID类型
- LeftQueryWithRightQueryParams request.WithID
- // 更新右实体关联使用的请求参数,注意是WithID类型
- RightUpdateJsonBody request.WithID
- // 查询右实体关联使用的请求参数,注意是WithID类型
- RightQueryQueryParams request.WithID
- // 查询右实体带左实体信息使用的请求参数,注意是WithID类型
- RightQueryWithLeftQueryParams request.WithID
- // 可选配置项,通过WithXXX配置
- options *Options
- }
- func (simple *Simple[LI, RI]) bind(binder *binding.Binder) {
- options := simple.options
- leftRelationFieldName := fmt.Sprintf("%sID", simple.Right.DomainCamelName())
- rightRelationFieldName := fmt.Sprintf("%sID", simple.Left.DomainCamelName())
- leftRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(simple.Right.DomainCamelName()))
- rightRelationColumnName := fmt.Sprintf("%s_id", strcase.ToSnake(simple.Left.DomainCamelName()))
- if !options.disableLeft {
- if !options.disableLeftUpdate {
- // 左到右更新
- binding.PostBind(binder, &binding.SimpleBindItem[any]{
- Path: simple.LeftDomainPath + simple.RightDomainPath + "/update",
- ResponseFunc: response.SendMsgResponse,
- RequestParams: simple.LeftUpdateJsonBody,
- Objects: []domain.Object{simple.Left},
- ServiceFunc: Update(simple.LeftTableName, leftRelationFieldName, leftRelationColumnName,
- simple.RightTableName, simple.Right.DomainCNName(), rightRelationColumnName),
- })
- }
- if !options.disableLeftQuery {
- // 左到右查询
- binding.GetBind(binder, &binding.SimpleBindItem[RI]{
- Path: simple.LeftDomainPath + simple.RightDomainPath + "/query",
- ResponseFunc: response.SendInfoResponse[RI],
- RequestParams: simple.LeftQueryQueryParams,
- Objects: []domain.Object{simple.Left},
- ServiceFunc: Query[RI](simple.LeftTableName, simple.RightTableName, rightRelationColumnName),
- })
- }
- if !options.disableLeftWithRightQuery {
- // 左到右查询,携带右方信息
- binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
- Path: simple.LeftDomainPath + simple.RightDomainPath + "/queryWith",
- ResponseFunc: response.SendMapResponse,
- RequestParams: simple.LeftQueryWithRightQueryParams,
- Objects: []domain.Object{simple.Left},
- ServiceFunc: QueryWithOtherInfo[LI, RI](simple.LeftTableName, simple.RightTableName, rightRelationColumnName),
- })
- }
- }
- if !options.disableRight {
- if !options.disableRightUpdate {
- // 右到左更新
- binding.PostBind(binder, &binding.SimpleBindItem[any]{
- Path: simple.RightDomainPath + simple.LeftDomainPath + "/update",
- ResponseFunc: response.SendMsgResponse,
- RequestParams: simple.RightUpdateJsonBody,
- Objects: []domain.Object{simple.Right},
- ServiceFunc: Update(simple.RightTableName, rightRelationFieldName, rightRelationColumnName,
- simple.LeftTableName, simple.Left.DomainCNName(), leftRelationColumnName),
- })
- }
- if !options.disableRightQuery {
- // 右到左查询
- binding.GetBind(binder, &binding.SimpleBindItem[RI]{
- Path: simple.RightDomainPath + simple.LeftDomainPath + "/query",
- ResponseFunc: response.SendInfoResponse[RI],
- RequestParams: simple.RightQueryQueryParams,
- Objects: []domain.Object{simple.Right},
- ServiceFunc: Query[RI](simple.RightTableName, simple.LeftTableName, leftRelationColumnName),
- })
- }
- if !options.disableRightWithLeftQuery {
- // 右到左查询,携带左方信息
- binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
- Path: simple.RightDomainPath + simple.LeftDomainPath + "/queryWith",
- ResponseFunc: response.SendMapResponse,
- RequestParams: simple.RightQueryWithLeftQueryParams,
- Objects: []domain.Object{simple.Right},
- ServiceFunc: QueryWithOtherInfo[RI, LI](simple.RightTableName, simple.LeftTableName, leftRelationColumnName),
- })
- }
- }
- }
- 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
- // 关闭左实体带右实体信息查询
- disableLeftWithRightQuery bool
- // 关闭右实体带左实体信息查询
- disableRightWithLeftQuery bool
- }
- 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 WithDisableLeftWithRightQuery() Option {
- return func(options *Options) {
- options.disableLeftWithRightQuery = true
- }
- }
- func WithDisableRightWithLeftQuery() Option {
- return func(options *Options) {
- options.disableRightWithLeftQuery = true
- }
- }
|