byte_core_service.go 4.9 KB
package domainService

import (
	"bytes"
	"github.com/beego/beego/v2/client/httplib"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
	"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/excel"
)

type ByteCoreService struct {
	TempDataTable map[int]*domain.DataLoadDataTable
}

var ByteCore domain.ByteLibService = &WrapByteCoreService{} //&ByteCoreService{}

var _ domain.ByteLibService = (*ByteCoreService)(nil)

func (ptr *ByteCoreService) LoadDataTable(param domain.ReqLoadDataTable) (*domain.DataLoadDataTable, error) {
	if v, ok := ptr.load(param.FileId); ok {
		return v.Filter(param.Where), nil
	}
	file, err := httplib.Get(param.Url).Bytes()
	if err != nil {
		return nil, err
	}
	reader := bytes.NewReader(file)
	var importer *excel.Importer
	if param.Ext == domain.XLSX {
		importer = excel.NewExcelImport(&excel.XLXSReader{})
	} else if param.Ext == domain.XLS {
		importer = excel.NewExcelImport(&excel.XLSReader{})
	}
	data, err := importer.OpenExcelFromIoReader(reader)
	if err != nil {
		return nil, err
	}
	cols := importer.Reader().Header().Columns

	var response = &domain.DataLoadDataTable{
		FileId:       param.FileId,
		Fields:       columnToField(cols),
		Data:         data,
		Total:        len(data),
		PageNumber:   param.PageNumber,
		InValidCells: make([]domain.InValidCell, 0),
	}

	ptr.save(param.FileId, response)
	return response.Filter(param.Where), nil
}

func (ptr *ByteCoreService) EditTable(param domain.ReqEditDataTable) (*domain.DataEditDataTable, error) {
	apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
	return apiByteLib.EditTable(param)
}

func (ptr *ByteCoreService) save(fileId int, dataTable *domain.DataLoadDataTable) {
	// Once
	if ptr.TempDataTable == nil {
		ptr.TempDataTable = make(map[int]*domain.DataLoadDataTable)
	}
	ptr.TempDataTable[fileId] = dataTable
}

func (ptr *ByteCoreService) load(fileId int) (*domain.DataLoadDataTable, bool) {
	v, ok := ptr.TempDataTable[fileId]
	return v, ok
}

func columnToField(cols []string) []*domain.Field {
	var fields []*domain.Field
	for i, col := range cols {
		fields = append(fields, &domain.Field{
			Name:    col,
			Index:   i + 1,
			SQLType: domain.String.ToString(),
		})
	}
	return fields
}

func (ptr *ByteCoreService) SaveTable(param domain.ReqSaveTable) (*domain.DataSaveTable, error) {
	apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
	return apiByteLib.SaveTable(param)
}

func (ptr *ByteCoreService) GenerateTable(ctx *domain.Context, param domain.ReqGenerateTable) (*domain.DataGenerateTable, error) {
	apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
	return apiByteLib.GenerateTable(param)
}

func (ptr *ByteCoreService) CopyTable(param domain.ReqCopyTable) (*domain.DataCopyTable, error) {
	apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
	return apiByteLib.CopyTable(param)
}

func (ptr *ByteCoreService) AppendData(param domain.ReqAppendData) (*domain.DataAppendData, error) {
	apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
	return apiByteLib.AppendData(param)
}

func (ptr *ByteCoreService) DeleteTable(param domain.ReqDeleteTable) (*domain.DataDeleteTable, error) {
	apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
	return apiByteLib.DeleteTable(param)
}

func (ptr *ByteCoreService) CancelFile(param domain.ReqCancelFile) (*domain.DataCancelFile, error) {
	apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
	return apiByteLib.CancelFile(param)
}

func (ptr *ByteCoreService) EditTableData(param domain.ReqEditTableData) (*domain.DataEditTableData, error) {
	apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
	return apiByteLib.EditTableData(param)
}

func (ptr *ByteCoreService) SplitTable(param domain.ReqSplitTable) (*domain.DataSplitTable, error) {
	apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
	return apiByteLib.SplitTable(param)
}

func (ptr *ByteCoreService) FieldOptionalValues(param domain.ReqFieldOptionalValues) (*domain.DataFieldOptionalValues, error) {
	apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
	return apiByteLib.FieldOptionalValues(param)
}

//////////////
// 字库核心
//////////////

func CreateByteCoreService() (domain.ByteLibService, error) {
	return ByteCore, nil
}

type WrapByteCoreService struct {
	ByteCoreService
}

func (ptr *WrapByteCoreService) LoadDataTable(param domain.ReqLoadDataTable) (*domain.DataLoadDataTable, error) {
	apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
	return apiByteLib.LoadDataTable(param)
}

//func (ptr *WrapByteCoreService) GenerateTable(ctx *domain.Context, param bytecore.ReqGenerateTable) (*bytecore.DataGenerateTable, error) {
//	return nil, nil
//}