作者 yangfu

feat: data layout v1

... ... @@ -28,6 +28,8 @@ type LayoutCell struct {
X int `json:"-,omitempty"`
Y int `json:"-,omitempty"`
Length int `json:"-"`
BlockData []string `json:"-"`
ImageData string `json:"-"`
}
type LayoutCellData struct {
... ...
... ... @@ -27,6 +27,19 @@ func (ptr *QuerySetService) LoadCalculateSetData(ctx *domain.Context, qs *domain
q := queryComponents[0]
cells := q.Layout.LayoutCells()
dataTables = ptr.LoadDataTables(ctx, cells)
// 设置数据
dt := &DataLayoutDataTable{
DataTable: res,
MapDataTables: dataTables,
unprocessed: cells,
}
for i := range cells {
blockData, length := dt.BlockData(cells[i])
cells[i].Length = length
cells[i].BlockData = blockData
}
// 根据数据修改便宜
CellsLocationAdjust(cells)
// 数据布局
res, err = DataLayout(res, dataTables, cells)
if err != nil {
... ... @@ -36,6 +49,98 @@ func (ptr *QuerySetService) LoadCalculateSetData(ctx *domain.Context, qs *domain
return res, nil
}
func CellsLocationAdjust(cells []*domain.LayoutCell) {
xMin := 0
xMax := 0
yMin := 0
yMax := 0
if len(cells) > 0 {
xMin = cells[0].X
xMax = cells[0].X
yMin = cells[0].Y
yMax = cells[0].Y
}
min := func(a, b int) int {
if a > b {
return b
}
return a
}
max := func(a, b int) int {
if a < b {
return b
}
return a
}
for i := 1; i <= len(cells)-1; i++ {
cell := cells[i]
xMin = min(xMin, cell.X)
xMax = max(xMax, cell.X+cell.Length)
yMin = min(yMin, cell.Y)
yMax = max(yMax, cell.Y+cell.Length)
}
for j := yMin; j <= yMax; j++ {
move := 0
position := -1
for _, cell := range cells {
if cell.Y != j {
continue
}
if cell.Direction != domain.DirectionRight {
continue
}
move = cell.Length
position = cell.Y
break
}
if move == 0 || position == -1 {
continue
}
j = position + 1
ChangeLocation(cells, domain.DirectionRight, position, move)
}
for i := xMin; i <= xMax; i++ {
move := 0
position := -1
for _, cell := range cells {
if cell.X != i {
continue
}
if cell.Direction != domain.DirectionDown {
continue
}
move = cell.Length
position = cell.X
break
}
if move == 0 || position == -1 {
continue
}
i = position + 1
ChangeLocation(cells, domain.DirectionDown, position, move)
}
}
func ChangeLocation(cells []*domain.LayoutCell, direction string, position, move int) {
// log.Logger.Info("修改定位点")
if move == 0 {
return
}
for _, cell := range cells {
switch direction {
case domain.DirectionRight:
if cell.Y > position {
cell.Y += move - 1
}
case domain.DirectionDown:
if cell.X > position {
cell.X += move - 1
}
}
// log.Logger.Info(fmt.Sprintf("%s %s X:%d Y:%d", cell.Data.Field.SQLName, cell.Direction, cell.X, cell.Y))
}
}
func FastTable(table *domain.Table) (*domain.DataTable, error) {
var err error
var options = starrocks.QueryOptions{
... ... @@ -102,9 +207,9 @@ func (ptr *QuerySetService) LoadDataTables(ctx *domain.Context, cells []*domain.
func DataLayout(res *domain.DataTable, dataTables map[int]*domain.DataTable, cells []*domain.LayoutCell) (*domain.DataTable, error) {
dt := &DataLayoutDataTable{
DataTable: res,
MapDataTables: dataTables,
unprocessed: cells,
DataTable: res,
//MapDataTables: dataTables,
unprocessed: cells,
}
dt.Init(DefaultExpandNum)
for {
... ... @@ -113,13 +218,18 @@ func DataLayout(res *domain.DataTable, dataTables map[int]*domain.DataTable, cel
}
cell := dt.unprocessed[0]
dt.unprocessed = dt.unprocessed[1:]
blockData, length := dt.BlockData(cell)
if err := dt.Expand(cell, length); err != nil {
//blockData, length := dt.BlockData(cell)
//if err := dt.Expand(cell, length); err != nil {
// return nil, err
//}
//dt.addByLocation(cell, blockData)
//blockData, length := dt.BlockData(cell)
// 当前单元格子 影响其他格子坐标
//dt.changeUnProcessedLocation(cell, cell.Length)
if err := dt.Expand(cell, cell.Length); err != nil {
return nil, err
}
dt.addByLocation(cell, blockData)
// 当前单元格子 影响其他格子坐标
dt.changeUnProcessedLocation(cell, length)
dt.addByLocation(cell, cell.BlockData)
dt.processed = append(dt.processed, cell)
dt.LastCell = cell
}
... ...
package domainService
import (
"fmt"
"github.com/stretchr/testify/assert"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"strings"
"testing"
)
func TestDataLayout(t *testing.T) {
inputs := []struct {
cells []*domain.LayoutCell
flag Location
title string
}{
{
title: "配置组多组混合",
cells: []*domain.LayoutCell{
// 分组一
{
X: 0,
Y: 0,
Length: 2,
ImageData: "2",
Direction: domain.DirectionRight,
},
{
X: 0,
Y: 1,
Length: 3,
ImageData: "3",
Direction: domain.DirectionRight,
},
{
X: 1,
Y: 0,
Length: 2,
ImageData: "a",
Direction: domain.DirectionDown,
},
{
X: 1,
Y: 1,
Length: 2,
ImageData: "f",
Direction: domain.DirectionDown,
},
{
X: 1,
Y: 2,
Length: 2,
ImageData: "b",
Direction: domain.DirectionDown,
},
{
X: 1,
Y: 3,
Length: 2,
ImageData: "e",
Direction: domain.DirectionDown,
},
{
X: 2,
Y: 0,
Length: 1,
ImageData: "c",
Direction: domain.DirectionNone,
},
{
X: 2,
Y: 1,
Length: 1,
ImageData: "d",
Direction: domain.DirectionNone,
},
// 分组二 右平移10
{
X: 0,
Y: 10,
Length: 2,
ImageData: "2",
Direction: domain.DirectionRight,
},
{
X: 0,
Y: 11,
Length: 3,
ImageData: "3",
Direction: domain.DirectionRight,
},
{
X: 1,
Y: 10,
Length: 2,
ImageData: "a",
Direction: domain.DirectionDown,
},
{
X: 1,
Y: 11,
Length: 2,
ImageData: "f",
Direction: domain.DirectionDown,
},
{
X: 1,
Y: 12,
Length: 2,
ImageData: "b",
Direction: domain.DirectionDown,
},
{
X: 1,
Y: 13,
Length: 2,
ImageData: "e",
Direction: domain.DirectionDown,
},
{
X: 2,
Y: 10,
Length: 1,
ImageData: "c",
Direction: domain.DirectionNone,
},
{
X: 2,
Y: 11,
Length: 1,
ImageData: "d",
Direction: domain.DirectionNone,
},
},
flag: Location{X: 3, Y: 2},
},
{
title: "正常多字段横排",
cells: []*domain.LayoutCell{
// 分组一
{
X: 0,
Y: 0,
Length: 5,
ImageData: "a",
Direction: domain.DirectionRight,
},
{
X: 1,
Y: 0,
Length: 5,
ImageData: "b",
Direction: domain.DirectionRight,
},
{
X: 2,
Y: 0,
Length: 5,
ImageData: "c",
Direction: domain.DirectionRight,
},
{
X: 3,
Y: 0,
Length: 5,
ImageData: "d",
Direction: domain.DirectionRight,
},
// 分组二 平移10
{
X: 0,
Y: 10,
Length: 5,
ImageData: "a",
Direction: domain.DirectionRight,
},
{
X: 1,
Y: 10,
Length: 5,
ImageData: "b",
Direction: domain.DirectionRight,
},
{
X: 2,
Y: 10,
Length: 5,
ImageData: "c",
Direction: domain.DirectionRight,
},
{
X: 3,
Y: 10,
Length: 5,
ImageData: "d",
Direction: domain.DirectionRight,
},
},
flag: Location{X: 3, Y: 4},
},
{
title: "正常多字段横排+计算项目",
cells: []*domain.LayoutCell{
// 分组一
{
X: 0,
Y: 0,
Length: 5,
ImageData: "a",
Direction: domain.DirectionRight,
},
{
X: 1,
Y: 0,
Length: 5,
ImageData: "b",
Direction: domain.DirectionRight,
},
{
X: 2,
Y: 0,
Length: 1,
ImageData: "c",
Direction: domain.DirectionNone,
},
{
X: 3,
Y: 0,
Length: 1,
ImageData: "d",
Direction: domain.DirectionNone,
},
// 分组二 平移10
{
X: 0,
Y: 10,
Length: 1,
ImageData: "a",
Direction: domain.DirectionRight,
},
{
X: 1,
Y: 10,
Length: 5,
ImageData: "b",
Direction: domain.DirectionRight,
},
{
X: 2,
Y: 10,
Length: 1,
ImageData: "c",
Direction: domain.DirectionNone,
},
{
X: 2,
Y: 11,
Length: 1,
ImageData: "e",
Direction: domain.DirectionNone,
},
{
X: 3,
Y: 10,
Length: 1,
ImageData: "d",
Direction: domain.DirectionNone,
},
},
flag: Location{X: 2, Y: 1},
},
{
title: "正常多字段横排+竖排",
cells: []*domain.LayoutCell{
// 分组一
{
X: 0,
Y: 0,
Length: 5,
ImageData: "a",
Direction: domain.DirectionRight,
},
{
X: 0,
Y: 1,
Length: 5,
ImageData: "b",
Direction: domain.DirectionDown,
},
{
X: 1,
Y: 0,
Length: 5,
ImageData: "c",
Direction: domain.DirectionRight,
},
{
X: 2,
Y: 0,
Length: 5,
ImageData: "d",
Direction: domain.DirectionRight,
},
},
flag: Location{X: 2, Y: 1},
},
}
padding := func(cells []*domain.LayoutCell) {
for _, cell := range cells {
for i := 0; i < cell.Length; i++ {
cell.BlockData = append(cell.BlockData, cell.ImageData)
}
}
}
for _, input := range inputs {
padding(input.cells)
// 根据数据修改位移
CellsLocationAdjust(input.cells)
// 数据布局
res := &domain.DataTable{}
res, err := DataLayout(res, nil, input.cells)
if err != nil {
assert.NoError(t, err)
}
printRes(res)
assert.NotEmptyf(t, res.Data[input.flag.X][input.flag.Y], "单元格有值")
}
}
func printRes(res *domain.DataTable) {
strBuilder := strings.Builder{}
for i := range res.Data {
values := res.Data[i]
for i := 0; i < len(values)-1; i++ {
if values[i] == "" {
values[i] = " "
}
}
strBuilder.WriteString(strings.Join(values, " | "))
strBuilder.WriteString("\n")
}
fmt.Println(strBuilder.String())
}
... ...