load_data_table_service.go 1.8 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/domain/bytecore"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
)

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

func (ptr *LoadDataTableService) Load(ctx *domain.Context, fileId int, 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("文件不存在")
	}

	// 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
		}
	}
	//TEST
	ptr.FileId = file.FileId

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

	return response, nil
}

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

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