file_table_edit_data_table_service.go
4.3 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
package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/redis"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
)
type EditDataTableService struct {
transactionContext *pgTransaction.TransactionContext
}
// Edit 表结构编辑 【data-table】
func (ptr *EditDataTableService) Edit(ctx *domain.Context, req domain.EditTableRequest) (*domain.DataEditDataTable, error) {
fileRepository, _ := repository.NewFileRepository(ptr.transactionContext)
file, err := fileRepository.FindOne(map[string]interface{}{"fileId": req.FileId})
if err != nil {
return nil, fmt.Errorf("文件不存在")
}
if file.FileType != domain.TemporaryFile.ToString() {
return nil, fmt.Errorf("文件未校验")
}
// 通知底层进行文件表编辑
response, err := ByteCore.EditTable(domain.ReqEditDataTable{
FileId: file.FileId,
PageNumber: req.Where.PageNumber,
PageSize: req.Where.PageSize,
Fields: domain.ToFields(req.Fields),
ProcessFields: domain.ToFields(req.ProcessFields),
Action: req.Action,
Params: req.Params,
HeaderRow: req.Where.HeaderRow,
})
if err != nil {
return nil, err
//log.Logger.Error(err.Error())
}
// 处理错误
level := domain.LevelInfo
errMsg := ""
if len(response.InValidCells) > 0 {
level = domain.LevelError
errMsg = response.InValidCells[0].Err
}
// 日志
var operateName string = actionName(req.Action, req.Params)
if err = FastLog(ptr.transactionContext, domain.VerifiedStepLog, file.FileId, &ExcelTableEditLog{
LogEntry: domain.NewLogEntry(file.FileInfo.Name, domain.VerifiedFile.ToString(), domain.FileVerify,
ctx.WithValue(domain.ContextWithLogLevel, level).
WithValue(domain.ContextWithLogMsg, errMsg)),
ProcessFields: req.ProcessFields,
OperateName: operateName,
}); err != nil {
return nil, err
}
// 1.有修改表类型的,更新缓存数据列类型
if response != nil && len(response.Fields) > 0 {
// 特殊处理修改类型错误
options := make([]redis.FileCacheOptionsFunc, 0)
cacheService := redis.NewFileCacheService()
if req.Action == "convert-column-type" {
var toType = req.Params["convertType"].(string)
var fieldName = req.ProcessFieldNames[0]
if level == domain.LevelError {
options = append(options, redis.WithAddConvertTypeErrors([]redis.ConvertTypeError{{FieldName: fieldName, ErrMsg: errMsg, ToType: toType}}))
} else {
options = append(options, redis.WithRemoveConvertTypeErrors([]redis.ConvertTypeError{{FieldName: fieldName, ErrMsg: errMsg, ToType: toType}}))
}
// 底层未返回更改类型以后的字段列表,手动修改缓存
if file, err := cacheService.UpdateField(redis.KeyTemporaryFileInfo(file.FileId), file, options...); err != nil {
return nil, err
} else {
response.Fields = file.Fields
}
return response, nil
}
if _, err := cacheService.Update(redis.KeyTemporaryFileInfo(file.FileId), file, response.Fields, response.Total, options...); err != nil {
return nil, err
}
}
return response, nil
}
func actionName(action string, params map[string]interface{}) string {
if v, ok := domain.MapActionCommon[action]; ok {
return v.Desc
}
if params != nil {
switch action {
case domain.FormatColumn:
if v, ok := domain.MapActionFormat[params["formatMethod"].(string)]; ok {
return v.Desc
}
case domain.SplitColumn:
if v, ok := domain.MapActionSplitColumn[params["splitMethod"].(string)]; ok {
return v.Desc
}
case domain.ExtractColumn:
if v, ok := domain.MapActionExtractColumn[params["extractMethod"].(string)]; ok {
return v.Desc
}
case domain.ReplaceColumn:
if v, ok := domain.MapActionReplaceColumn[params["replaceMethod"].(string)]; ok {
return v.Desc
}
}
}
return action
}
func NewEditDataTableService(transactionContext *pgTransaction.TransactionContext) (*EditDataTableService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &EditDataTableService{
transactionContext: transactionContext,
}, nil
}
}