file_table_preview_data_table_service.go 3.1 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/api/bytelib"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
)

type PreviewDataTableService struct {
	FileId             int
	transactionContext *pgTransaction.TransactionContext
}

// Preview 预览  【data-table】
func (ptr *PreviewDataTableService) Preview(ctx *domain.Context, fileId int, fields []*domain.Field, where domain.Where) (interface{}, error) {
	fileRepository, _ := repository.NewFileRepository(ptr.transactionContext)
	file, err := fileRepository.FindOne(map[string]interface{}{"fileId": fileId})
	if err != nil {
		return nil, fmt.Errorf("文件不存在")
	}
	isSourceFile := false
	fileUrl := ""
	// Copy to TemporaryFile
	if file.FileType != domain.TemporaryFile.ToString() {
		file = file.CopyTo(domain.TemporaryFile, ctx)
		if file, err = fileRepository.Save(file); err != nil {
			return nil, err
		}
		isSourceFile = true
		fileUrl = file.FileInfo.Url
	}
	//TEST
	ptr.FileId = file.FileId

	// Load Data From Excel(python api)
	byteCore, _ := CreateByteCoreService()
	response, err := byteCore.LoadDataTable(domain.ReqLoadDataTable{
		FileId:   file.FileId,
		FileName: file.FileInfo.Name,
		Url:      file.FileInfo.Url,
		Ext:      file.FileInfo.Ext,
		Where:    where,

		OriginalTableId:     fmt.Sprintf("%v", file.FileId),
		IsFromOriginalTable: isSourceFile,
		TableFileUrl:        fileUrl,
		ColumnSchemas:       bytelib.DomainFieldsToColumnSchemas(fields),
		QueryParameters:     make(map[string]interface{}),
		SortParameters:      make(map[string]interface{}),
	})
	if err != nil {
		return nil, err
	}
	var responseDto = &FilePreviewDto{}
	responseDto.Load(file.FileId, response)
	return responseDto, nil
}

type FilePreviewDto struct {
	ObjectId     int                  `json:"objectId"`
	ObjectType   string               `json:"objectType"`
	TableType    string               `json:"tableType"`
	Fields       []*domain.Field      `json:"fields"`
	Data         interface{}          `json:"grid"`
	PageNumber   int                  `json:"pageNumber"`
	InValidCells []domain.InValidCell `json:"inValidCells"`
}

func (d *FilePreviewDto) Load(fileId int, m *domain.DataLoadDataTable) {
	d.ObjectId = fileId
	d.ObjectType = domain.ObjectFile
	d.TableType = domain.ExcelTable.ToString()
	d.Fields = m.Fields

	mapData := domain.ToFieldData(m.Fields, m.Data, true)
	d.Data = domain.GripData(mapData, int64(m.Total))
	d.PageNumber = m.PageNumber
	d.InValidCells = domain.NewInValidCells(m.Fields, mapData)
}

func (ptr *PreviewDataTableService) GetFileId() int {
	return ptr.FileId
}

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