table_dto.go 1.3 KB
package dto

import (
	"strings"

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

type TableDto struct {
	// 表Id
	TableId int `json:"tableId"`
	// 表类型 MainTable:主表 SideTable:副表 SubTable:分表  ExcelTable:Excel表
	TableType string `json:"tableType"`
	// 名称
	Name string `json:"name"`
	// 父级ID
	ParentId int `json:"parentId"`
}

func (d *TableDto) Load(m *domain.Table) {
	d.TableId = m.TableId
	d.TableType = m.TableType
	d.Name = m.Name
	d.ParentId = m.ParentId
}

func SuitTableByView(tables []*TableDto, view string, name string) []*TableDto {
	var mainTables = make(map[int]*TableDto)
	var res = make([]*TableDto, 0)
	if view == "main" {
		for i := range tables {
			t := tables[i]
			if t.ParentId == 0 && t.TableType == domain.MainTable.ToString() {
				mainTables[t.TableId] = t
				res = append(res, t)
			}
		}
		for i := range tables {
			t := tables[i]
			if t.TableType == domain.SubTable.ToString() {
				if _, ok := mainTables[t.ParentId]; ok {
					res = append(res, t)
				}
			}
		}
	} else {
		res = tables
	}
	var matchName = make([]*TableDto, 0)
	if len(name) > 0 {
		for i := range res {
			t := res[i]
			if strings.Contains(t.Name, name) {
				matchName = append(matchName, t)
			}
		}
		res = matchName
	}
	return res
}