作者 yangfu

feat: data layout v1

@@ -28,6 +28,8 @@ type LayoutCell struct { @@ -28,6 +28,8 @@ type LayoutCell struct {
28 X int `json:"-,omitempty"` 28 X int `json:"-,omitempty"`
29 Y int `json:"-,omitempty"` 29 Y int `json:"-,omitempty"`
30 Length int `json:"-"` 30 Length int `json:"-"`
  31 + BlockData []string `json:"-"`
  32 + ImageData string `json:"-"`
31 } 33 }
32 34
33 type LayoutCellData struct { 35 type LayoutCellData struct {
@@ -27,6 +27,19 @@ func (ptr *QuerySetService) LoadCalculateSetData(ctx *domain.Context, qs *domain @@ -27,6 +27,19 @@ func (ptr *QuerySetService) LoadCalculateSetData(ctx *domain.Context, qs *domain
27 q := queryComponents[0] 27 q := queryComponents[0]
28 cells := q.Layout.LayoutCells() 28 cells := q.Layout.LayoutCells()
29 dataTables = ptr.LoadDataTables(ctx, cells) 29 dataTables = ptr.LoadDataTables(ctx, cells)
  30 + // 设置数据
  31 + dt := &DataLayoutDataTable{
  32 + DataTable: res,
  33 + MapDataTables: dataTables,
  34 + unprocessed: cells,
  35 + }
  36 + for i := range cells {
  37 + blockData, length := dt.BlockData(cells[i])
  38 + cells[i].Length = length
  39 + cells[i].BlockData = blockData
  40 + }
  41 + // 根据数据修改便宜
  42 + CellsLocationAdjust(cells)
30 // 数据布局 43 // 数据布局
31 res, err = DataLayout(res, dataTables, cells) 44 res, err = DataLayout(res, dataTables, cells)
32 if err != nil { 45 if err != nil {
@@ -36,6 +49,98 @@ func (ptr *QuerySetService) LoadCalculateSetData(ctx *domain.Context, qs *domain @@ -36,6 +49,98 @@ func (ptr *QuerySetService) LoadCalculateSetData(ctx *domain.Context, qs *domain
36 return res, nil 49 return res, nil
37 } 50 }
38 51
  52 +func CellsLocationAdjust(cells []*domain.LayoutCell) {
  53 + xMin := 0
  54 + xMax := 0
  55 + yMin := 0
  56 + yMax := 0
  57 + if len(cells) > 0 {
  58 + xMin = cells[0].X
  59 + xMax = cells[0].X
  60 + yMin = cells[0].Y
  61 + yMax = cells[0].Y
  62 + }
  63 + min := func(a, b int) int {
  64 + if a > b {
  65 + return b
  66 + }
  67 + return a
  68 + }
  69 + max := func(a, b int) int {
  70 + if a < b {
  71 + return b
  72 + }
  73 + return a
  74 + }
  75 + for i := 1; i <= len(cells)-1; i++ {
  76 + cell := cells[i]
  77 + xMin = min(xMin, cell.X)
  78 + xMax = max(xMax, cell.X+cell.Length)
  79 + yMin = min(yMin, cell.Y)
  80 + yMax = max(yMax, cell.Y+cell.Length)
  81 + }
  82 + for j := yMin; j <= yMax; j++ {
  83 + move := 0
  84 + position := -1
  85 + for _, cell := range cells {
  86 + if cell.Y != j {
  87 + continue
  88 + }
  89 + if cell.Direction != domain.DirectionRight {
  90 + continue
  91 + }
  92 + move = cell.Length
  93 + position = cell.Y
  94 + break
  95 + }
  96 + if move == 0 || position == -1 {
  97 + continue
  98 + }
  99 + j = position + 1
  100 + ChangeLocation(cells, domain.DirectionRight, position, move)
  101 + }
  102 + for i := xMin; i <= xMax; i++ {
  103 + move := 0
  104 + position := -1
  105 + for _, cell := range cells {
  106 + if cell.X != i {
  107 + continue
  108 + }
  109 + if cell.Direction != domain.DirectionDown {
  110 + continue
  111 + }
  112 + move = cell.Length
  113 + position = cell.X
  114 + break
  115 + }
  116 + if move == 0 || position == -1 {
  117 + continue
  118 + }
  119 + i = position + 1
  120 + ChangeLocation(cells, domain.DirectionDown, position, move)
  121 + }
  122 +}
  123 +
  124 +func ChangeLocation(cells []*domain.LayoutCell, direction string, position, move int) {
  125 + // log.Logger.Info("修改定位点")
  126 + if move == 0 {
  127 + return
  128 + }
  129 + for _, cell := range cells {
  130 + switch direction {
  131 + case domain.DirectionRight:
  132 + if cell.Y > position {
  133 + cell.Y += move - 1
  134 + }
  135 + case domain.DirectionDown:
  136 + if cell.X > position {
  137 + cell.X += move - 1
  138 + }
  139 + }
  140 + // log.Logger.Info(fmt.Sprintf("%s %s X:%d Y:%d", cell.Data.Field.SQLName, cell.Direction, cell.X, cell.Y))
  141 + }
  142 +}
  143 +
39 func FastTable(table *domain.Table) (*domain.DataTable, error) { 144 func FastTable(table *domain.Table) (*domain.DataTable, error) {
40 var err error 145 var err error
41 var options = starrocks.QueryOptions{ 146 var options = starrocks.QueryOptions{
@@ -102,9 +207,9 @@ func (ptr *QuerySetService) LoadDataTables(ctx *domain.Context, cells []*domain. @@ -102,9 +207,9 @@ func (ptr *QuerySetService) LoadDataTables(ctx *domain.Context, cells []*domain.
102 207
103 func DataLayout(res *domain.DataTable, dataTables map[int]*domain.DataTable, cells []*domain.LayoutCell) (*domain.DataTable, error) { 208 func DataLayout(res *domain.DataTable, dataTables map[int]*domain.DataTable, cells []*domain.LayoutCell) (*domain.DataTable, error) {
104 dt := &DataLayoutDataTable{ 209 dt := &DataLayoutDataTable{
105 - DataTable: res,  
106 - MapDataTables: dataTables,  
107 - unprocessed: cells, 210 + DataTable: res,
  211 + //MapDataTables: dataTables,
  212 + unprocessed: cells,
108 } 213 }
109 dt.Init(DefaultExpandNum) 214 dt.Init(DefaultExpandNum)
110 for { 215 for {
@@ -113,13 +218,18 @@ func DataLayout(res *domain.DataTable, dataTables map[int]*domain.DataTable, cel @@ -113,13 +218,18 @@ func DataLayout(res *domain.DataTable, dataTables map[int]*domain.DataTable, cel
113 } 218 }
114 cell := dt.unprocessed[0] 219 cell := dt.unprocessed[0]
115 dt.unprocessed = dt.unprocessed[1:] 220 dt.unprocessed = dt.unprocessed[1:]
116 - blockData, length := dt.BlockData(cell)  
117 - if err := dt.Expand(cell, length); err != nil { 221 + //blockData, length := dt.BlockData(cell)
  222 + //if err := dt.Expand(cell, length); err != nil {
  223 + // return nil, err
  224 + //}
  225 + //dt.addByLocation(cell, blockData)
  226 + //blockData, length := dt.BlockData(cell)
  227 + // 当前单元格子 影响其他格子坐标
  228 + //dt.changeUnProcessedLocation(cell, cell.Length)
  229 + if err := dt.Expand(cell, cell.Length); err != nil {
118 return nil, err 230 return nil, err
119 } 231 }
120 - dt.addByLocation(cell, blockData)  
121 - // 当前单元格子 影响其他格子坐标  
122 - dt.changeUnProcessedLocation(cell, length) 232 + dt.addByLocation(cell, cell.BlockData)
123 dt.processed = append(dt.processed, cell) 233 dt.processed = append(dt.processed, cell)
124 dt.LastCell = cell 234 dt.LastCell = cell
125 } 235 }
  1 +package domainService
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/stretchr/testify/assert"
  6 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  7 + "strings"
  8 + "testing"
  9 +)
  10 +
  11 +func TestDataLayout(t *testing.T) {
  12 + inputs := []struct {
  13 + cells []*domain.LayoutCell
  14 + flag Location
  15 + title string
  16 + }{
  17 + {
  18 + title: "配置组多组混合",
  19 + cells: []*domain.LayoutCell{
  20 + // 分组一
  21 + {
  22 + X: 0,
  23 + Y: 0,
  24 + Length: 2,
  25 + ImageData: "2",
  26 + Direction: domain.DirectionRight,
  27 + },
  28 + {
  29 + X: 0,
  30 + Y: 1,
  31 + Length: 3,
  32 + ImageData: "3",
  33 + Direction: domain.DirectionRight,
  34 + },
  35 + {
  36 + X: 1,
  37 + Y: 0,
  38 + Length: 2,
  39 + ImageData: "a",
  40 + Direction: domain.DirectionDown,
  41 + },
  42 + {
  43 + X: 1,
  44 + Y: 1,
  45 + Length: 2,
  46 + ImageData: "f",
  47 + Direction: domain.DirectionDown,
  48 + },
  49 + {
  50 + X: 1,
  51 + Y: 2,
  52 + Length: 2,
  53 + ImageData: "b",
  54 + Direction: domain.DirectionDown,
  55 + },
  56 + {
  57 + X: 1,
  58 + Y: 3,
  59 + Length: 2,
  60 + ImageData: "e",
  61 + Direction: domain.DirectionDown,
  62 + },
  63 + {
  64 + X: 2,
  65 + Y: 0,
  66 + Length: 1,
  67 + ImageData: "c",
  68 + Direction: domain.DirectionNone,
  69 + },
  70 + {
  71 + X: 2,
  72 + Y: 1,
  73 + Length: 1,
  74 + ImageData: "d",
  75 + Direction: domain.DirectionNone,
  76 + },
  77 +
  78 + // 分组二 右平移10
  79 + {
  80 + X: 0,
  81 + Y: 10,
  82 + Length: 2,
  83 + ImageData: "2",
  84 + Direction: domain.DirectionRight,
  85 + },
  86 + {
  87 + X: 0,
  88 + Y: 11,
  89 + Length: 3,
  90 + ImageData: "3",
  91 + Direction: domain.DirectionRight,
  92 + },
  93 + {
  94 + X: 1,
  95 + Y: 10,
  96 + Length: 2,
  97 + ImageData: "a",
  98 + Direction: domain.DirectionDown,
  99 + },
  100 + {
  101 + X: 1,
  102 + Y: 11,
  103 + Length: 2,
  104 + ImageData: "f",
  105 + Direction: domain.DirectionDown,
  106 + },
  107 + {
  108 + X: 1,
  109 + Y: 12,
  110 + Length: 2,
  111 + ImageData: "b",
  112 + Direction: domain.DirectionDown,
  113 + },
  114 + {
  115 + X: 1,
  116 + Y: 13,
  117 + Length: 2,
  118 + ImageData: "e",
  119 + Direction: domain.DirectionDown,
  120 + },
  121 + {
  122 + X: 2,
  123 + Y: 10,
  124 + Length: 1,
  125 + ImageData: "c",
  126 + Direction: domain.DirectionNone,
  127 + },
  128 + {
  129 + X: 2,
  130 + Y: 11,
  131 + Length: 1,
  132 + ImageData: "d",
  133 + Direction: domain.DirectionNone,
  134 + },
  135 + },
  136 + flag: Location{X: 3, Y: 2},
  137 + },
  138 + {
  139 + title: "正常多字段横排",
  140 + cells: []*domain.LayoutCell{
  141 + // 分组一
  142 + {
  143 + X: 0,
  144 + Y: 0,
  145 + Length: 5,
  146 + ImageData: "a",
  147 + Direction: domain.DirectionRight,
  148 + },
  149 + {
  150 + X: 1,
  151 + Y: 0,
  152 + Length: 5,
  153 + ImageData: "b",
  154 + Direction: domain.DirectionRight,
  155 + },
  156 + {
  157 + X: 2,
  158 + Y: 0,
  159 + Length: 5,
  160 + ImageData: "c",
  161 + Direction: domain.DirectionRight,
  162 + },
  163 + {
  164 + X: 3,
  165 + Y: 0,
  166 + Length: 5,
  167 + ImageData: "d",
  168 + Direction: domain.DirectionRight,
  169 + },
  170 +
  171 + // 分组二 平移10
  172 + {
  173 + X: 0,
  174 + Y: 10,
  175 + Length: 5,
  176 + ImageData: "a",
  177 + Direction: domain.DirectionRight,
  178 + },
  179 + {
  180 + X: 1,
  181 + Y: 10,
  182 + Length: 5,
  183 + ImageData: "b",
  184 + Direction: domain.DirectionRight,
  185 + },
  186 + {
  187 + X: 2,
  188 + Y: 10,
  189 + Length: 5,
  190 + ImageData: "c",
  191 + Direction: domain.DirectionRight,
  192 + },
  193 + {
  194 + X: 3,
  195 + Y: 10,
  196 + Length: 5,
  197 + ImageData: "d",
  198 + Direction: domain.DirectionRight,
  199 + },
  200 + },
  201 + flag: Location{X: 3, Y: 4},
  202 + },
  203 +
  204 + {
  205 + title: "正常多字段横排+计算项目",
  206 + cells: []*domain.LayoutCell{
  207 + // 分组一
  208 + {
  209 + X: 0,
  210 + Y: 0,
  211 + Length: 5,
  212 + ImageData: "a",
  213 + Direction: domain.DirectionRight,
  214 + },
  215 + {
  216 + X: 1,
  217 + Y: 0,
  218 + Length: 5,
  219 + ImageData: "b",
  220 + Direction: domain.DirectionRight,
  221 + },
  222 + {
  223 + X: 2,
  224 + Y: 0,
  225 + Length: 1,
  226 + ImageData: "c",
  227 + Direction: domain.DirectionNone,
  228 + },
  229 + {
  230 + X: 3,
  231 + Y: 0,
  232 + Length: 1,
  233 + ImageData: "d",
  234 + Direction: domain.DirectionNone,
  235 + },
  236 +
  237 + // 分组二 平移10
  238 + {
  239 + X: 0,
  240 + Y: 10,
  241 + Length: 1,
  242 + ImageData: "a",
  243 + Direction: domain.DirectionRight,
  244 + },
  245 + {
  246 + X: 1,
  247 + Y: 10,
  248 + Length: 5,
  249 + ImageData: "b",
  250 + Direction: domain.DirectionRight,
  251 + },
  252 + {
  253 + X: 2,
  254 + Y: 10,
  255 + Length: 1,
  256 + ImageData: "c",
  257 + Direction: domain.DirectionNone,
  258 + },
  259 + {
  260 + X: 2,
  261 + Y: 11,
  262 + Length: 1,
  263 + ImageData: "e",
  264 + Direction: domain.DirectionNone,
  265 + },
  266 + {
  267 + X: 3,
  268 + Y: 10,
  269 + Length: 1,
  270 + ImageData: "d",
  271 + Direction: domain.DirectionNone,
  272 + },
  273 + },
  274 + flag: Location{X: 2, Y: 1},
  275 + },
  276 +
  277 + {
  278 + title: "正常多字段横排+竖排",
  279 + cells: []*domain.LayoutCell{
  280 + // 分组一
  281 + {
  282 + X: 0,
  283 + Y: 0,
  284 + Length: 5,
  285 + ImageData: "a",
  286 + Direction: domain.DirectionRight,
  287 + },
  288 + {
  289 + X: 0,
  290 + Y: 1,
  291 + Length: 5,
  292 + ImageData: "b",
  293 + Direction: domain.DirectionDown,
  294 + },
  295 + {
  296 + X: 1,
  297 + Y: 0,
  298 + Length: 5,
  299 + ImageData: "c",
  300 + Direction: domain.DirectionRight,
  301 + },
  302 + {
  303 + X: 2,
  304 + Y: 0,
  305 + Length: 5,
  306 + ImageData: "d",
  307 + Direction: domain.DirectionRight,
  308 + },
  309 + },
  310 + flag: Location{X: 2, Y: 1},
  311 + },
  312 + }
  313 + padding := func(cells []*domain.LayoutCell) {
  314 + for _, cell := range cells {
  315 + for i := 0; i < cell.Length; i++ {
  316 + cell.BlockData = append(cell.BlockData, cell.ImageData)
  317 + }
  318 + }
  319 + }
  320 + for _, input := range inputs {
  321 + padding(input.cells)
  322 + // 根据数据修改位移
  323 + CellsLocationAdjust(input.cells)
  324 + // 数据布局
  325 + res := &domain.DataTable{}
  326 + res, err := DataLayout(res, nil, input.cells)
  327 + if err != nil {
  328 + assert.NoError(t, err)
  329 + }
  330 + printRes(res)
  331 + assert.NotEmptyf(t, res.Data[input.flag.X][input.flag.Y], "单元格有值")
  332 + }
  333 +}
  334 +
  335 +func printRes(res *domain.DataTable) {
  336 + strBuilder := strings.Builder{}
  337 + for i := range res.Data {
  338 + values := res.Data[i]
  339 + for i := 0; i < len(values)-1; i++ {
  340 + if values[i] == "" {
  341 + values[i] = " "
  342 + }
  343 + }
  344 + strBuilder.WriteString(strings.Join(values, " | "))
  345 + strBuilder.WriteString("\n")
  346 + }
  347 + fmt.Println(strBuilder.String())
  348 +}