table.go
4.7 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package domain
import (
"fmt"
"math/rand"
"strings"
"time"
)
// Table 表
type Table struct {
// 表Id
TableId int `json:"tableId"`
// 表类型 MainTable:主表 SideTable:副表 SubTable:分表 ExcelTable:Excel表
TableType string `json:"tableType"`
// 名称
Name string `json:"name"`
// 对应数据库名称
SQLName string `json:"sqlName"`
// 父级ID
ParentId int `json:"parentId"`
// 数据字段序号
DataFieldIndex int `json:"dataFieldIndex"`
// 主键字段
PK *Field `json:"pK"`
// 数据列
DataFields []*Field `json:"dataFields"`
// 手动添加的列
ManualFields []*Field `json:"manualFields"`
// 默认排序列
//OrderFields []*Field `json:"orderFields"`
// 创建时间
CreatedAt time.Time `json:"createdAt"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt"`
// 删除时间
DeletedAt time.Time `json:"deletedAt"`
// 版本
Version int `json:"version"`
fields []*Field // 所有列 // PKs + DataFields + ManualFields
// 业务字段
RowCount int `json:"rowCount,omitempty"`
// 扩展
Context *Context `json:"context"`
// 表信息
TableInfo *TableInfo `json:"tableInfo"`
// 应用于的时间
ApplyAt time.Time `json:"applyAt"`
// 表头行号 从0开始
HeaderRow int `json:"-"`
}
type TableRepository interface {
Save(table *Table) (*Table, error)
Remove(table *Table) (*Table, error)
FindOne(queryOptions map[string]interface{}) (*Table, error)
FindBusinessTable(companyId int64, sqlName string) (*Table, error)
Find(queryOptions map[string]interface{}) (int64, []*Table, error)
}
func (table *Table) Identify() interface{} {
if table.TableId == 0 {
return nil
}
return table.TableId
}
func (table *Table) TableIdString() string {
return fmt.Sprintf("%v", table.TableId)
}
func (table *Table) WithContext(ctx *Context) *Table {
rand.Seed(time.Now().Unix())
table.SQLName = fmt.Sprintf("%v_t%v_c%v", limitStringLen(table.SQLName, 30), rand.Intn(1000000), limitStringLen(fmt.Sprintf("%d", ctx.CompanyId), 8))
table.Context = ctx
return table
}
func limitStringLen(s string, l int) string {
result := s
subLength := l / 2
if len(result) > l {
result = result[:subLength] + result[len(result)-subLength:]
}
return result
}
func (table *Table) WithPrefix(prefix string) *Table {
if strings.HasPrefix(table.SQLName, "_") {
table.SQLName = fmt.Sprintf("%v%v", strings.ToLower(prefix), table.SQLName)
return table
}
table.SQLName = fmt.Sprintf("%v_%v", strings.ToLower(prefix), table.SQLName)
return table
}
func (table *Table) ApplyDefaultModule() *Table {
if table.TableType == CalculateTable.ToString() || table.TableType == SubProcessTable.ToString() {
table.TableInfo.SetApplyOn(ModuleQuerySetCenter)
}
if table.TableType == SchemaTable.ToString() {
table.TableInfo.SetApplyOn(ModuleQuerySetCenter | ModuleCalculateCenter)
}
return table
}
func (table *Table) WithParentId(parentId int) *Table {
table.ParentId = parentId
return table
}
func (table *Table) WithTableInfo(t *Table) *Table {
if t.TableInfo == nil {
return table
}
table.TableInfo.TableFrom = t.TableInfo.TableFrom
return table
}
func (table *Table) WithDataFieldIndex(dataFieldIndex int) *Table {
table.DataFieldIndex = dataFieldIndex
return table
}
func (table *Table) Update(data map[string]interface{}) error {
return nil
}
func (table *Table) Fields(includePK bool) []*Field {
var fields []*Field
if includePK && table.PK != nil {
fields = append(fields, table.PK)
}
fields = append(fields, table.DataFields...)
if table.TableType == SubTable.ToString() {
fields = append(fields, table.ManualFields...)
}
table.fields = fields
return table.fields
}
func (table *Table) MatchField(field *Field) (*Field, bool) {
if len(table.fields) == 0 {
table.fields = table.Fields(true)
}
mField := (Fields)(table.fields).ToMap()
if v, ok := mField[field.Name]; ok {
return v, true
}
return nil, false
}
func (table *Table) DependencyTables() []int {
if table.TableInfo == nil {
return []int{}
}
return table.TableInfo.DependencyTables
}
func (table *Table) AssertTableType(types ...TableType) bool {
for _, item := range types {
if table.TableType == item.ToString() {
return true
}
}
return false
}
func TableTypesToStringList(list ...TableType) []string {
var result = make([]string, 0)
for _, item := range list {
result = append(result, item.ToString())
}
return result
}
type Tables []*Table
func (tables Tables) ToMap() map[int]*Table {
var result = make(map[int]*Table)
for i := range tables {
result[tables[i].TableId] = tables[i]
}
return result
}
func AssertTableType(tableType string, types ...TableType) bool {
for _, item := range types {
if tableType == item.ToString() {
return true
}
}
return false
}