byte_core_service.go 7.4 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"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
)

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 (ptr *ByteCoreService) FormulasGenerate(param domain.ReqFormulasGenerate) (*domain.DataFormulasGenerate, error) {
	apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
	//if param.QuerySet.Type == domain.CalculateItem.ToString() {
	//	if err := starrocks.Exec(starrocks.DB,
	//		starrocks.CreateViewSql(param.Table.SQLName, param.Table.DataFields,
	//			starrocks.CalculateItemViewSql(&param.QueryComponents[0].Formula.FieldExpr),
	//		)); err != nil {
	//		return nil, err
	//	}
	//	return &domain.DataFormulasGenerate{}, nil
	//}
	//if param.QuerySet.Type == domain.CalculateTable.ToString() {
	//	if err := starrocks.Exec(starrocks.DB,
	//		starrocks.CreateViewSql(param.Table.SQLName, param.Table.DataFields,
	//			starrocks.CalculateTableViewSql(param.QueryComponents[0].MasterTable.SQLName, &param.QueryComponents[0].Aggregation.Aggregation),
	//		)); err != nil {
	//		return nil, err
	//	}
	//	return &domain.DataFormulasGenerate{}, nil
	//}
	if param.QuerySet.Type == domain.CalculateItem.ToString() {
		if param.QuerySet.QueryComponents[0].Formula.ExprMode != 0 {
			err := ptr.ExcelExprCalcPersistence(param.QuerySet.QueryComponents[0].Formula, param, true)
			return &domain.DataFormulasGenerate{}, err
		}
	}
	if param.QuerySet.Type == domain.CalculateSet.ToString() {
		_, err := param.QuerySetService.(*QuerySetService).LoadCalculateSetData(param.Context.(*domain.Context), param.QuerySet, param.QueryComponents)
		if err != nil {
			return nil, err
		}
		return &domain.DataFormulasGenerate{}, nil
	}
	return apiByteLib.FormulasGenerate(param)
}

func (ptr *ByteCoreService) FormulasClear(param domain.ReqFormulasClear) (*domain.DataFormulasClear, error) {
	apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
	return apiByteLib.FormulasClear(param)
}

func (ptr *ByteCoreService) ExcelExprCalcPersistence(expr *domain.FieldFormulaExpr, param domain.ReqFormulasGenerate, persistence bool) error {
	if len(param.QueryComponents) == 0 {
		return nil
	}
	// 加载Tables数据
	q := param.QueryComponents[0]
	result, err := param.QuerySetService.(*QuerySetService).LoadCalculateItemData(param.Context.(*domain.Context), param.Table, q.Formula)
	if err != nil {
		return err
	}
	if persistence {
		starrocks.DropView(starrocks.DB, param.Table.SQLName)
		starrocks.DropTable(starrocks.DB, param.Table.SQLName)
		return starrocks.Exec(starrocks.DB, starrocks.CreateTableSql(param.Table.SQLName, param.Table.Fields(false), result.Data[0]))
	}
	return nil
}

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

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
//}