作者 yangfu
提交者 yangfu

refactor: save table

@@ -66,6 +66,23 @@ func (tableService *TableService) ExportDataTable(ctx *domain.Context, cmd *comm @@ -66,6 +66,23 @@ func (tableService *TableService) ExportDataTable(ctx *domain.Context, cmd *comm
66 }, nil 66 }, nil
67 } 67 }
68 68
  69 +func MakeToInterfaces(fields []*domain.Field) func([]string) []interface{} {
  70 + return func(input []string) []interface{} {
  71 + output := make([]interface{}, len(input))
  72 + for i, v := range input {
  73 + if i < len(fields) {
  74 + convValue, err := domain.ValueToType(v, fields[i].SQLType)
  75 + if err == nil {
  76 + output[i] = convValue
  77 + continue
  78 + }
  79 + }
  80 + output[i] = v
  81 + }
  82 + return output
  83 + }
  84 +}
  85 +
69 func (tableService *TableService) ExportDataTableV2(ctx *domain.Context, cmd *command.TablePreviewCommand) (interface{}, error) { 86 func (tableService *TableService) ExportDataTableV2(ctx *domain.Context, cmd *command.TablePreviewCommand) (interface{}, error) {
70 if err := cmd.ValidateCommand(); err != nil { 87 if err := cmd.ValidateCommand(); err != nil {
71 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 88 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -182,6 +199,7 @@ func exportTableTo(ctx *domain.Context, cmd *command.TablePreviewCommand, table @@ -182,6 +199,7 @@ func exportTableTo(ctx *domain.Context, cmd *command.TablePreviewCommand, table
182 filename := fmt.Sprintf("%v_%v.xlsx", table.Name, time.Now().Format("060102150405")) 199 filename := fmt.Sprintf("%v_%v.xlsx", table.Name, time.Now().Format("060102150405"))
183 path := fmt.Sprintf("public/%v", filename) 200 path := fmt.Sprintf("public/%v", filename)
184 excelWriter := excel.NewXLXSWriterTo(domain.Fields(table.Fields(false)).NameArrayString(), dataTable.Data) // 201 excelWriter := excel.NewXLXSWriterTo(domain.Fields(table.Fields(false)).NameArrayString(), dataTable.Data) //
  202 + excelWriter.ToInterfaces = MakeToInterfaces(table.Fields(false))
185 if err = excelWriter.Save(path); err != nil { 203 if err = excelWriter.Save(path); err != nil {
186 return nil, factory.FastError(err) 204 return nil, factory.FastError(err)
187 } 205 }
@@ -130,6 +130,7 @@ type ( @@ -130,6 +130,7 @@ type (
130 type ( 130 type (
131 ReqSaveTable struct { 131 ReqSaveTable struct {
132 FileId int 132 FileId int
  133 + Table *Table
133 } 134 }
134 135
135 DataSaveTable struct { 136 DataSaveTable struct {
@@ -67,6 +67,8 @@ func (gateway ApiByteLib) SaveTable(param domain.ReqSaveTable) (*domain.DataSave @@ -67,6 +67,8 @@ func (gateway ApiByteLib) SaveTable(param domain.ReqSaveTable) (*domain.DataSave
67 var data ResponseCheckoutTablesSave 67 var data ResponseCheckoutTablesSave
68 var request = RequestCheckoutTablesSave{ 68 var request = RequestCheckoutTablesSave{
69 OriginalTableId: fmt.Sprintf("%v", param.FileId), 69 OriginalTableId: fmt.Sprintf("%v", param.FileId),
  70 + FilePostfix: "xlsx",
  71 + ColumnSchemas: FieldsToColumnSchemas(param.Table.DataFields),
70 } 72 }
71 err := gateway.FastDoRequest(url, method, request, &data) 73 err := gateway.FastDoRequest(url, method, request, &data)
72 if err != nil { 74 if err != nil {
@@ -87,7 +87,9 @@ func NewRequestCheckoutTablesPreProccess(param domain.ReqEditDataTable) RequestC @@ -87,7 +87,9 @@ func NewRequestCheckoutTablesPreProccess(param domain.ReqEditDataTable) RequestC
87 } 87 }
88 88
89 type RequestCheckoutTablesSave struct { 89 type RequestCheckoutTablesSave struct {
90 - OriginalTableId string `json:"originalTableId"` 90 + OriginalTableId string `json:"originalTableId"`
  91 + FilePostfix string `json:"filePostfix"`
  92 + ColumnSchemas []domain.ColumnSchema `json:"columnSchemas"`
91 } 93 }
92 94
93 type ResponseCheckoutTablesSave struct { 95 type ResponseCheckoutTablesSave struct {
@@ -33,7 +33,7 @@ func (ptr *FlushDataTableService) Flush(ctx *domain.Context, fileId int, table * @@ -33,7 +33,7 @@ func (ptr *FlushDataTableService) Flush(ctx *domain.Context, fileId int, table *
33 table = NewTable(domain.ExcelTable, file.FileInfo.Name, table.DataFields, table.RowCount).WithContext(ctx) 33 table = NewTable(domain.ExcelTable, file.FileInfo.Name, table.DataFields, table.RowCount).WithContext(ctx)
34 // 通知底层保存、进行回调 34 // 通知底层保存、进行回调
35 var response *domain.DataSaveTable 35 var response *domain.DataSaveTable
36 - response, err = ByteCore.SaveTable(domain.ReqSaveTable{FileId: fileId}) 36 + response, err = ByteCore.SaveTable(domain.ReqSaveTable{FileId: fileId, Table: table})
37 if err != nil { 37 if err != nil {
38 return nil, err 38 return nil, err
39 } 39 }
@@ -11,8 +11,9 @@ import ( @@ -11,8 +11,9 @@ import (
11 ) 11 )
12 12
13 type XLXSWriterTo struct { 13 type XLXSWriterTo struct {
14 - data [][]string  
15 - title []string 14 + data [][]string
  15 + title []string
  16 + ToInterfaces func([]string) []interface{}
16 } 17 }
17 18
18 func (wt *XLXSWriterTo) WriteTo(w io.Writer) (n int64, err error) { 19 func (wt *XLXSWriterTo) WriteTo(w io.Writer) (n int64, err error) {
@@ -75,6 +76,9 @@ func (wt *XLXSWriterTo) newFile() (*excelize.File, error) { @@ -75,6 +76,9 @@ func (wt *XLXSWriterTo) newFile() (*excelize.File, error) {
75 var rowID = 2 76 var rowID = 2
76 for i := 0; i < len(wt.data); i++ { 77 for i := 0; i < len(wt.data); i++ {
77 row := stringsToInterfaces(wt.data[i]) 78 row := stringsToInterfaces(wt.data[i])
  79 + if wt.ToInterfaces != nil {
  80 + row = wt.ToInterfaces(wt.data[i])
  81 + }
78 cell, _ := excelize.CoordinatesToCellName(1, rowID) 82 cell, _ := excelize.CoordinatesToCellName(1, rowID)
79 if err := streamWriter.SetRow(cell, row); err != nil { 83 if err := streamWriter.SetRow(cell, row); err != nil {
80 return nil, err 84 return nil, err