api_byte_lib.go 4.5 KB
package bytelib

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

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

func NewApiByteLib(host string) *ApiByteLib {
	gt := api.NewBaseServiceGateway(host)
	gt.ConnectTimeout = 10 * time.Second
	gt.ReadWriteTimeout = 10 * time.Second
	gt.Interceptor = func(msg string) {
		//log.Logger.Info(msg)
		logs.Debug(msg)
	}
	return &ApiByteLib{
		BaseServiceGateway: gt,
	}
}

// 加载数据

func (gateway ApiByteLib) LoadDataTable(param bytecore.ReqLoadDataTable) (*bytecore.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 bytecore.ReqEditDataTable) (*bytecore.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 &bytecore.DataEditDataTable{
		DataLoadDataTable: *response,
	}, nil
}

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

// GenerateTable 生成主表
func (gateway ApiByteLib) GenerateTable(param bytecore.ReqGenerateTable) (*bytecore.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 = bytecore.DataGenerateTable{
		TableName: data.MasterTableName,
	}
	return &response, nil
}

// CopyTable 表复制
func (gateway ApiByteLib) CopyTable(param bytecore.ReqCopyTable) (*bytecore.DataCopyTable, error) {
	url := gateway.Host() + "/table/copy"
	method := "post"
	var data bytecore.DataCopyTable
	err := gateway.FastDoRequest(url, method, param, &data)
	if err != nil {
		return nil, err
	}
	return &data, nil
}

// AppendData 追加数据
func (gateway ApiByteLib) AppendData(param bytecore.ReqAppendData) (*bytecore.DataAppendData, error) {
	url := gateway.Host() + "/table/append"
	method := "post"
	var data bytecore.DataAppendData
	err := gateway.FastDoRequest(url, method, param, &data)
	if err != nil {
		return nil, err
	}
	return &data, nil
}

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

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

// 表拆分

// 更新表结构(分表)

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