bytelib.go 4.1 KB
package bytecore

import "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"

type ByteLibService interface {
	LoadDataTable(param ReqLoadDataTable) (*DataLoadDataTable, error)
	EditTable(param ReqEditDataTable) (*DataEditDataTable, error)
	SaveTable(param ReqSaveTable) (*DataSaveTable, error)
	GenerateTable(ctx *domain.Context, param ReqGenerateTable) (*DataGenerateTable, error)
	CopyTable(param ReqCopyTable) (*DataCopyTable, error)
	AppendData(param ReqAppendData) (*DataAppendData, error)
	DeleteTable(param ReqDeleteTable) (*DataDeleteTable, error)
	CancelFile(param ReqCancelFile) (*DataCancelFile, error)
	EditTableData(param ReqEditTableData) (*DataEditTableData, error)
}

type (
	ReqLoadDataTable struct {
		FileId   int    `json:"file_id"`
		FileName string `json:"file_name"`
		Url      string `json:"url"`
		Ext      string `json:"ext"`
		domain.Where
		OriginalTableId     string         `json:"originalTableId"`
		IsFromOriginalTable bool           `json:"isFromOriginalTable"`
		TableFileUrl        string         `json:"tableFileUrl"`
		ColumnSchemas       []ColumnSchema `json:"columnSchemas"`
		//PageNumber          int                    `json:"pageNumber"`
		//PageSize            int                    `json:"pageSize"`
		QueryParameters map[string]interface{} `json:"queryParameters"`
		SortParameters  map[string]interface{} `json:"sortParameters"`
	}

	DataLoadDataTable struct {
		FileId       int           `json:"objectId"`
		ObjectType   string        `json:"objectType"`
		TableType    string        `json:"tableType"`
		Fields       []*Field      `json:"fields"`
		Data         [][]string    `json:"data"`
		Total        int           `json:"total"`
		PageNumber   int           `json:"pageNumber"`
		InValidCells []InValidCell `json:"inValidCells"`
	}

	Field struct {
		// 索引序号
		Index int `json:"index"`
		// 名称
		Name string `json:"name"`
		// 对应数据库类型
		Type string `json:"sqlType"`
	}

	InValidCell struct {
		X     int    `json:"x"`
		Y     int    `json:"y"`
		Error string `json:"error"`
	}
)

// https://github.com/go-gota/gota 类似pandas的数据处理包
// https://github.com/gonum/gonum

func (table DataLoadDataTable) Filter(where domain.Where) *DataLoadDataTable {
	begin := (where.PageNumber - 1) * where.PageSize
	if begin < 0 {
		begin = 0
	}
	end := begin + where.PageSize
	data := make([][]string, 0)
	if begin < table.Total {
		if end < table.Total {
			data = table.Data[begin:end]
		} else {
			data = table.Data[begin:]
		}
	}
	return &DataLoadDataTable{
		FileId:       table.FileId,
		Fields:       table.Fields,
		Data:         data,
		Total:        table.Total,
		PageNumber:   where.PageNumber,
		InValidCells: make([]InValidCell, 0),
	}
}

type (
	ReqEditDataTable struct {
		FileId        int                    `json:"fileId"`
		PageNumber    int                    `json:"pageNumber"`
		PageSize      int                    `json:"pageSize"`
		Fields        []*Field               `json:"fields"`
		ProcessFields []*Field               `json:"processFields"`
		Action        string                 `json:"action"`
		Params        map[string]interface{} `json:"params"`
	}

	DataEditDataTable struct {
		DataLoadDataTable
	}
)

type (
	ReqSaveTable struct {
		FileId int
	}

	DataSaveTable struct {
		Url string `json:"url"`
	}
)

type (
	ReqGenerateTable struct {
		FileId  int
		FileUrl string
		Table   *domain.Table
	}

	DataGenerateTable struct {
		TableName string
	}
)

type (
	ReqCopyTable struct {
	}

	DataCopyTable struct {
	}
)

type (
	ReqAppendData struct {
	}

	DataAppendData struct {
	}
)

type (
	ReqDeleteTable struct {
	}

	DataDeleteTable struct {
	}
)

type (
	ReqCancelFile struct {
	}

	DataCancelFile struct {
	}
)

type (
	ReqEditTableData struct {
	}

	DataEditTableData struct {
	}
)

type ColumnSchema struct {
	ColumnName string `json:"columnName"`
	ColumnType string `json:"columnType"`
}

func ToFields(fields []*domain.Field) []*Field {
	result := make([]*Field, 0)
	for _, f := range fields {
		result = append(result, &Field{
			Index: f.Index,
			Name:  f.Name,
			Type:  f.SQLType,
		})
	}
	return result
}