package sandbox

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

type SandboxClassify struct {
	SandboxClassifyId   int       `orm:"column(sandbox_classify_id);pk"`
	SandboxClassifyName 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:"层级"`
	Sort              int       `description:"排序字段，越小越靠前，默认值：10"`
}

func AddSandboxClassify(item *SandboxClassify) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("data")
	lastId, err = o.Insert(item)
	return
}

// GetSandboxClassifyByParentId
func GetSandboxClassifyByParentId(parentId int) (items []*SandboxClassifyItems, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM sandbox_classify WHERE parent_id=? order by sort asc,sandbox_classify_id asc`
	_, err = o.Raw(sql, parentId).QueryRows(&items)
	return
}

// GetSandboxClassifyAll
func GetSandboxClassifyAll() (items []*SandboxClassifyItems, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM sandbox_classify WHERE parent_id<>0 order by sort asc,sandbox_classify_id asc`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type SandboxClassifyItems struct {
	SandboxClassifyId   int       `orm:"column(sandbox_classify_id);pk"`
	SandboxClassifyName 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:"层级"`
	Sort                int       `description:"排序字段，越小越靠前，默认值：10"`
	SandboxId           int       `description:"沙盘id"`
	Children            []*SandboxClassifyItems
}

type SandboxClassifyListResp struct {
	AllNodes []*SandboxClassifyItems
}

type AddSandboxClassifyReq struct {
	SandboxClassifyName string `description:"分类名称"`
	ParentId            int    `description:"父级id,第一级传0"`
	Level               int    `description:"层级,第一级传0，其余传上一级的层级"`
}

func GetSandboxClassifyCount(sandboxClassifyName string, parentId int) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT COUNT(1) AS count FROM sandbox_classify WHERE parent_id=? AND sandbox_classify_name=? `
	err = o.Raw(sql, parentId, sandboxClassifyName).QueryRow(&count)
	return
}

// GetSandboxClassifyMaxSort 获取沙盘分类下最大的排序数
func GetSandboxClassifyMaxSort(parentId int) (sort int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT Max(sort) AS sort FROM sandbox_classify WHERE parent_id=? `
	err = o.Raw(sql, parentId).QueryRow(&sort)
	return
}

type EditSandboxClassifyReq struct {
	SandboxClassifyName string `description:"分类名称"`
	SandboxClassifyId   int    `description:"分类id"`
}

func GetSandboxClassifyById(classifyId int) (item *SandboxClassify, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM sandbox_classify WHERE sandbox_classify_id=? `
	err = o.Raw(sql, classifyId).QueryRow(&item)
	return
}