chart.go
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/tool"
"golang.org/x/exp/slices"
"sort"
"strings"
)
type Chart struct {
Id int64 // ID
Pid int64 // 父级ID
Type string // 类型
Sort int // 排序
Name string // 名称
Group string // 分组
TenantId int64 // 租户ID
Cover string // 封面
ChartType string // 图表类型 (记录型表格:RecordTable-1 总体指标:MetricsCard-1 容器卡片:ContainerCard-1 四分图:QuarterChart-1)
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
Version int `json:",omitempty"`
}
type ChartRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *Chart) (*Chart, error)
Update(ctx context.Context, conn transaction.Conn, dm *Chart) (*Chart, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Chart) (*Chart, error)
Delete(ctx context.Context, conn transaction.Conn, dm *Chart) (*Chart, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Chart, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*Chart, error)
FindOneByGroup(ctx context.Context, conn transaction.Conn, tenantId, pid int64) (*Chart, error)
FindByTypeAndName(ctx context.Context, conn transaction.Conn, tenantId int64, t string, name string) (int64, []*Chart, error)
FindCompanyIds(ctx context.Context, conn transaction.Conn) ([]int64, error)
}
/*************** 索引函数 开始****************/
func IndexTenantId(tenantId int64) IndexQueryOptionFunc {
return func() QueryOptions {
return NewQueryOptions().WithKV("tenantId", tenantId)
}
}
func IndexChartPid(tenantId, pid int64) IndexQueryOptionFunc {
return func() QueryOptions {
return NewQueryOptions().WithKV("tenantId", tenantId).WithKV("pid", pid)
}
}
func (m *Chart) Identify() interface{} {
if m.Id == 0 {
return nil
}
return m.Id
}
func (m *Chart) RandName() string {
randNumber := tool.Krand(6, tool.KC_RAND_KIND_NUM)
var result strings.Builder
switch m.Type {
case ChartTypeReport:
result.WriteString("未命名报表")
case ChartTypeGroup:
result.WriteString("未命名分组")
case ChartTypeChart:
result.WriteString("未命名图表")
}
result.WriteString(randNumber)
return result.String()
}
// Reorder 重新排序charts
// appendFlag:true 追加 target 到 charts
// appendFlag:false 不追加
func Reorder(charts []*Chart, target *Chart, appendFlag bool) []*Chart {
// 重排序
var result = make([]*Chart, 0)
sort.SliceStable(charts, func(i, j int) bool {
return charts[i].Sort < charts[j].Sort
})
for index, c := range charts {
c.Sort = index + 1
if c.Id == target.Id {
result = append(result, &Chart{Sort: c.Sort}) // 旧节点继续追击啊,ID设置为0,后面跳过更新
continue
}
result = append(result, c)
}
// 插入新的节点
if appendFlag {
index := slices.IndexFunc(result, func(c *Chart) bool {
if float64(c.Sort) > float64(target.Sort)-0.5 {
return true
}
return false
})
if index == -1 {
index = len(result)
}
result = slices.Insert(result, index, target)
}
// 重排序
var index = 1
for i := range result {
if result[i].Id == 0 {
continue
}
result[i].Sort = index
index++
}
return result
}