query_set.go 3.1 KB
package domain

import (
	"fmt"
	"github.com/google/uuid"
	"github.com/zeromicro/go-zero/core/collection"
	"sort"
	"time"
)

// 查询集合
type QuerySet struct {
	// 查询集合ID
	QuerySetId int `json:"querySetId"`
	// Schema:方案  SubProcess:子过程
	Type string `json:"type"`
	// 标识 分组:Group 子过程/方案:Set
	Flag string `json:"flag"`
	// 名称
	Name string `json:"name"`
	// 拼音(排序使用)
	PinName string `json:"pinName"`
	// 父级ID
	ParentId int `json:"parentId"`
	// 状态  1:启用 2:关闭 (子过程默认启用)
	Status int `json:"status"`
	// 查询集合信息
	QuerySetInfo *QuerySetInfo `json:"querySetInfo"`
	// 查询组件
	QueryComponents []*QueryComponent `json:"queryComponents"`
	// 排序
	Sort int `json:"sort"`
	// 创建时间
	CreatedAt time.Time `json:"createdAt"`
	// 更新时间
	UpdatedAt time.Time `json:"updatedAt"`
	// 删除时间
	DeletedAt time.Time `json:"deletedAt"`
	// 扩展
	Context *Context `json:"context"`
}

type QuerySetRepository interface {
	Save(querySet *QuerySet) (*QuerySet, error)
	Remove(querySet *QuerySet) (*QuerySet, error)
	FindOne(queryOptions map[string]interface{}) (*QuerySet, error)
	Find(queryOptions map[string]interface{}) (int64, []*QuerySet, error)
}

func (querySet *QuerySet) Identify() interface{} {
	if querySet.QuerySetId == 0 {
		return nil
	}
	return querySet.QuerySetId
}

func (querySet *QuerySet) Update(queryComponents []*QueryComponent, tableId int) error {
	for i := range queryComponents {
		if len(queryComponents[i].Id) == 0 {
			uuid, _ := uuid.NewUUID()
			queryComponents[i].Id = uuid.String()
		}
		for j := range queryComponents[i].Conditions {
			if len(queryComponents[i].Conditions[j].Id) == 0 {
				uuid, _ := uuid.NewUUID()
				queryComponents[i].Conditions[j].Id = uuid.String()
			}
		}
		for j := range queryComponents[i].Selects {
			if len(queryComponents[i].Selects[j].Id) == 0 {
				uuid, _ := uuid.NewUUID()
				queryComponents[i].Selects[j].Id = uuid.String()
			}
		}
	}
	querySet.QueryComponents = queryComponents
	if querySet.QuerySetInfo.BindTableId == 0 {
		querySet.QuerySetInfo.BindTableId = tableId
	}
	querySet.UpdatedAt = time.Now()
	return nil
}

func ValidQuerySetType(t string) error {
	if t == SchemaTable.ToString() || t == SubProcessTable.ToString() {
		return nil
	}
	return fmt.Errorf("类型有误")
}

func ValidQuerySetFlag(t string) error {
	if t == FlagGroup || t == FlagSet {
		return nil
	}
	return fmt.Errorf("标识有误")
}

func (querySet *QuerySet) GetDependencyTables(queryComponents []*QueryComponent) []int {
	set := collection.NewSet()

	for i := range queryComponents {
		set.AddInt(queryComponents[i].MasterTable.TableId)
		for _, c := range queryComponents[i].Conditions {
			for _, f := range c.FieldLeft.TableFields {
				set.AddInt(f.TableId)
			}
			for _, f := range c.FieldRight.TableFields {
				set.AddInt(f.TableId)
			}
		}
		for _, s := range queryComponents[i].Selects {
			for _, f := range s.FieldLeft.TableFields {
				set.AddInt(f.TableId)
			}
			for _, f := range s.FieldRight.TableFields {
				set.AddInt(f.TableId)
			}
		}
	}
	res := set.KeysInt()
	sort.Ints(res)
	return res
}