api_byte_lib.go 9.3 KB
package bytelib

import (
	"fmt"
	"github.com/beego/beego/v2/core/logs"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api"
	"time"
)

// ApiByteLib 字库底层接口
type ApiByteLib struct {
	api.BaseServiceGateway
}

func NewApiByteLib(host string) *ApiByteLib {
	gt := api.NewBaseServiceGateway(host)
	gt.ConnectTimeout = 360 * time.Second
	gt.ReadWriteTimeout = 360 * time.Second
	gt.Interceptor = func(msg string) {
		//log.Logger.Info(msg)
		logs.Debug(msg)
	}
	gt.ServiceName = "【字库核心】"
	return &ApiByteLib{
		BaseServiceGateway: gt,
	}
}

// 加载数据

func (gateway ApiByteLib) LoadDataTable(param domain.ReqLoadDataTable) (*domain.DataLoadDataTable, error) {
	url := gateway.Host() + "/checkout-tables/query"
	method := "post"
	var data DataCheckoutTables
	request := NewRequestCheckoutTablesQuery(param)
	err := gateway.FastDoRequest(url, method, request, &data)
	if err != nil {
		return nil, err
	}

	response := ToDataLoadDataTable(data)
	return response, nil
}

// EditTable 编辑表格
func (gateway ApiByteLib) EditTable(param domain.ReqEditDataTable) (*domain.DataEditDataTable, error) {
	url := gateway.Host() + "/checkout-tables/pre-proccess"
	method := "post"
	var data DataCheckoutTables
	request := NewRequestCheckoutTablesPreProccess(param)
	err := gateway.FastDoRequest(url, method, request, &data)
	if err != nil {
		return nil, err
	}

	response := ToDataLoadDataTable(data)
	return &domain.DataEditDataTable{
		DataLoadDataTable: *response,
	}, nil
}

// SaveTable 保存校验文件 (文件地址)
func (gateway ApiByteLib) SaveTable(param domain.ReqSaveTable) (*domain.DataSaveTable, error) {
	url := gateway.Host() + "/checkout-tables/save"
	method := "post"
	var data ResponseCheckoutTablesSave
	var request = RequestCheckoutTablesSave{
		OriginalTableId: fmt.Sprintf("%v", param.FileId),
		FilePostfix:     "xlsx",
		ColumnSchemas:   FieldsToColumnSchemas(param.Table.DataFields),
	}
	err := gateway.FastDoRequest(url, method, request, &data)
	if err != nil {
		return nil, err
	}
	var response = &domain.DataSaveTable{
		Url: data.CheckoutTableUrl,
	}
	return response, nil
}

// GenerateTable 生成主表
func (gateway ApiByteLib) GenerateTable(param domain.ReqGenerateTable) (*domain.DataGenerateTable, error) {
	url := gateway.Host() + "/checkout-tables/generate-master-table"
	method := "post"
	var data DataCheckoutTablesGenerateMasterTable
	request := NewRequestCheckoutTablesGenerateMasterTable(param)
	err := gateway.FastDoRequest(url, method, request, &data)
	if err != nil {
		return nil, err
	}
	var response = domain.DataGenerateTable{
		TableName: data.MasterTableName,
	}
	return &response, nil
}

// CopyTable 表复制
func (gateway ApiByteLib) CopyTable(param domain.ReqCopyTable) (*domain.DataCopyTable, error) {
	var url string
	method := "post"
	var data TableAppendResponse
	var request interface{}
	var copyRequest = NewCopyTableRequest(param)
	switch param.Table.TableType {
	case domain.MainTable.ToString():
		request = CopyMasterTableRequest{
			MasterTableId:    intToString(param.Table.TableId),
			CopyTableRequest: copyRequest,
		}
		url = gateway.Host() + "/master-tables/copy"
	case domain.SubTable.ToString():
		copyRequest.DatabaseTableName = param.MainTable.SQLName // 分表复制时 传入主表的表名
		request = CopyMasterTableRequest{
			MasterTableId:    intToString(param.Table.TableId),
			CopyTableRequest: copyRequest,
		}
		url = gateway.Host() + "/master-tables/copy"
	case domain.SideTable.ToString():
		request = CopyReplicationTableRequest{
			ReplicationTableId: intToString(param.Table.TableId),
			CopyTableRequest:   copyRequest,
		}
		url = gateway.Host() + "/replication-tables/copy"
	}

	err := gateway.FastDoRequest(url, method, request, &data)
	if err != nil {
		return nil, err
	}
	return &domain.DataCopyTable{}, nil
}

// AppendData 追加数据
func (gateway ApiByteLib) AppendData(param domain.ReqAppendData) (*domain.DataAppendData, error) {
	var url string
	method := "post"
	var data TableAppendResponse
	var request interface{}
	var appendRequest = NewTableAppendRequest(param)
	switch param.Table.TableType {
	case domain.MainTable.ToString():
		request = MasterTablesAppendRequest{
			MasterTableId:      intToString(param.Table.TableId),
			TableAppendRequest: appendRequest,
		}
		url = gateway.Host() + "/master-tables/append"
	case domain.SideTable.ToString():
		request = ReplicationTablesAppendRequest{
			ReplicationTableId: intToString(param.Table.TableId),
			TableAppendRequest: appendRequest,
		}
		url = gateway.Host() + "/replication-tables/append"
	}

	err := gateway.FastDoRequest(url, method, request, &data)
	if err != nil {
		return nil, err
	}
	return &domain.DataAppendData{
		AppendCount: data.AppendCount,
	}, nil
}

