正在显示
9 个修改的文件
包含
272 行增加
和
8 行删除
@@ -10,4 +10,7 @@ type AppTableFileAppendDataCommand struct { | @@ -10,4 +10,7 @@ type AppTableFileAppendDataCommand struct { | ||
10 | Data []map[string]string `json:"data"` | 10 | Data []map[string]string `json:"data"` |
11 | 11 | ||
12 | AppKey string `json:"appKey"` | 12 | AppKey string `json:"appKey"` |
13 | + | ||
14 | + // 追加表数据标识 true:往应用表里面追加数据 false:跳过 | ||
15 | + AppendTableDataFlag bool `json:"appendTableDataFlag"` | ||
13 | } | 16 | } |
@@ -8,4 +8,6 @@ type CreateAppTableFileCommand struct { | @@ -8,4 +8,6 @@ type CreateAppTableFileCommand struct { | ||
8 | Fields []*domain.Field `json:"fields"` | 8 | Fields []*domain.Field `json:"fields"` |
9 | // 数据列表 key:name(字段中文名) value:值(字符串类型) | 9 | // 数据列表 key:name(字段中文名) value:值(字符串类型) |
10 | Data []map[string]string `json:"data"` | 10 | Data []map[string]string `json:"data"` |
11 | + // 生成表标识 true:实例化一个表 false:跳过 | ||
12 | + GenerateTableFlag bool `json:"generateTableFlag"` | ||
11 | } | 13 | } |
@@ -21,6 +21,11 @@ type CreateFileCommand struct { | @@ -21,6 +21,11 @@ type CreateFileCommand struct { | ||
21 | FileFrom string `json:"-"` | 21 | FileFrom string `json:"-"` |
22 | // AppKey | 22 | // AppKey |
23 | AppKey string `json:"-"` | 23 | AppKey string `json:"-"` |
24 | + | ||
25 | + // 生成表标识 true:实例化一个表 false:跳过 | ||
26 | + GenerateTableFlag bool `json:"-"` | ||
27 | + // name 字段中文名 | ||
28 | + Fields []*domain.Field `json:"-"` | ||
24 | } | 29 | } |
25 | 30 | ||
26 | var MaxFileSize = 50 * 1024 * 1024 | 31 | var MaxFileSize = 50 * 1024 * 1024 |
@@ -35,6 +40,14 @@ func (createFileCommand *CreateFileCommand) Valid(validation *validation.Validat | @@ -35,6 +40,14 @@ func (createFileCommand *CreateFileCommand) Valid(validation *validation.Validat | ||
35 | validation.Error("文件大小超过50M") | 40 | validation.Error("文件大小超过50M") |
36 | return | 41 | return |
37 | } | 42 | } |
43 | + if createFileCommand.GenerateTableFlag { | ||
44 | + for _, f := range createFileCommand.Fields { | ||
45 | + if err := f.Valid(); err != nil { | ||
46 | + validation.Error(err.Error()) | ||
47 | + return | ||
48 | + } | ||
49 | + } | ||
50 | + } | ||
38 | } | 51 | } |
39 | 52 | ||
40 | func (createFileCommand *CreateFileCommand) ValidateCommand() error { | 53 | func (createFileCommand *CreateFileCommand) ValidateCommand() error { |
@@ -22,6 +22,10 @@ type FileDto struct { | @@ -22,6 +22,10 @@ type FileDto struct { | ||
22 | HeaderRow int `json:"headerRow"` | 22 | HeaderRow int `json:"headerRow"` |
23 | // 所属应用 | 23 | // 所属应用 |
24 | AppKey string `json:"appKey"` | 24 | AppKey string `json:"appKey"` |
25 | + // 表ID | ||
26 | + TableId int `json:"tableId"` | ||
27 | + // 允许表生成标识 1:允许生成分表 0:不允许 | ||
28 | + AllowTableGenerateFlag int `json:"allowTableGenerateFlag"` | ||
25 | } | 29 | } |
26 | 30 | ||
27 | func (d *FileDto) Load(f *domain.File) *FileDto { | 31 | func (d *FileDto) Load(f *domain.File) *FileDto { |
@@ -36,6 +40,10 @@ func (d *FileDto) Load(f *domain.File) *FileDto { | @@ -36,6 +40,10 @@ func (d *FileDto) Load(f *domain.File) *FileDto { | ||
36 | d.Time = xtime.New(f.UpdatedAt).Local().Format("2006-01-02 15:04:05") | 40 | d.Time = xtime.New(f.UpdatedAt).Local().Format("2006-01-02 15:04:05") |
37 | d.HeaderRow = domain.GetHeaderRow(f.FileInfo.HeaderRow) | 41 | d.HeaderRow = domain.GetHeaderRow(f.FileInfo.HeaderRow) |
38 | d.AppKey = f.AppKey | 42 | d.AppKey = f.AppKey |
43 | + if len(f.AppKey) > 0 { | ||
44 | + d.TableId = f.FileInfo.TableId | ||
45 | + d.AllowTableGenerateFlag = 1 | ||
46 | + } | ||
39 | return d | 47 | return d |
40 | } | 48 | } |
41 | 49 |
@@ -6,12 +6,14 @@ import ( | @@ -6,12 +6,14 @@ import ( | ||
6 | "fmt" | 6 | "fmt" |
7 | "github.com/beego/beego/v2/client/httplib" | 7 | "github.com/beego/beego/v2/client/httplib" |
8 | "github.com/linmadan/egglib-go/core/application" | 8 | "github.com/linmadan/egglib-go/core/application" |
9 | + pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | ||
9 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory" | 10 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory" |
10 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/file/command" | 11 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/file/command" |
11 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/file/query" | 12 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/file/query" |
12 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant" | 13 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant" |
13 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain" | 14 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain" |
14 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/apilib" | 15 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/apilib" |
16 | + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService" | ||
15 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/excel" | 17 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/excel" |
16 | "os" | 18 | "os" |
17 | "strings" | 19 | "strings" |
@@ -83,6 +85,34 @@ func saveFile(name string, title []string, dataList [][]string, toInterfaces fun | @@ -83,6 +85,34 @@ func saveFile(name string, title []string, dataList [][]string, toInterfaces fun | ||
83 | return response, nil | 85 | return response, nil |
84 | } | 86 | } |
85 | 87 | ||
88 | +func saveCsvFile(name string, title []string, dataList [][]string, toInterfaces func([]string) []interface{}) (FileUpload, error) { | ||
89 | + var ( | ||
90 | + response = FileUpload{} | ||
91 | + err error | ||
92 | + ) | ||
93 | + var writerTo = excel.NewCSVWriterTo(title, dataList) | ||
94 | + filename := fmt.Sprintf("%v_%v.csv", name, time.Now().Format("060102150405")) | ||
95 | + path := fmt.Sprintf("public/%v", filename) | ||
96 | + if err = writerTo.Save(path); err != nil { | ||
97 | + return response, factory.FastError(err) | ||
98 | + } | ||
99 | + api := apilib.NewApiAuthLib(constant.OPEN_API_HOST) | ||
100 | + uploadResponse, err := api.Upload(apilib.RequestUpload{ | ||
101 | + UploadFileMap: map[string]string{"file": path}, | ||
102 | + }) | ||
103 | + if err != nil { | ||
104 | + return response, err | ||
105 | + } | ||
106 | + if stat, err := os.Stat(path); err == nil { | ||
107 | + response.FileSize = stat.Size() | ||
108 | + } | ||
109 | + response.Url = domain.ConvertInternalFileUrlToPublic(uploadResponse.Path) | ||
110 | + response.FileName = name | ||
111 | + response.Ext = domain.CSV | ||
112 | + | ||
113 | + return response, nil | ||
114 | +} | ||
115 | + | ||
86 | type FileUpload struct { | 116 | type FileUpload struct { |
87 | Url string `json:"url"` | 117 | Url string `json:"url"` |
88 | Ext string `json:"ext"` | 118 | Ext string `json:"ext"` |
@@ -146,7 +176,10 @@ func (fileService *FileService) AppTableFileAppendData(ctx *domain.Context, cmd | @@ -146,7 +176,10 @@ func (fileService *FileService) AppTableFileAppendData(ctx *domain.Context, cmd | ||
146 | return nil, factory.FastError(err) | 176 | return nil, factory.FastError(err) |
147 | } | 177 | } |
148 | reader := bytes.NewReader(f) | 178 | reader := bytes.NewReader(f) |
149 | - var importer *excel.Importer = excel.NewExcelImportByFile(file.FileInfo.Ext) | 179 | + var ( |
180 | + importer *excel.Importer = excel.NewExcelImportByFile(file.FileInfo.Ext) | ||
181 | + appendTableDataList = make([][]string, 0) | ||
182 | + ) | ||
150 | data, err := importer.OpenExcelFromIoReader(reader) | 183 | data, err := importer.OpenExcelFromIoReader(reader) |
151 | if err != nil { | 184 | if err != nil { |
152 | return nil, factory.FastError(err) | 185 | return nil, factory.FastError(err) |
@@ -183,14 +216,17 @@ func (fileService *FileService) AppTableFileAppendData(ctx *domain.Context, cmd | @@ -183,14 +216,17 @@ func (fileService *FileService) AppTableFileAppendData(ctx *domain.Context, cmd | ||
183 | } | 216 | } |
184 | } | 217 | } |
185 | data = append(data, row) | 218 | data = append(data, row) |
219 | + if cmd.AppendTableDataFlag { | ||
220 | + appendTableDataList = append(appendTableDataList, row) | ||
221 | + } | ||
186 | } | 222 | } |
187 | 223 | ||
224 | + //if !cmd.AppendTableDataFlag { | ||
188 | // 上传文件 | 225 | // 上传文件 |
189 | fileUpload, err := saveFile(cmd.Name, titles, data, nil) | 226 | fileUpload, err := saveFile(cmd.Name, titles, data, nil) |
190 | if err != nil { | 227 | if err != nil { |
191 | return nil, factory.FastError(err) | 228 | return nil, factory.FastError(err) |
192 | } | 229 | } |
193 | - | ||
194 | // 更新文件 | 230 | // 更新文件 |
195 | file.FileInfo.Url = fileUpload.Url | 231 | file.FileInfo.Url = fileUpload.Url |
196 | file.FileInfo.FileSize = int(fileUpload.FileSize) | 232 | file.FileInfo.FileSize = int(fileUpload.FileSize) |
@@ -199,6 +235,111 @@ func (fileService *FileService) AppTableFileAppendData(ctx *domain.Context, cmd | @@ -199,6 +235,111 @@ func (fileService *FileService) AppTableFileAppendData(ctx *domain.Context, cmd | ||
199 | if err != nil { | 235 | if err != nil { |
200 | return nil, factory.FastError(err) | 236 | return nil, factory.FastError(err) |
201 | } | 237 | } |
238 | + //} | ||
239 | + //else if cmd.AppendTableDataFlag && file.FileInfo.TableId != 0 { // 追加数据到应用表 | ||
240 | + //var table *domain.Table | ||
241 | + //if _, table, err = factory.FastPgTable(transactionContext, file.FileInfo.TableId); err != nil { | ||
242 | + // return nil, factory.FastError(err) | ||
243 | + //} | ||
244 | + //// 上传文件 | ||
245 | + //fileUpload, err := saveCsvFile(cmd.Name, titles, appendTableDataList, nil) | ||
246 | + //if err != nil { | ||
247 | + // return nil, factory.FastError(err) | ||
248 | + //} | ||
249 | + //appendDataToTableService, _ := domainService.NewAppendDataToTableService(transactionContext.(*pgTransaction.TransactionContext)) | ||
250 | + //var mappingFields = make([]*domain.MappingField, 0) | ||
251 | + //for _, f := range cmd.Fields { | ||
252 | + // mappingFields = append(mappingFields, &domain.MappingField{ | ||
253 | + // MainTableField: &domain.Field{Name: f.Name}, | ||
254 | + // VerifiedFileFieldName: f.Name, | ||
255 | + // }) | ||
256 | + //} | ||
257 | + //if _, err = appendDataToTableService.AppendDataDirectly(ctx, fileUpload.Url, table, mappingFields); err != nil { | ||
258 | + // return nil, factory.FastError(err) | ||
259 | + //} | ||
260 | + //} | ||
261 | + | ||
262 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
263 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
264 | + } | ||
265 | + return struct{}{}, nil | ||
266 | +} | ||
267 | + | ||
268 | +func (fileService *FileService) AppTableAppendData(ctx *domain.Context, cmd *command.AppTableFileAppendDataCommand) (interface{}, error) { | ||
269 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
270 | + if err != nil { | ||
271 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
272 | + } | ||
273 | + if err := transactionContext.StartTransaction(); err != nil { | ||
274 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
275 | + } | ||
276 | + defer func() { | ||
277 | + transactionContext.RollbackTransaction() | ||
278 | + }() | ||
279 | + | ||
280 | + fileRepository, file, _ := factory.FastPgFile(transactionContext, 0) | ||
281 | + file, err = fileRepository.FindOne(map[string]interface{}{"appKey": cmd.AppKey, "fileName": cmd.Name, "fileType": domain.SourceFile}) | ||
282 | + if err == domain.ErrorNotFound { | ||
283 | + return nil, factory.FastError(errors.New("文件不存在")) | ||
284 | + } | ||
285 | + if err != nil { | ||
286 | + return nil, factory.FastError(err) | ||
287 | + } | ||
288 | + if file.FileInfo.TableId == 0 { | ||
289 | + return nil, factory.FastError(errors.New("表不存在")) | ||
290 | + } | ||
291 | + var ( | ||
292 | + appendTableDataList = make([][]string, 0) | ||
293 | + titles = make([]string, 0) | ||
294 | + table *domain.Table | ||
295 | + ) | ||
296 | + _, table, err = factory.FastPgTable(transactionContext, file.FileInfo.TableId) | ||
297 | + if err != nil { | ||
298 | + return nil, factory.FastError(err) | ||
299 | + } | ||
300 | + for _, f := range table.Fields(false) { | ||
301 | + titles = append(titles, f.Name) | ||
302 | + } | ||
303 | + for _, f := range cmd.Fields { | ||
304 | + found := false | ||
305 | + for _, column := range titles { | ||
306 | + if column == f.Name { | ||
307 | + found = true | ||
308 | + break | ||
309 | + } | ||
310 | + } | ||
311 | + if !found { | ||
312 | + titles = append(titles, f.Name) | ||
313 | + } | ||
314 | + } | ||
315 | + for i := range cmd.Data { | ||
316 | + row := make([]string, 0) | ||
317 | + for _, filed := range titles { | ||
318 | + if v, ok := cmd.Data[i][filed]; ok { | ||
319 | + row = append(row, v) | ||
320 | + } else { | ||
321 | + row = append(row, "") | ||
322 | + } | ||
323 | + } | ||
324 | + appendTableDataList = append(appendTableDataList, row) | ||
325 | + } | ||
326 | + | ||
327 | + // 上传文件 | ||
328 | + fileUpload, err := saveCsvFile(cmd.Name, titles, appendTableDataList, nil) | ||
329 | + if err != nil { | ||
330 | + return nil, factory.FastError(err) | ||
331 | + } | ||
332 | + appendDataToTableService, _ := domainService.NewAppendDataToTableService(transactionContext.(*pgTransaction.TransactionContext)) | ||
333 | + var mappingFields = make([]*domain.MappingField, 0) | ||
334 | + for _, f := range cmd.Fields { | ||
335 | + mappingFields = append(mappingFields, &domain.MappingField{ | ||
336 | + MainTableField: &domain.Field{Name: f.Name}, | ||
337 | + VerifiedFileFieldName: f.Name, | ||
338 | + }) | ||
339 | + } | ||
340 | + if _, err = appendDataToTableService.AppendDataDirectly(ctx, fileUpload.Url, table, mappingFields); err != nil { | ||
341 | + return nil, factory.FastError(err) | ||
342 | + } | ||
202 | 343 | ||
203 | if err := transactionContext.CommitTransaction(); err != nil { | 344 | if err := transactionContext.CommitTransaction(); err != nil { |
204 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | 345 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) |
@@ -4,6 +4,7 @@ import ( | @@ -4,6 +4,7 @@ import ( | ||
4 | "fmt" | 4 | "fmt" |
5 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant" | 5 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant" |
6 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/authlib" | 6 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/authlib" |
7 | + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/bytelib" | ||
7 | "path/filepath" | 8 | "path/filepath" |
8 | "strings" | 9 | "strings" |
9 | "time" | 10 | "time" |
@@ -24,7 +25,7 @@ type FileService struct { | @@ -24,7 +25,7 @@ type FileService struct { | ||
24 | } | 25 | } |
25 | 26 | ||
26 | // CreateFile 创建文件服务 | 27 | // CreateFile 创建文件服务 |
27 | -func (fileService *FileService) CreateFile(ctx *domain.Context, createFileCommand *command.CreateFileCommand) (interface{}, error) { | 28 | +func (fileService *FileService) CreateFile(ctx *domain.Context, createFileCommand *command.CreateFileCommand) (*domain.File, error) { |
28 | if err := createFileCommand.ValidateCommand(); err != nil { | 29 | if err := createFileCommand.ValidateCommand(); err != nil { |
29 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | 30 | return nil, application.ThrowError(application.ARG_ERROR, err.Error()) |
30 | } | 31 | } |
@@ -57,7 +58,7 @@ func (fileService *FileService) CreateFile(ctx *domain.Context, createFileComman | @@ -57,7 +58,7 @@ func (fileService *FileService) CreateFile(ctx *domain.Context, createFileComman | ||
57 | } | 58 | } |
58 | fileRepository, _, _ := factory.FastPgFile(transactionContext, 0) | 59 | fileRepository, _, _ := factory.FastPgFile(transactionContext, 0) |
59 | 60 | ||
60 | - // 文件名相同进行替换 | 61 | + // 文件名相同,进行替换 |
61 | if oldFile, findOldFileErr := fileRepository.FindOne(map[string]interface{}{ | 62 | if oldFile, findOldFileErr := fileRepository.FindOne(map[string]interface{}{ |
62 | "context": ctx, | 63 | "context": ctx, |
63 | "fileName": fileInfo.Name, | 64 | "fileName": fileInfo.Name, |
@@ -78,6 +79,41 @@ func (fileService *FileService) CreateFile(ctx *domain.Context, createFileComman | @@ -78,6 +79,41 @@ func (fileService *FileService) CreateFile(ctx *domain.Context, createFileComman | ||
78 | if err != nil { | 79 | if err != nil { |
79 | return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | 80 | return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) |
80 | } | 81 | } |
82 | + // 同时生成主表 | ||
83 | + // 前置需要进行预览、保持文件,才能生成主表 | ||
84 | + if createFileCommand.GenerateTableFlag { | ||
85 | + table := domainService.NewTable(domain.MainTable, fileInfo.Name, createFileCommand.Fields, 0). | ||
86 | + WithPrefix(domain.MainTable.ToString()).WithContext(&domain.Context{}) | ||
87 | + tableRepository, _, _ := factory.FastPgTable(transactionContext, 0) | ||
88 | + if table, err = tableRepository.Save(table); err != nil { | ||
89 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
90 | + } | ||
91 | + domainService.ByteCore.LoadDataTable(domain.ReqLoadDataTable{ | ||
92 | + FileId: file.FileId, | ||
93 | + FileName: file.FileInfo.Name, | ||
94 | + Url: file.FileInfo.Url, | ||
95 | + Ext: file.FileInfo.Ext, | ||
96 | + //Where: where, | ||
97 | + OriginalTableId: fmt.Sprintf("%v", file.FileId), | ||
98 | + IsFromOriginalTable: true, | ||
99 | + TableFileUrl: file.FileInfo.Url, | ||
100 | + ColumnSchemas: bytelib.DomainFieldsToColumnSchemas(table.Fields(false)), | ||
101 | + SortParameters: make(map[string]interface{}), | ||
102 | + }) | ||
103 | + response, err := domainService.ByteCore.SaveTable(domain.ReqSaveTable{FileId: file.FileId, Table: table}) | ||
104 | + if err != nil { | ||
105 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
106 | + } | ||
107 | + if _, err = domainService.ByteCore.GenerateTable(ctx, domain.ReqGenerateTable{ | ||
108 | + file.FileId, response.Url, table, | ||
109 | + }); err != nil { | ||
110 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
111 | + } | ||
112 | + file.FileInfo.TableId = table.TableId | ||
113 | + if file, err = fileRepository.Save(file); err != nil { | ||
114 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
115 | + } | ||
116 | + } | ||
81 | if err = factory.FastLog(transactionContext, domain.CommonLog, file.FileId, &domainService.FileUploadSuccessLog{ | 117 | if err = factory.FastLog(transactionContext, domain.CommonLog, file.FileId, &domainService.FileUploadSuccessLog{ |
82 | LogEntry: domain.NewLogEntry(file.FileInfo.Name, domain.SourceFile.ToString(), domain.FileUpload, ctx), | 118 | LogEntry: domain.NewLogEntry(file.FileInfo.Name, domain.SourceFile.ToString(), domain.FileUpload, ctx), |
83 | }); err != nil { | 119 | }); err != nil { |
@@ -86,7 +122,7 @@ func (fileService *FileService) CreateFile(ctx *domain.Context, createFileComman | @@ -86,7 +122,7 @@ func (fileService *FileService) CreateFile(ctx *domain.Context, createFileComman | ||
86 | if err := transactionContext.CommitTransaction(); err != nil { | 122 | if err := transactionContext.CommitTransaction(); err != nil { |
87 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | 123 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) |
88 | } | 124 | } |
89 | - return struct{}{}, nil | 125 | + return file, nil |
90 | } | 126 | } |
91 | 127 | ||
92 | // GetFile 返回文件服务 | 128 | // GetFile 返回文件服务 |
@@ -98,6 +98,50 @@ func (ptr *AppendDataToTableService) AppendData(ctx *domain.Context, fileId int, | @@ -98,6 +98,50 @@ func (ptr *AppendDataToTableService) AppendData(ctx *domain.Context, fileId int, | ||
98 | }, nil | 98 | }, nil |
99 | } | 99 | } |
100 | 100 | ||
101 | +// AppendDataDirectly 直接追加数据 | ||
102 | +func (ptr *AppendDataToTableService) AppendDataDirectly(ctx *domain.Context, fileUrl string, table *domain.Table, mappingFields []*domain.MappingField) (interface{}, error) { | ||
103 | + defer func() { | ||
104 | + //AsyncEvent(domain.NewEventTable(ctx, domain.TableDataImportEvent).WithTable(table)) | ||
105 | + }() | ||
106 | + if !(table.TableType == domain.MainTable.ToString() || table.TableType == domain.SideTable.ToString()) { | ||
107 | + return nil, fmt.Errorf("只能追加数据到主表或者副表") | ||
108 | + } | ||
109 | + var err error | ||
110 | + // 通知底层进行追加数据 | ||
111 | + requestData := domain.ReqAppendData{Table: table, FileId: int(time.Now().Unix()), FileUrl: fileUrl, ExcelTable: table} | ||
112 | + if len(mappingFields) > 0 { | ||
113 | + for _, m := range mappingFields { | ||
114 | + var toField, fromField *domain.Field | ||
115 | + var ok bool | ||
116 | + toField, ok = table.MatchField(m.MainTableField) | ||
117 | + if !ok { | ||
118 | + continue | ||
119 | + } | ||
120 | + if len(m.VerifiedFileFieldName) == 0 { | ||
121 | + fromField = &domain.Field{} | ||
122 | + } else { | ||
123 | + fromField, ok = table.MatchField(&domain.Field{Name: m.VerifiedFileFieldName}) | ||
124 | + if !ok { | ||
125 | + continue | ||
126 | + } | ||
127 | + } | ||
128 | + if fromField.SQLType != "" && !toField.SqlTypeEqual(fromField) { | ||
129 | + //return nil, fmt.Errorf("字段【%s】的类型与导入数据表的类型不匹配", toField.Name) | ||
130 | + return map[string]interface{}{ | ||
131 | + "result": fmt.Sprintf("字段【%s】的类型与导入数据表的类型不匹配", toField.Name), | ||
132 | + }, nil | ||
133 | + } | ||
134 | + fromField.SQLType = toField.SQLType // 兼容 INT BIGINT | ||
135 | + requestData.To = append(requestData.To, toField) | ||
136 | + requestData.From = append(requestData.From, fromField) | ||
137 | + } | ||
138 | + } | ||
139 | + if _, err = ByteCore.AppendData(requestData); err != nil { | ||
140 | + return nil, err | ||
141 | + } | ||
142 | + return nil, nil | ||
143 | +} | ||
144 | + | ||
101 | // PreflightCheck 预检 | 145 | // PreflightCheck 预检 |
102 | func (ptr *AppendDataToTableService) PreflightCheck(ctx *domain.Context, fileId int, tableId int, mappingFields []*domain.MappingField) (interface{}, error) { | 146 | func (ptr *AppendDataToTableService) PreflightCheck(ctx *domain.Context, fileId int, tableId int, mappingFields []*domain.MappingField) (interface{}, error) { |
103 | tableRepository, _ := repository.NewTableRepository(ptr.transactionContext) | 147 | tableRepository, _ := repository.NewTableRepository(ptr.transactionContext) |
@@ -34,8 +34,14 @@ func (controller *FileController) CreateAppTableFile() { | @@ -34,8 +34,14 @@ func (controller *FileController) CreateAppTableFile() { | ||
34 | return | 34 | return |
35 | } | 35 | } |
36 | createFileCommand.AppKey = ParseAppKey(controller.BaseController) | 36 | createFileCommand.AppKey = ParseAppKey(controller.BaseController) |
37 | - data, err := fileService.CreateFile(&domain.Context{}, createFileCommand) | ||
38 | - controller.Response(data, err) | 37 | + // GenerateTableFlag 如果是true,生成主表 |
38 | + if createDigitalAppFileCommand.GenerateTableFlag { | ||
39 | + createFileCommand.GenerateTableFlag = true | ||
40 | + createFileCommand.Fields = createDigitalAppFileCommand.Fields | ||
41 | + } | ||
42 | + _, err = fileService.CreateFile(&domain.Context{}, createFileCommand) | ||
43 | + | ||
44 | + controller.Response(struct{}{}, err) | ||
39 | } | 45 | } |
40 | 46 | ||
41 | func (controller *FileController) DeleteAppTableFile() { | 47 | func (controller *FileController) DeleteAppTableFile() { |
@@ -52,7 +58,17 @@ func (controller *FileController) AppendDataAppTableFile() { | @@ -52,7 +58,17 @@ func (controller *FileController) AppendDataAppTableFile() { | ||
52 | cmd := &command.AppTableFileAppendDataCommand{} | 58 | cmd := &command.AppTableFileAppendDataCommand{} |
53 | controller.Unmarshal(cmd) | 59 | controller.Unmarshal(cmd) |
54 | cmd.AppKey = ParseAppKey(controller.BaseController) | 60 | cmd.AppKey = ParseAppKey(controller.BaseController) |
55 | - data, err := fileService.AppTableFileAppendData(&domain.Context{}, cmd) | 61 | + var ( |
62 | + data interface{} | ||
63 | + err error | ||
64 | + ) | ||
65 | + // AppendDataToTableFlag 如果是true,生成主表 追加数据道表 | ||
66 | + if cmd.AppendTableDataFlag { | ||
67 | + data, err = fileService.AppTableAppendData(&domain.Context{}, cmd) | ||
68 | + } else { | ||
69 | + data, err = fileService.AppTableFileAppendData(&domain.Context{}, cmd) | ||
70 | + } | ||
71 | + | ||
56 | controller.Response(data, err) | 72 | controller.Response(data, err) |
57 | } | 73 | } |
58 | 74 |
-
请 注册 或 登录 后发表评论