作者 yangfu

chore:file duplicate append judgment

@@ -212,6 +212,33 @@ func (fileService *FileService) AppendDataToTable(ctx *domain.Context, cmd *comm @@ -212,6 +212,33 @@ func (fileService *FileService) AppendDataToTable(ctx *domain.Context, cmd *comm
212 return result, nil 212 return result, nil
213 } 213 }
214 214
  215 +// AppendDataToTable 追加数据
  216 +func (fileService *FileService) AppendDataToTablePreflightCheck(ctx *domain.Context, cmd *command.AppendDataToTableCommand) (interface{}, error) {
  217 + if err := cmd.ValidateCommand(); err != nil {
  218 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  219 + }
  220 + transactionContext, err := factory.CreateTransactionContext(nil)
  221 + if err != nil {
  222 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  223 + }
  224 + if err := transactionContext.StartTransaction(); err != nil {
  225 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  226 + }
  227 + defer func() {
  228 + transactionContext.RollbackTransaction()
  229 + }()
  230 +
  231 + generateMainTableService, _ := factory.CreateAppendDataToTableService(transactionContext)
  232 + result, err := generateMainTableService.PreflightCheck(ctx, cmd.FileId, cmd.TableId, cmd.MappingFields)
  233 + if err != nil {
  234 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  235 + }
  236 + if err := transactionContext.CommitTransaction(); err != nil {
  237 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  238 + }
  239 + return result, nil
  240 +}
  241 +
