作者 yangfu

chore:file duplicate append judgment

... ... @@ -212,6 +212,33 @@ func (fileService *FileService) AppendDataToTable(ctx *domain.Context, cmd *comm
return result, nil
}
// AppendDataToTable 追加数据
func (fileService *FileService) AppendDataToTablePreflightCheck(ctx *domain.Context, cmd *command.AppendDataToTableCommand) (interface{}, error) {
if err := cmd.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
generateMainTableService, _ := factory.CreateAppendDataToTableService(transactionContext)
result, err := generateMainTableService.PreflightCheck(ctx, cmd.FileId, cmd.TableId, cmd.MappingFields)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return result, nil
}
// ExportFile 文件下载
func (fileService *FileService) ExportFile(ctx *domain.Context, cmd *command.ExportFileCommand) (interface{}, error) {
if err := cmd.ValidateCommand(); err != nil {
... ...
... ... @@ -50,6 +50,7 @@ type DeleteDataTableService interface {
type AppendDataToTableService interface {
AppendData(ctx *Context, fileId int, tableId int, mappingFields []*MappingField) (interface{}, error)
PreflightCheck(ctx *Context, fileId int, tableId int, mappingFields []*MappingField) (interface{}, error)
}
/************************************/
... ...
... ... @@ -18,6 +18,8 @@ type LogEntry struct {
Level string `json:"level"`
// 日志时间
LogTime string `json:"logTime"`
// 追加文件ID (操作类型 主表生成、数据导入时有效)
AppendFileId int `json:"appendFileId,omitempty"`
// 错误信息
Error string `json:"error"`
ctx *Context `json:"-"`
... ... @@ -40,6 +42,11 @@ func (l LogEntry) OperateType() string {
return l.OperationType
}
func (l *LogEntry) WithAppendFileId(fileId int) LogEntry {
l.AppendFileId = fileId
return *l
}
func NewLogEntry(fileOrTableName string, objectType string, operationType OperationType, ctx *Context) LogEntry {
return LogEntry{
ObjectName: fileOrTableName,
... ...
... ... @@ -37,8 +37,9 @@ func (ptr *GenerateMainTableService) GenerateTable(ctx *domain.Context, fileId i
return nil, err
}
// 日志
entry := domain.NewLogEntry(tableName, domain.MainTable.ToString(), domain.GenerateMainTable, ctx)
if err = FastLog(ptr.transactionContext, domain.CommonLog, mainTable.TableId, &GenerateMainTableLog{
LogEntry: domain.NewLogEntry(tableName, domain.MainTable.ToString(), domain.GenerateMainTable, ctx),
LogEntry: (&entry).WithAppendFileId(file.FileId),
FileName: file.FileInfo.Name,
}); err != nil {
return nil, err
... ...
... ... @@ -49,8 +49,9 @@ func (ptr *AppendDataToTableService) AppendData(ctx *domain.Context, fileId int,
}
// 日志
entry := domain.NewLogEntry(table.Name, domain.MainTable.ToString(), domain.AppendData, ctx)
if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &AppendDataToTableLog{
LogEntry: domain.NewLogEntry(table.Name, domain.MainTable.ToString(), domain.AppendData, ctx),
LogEntry: (&entry).WithAppendFileId(fileId),
File: file,
Table: table,
SubTables: subTables,
... ... @@ -90,6 +91,39 @@ func (ptr *AppendDataToTableService) AppendData(ctx *domain.Context, fileId int,
}, nil
}
// PreflightCheck 预检
func (ptr *AppendDataToTableService) PreflightCheck(ctx *domain.Context, fileId int, tableId int, mappingFields []*domain.MappingField) (interface{}, error) {
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
table, err := tableRepository.FindOne(map[string]interface{}{"tableId": tableId})
if err != nil {
return nil, fmt.Errorf("表不存在")
}
inSourceId := []int{table.TableId}
if table.ParentId != 0 {
inSourceId = append(inSourceId, table.ParentId)
}
logRepository, _ := repository.NewLogRepository(ptr.transactionContext)
_, logs, err := logRepository.Find(map[string]interface{}{
"inSourceId": inSourceId,
"inOperationType": []string{domain.GenerateMainTable.ToString(), domain.AppendData.ToString()},
"limit": 500,
})
if err != nil {
return nil, err
}
for _, log := range logs {
if log.Entry.AppendFileId == fileId {
return map[string]interface{}{
"fileAppended": true,
}, nil
}
}
return map[string]interface{}{
"fileAppended": false,
}, nil
}
func NewAppendDataToTableService(transactionContext *pgTransaction.TransactionContext) (*AppendDataToTableService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
... ...
... ... @@ -144,6 +144,9 @@ func (repository *LogRepository) Find(queryOptions map[string]interface{}) (int6
if v, ok := queryOptions["inSourceId"]; ok && len(v.([]int)) > 0 {
query.Where("source_id in (?)", pg.In(v.([]int)))
}
if v, ok := queryOptions["inOperationType"]; ok && len(v.([]string)) > 0 {
query.Where("entry->>'operationType' in (?)", pg.In(v.([]string)))
}
if v, ok := queryOptions["matchContent"]; ok && len(v.(string)) > 0 {
query.WhereGroup(func(query *orm.Query) (*orm.Query, error) {
matchContent := v.(string)
... ...
... ... @@ -138,6 +138,14 @@ func (controller *FileController) AppendDataToTable() {
controller.Response(data, err)
}
func (controller *FileController) AppendDataToTablePreflightCheck() {
fileService := service.NewFileService(nil)
cmd := &command.AppendDataToTableCommand{}
controller.Unmarshal(cmd)
data, err := fileService.AppendDataToTablePreflightCheck(ParseContext(controller.BaseController), cmd)
controller.Response(data, err)
}
func (controller *FileController) CancelVerifyingFile() {
fileService := service.NewFileService(nil)
cmd := &command.CancelVerifyingFileCommand{}
... ...
... ... @@ -24,4 +24,5 @@ func init() {
web.Router("/data/flush-data-table", &controllers.FileController{}, "Post:FlushDataTable")
web.Router("/data/generate-main-table", &controllers.FileController{}, "Post:GenerateMainTable")
web.Router("/data/append-data-to-table", &controllers.FileController{}, "Post:AppendDataToTable")
web.Router("/data/append-data-preflight-check", &controllers.FileController{}, "Post:AppendDataToTablePreflightCheck")
}
... ...