package data_manage

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strings"
)

// EdbSource 指标来源表
type EdbSource struct {
	EdbSourceId int    `orm:"column(edb_source_id);pk"`
	SourceName  string `description:"指标来源名称"`
	TableName   string `description:"数据表名"`
	IsBase      int    `description:"是否为基础指标: 0-否; 1-是"`
}

// GetEdbSourceItemsByCondition 获取指标来源
func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
	o := orm.NewOrmUsingDB("data")
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY edb_source_id ASC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM edb_source WHERE 1=1 %s %s`, fields, condition, order)
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// EdbSourceChild 指标来源表
type EdbSourceChild struct {
	EdbSourceId int    `orm:"column(edb_source_id);pk"`
	SourceName  string `description:"指标来源名称"`
	TableName   string `description:"数据表名"`
	IsBase      int    `description:"是否为基础指标: 2-否; 1-是"`
	Child       []EdbSourceChild
}