package models

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

type EdbClassify struct {
	ClassifyId      int       `orm:"column(classify_id);pk"`
	ClassifyType    uint8     `description:"分类类型，0：普通指标分类，1：预测指标分类"`
	ClassifyName    string    `description:"分类名称"`
	ParentId        int       `description:"父级id"`
	HasData         int       `description:"是否含有指标数据"`
	CreateTime      time.Time `description:"创建时间"`
	ModifyTime      time.Time `description:"修改时间"`
	SysUserId       int       `description:"创建人id"`
	SysUserRealName string    `description:"创建人姓名"`
	Level           int       `description:"层级"`
	UniqueCode      string    `description:"唯一编码"`
	Sort            int       `description:"排序字段，越小越靠前，默认值：10"`
}

func GetEdbClassifyById(classifyId int) (item *EdbClassify, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM edb_classify WHERE classify_id=? `
	err = o.Raw(sql, classifyId).QueryRow(&item)
	return
}

type EdbClassifySimplify struct {
	ClassifyId   int    `description:"分类id"`
	ClassifyName string `description:"分类名称"`
	ParentId     int
}

//递归获取所有目录
func GetClassifyAllByClassifyId(classifyId int) (resultList []*EdbClassifySimplify, err error) {
	o := orm.NewOrm()
	for {
		sql := ` SELECT * FROM edb_classify AS a WHERE a.classify_id=? order by sort asc,classify_id asc`
		item := new(EdbClassifySimplify)
		err = o.Raw(sql, classifyId).QueryRow(&item)
		if err != nil {
			break
		}
		if item.ClassifyId > 0 {
			resultList = append(resultList, item)
			classifyId = item.ParentId
		} else {
			break
		}
	}
	return resultList, err
}