// DeleteTable 表删除 (主表、副表、分表)
func (gateway ApiByteLib) DeleteTable(param domain.ReqDeleteTable) (*domain.DataDeleteTable, error) {
	url := gateway.Host() + "/table/delete"
	method := "post"
	var data domain.DataDeleteTable
	err := gateway.FastDoRequest(url, method, param, &data)
	if err != nil {
		return nil, err
	}
	return &data, nil
}

// CancelFile 表删除 (主表、副表、分表)
func (gateway ApiByteLib) CancelFile(param domain.ReqCancelFile) (*domain.DataCancelFile, error) {
	url := gateway.Host() + "/table/cancel-file"
	method := "post"
	var data domain.DataCancelFile
	err := gateway.FastDoRequest(url, method, param, &data)
	if err != nil {
		return nil, err
	}
	return &data, nil
}

// 表拆分
func (gateway ApiByteLib) SplitTable(param domain.ReqSplitTable) (*domain.DataSplitTable, error) {
	var url string
	method := "post"
	var data TableAppendResponse
	var request interface{}
	var splitRequest = NewSplitTableRequest(param)
	switch param.FromTable.TableType {
	case domain.MainTable.ToString():
		request = SplitMasterTableRequest{
			MasterTableId:     intToString(param.FromTable.TableId),
			SplitTableRequest: splitRequest,
		}
		url = gateway.Host() + "/master-tables/split"
	case domain.SideTable.ToString():
		request = SplitReplicationTableRequest{
			ReplicationTableId: intToString(param.FromTable.TableId),
			SplitTableRequest:  splitRequest,
		}
		url = gateway.Host() + "/replication-tables/split"
	}

	err := gateway.FastDoRequest(url, method, request, &data)
	if err != nil {
		return nil, err
	}
	return &domain.DataSplitTable{}, nil
}

// 更新表结构(分表)

// EditTableData 编辑、添加、删除表数据(副表)
func (gateway ApiByteLib) EditTableData(param domain.ReqEditTableData) (*domain.DataEditTableData, error) {
	url := gateway.Host() + "/table/cancel-file"
	method := "post"
	var data domain.DataEditTableData
	err := gateway.FastDoRequest(url, method, param, &data)
	if err != nil {
		return nil, err
	}
	return &data, nil
}

// FieldOptionalValues 字段可选值
func (gateway ApiByteLib) FieldOptionalValues(param domain.ReqFieldOptionalValues) (*domain.DataFieldOptionalValues, error) {
	url := gateway.Host() + "/checkout-tables/filter-column-data"
	method := "post"
	var data CheckoutTablesFilterColumnDataResponse
	request := NewCheckoutTablesFilterColumnDataRequest(param)
	err := gateway.FastDoRequest(url, method, request, &data)
	if err != nil {
		return nil, err
	}
	var response = domain.DataFieldOptionalValues{
		//TableName: data.MasterTableName,
		Field:  param.Field,
		Values: data.ShowData,
		Total:  data.DataCount,
	}
	return &response, nil
}

func (gateway ApiByteLib) FormulasGenerate(param domain.ReqFormulasGenerate) (*domain.DataFormulasGenerate, error) {
	url := gateway.Host() + "/formulas/generate"
	method := "post"
	var data FormulasGenerateResponse
	request := NewFormulasGenerateRequest(param.QuerySet, param.Table, param.QueryComponents)
	err := gateway.FastDoRequest(url, method, request, &data)
	if err != nil {
		return nil, err
	}
	var response = domain.DataFormulasGenerate{
		FormulaName: data.FormulaName,
	}
	return &response, nil
}

func (gateway ApiByteLib) FormulasClear(param domain.ReqFormulasClear) (*domain.DataFormulasClear, error) {
	url := gateway.Host() + "/formulas/clear"
	method := "post"
	var data FormulasGenerateResponse
	request := FormulasClearRequest{
		FormulaId: intToString(param.QuerySetId),
	}
	err := gateway.FastDoRequest(url, method, request, &data)
	if err != nil {
		return nil, err
	}
	var response = domain.DataFormulasClear{}
	return &response, nil
}

func (gateway ApiByteLib) ShowBusinessDatabases(request ReqShowBusinessDatabases) (*DataShowBusinessDatabases, error) {
	url := gateway.Host() + "/databases/show-business-databases"
	method := "post"
	var data DataShowBusinessDatabases
	err := gateway.FastDoRequest(url, method, request, &data)
	if err != nil {
		return nil, err
	}
	return &data, nil
}

func (gateway ApiByteLib) ShowBusinessTables(request ReqShowBusinessTables) (*DataShowBusinessTables, error) {
	url := gateway.Host() + "/databases/show-tables"
	method := "post"
	var data DataShowBusinessTables
	err := gateway.FastDoRequest(url, method, request, &data)
	if err != nil {
		return nil, err
	}
	return &data, nil
}

func (gateway ApiByteLib) QueryBusinessTable(request ReqQueryBusinessTable) (*DataQueryBusinessTable, error) {
	url := gateway.Host() + "/databases/query-table"
	method := "post"
	var data DataQueryBusinessTable
	err := gateway.FastDoRequest(url, method, request, &data)
	if err != nil {
		return nil, err
	}
	return &data, nil
}