export_table.go
7.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/zeromicro/go-zero/core/mr"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/command"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/excel"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/redis"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
"time"
)
func (tableService *TableService) ExportDataTable(ctx *domain.Context, cmd *command.TablePreviewCommand) (interface{}, error) {
if err := cmd.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var table *domain.Table
//var mainTable *domain.Table
_, table, err = factory.FastPgTable(transactionContext, cmd.TableId)
if err != nil {
return nil, factory.FastError(err)
}
var options = starrocks.QueryOptions{
TableName: table.SQLName,
Select: table.Fields(true),
}
// 待优化分批下载,压缩
var dataTable *domain.DataTable
dataTable, err = starrocks.Query(options, starrocks.DefaultQueryFunc)
if err != nil {
return nil, factory.FastError(err)
}
count, err := starrocks.QueryCount(options)
if err != nil {
return nil, factory.FastError(err)
}
filename := fmt.Sprintf("%v_%v.xlsx", table.Name, time.Now().Format("060102150405"))
path := fmt.Sprintf("public/%v", filename)
excelWriter := excel.NewXLXSWriterTo(domain.Fields(table.Fields(false)).NameArrayString(), dataTable.Data) //
if err = excelWriter.Save(path); err != nil {
return nil, factory.FastError(err)
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"url": domain.DownloadUrl(filename),
"count": count,
}, nil
}
func MakeToInterfaces(fields []*domain.Field) func([]string) []interface{} {
return func(input []string) []interface{} {
output := make([]interface{}, len(input))
for i, v := range input {
if i < len(fields) {
convValue, err := domain.ValueToType(v, fields[i].SQLType)
if err == nil {
output[i] = convValue
continue
}
}
output[i] = v
}
return output
}
}
func (tableService *TableService) ExportDataTableV2(ctx *domain.Context, cmd *command.TablePreviewCommand) (interface{}, error) {
if err := cmd.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
locker := redis.NewLock(redis.KeyExportTable(ctx, cmd.TableId))
locker.SetExpire(60 * 2)
ok, err := locker.Acquire()
if err != nil {
return nil, factory.FastError(err)
}
if !ok {
return nil, factory.FastError(fmt.Errorf("点击过于频繁,请稍后再试"))
}
defer locker.Release()
var table *domain.Table
//var mainTable *domain.Table
if cmd.ObjectType == domain.ObjectDBTable {
table = domain.DBTables[cmd.TableId]
if table == nil {
return nil, factory.FastError(fmt.Errorf("表不存在"))
}
} else {
_, table, err = factory.FastPgTable(transactionContext, cmd.TableId)
if err != nil {
return nil, factory.FastError(err)
}
}
data, err := exportTableTo(ctx, cmd, table, 10000)
if err != nil {
return nil, factory.FastError(err)
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return data, nil
}
func exportTableTo(ctx *domain.Context, cmd *command.TablePreviewCommand, table *domain.Table, blockSize int) (interface{}, error) {
var options = starrocks.QueryOptions{
TableName: table.SQLName,
Select: table.Fields(false),
//Table: table,
}
db := starrocks.DB
if table.TableType == domain.ObjectDBTable {
db = pg.GormDB
}
count, err := starrocks.WrapQueryCountWithDB(options, db)()
if err != nil {
return nil, factory.FastError(err)
}
generate := func(source chan<- interface{}) {
// generator
t := int(count)/blockSize + 1
for i := 0; i < t; i++ {
options := starrocks.QueryOptions{
TableName: table.SQLName,
Select: table.Fields(false),
//Where: cmd.Where.Conditions,
Offset: i * blockSize,
Limit: blockSize,
Table: table,
}
options.SetCondition(cmd.Where.Conditions).SetDefaultOrder()
source <- Query{
Index: i,
Options: options,
}
}
}
mapper := func(item interface{}, writer mr.Writer, cancel func(error)) {
// mapper
query := item.(Query)
var dataTable *domain.DataTable
dataTable, err = starrocks.Query(query.Options, starrocks.WrapQueryFuncWithDB(db))
if err != nil {
log.Logger.Error(err.Error(), map[string]interface{}{"mapper": query})
return
}
writer.Write(QueryResult{
Index: query.Index,
DataTable: dataTable,
})
}
reducer := func(pipe <-chan interface{}, writer mr.Writer, cancel func(error)) {
// reducer
var data = make(map[int]QueryResult)
for i := range pipe {
item := i.(QueryResult)
data[item.Index] = item
}
writer.Write(data)
}
result, err := mr.MapReduce(generate, mapper, reducer, mr.WithWorkers(5))
if err != nil {
return nil, err
}
dataTable := resultToDataTable(result)
filename := fmt.Sprintf("%v_%v.xlsx", table.Name, time.Now().Format("060102150405"))
path := fmt.Sprintf("public/%v", filename)
excelWriter := excel.NewXLXSWriterTo(domain.Fields(table.Fields(false)).NameArrayString(), dataTable.Data) //
excelWriter.ToInterfaces = MakeToInterfaces(table.Fields(false))
if err = excelWriter.Save(path); err != nil {
return nil, factory.FastError(err)
}
return map[string]interface{}{
"url": domain.DownloadUrl(filename),
"count": count,
}, err
}
func resultToDataTable(result interface{}) *domain.DataTable {
var dataTable *domain.DataTable = &domain.DataTable{}
queryResult := result.(map[int]QueryResult)
for i := 0; i < len(queryResult); i++ {
if v, ok := queryResult[i]; ok {
if dataTable == nil {
dataTable = v.DataTable
continue
}
dataTable.Total += v.DataTable.Total
dataTable.Data = append(dataTable.Data, v.DataTable.Data...)
}
}
return dataTable
}
type Query struct {
Index int
Data struct{}
Options starrocks.QueryOptions
}
type QueryResult struct {
Index int
DataTable *domain.DataTable
}