作者 yangfu
提交者 yangfu

refactor: save table

... ... @@ -66,6 +66,23 @@ func (tableService *TableService) ExportDataTable(ctx *domain.Context, cmd *comm
}, 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())
... ... @@ -182,6 +199,7 @@ func exportTableTo(ctx *domain.Context, cmd *command.TablePreviewCommand, table
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)
}
... ...
... ... @@ -130,6 +130,7 @@ type (
type (
ReqSaveTable struct {
FileId int
Table *Table
}
DataSaveTable struct {
... ...
... ... @@ -67,6 +67,8 @@ func (gateway ApiByteLib) SaveTable(param domain.ReqSaveTable) (*domain.DataSave
var data ResponseCheckoutTablesSave
var request = RequestCheckoutTablesSave{
OriginalTableId: fmt.Sprintf("%v", param.FileId),
FilePostfix: "xlsx",
ColumnSchemas: FieldsToColumnSchemas(param.Table.DataFields),
}
err := gateway.FastDoRequest(url, method, request, &data)
if err != nil {
... ...
... ... @@ -87,7 +87,9 @@ func NewRequestCheckoutTablesPreProccess(param domain.ReqEditDataTable) RequestC
}
type RequestCheckoutTablesSave struct {
OriginalTableId string `json:"originalTableId"`
OriginalTableId string `json:"originalTableId"`
FilePostfix string `json:"filePostfix"`
ColumnSchemas []domain.ColumnSchema `json:"columnSchemas"`
}
type ResponseCheckoutTablesSave struct {
... ...
... ... @@ -33,7 +33,7 @@ func (ptr *FlushDataTableService) Flush(ctx *domain.Context, fileId int, table *
table = NewTable(domain.ExcelTable, file.FileInfo.Name, table.DataFields, table.RowCount).WithContext(ctx)
// 通知底层保存、进行回调
var response *domain.DataSaveTable
response, err = ByteCore.SaveTable(domain.ReqSaveTable{FileId: fileId})
response, err = ByteCore.SaveTable(domain.ReqSaveTable{FileId: fileId, Table: table})
if err != nil {
return nil, err
}
... ...
... ... @@ -11,8 +11,9 @@ import (
)
type XLXSWriterTo struct {
data [][]string
title []string
data [][]string
title []string
ToInterfaces func([]string) []interface{}
}
func (wt *XLXSWriterTo) WriteTo(w io.Writer) (n int64, err error) {
... ... @@ -75,6 +76,9 @@ func (wt *XLXSWriterTo) newFile() (*excelize.File, error) {
var rowID = 2
for i := 0; i < len(wt.data); i++ {
row := stringsToInterfaces(wt.data[i])
if wt.ToInterfaces != nil {
row = wt.ToInterfaces(wt.data[i])
}
cell, _ := excelize.CoordinatesToCellName(1, rowID)
if err := streamWriter.SetRow(cell, row); err != nil {
return nil, err
... ...