table_append_data_to_table_service.go 3.3 KB
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/repository"
	"time"
)

type AppendDataToTableService struct {
	transactionContext *pgTransaction.TransactionContext
}

// AppendData 追加数据 【data-table】
func (ptr *AppendDataToTableService) AppendData(ctx *domain.Context, fileId int, tableId int, mappingFields []*domain.MappingField) (interface{}, error) {
	fileRepository, _ := repository.NewFileRepository(ptr.transactionContext)
	file, err := fileRepository.FindOne(map[string]interface{}{"fileId": fileId})
	if err != nil {
		return nil, fmt.Errorf("文件不存在")
	}
	tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
	table, err := tableRepository.FindOne(map[string]interface{}{"tableId": tableId})
	if err != nil {
		return nil, fmt.Errorf("表不存在")
	}

	excelTable, err := tableRepository.FindOne(map[string]interface{}{"tableId": file.FileInfo.TableId})
	if err != nil {
		return nil, fmt.Errorf("文件未校验")
	}

	if !(table.TableType == domain.MainTable.ToString() || table.TableType == domain.SideTable.ToString()) {
		return nil, fmt.Errorf("只能追加数据到主表或者副表")
	}
	var subTables []*domain.Table
	_, subTables, err = tableRepository.Find(map[string]interface{}{"parentId": tableId, "tableTypes": []string{domain.SubTable.ToString()}})
	if err != nil {
		return nil, err
	}

	// 更新表数据
	table.RowCount += excelTable.RowCount
	table.UpdatedAt = time.Now()
	if _, err = tableRepository.Save(table); err != nil {
		return nil, err
	}

	// 日志
	if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &AppendDataToTableLog{
		LogEntry:  domain.NewLogEntry(table.Name, domain.MainTable.ToString(), domain.AppendData, ctx),
		File:      file,
		Table:     table,
		SubTables: subTables,
		RowCount:  excelTable.RowCount,
	}); err != nil {
		return nil, err
	}

	// 通知底层进行追加数据
	requestData := domain.ReqAppendData{Table: table, FileId: fileId, FileUrl: file.FileInfo.Url, ExcelTable: excelTable}
	if len(mappingFields) > 0 {
		for _, m := range mappingFields {
			var toField, fromField *domain.Field
			var ok bool
			toField, ok = table.MatchField(m.MainTableField)
			if !ok {
				continue
			}
			if len(m.VerifiedFileFieldName) == 0 {
				fromField = &domain.Field{}
				// continue
			} else {
				fromField, ok = excelTable.MatchField(&domain.Field{Name: m.VerifiedFileFieldName})
				if !ok {
					continue
				}
			}
			requestData.To = append(requestData.To, toField)
			requestData.From = append(requestData.From, fromField)
		}
	}
	if _, err = ByteCore.AppendData(requestData); err != nil {
		return nil, err
	}
	return map[string]interface{}{
		"result": fmt.Sprintf("源数据%v条,成功追加%v条;", excelTable.RowCount, excelTable.RowCount),
	}, nil
}

func NewAppendDataToTableService(transactionContext *pgTransaction.TransactionContext) (*AppendDataToTableService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &AppendDataToTableService{
			transactionContext: transactionContext,
		}, nil
	}
}