bytecore.go 5.9 KB
package domain

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

type (
	ReqLoadDataTable struct {
		FileId   int    `json:"file_id"`
		FileType string `json:"file_type"`
		FileName string `json:"file_name"`
		Url      string `json:"url"`
		Ext      string `json:"ext"`
		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 []QueryParameter       `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"`
	}

	QueryParameter struct {
		ColumnName       string   `json:"columnName"`
		ColumnContents   []string `json:"columnContents"`
		IsContainContent bool     `json:"isContainContent"`
	}
	//Field struct {
	//	// 索引序号
	//	Index int `json:"index"`
	//	// 名称
	//	Name string `json:"name"`
	//	// 对应数据库类型
	//	Type string `json:"sqlType"`
	//}

	InValidCell struct {
		Col   string `json:"col"`
		Index int    `json:"index"`
		Err   string `json:"err"`
	}
)

func NewInValidCells(fields Fields, mapData []map[string]string) []InValidCell {
	inValidCells := make([]InValidCell, 0)
	mapField := fields.ToMap()
	for index, m := range mapData {
		for k, v := range m {
			if field, ok := mapField[k]; ok && len(v) > 0 {
				_, err := ValueToType(v, field.SQLType)
				if err != nil {
					inValidCells = append(inValidCells, InValidCell{
						Col:   field.Name,
						Index: index,
						Err:   err.Error(),
					})
				}
			}
		}
	}
	return inValidCells
}

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

func (table DataLoadDataTable) Filter(where 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
		Table  *Table
	}

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

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

	DataGenerateTable struct {
		TableName string
	}
)

type (
	ReqCopyTable struct {
		MainTable   *Table
		Table       *Table
		CopyToTable *Table

		// 数据列
		//DataFields []*Field `json:"dataFields"`
		// 手动添加的列
		//ManualFields []*Field `json:"manualFields"`
	}

	DataCopyTable struct {
	}
)

type (
	ReqAppendData struct {
		FileId     int
		FileUrl    string
		Table      *Table
		From       []*Field
		To         []*Field
		ExcelTable *Table
	}

	DataAppendData struct {
		AppendCount int `json:"appendCount"`
	}
)

type (
	ReqDeleteTable struct {
	}

	DataDeleteTable struct {
	}
)

type (
	ReqSplitTable struct {
		FromTable  *Table
		ToSubTable *Table
	}

	DataSplitTable struct {
	}
)

type (
	ReqFieldOptionalValues struct {
		FileId int
		Field  *Field
		Where  Where
		Match  string
	}

	DataFieldOptionalValues struct {
		Field  *Field
		Values []string
		Total  int
	}
)

type (
	ReqCancelFile struct {
	}

	DataCancelFile struct {
	}
)

type (
	ReqEditTableData struct {
	}

	DataEditTableData struct {
	}
)

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

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

//func ToFieldData(fields []*Field, data [][]string, byteName bool) []map[string]string {
//	var result = make([]map[string]string, 0)
//	var key string
//	for _, d := range data {
//		var item = make(map[string]string)
//		for j, f := range fields {
//			key = f.Name
//			if byteName {
//				key = f.Name
//			}
//			if len(d) >= j {
//				item[key] = d[j]
//			} else {
//				item[key] = ""
//			}
//		}
//		result = append(result, item)
//	}
//	return result
//}