...
|
...
|
@@ -20,6 +20,7 @@ type TemporaryFileInfo struct { |
|
|
FileId int `json:"fileId"`
|
|
|
FileType string `json:"fileType"`
|
|
|
Total int `json:"total"`
|
|
|
HeaderRow int `json:"headerRow"`
|
|
|
Fields []*domain.Field `json:"fields"`
|
|
|
// 编辑表错误,有错误不允许保存成校验文件
|
|
|
// 行记录错误
|
...
|
...
|
@@ -66,6 +67,11 @@ func (f *TemporaryFileInfo) SetTotal(total int) *TemporaryFileInfo { |
|
|
return f
|
|
|
}
|
|
|
|
|
|
func (f *TemporaryFileInfo) SetHeaderRow(headerRow int) *TemporaryFileInfo {
|
|
|
f.HeaderRow = headerRow
|
|
|
return f
|
|
|
}
|
|
|
|
|
|
func (f *TemporaryFileInfo) AddConvertTypeError(e ConvertTypeError) *TemporaryFileInfo {
|
|
|
f.RemoveConvertTypeError(e)
|
|
|
f.addConvertTypeError(e)
|
...
|
...
|
@@ -99,46 +105,52 @@ type ConvertTypeError struct { |
|
|
type FileCacheService struct {
|
|
|
}
|
|
|
|
|
|
func (s *FileCacheService) Update(key string, file *domain.File, fields []*domain.Field, total int, errors ...FileCacheOptionsFunc) (*TemporaryFileInfo, error) {
|
|
|
func (s *FileCacheService) Update(key string, file *domain.File, fields []*domain.Field, total int, option ...FileCacheOptionsFunc) (*TemporaryFileInfo, error) {
|
|
|
options := NewFileCacheOptions(option...)
|
|
|
ok, err := ZeroCoreRedis.Exists(key)
|
|
|
var response = &TemporaryFileInfo{}
|
|
|
var tmpFile = &TemporaryFileInfo{}
|
|
|
if err != nil {
|
|
|
return response, err
|
|
|
return tmpFile, err
|
|
|
}
|
|
|
if !ok {
|
|
|
response.SetFile(file).SetFields(fields).SetTotal(total)
|
|
|
return response, ZeroCoreRedis.Setex(key, json.MarshalToString(response), TemporaryFileExpire)
|
|
|
tmpFile.SetFile(file).SetFields(fields).SetTotal(total)
|
|
|
if options.HasSetHeaderRow {
|
|
|
tmpFile.SetHeaderRow(options.HeaderRow)
|
|
|
}
|
|
|
return tmpFile, ZeroCoreRedis.Setex(key, json.MarshalToString(tmpFile), TemporaryFileExpire)
|
|
|
}
|
|
|
data, err := ZeroCoreRedis.Get(key)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
err = json.UnmarshalFromString(data, response)
|
|
|
err = json.UnmarshalFromString(data, tmpFile)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
response.SetFields(fields)
|
|
|
tmpFile.SetFields(fields)
|
|
|
if options.HasSetHeaderRow {
|
|
|
tmpFile.SetHeaderRow(options.HeaderRow)
|
|
|
}
|
|
|
|
|
|
options := NewFileCacheOptions(errors...)
|
|
|
for i := range options.AddConvertTypeErrors {
|
|
|
response.AddConvertTypeError(options.AddConvertTypeErrors[i])
|
|
|
tmpFile.AddConvertTypeError(options.AddConvertTypeErrors[i])
|
|
|
}
|
|
|
for i := range options.RemoveConvertTypeErrors {
|
|
|
convertType := options.RemoveConvertTypeErrors[i]
|
|
|
response.RemoveConvertTypeError(options.RemoveConvertTypeErrors[i])
|
|
|
for j := range response.Fields {
|
|
|
if response.Fields[j].Name == convertType.FieldName {
|
|
|
response.Fields[j].SQLType = convertType.ToType
|
|
|
tmpFile.RemoveConvertTypeError(options.RemoveConvertTypeErrors[i])
|
|
|
for j := range tmpFile.Fields {
|
|
|
if tmpFile.Fields[j].Name == convertType.FieldName {
|
|
|
tmpFile.Fields[j].SQLType = convertType.ToType
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
err = ZeroCoreRedis.Setex(key, json.MarshalToString(response), TemporaryFileExpire)
|
|
|
err = ZeroCoreRedis.Setex(key, json.MarshalToString(tmpFile), TemporaryFileExpire)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
return response, err
|
|
|
return tmpFile, err
|
|
|
}
|
|
|
|
|
|
func (s *FileCacheService) UpdateField(key string, file *domain.File, errors ...FileCacheOptionsFunc) (*TemporaryFileInfo, error) {
|
...
|
...
|
@@ -209,6 +221,8 @@ type FileCacheOptions struct { |
|
|
//OriginalFileId int
|
|
|
RemoveConvertTypeErrors []ConvertTypeError
|
|
|
AddConvertTypeErrors []ConvertTypeError
|
|
|
HeaderRow int
|
|
|
HasSetHeaderRow bool
|
|
|
}
|
|
|
|
|
|
type FileCacheOptionsFunc func(o *FileCacheOptions)
|
...
|
...
|
@@ -225,6 +239,13 @@ func WithAddConvertTypeErrors(errors []ConvertTypeError) FileCacheOptionsFunc { |
|
|
}
|
|
|
}
|
|
|
|
|
|
func WithHeaderRow(headerRow int) FileCacheOptionsFunc {
|
|
|
return func(o *FileCacheOptions) {
|
|
|
o.HeaderRow = headerRow
|
|
|
o.HasSetHeaderRow = true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//func WithOriginalFileId(originalFileId int) FileCacheOptionsFunc {
|
|
|
// return func(o *FileCacheOptions) {
|
|
|
// o.OriginalFileId = originalFileId
|
...
|
...
|
|