作者 yangfu

feat: data layout v2

@@ -81,7 +81,7 @@ func CellsLocationAdjust(cells []*domain.LayoutCell) { @@ -81,7 +81,7 @@ func CellsLocationAdjust(cells []*domain.LayoutCell) {
81 } 81 }
82 for j := yMin; j <= yMax; j++ { 82 for j := yMin; j <= yMax; j++ {
83 move := 0 83 move := 0
84 - position := -1 84 + var c *domain.LayoutCell
85 for _, cell := range cells { 85 for _, cell := range cells {
86 if cell.Y != j { 86 if cell.Y != j {
87 continue 87 continue
@@ -89,19 +89,19 @@ func CellsLocationAdjust(cells []*domain.LayoutCell) { @@ -89,19 +89,19 @@ func CellsLocationAdjust(cells []*domain.LayoutCell) {
89 if cell.Direction != domain.DirectionRight { 89 if cell.Direction != domain.DirectionRight {
90 continue 90 continue
91 } 91 }
92 - move = cell.Length  
93 - position = cell.Y  
94 - break  
95 - }  
96 - if move == 0 || position == -1 {  
97 - continue 92 + //move = cell.Length
  93 + //c = cell
  94 + if max(move, cell.Length) != move {
  95 + c = cell
  96 + }
  97 + move = max(move, cell.Length)
  98 + //break
98 } 99 }
99 - j = position + 1  
100 - ChangeLocation(cells, domain.DirectionRight, position, move) 100 + ChangeLocation(cells, domain.DirectionRight, j, move, c)
101 } 101 }
102 for i := xMin; i <= xMax; i++ { 102 for i := xMin; i <= xMax; i++ {
103 move := 0 103 move := 0
104 - position := -1 104 + var c *domain.LayoutCell
105 for _, cell := range cells { 105 for _, cell := range cells {
106 if cell.X != i { 106 if cell.X != i {
107 continue 107 continue
@@ -109,19 +109,19 @@ func CellsLocationAdjust(cells []*domain.LayoutCell) { @@ -109,19 +109,19 @@ func CellsLocationAdjust(cells []*domain.LayoutCell) {
109 if cell.Direction != domain.DirectionDown { 109 if cell.Direction != domain.DirectionDown {
110 continue 110 continue
111 } 111 }
112 - move = cell.Length  
113 - position = cell.X  
114 - break  
115 - }  
116 - if move == 0 || position == -1 {  
117 - continue 112 + //move = cell.Length
  113 + //c = cell
  114 + if max(move, cell.Length) != move {
  115 + c = cell
  116 + }
  117 + move = max(move, cell.Length)
  118 + //break
118 } 119 }
119 - i = position + 1  
120 - ChangeLocation(cells, domain.DirectionDown, position, move) 120 + ChangeLocation(cells, domain.DirectionDown, i, move, c)
121 } 121 }
122 } 122 }
123 123
124 -func ChangeLocation(cells []*domain.LayoutCell, direction string, position, move int) { 124 +func ChangeLocation(cells []*domain.LayoutCell, direction string, position, move int, c *domain.LayoutCell) {
125 // log.Logger.Info("修改定位点") 125 // log.Logger.Info("修改定位点")
126 if move == 0 { 126 if move == 0 {
127 return 127 return
@@ -133,7 +133,7 @@ func ChangeLocation(cells []*domain.LayoutCell, direction string, position, move @@ -133,7 +133,7 @@ func ChangeLocation(cells []*domain.LayoutCell, direction string, position, move
133 cell.Y += move - 1 133 cell.Y += move - 1
134 } 134 }
135 case domain.DirectionDown: 135 case domain.DirectionDown:
136 - if cell.X > position { 136 + if cell.X > position && cell.Y >= c.Y {
137 cell.X += move - 1 137 cell.X += move - 1
138 } 138 }
139 } 139 }
@@ -75,7 +75,7 @@ func TestDataLayout(t *testing.T) { @@ -75,7 +75,7 @@ func TestDataLayout(t *testing.T) {
75 Direction: domain.DirectionNone, 75 Direction: domain.DirectionNone,
76 }, 76 },
77 77
78 - // 分组二 右平移10 78 + //分组二 右平移10
79 { 79 {
80 X: 0, 80 X: 0,
81 Y: 10, 81 Y: 10,
@@ -133,7 +133,7 @@ func TestDataLayout(t *testing.T) { @@ -133,7 +133,7 @@ func TestDataLayout(t *testing.T) {
133 Direction: domain.DirectionNone, 133 Direction: domain.DirectionNone,
134 }, 134 },
135 }, 135 },
136 - flag: Location{X: 3, Y: 2}, 136 + flag: Location{X: 2, Y: 2},
137 }, 137 },
138 { 138 {
139 title: "正常多字段横排", 139 title: "正常多字段横排",
@@ -309,6 +309,106 @@ func TestDataLayout(t *testing.T) { @@ -309,6 +309,106 @@ func TestDataLayout(t *testing.T) {
309 }, 309 },
310 flag: Location{X: 2, Y: 1}, 310 flag: Location{X: 2, Y: 1},
311 }, 311 },
  312 +
  313 + {
  314 + title: "正常多字段横排(长度不一样)+竖排",
  315 + cells: []*domain.LayoutCell{
  316 + // 分组一
  317 + {
  318 + X: 0,
  319 + Y: 0,
  320 + Length: 5,
  321 + ImageData: "a",
  322 + Direction: domain.DirectionRight,
  323 + },
  324 + {
  325 + X: 0,
  326 + Y: 1,
  327 + Length: 5,
  328 + ImageData: "b",
  329 + Direction: domain.DirectionDown,
  330 + },
  331 + {
  332 + X: 1,
  333 + Y: 0,
  334 + Length: 6,
  335 + ImageData: "c",
  336 + Direction: domain.DirectionRight,
  337 + },
  338 + {
  339 + X: 2,
  340 + Y: 0,
  341 + Length: 7,
  342 + ImageData: "d",
  343 + Direction: domain.DirectionRight,
  344 + },
  345 + },
  346 + flag: Location{X: 2, Y: 1},
  347 + },
  348 +
  349 + {
  350 + title: "测试用例1",
  351 + cells: []*domain.LayoutCell{
  352 + // 分组一
  353 + {
  354 + X: 0,
  355 + Y: 0,
  356 + Length: 1,
  357 + ImageData: "a",
  358 + Direction: domain.DirectionNone,
  359 + },
  360 + {
  361 + X: 0,
  362 + Y: 1,
  363 + Length: 1,
  364 + ImageData: "b",
  365 + Direction: domain.DirectionNone,
  366 + },
  367 + {
  368 + X: 2,
  369 + Y: 2,
  370 + Length: 5,
  371 + ImageData: "c",
  372 + Direction: domain.DirectionRight,
  373 + },
  374 + },
  375 + flag: Location{X: 0, Y: 0},
  376 + },
  377 + {
  378 + title: "测试用例3",
  379 + cells: []*domain.LayoutCell{
  380 + // 分组一
  381 + {
  382 + X: 0,
  383 + Y: 0,
  384 + Length: 5,
  385 + ImageData: "a",
  386 + Direction: domain.DirectionRight,
  387 + },
  388 + {
  389 + X: 1,
  390 + Y: 0,
  391 + Length: 5,
  392 + ImageData: "b",
  393 + Direction: domain.DirectionDown,
  394 + },
  395 + {
  396 + X: 1,
  397 + Y: 1,
  398 + Length: 5,
  399 + ImageData: "c",
  400 + Direction: domain.DirectionRight,
  401 + },
  402 + {
  403 + X: 2,
  404 + Y: 1,
  405 + Length: 5,
  406 + ImageData: "d",
  407 + Direction: domain.DirectionRight,
  408 + },
  409 + },
  410 + flag: Location{X: 2, Y: 1},
  411 + },
312 } 412 }
313 padding := func(cells []*domain.LayoutCell) { 413 padding := func(cells []*domain.LayoutCell) {
314 for _, cell := range cells { 414 for _, cell := range cells {
@@ -328,10 +428,53 @@ func TestDataLayout(t *testing.T) { @@ -328,10 +428,53 @@ func TestDataLayout(t *testing.T) {
328 assert.NoError(t, err) 428 assert.NoError(t, err)
329 } 429 }
330 printRes(res) 430 printRes(res)
331 - assert.NotEmptyf(t, res.Data[input.flag.X][input.flag.Y], "单元格有值") 431 + if input.flag.X > 0 && input.flag.Y > 0 {
  432 + assert.NotEmptyf(t, res.Data[input.flag.X][input.flag.Y], "单元格有值")
  433 + }
332 } 434 }
333 } 435 }
334 436
  437 +/*
  438 +output
  439 +
  440 +2 | 2 | 3 | 3 | 3 | | | | | | | | | 2 | 2 | 3 | 3 | 3 | |
  441 +a | | f | | | b | e | | | | | | | a | | f | | | b | e
  442 +a | | f | | | b | e | | | | | | | a | | f | | | b | e
  443 +c | | d | | | | | | | | | | | c | | d | | | |
  444 +
  445 +a | a | a | a | a | | | | | | | | | | a | a | a | a | a
  446 +b | b | b | b | b | | | | | | | | | | b | b | b | b | b
  447 +c | c | c | c | c | | | | | | | | | | c | c | c | c | c
  448 +d | d | d | d | d | | | | | | | | | | d | d | d | d | d
  449 +
  450 +a | a | a | a | a | | | | | | | | | | a | | | | |
  451 +b | b | b | b | b | | | | | | | | | | b | b | b | b | b |
  452 +c | | | | | | | | | | | | | | c | | | | | e
  453 +d | | | | | | | | | | | | | | d | | | | |
  454 +
  455 +a | a | a | a | a | b
  456 +c | c | c | c | c | b
  457 +d | d | d | d | d | b
  458 + | | | | | b
  459 + | | | | | b
  460 +
  461 +a | a | a | a | a | | | b
  462 +c | c | c | c | c | c | | b
  463 +d | d | d | d | d | d | d | b
  464 + | | | | | | | b
  465 + | | | | | | | b
  466 +
  467 +a | a | a | a | a | | | | |
  468 +b | | | | | c | c | c | c | c
  469 +b | | | | | | | | |
  470 +b | | | | | | | | |
  471 +b | | | | | | | | |
  472 +b | | | | | | | | |
  473 + | | | | | d | d | d | d | d
  474 +
  475 +
  476 +*/
  477 +
335 func printRes(res *domain.DataTable) { 478 func printRes(res *domain.DataTable) {
336 strBuilder := strings.Builder{} 479 strBuilder := strings.Builder{}
337 for i := range res.Data { 480 for i := range res.Data {