query_set.go 4.3 KB
package domain

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

// QuerySet 查询集合
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() ||
		t == CalculateItem.ToString() ||
		t == CalculateTable.ToString() ||
		t == CalculateSet.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 {
		if queryComponents[i].MasterTable != nil && queryComponents[i].MasterTable.TableId > 0 {
			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)
			}
		}
		if queryComponents[i].Formula != nil {
			for _, f := range queryComponents[i].Formula.TableFields {
				set.AddInt(f.TableId)
			}
		}
		if queryComponents[i].Layout != nil {
			for _, c := range queryComponents[i].Layout.LayoutCells() {
				set.AddInt(c.Data.TableField.TableId)
			}
		}
	}
	res := set.KeysInt()
	sort.Ints(res)
	return res
}

func (querySet *QuerySet) Valid(queryComponents []*QueryComponent) error {
	switch querySet.Type {
	case CalculateTable.ToString():
		if len(queryComponents) == 0 {
			return fmt.Errorf("行、值不能同时为空")
		}
		qc := queryComponents[0]
		set := collection.NewSet()
		for _, f := range qc.Aggregation.AggregationFields() {
			if !set.Contains(f.DisplayName) {
				set.AddStr(f.DisplayName)
			} else {
				return fmt.Errorf("字段'%s'存在重名,请进行重命名", f.DisplayName)
			}
		}
	}
	return nil
}

type QuerySets []*QuerySet

func (querySets QuerySets) ToMap() map[int]*QuerySet {
	var result = make(map[int]*QuerySet)
	for i := range querySets {
		result[querySets[i].QuerySetId] = querySets[i]
	}
	return result
}