215 // ExportFile 文件下载 242 // ExportFile 文件下载
216 func (fileService *FileService) ExportFile(ctx *domain.Context, cmd *command.ExportFileCommand) (interface{}, error) { 243 func (fileService *FileService) ExportFile(ctx *domain.Context, cmd *command.ExportFileCommand) (interface{}, error) {
217 if err := cmd.ValidateCommand(); err != nil { 244 if err := cmd.ValidateCommand(); err != nil {
@@ -50,6 +50,7 @@ type DeleteDataTableService interface { @@ -50,6 +50,7 @@ type DeleteDataTableService interface {
50 50
51 type AppendDataToTableService interface { 51 type AppendDataToTableService interface {
52 AppendData(ctx *Context, fileId int, tableId int, mappingFields []*MappingField) (interface{}, error) 52 AppendData(ctx *Context, fileId int, tableId int, mappingFields []*MappingField) (interface{}, error)
  53 + PreflightCheck(ctx *Context, fileId int, tableId int, mappingFields []*MappingField) (interface{}, error)
53 } 54 }
54 55
55 /************************************/ 56 /************************************/
@@ -18,6 +18,8 @@ type LogEntry struct { @@ -18,6 +18,8 @@ type LogEntry struct {
18 Level string `json:"level"` 18 Level string `json:"level"`
19 // 日志时间 19 // 日志时间
20 LogTime string `json:"logTime"` 20 LogTime string `json:"logTime"`
  21 + // 追加文件ID (操作类型 主表生成、数据导入时有效)
  22 + AppendFileId int `json:"appendFileId,omitempty"`
21 // 错误信息 23 // 错误信息
22 Error string `json:"error"` 24 Error string `json:"error"`
23 ctx *Context `json:"-"` 25 ctx *Context `json:"-"`
@@ -40,6 +42,11 @@ func (l LogEntry) OperateType() string { @@ -40,6 +42,11 @@ func (l LogEntry) OperateType() string {
40 return l.OperationType 42 return l.OperationType
41 } 43 }
42 44
  45 +func (l *LogEntry) WithAppendFileId(fileId int) LogEntry {
  46 + l.AppendFileId = fileId
  47 + return *l
  48 +}
  49 +
43 func NewLogEntry(fileOrTableName string, objectType string, operationType OperationType, ctx *Context) LogEntry { 50 func NewLogEntry(fileOrTableName string, objectType string, operationType OperationType, ctx *Context) LogEntry {
44 return LogEntry{ 51 return LogEntry{
45 ObjectName: fileOrTableName, 52 ObjectName: fileOrTableName,
@@ -37,8 +37,9 @@ func (ptr *GenerateMainTableService) GenerateTable(ctx *domain.Context, fileId i @@ -37,8 +37,9 @@ func (ptr *GenerateMainTableService) GenerateTable(ctx *domain.Context, fileId i
37 return nil, err 37 return nil, err
38 } 38 }
39 // 日志 39 // 日志
  40 + entry := domain.NewLogEntry(tableName, domain.MainTable.ToString(), domain.GenerateMainTable, ctx)
40 if err = FastLog(ptr.transactionContext, domain.CommonLog, mainTable.TableId, &GenerateMainTableLog{ 41 if err = FastLog(ptr.transactionContext, domain.CommonLog, mainTable.TableId, &GenerateMainTableLog{
41 - LogEntry: domain.NewLogEntry(tableName, domain.MainTable.ToString(), domain.GenerateMainTable, ctx), 42 + LogEntry: (&entry).WithAppendFileId(file.FileId),
42 FileName: file.FileInfo.Name, 43 FileName: file.FileInfo.Name,
43 }); err != nil { 44 }); err != nil {
44 return nil, err 45 return nil, err
@@ -49,8 +49,9 @@ func (ptr *AppendDataToTableService) AppendData(ctx *domain.Context, fileId int, @@ -49,8 +49,9 @@ func (ptr *AppendDataToTableService) AppendData(ctx *domain.Context, fileId int,
49 } 49 }
50 50
51 // 日志 51 // 日志
  52 + entry := domain.NewLogEntry(table.Name, domain.MainTable.ToString(), domain.AppendData, ctx)
52 if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &AppendDataToTableLog{ 53 if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &AppendDataToTableLog{
53 - LogEntry: domain.NewLogEntry(table.Name, domain.MainTable.ToString(), domain.AppendData, ctx), 54 + LogEntry: (&entry).WithAppendFileId(fileId),
54 File: file, 55 File: file,
55 Table: table, 56 Table: table,
56 SubTables: subTables, 57 SubTables: subTables,
@@ -90,6 +91,39 @@ func (ptr *AppendDataToTableService) AppendData(ctx *domain.Context, fileId int, @@ -90,6 +91,39 @@ func (ptr *AppendDataToTableService) AppendData(ctx *domain.Context, fileId int,
90 }, nil 91 }, nil
91 } 92 }
92 93
  94 +// PreflightCheck 预检
  95 +func (ptr *AppendDataToTableService) PreflightCheck(ctx *domain.Context, fileId int, tableId int, mappingFields []*domain.MappingField) (interface{}, error) {
  96 + tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
  97 + table, err := tableRepository.FindOne(map[string]interface{}{"tableId": tableId})
  98 + if err != nil {
  99 + return nil, fmt.Errorf("表不存在")
  100 + }
  101 + inSourceId := []int{table.TableId}
  102 + if table.ParentId != 0 {
  103 + inSourceId = append(inSourceId, table.ParentId)
  104 + }
  105 + logRepository, _ := repository.NewLogRepository(ptr.transactionContext)
  106 + _, logs, err := logRepository.Find(map[string]interface{}{
  107 + "inSourceId": inSourceId,
  108 + "inOperationType": []string{domain.GenerateMainTable.ToString(), domain.AppendData.ToString()},
  109 + "limit": 500,
  110 + })
  111 + if err != nil {
  112 + return nil, err
  113 + }
  114 +
  115 + for _, log := range logs {
  116 + if log.Entry.AppendFileId == fileId {
  117 + return map[string]interface{}{
  118 + "fileAppended": true,
  119 + }, nil
  120 + }
  121 + }
  122 + return map[string]interface{}{
  123 + "fileAppended": false,
  124 + }, nil
  125 +}
  126 +
93 func NewAppendDataToTableService(transactionContext *pgTransaction.TransactionContext) (*AppendDataToTableService, error) { 127 func NewAppendDataToTableService(transactionContext *pgTransaction.TransactionContext) (*AppendDataToTableService, error) {
94 if transactionContext == nil { 128 if transactionContext == nil {
95 return nil, fmt.Errorf("transactionContext参数不能为nil") 129 return nil, fmt.Errorf("transactionContext参数不能为nil")
@@ -144,6 +144,9 @@ func (repository *LogRepository) Find(queryOptions map[string]interface{}) (int6 @@ -144,6 +144,9 @@ func (repository *LogRepository) Find(queryOptions map[string]interface{}) (int6
144 if v, ok := queryOptions["inSourceId"]; ok && len(v.([]int)) > 0 { 144 if v, ok := queryOptions["inSourceId"]; ok && len(v.([]int)) > 0 {
145 query.Where("source_id in (?)", pg.In(v.([]int))) 145 query.Where("source_id in (?)", pg.In(v.([]int)))
146 } 146 }
  147 + if v, ok := queryOptions["inOperationType"]; ok && len(v.([]string)) > 0 {
  148 + query.Where("entry->>'operationType' in (?)", pg.In(v.([]string)))
  149 + }
147 if v, ok := queryOptions["matchContent"]; ok && len(v.(string)) > 0 { 150 if v, ok := queryOptions["matchContent"]; ok && len(v.(string)) > 0 {
148 query.WhereGroup(func(query *orm.Query) (*orm.Query, error) { 151 query.WhereGroup(func(query *orm.Query) (*orm.Query, error) {
149 matchContent := v.(string) 152 matchContent := v.(string)
@@ -138,6 +138,14 @@ func (controller *FileController) AppendDataToTable() { @@ -138,6 +138,14 @@ func (controller *FileController) AppendDataToTable() {
138 controller.Response(data, err) 138 controller.Response(data, err)
139 } 139 }
140 140
  141 +func (controller *FileController) AppendDataToTablePreflightCheck() {
  142 + fileService := service.NewFileService(nil)
  143 + cmd := &command.AppendDataToTableCommand{}
  144 + controller.Unmarshal(cmd)
  145 + data, err := fileService.AppendDataToTablePreflightCheck(ParseContext(controller.BaseController), cmd)
  146 + controller.Response(data, err)
  147 +}
  148 +
141 func (controller *FileController) CancelVerifyingFile() { 149 func (controller *FileController) CancelVerifyingFile() {
142 fileService := service.NewFileService(nil) 150 fileService := service.NewFileService(nil)
143 cmd := &command.CancelVerifyingFileCommand{} 151 cmd := &command.CancelVerifyingFileCommand{}
@@ -24,4 +24,5 @@ func init() { @@ -24,4 +24,5 @@ func init() {
24 web.Router("/data/flush-data-table", &controllers.FileController{}, "Post:FlushDataTable") 24 web.Router("/data/flush-data-table", &controllers.FileController{}, "Post:FlushDataTable")
25 web.Router("/data/generate-main-table", &controllers.FileController{}, "Post:GenerateMainTable") 25 web.Router("/data/generate-main-table", &controllers.FileController{}, "Post:GenerateMainTable")
26 web.Router("/data/append-data-to-table", &controllers.FileController{}, "Post:AppendDataToTable") 26 web.Router("/data/append-data-to-table", &controllers.FileController{}, "Post:AppendDataToTable")
  27 + web.Router("/data/append-data-preflight-check", &controllers.FileController{}, "Post:AppendDataToTablePreflightCheck")
27 } 28 }