-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
208 lines (200 loc) · 4.86 KB
/
core.go
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
198
199
200
201
202
203
204
205
206
207
208
package goexcel
import (
"reflect"
"sort"
"strconv"
"strings"
)
type fieldInfo struct {
name string
headStyle string
cellStyle string
headInfo []string
kind reflect.Kind
sort int
index int
}
type modelInfo struct {
headMaxLevel int
CellFlags map[string]bool
field *newFieldInfo
allFields []*fieldInfo
}
func (m *modelInfo) IsCellFlags(axis string) (ok bool, cell string, row int) {
cell, row = AxisToCellRow(axis)
ok = m.CellFlags[cell]
return
}
type newFieldInfo struct {
Fields []*fieldInfo
CellFlag string
StartRows int
LevelIndex int
LevelIsStruct bool
LevelType reflect.Type
Level *newFieldInfo
}
func getExportSort(src reflect.Type, isRestSort bool) *modelInfo {
var list []*fieldInfo
startSort := 50
model := new(modelInfo)
model.CellFlags = map[string]bool{}
var f func(reflect.Type, *newFieldInfo)
f = func(dest reflect.Type, field *newFieldInfo) {
for dest.Kind() == reflect.Ptr {
dest = dest.Elem()
}
switch dest.Kind() {
case reflect.Struct:
for n := 0; n < dest.NumField(); n++ {
tmpField := field
tf := dest.Field(n)
if tf.Name[0] >= 'a' {
continue
}
tp := tf.Type
for tp.Kind() == reflect.Ptr {
tp = tp.Elem()
}
column, ok := tf.Tag.Lookup("export")
if tp.Kind() == reflect.Slice && !ok {
tmpField.Level = new(newFieldInfo)
tmpField.LevelIndex = n
tp = tp.Elem()
tmpField.LevelType = tp
for tp.Kind() == reflect.Ptr {
tp = tp.Elem()
}
if tp.Kind() == reflect.Struct {
tmpField.LevelIsStruct = true
f(tp, tmpField.Level)
continue
}
tmpField = tmpField.Level
}
if ok {
if column == "" {
column = strings.Split(tf.Tag.Get("json"), ",")[0]
if column == "" {
column = tf.Name
}
}
startSort++
sort := startSort
name := column
sp := strings.Split(column, ",")
if len(sp) > 1 {
sort, _ = strconv.Atoi(sp[1])
}
info := &fieldInfo{
name: name,
sort: sort,
index: n,
kind: tf.Type.Kind(),
headStyle: tf.Tag.Get("headStyle"),
cellStyle: tf.Tag.Get("cellStyle"),
}
if len(sp) > 0 {
name = sp[0]
list := strings.Split(name, "|")
info.name = list[len(list)-1]
info.headInfo = list
if model.headMaxLevel < len(list) {
model.headMaxLevel = len(list)
}
}
list = append(list, info)
tmpField.Fields = append(tmpField.Fields, info)
if len(tmpField.Fields) == 1 {
tmpField.CellFlag = GetCellCode(info.sort)
model.CellFlags[tmpField.CellFlag] = true
}
}
}
}
}
fieldTree := new(newFieldInfo)
model.field = fieldTree
fieldTree.StartRows = 1
f(src, fieldTree)
sort.Slice(list, func(i, j int) bool {
if list[i].sort == list[j].sort {
return list[i].name < list[i].name
}
return list[i].sort < list[j].sort
})
if isRestSort {
for k, v := range list {
v.sort = k + 1
}
}
model.allFields = list
return model
}
// getModelValueForFieldInfo 获取指定数据实体对应索引的值对指定索引执行 f 方法 f返回值不为空时 会返回
func getModelValueForFieldInfo(dest reflect.Value, field *newFieldInfo, f func(row, cell, endRow int, value interface{}) error) (err error) {
for dest.Kind() == reflect.Ptr {
dest = dest.Elem()
}
endRows := field.StartRows
if field.Level != nil {
field.Level.StartRows = field.StartRows
list := dest.Field(field.LevelIndex)
n := list.Len()
for i := 0; i < n; i++ {
value := list.Index(i)
for value.Kind() == reflect.Ptr {
value = value.Elem()
}
if field.LevelIsStruct {
err = getModelValueForFieldInfo(value, field.Level, f)
if err != nil {
return err
}
} else {
err = f(field.Level.StartRows, field.Level.Fields[0].sort, field.Level.StartRows+1, value)
field.Level.StartRows++
if err != nil {
return err
}
}
}
endRows = field.Level.StartRows
}
if endRows == field.StartRows {
endRows++
}
for _, v := range field.Fields {
value := dest.Field(v.index).Interface()
err = f(field.StartRows, v.sort, endRows, value)
if err != nil {
return err
}
}
field.StartRows = endRows
return
}
// getModelValueForIndex 获取指定数据实体对应索引的值对指定索引执行 f 方法 f返回值不为空时 会返回
func getModelValueForIndex(dest reflect.Value, indexes [][]int, f func(index int, value interface{}) error) (err error) {
for dest.Kind() == reflect.Ptr {
dest = dest.Elem()
}
cont:
for i := 0; i < len(indexes); i++ {
son := indexes[i]
destSon := dest
for j := 0; j < len(son); j++ {
destSon = destSon.Field(son[j])
if destSon.Kind() == reflect.Ptr {
if destSon.IsNil() {
continue cont
}
destSon = destSon.Elem()
}
}
if err = f(i, destSon.Interface()); err != nil {
return err
}
}
return
}