search_relation_graph.go 1.7 KB
package query

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

	"github.com/beego/beego/v2/core/validation"
)

type SearchTableRelationGraphQuery struct {
	// 表名称
	Name     string `cname:"表名称" json:"name"`
	ViewType string `cname:"视图类型 full:完整 main:主表关系" json:"viewType"`
	// 表类型 MainTable:主表 SideTable:副表 SubTable:分表  ExcelTable:Excel表 SubProcess:子过程 Schema:方案
	TableTypes []string `cname:"表类型 MainTable:主表 SideTable:副表 SubTable:分表  ExcelTable:Excel表" json:"tableTypes"`
	// 父级ID
	ParentId int `cname:"父级ID" json:"parentId"`
	// 模块 应用于模块 1:数控中心 2:拆解模块 4:计算模块
	Module int `json:"module"`
	// 父级ID
	ParentTableId          int  `cname:"父级ID" json:"parentTableId"`
	ReturnDetailStructInfo bool `cname:"返回具体的结构信息 默认不返回" json:"returnDetailStructInfo"`
	Context                *domain.Context
}

func (searchQuery *SearchTableRelationGraphQuery) Valid(validation *validation.Validation) {
	if searchQuery.ParentTableId > 0 && searchQuery.ParentId == 0 {
		searchQuery.ParentId = searchQuery.ParentTableId
	}
}

func (searchQuery *SearchTableRelationGraphQuery) ValidateQuery() error {
	valid := validation.Validation{}
	b, err := valid.Valid(searchQuery)
	if err != nil {
		return err
	}
	if !b {
		elem := reflect.TypeOf(searchQuery).Elem()
		for _, validErr := range valid.Errors {
			field, isExist := elem.FieldByName(validErr.Field)
			if isExist {
				return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
			} else {
				return fmt.Errorf(validErr.Message)
			}
		}
	}
	return nil
}