query_set_components_layout_rule.go 1.8 KB
package domain

import (
	"github.com/zeromicro/go-zero/core/collection"
	"sort"
	"strings"
)

const (
	CellTypeTable      = "Table"
	CellTypeTableField = "TableField"
	CellTypeText       = "Text"
	CellTypeNull       = "Null"
)

const (
	DirectionNone  = "Null"
	DirectionRight = "Right"
	DirectionDown  = "Down"
)

type LayoutRule struct {
	LayoutRuleItem
}

type LayoutRuleItem struct {
	Cells []*LayoutCell `json:"cells"`
}

type LayoutCell struct {
	Type      string          `json:"type,omitempty"` // Table TableField  Text Null
	Data      *LayoutCellData `json:"data,omitempty"`
	Direction string          `json:"direction,omitempty"` // 向右:Right 向下:Down
	Position  *Location       `json:"position"`
	X         int             `json:"-"`
	Y         int             `json:"-"`
	Length    int             `json:"-"`
	BlockData []string        `json:"-"`
	ImageData string          `json:"-"`
}
type Location struct {
	X int `json:"x"`
	Y int `json:"y"`
}

type LayoutCellData struct {
	TableField *TableField `json:"tableField"`
	Text       string      `json:"text,omitempty"`
}

func (l *LayoutRuleItem) LayoutCells() []*LayoutCell {
	var cells = make([]*LayoutCell, 0)
	// 排序
	sort.SliceStable(l.Cells, func(i, j int) bool {
		if l.Cells[i].Position.X == l.Cells[j].Position.X {
			return l.Cells[i].Position.Y < l.Cells[j].Position.Y
		}
		return l.Cells[i].Position.X < l.Cells[j].Position.X
	})
	for _, item := range l.Cells {
		item.X = item.Position.X
		item.Y = item.Position.Y
		cells = append(cells, item)
	}
	return cells
}

type LayoutCells []*LayoutCell

func (cells LayoutCells) CellsRange(direction string) []int {
	list := collection.NewSet()
	for i := range cells {
		if strings.ToLower(direction) == "x" {
			list.Add(cells[i].X)
		} else {
			list.Add(cells[i].Y)
		}
	}
	sortList := list.KeysInt()
	sort.Ints(sortList)
	return sortList
}