package models

import (
	"eta/eta_mini_crm_ht/utils"
	"time"

	"github.com/beego/beego/v2/client/orm"
)

type Classify struct {
	Id            int       `orm:"column(id);pk"`
	ClassifyName  string    `description:"分类名称"`
	Sort          int       `json:"-"`
	ParentId      int       `description:"父级分类id"`
	CreateTime    time.Time `description:"创建时间"`
	ModifyTime    time.Time `description:"修改时间"`
	ClassifyLabel string    `description:"分类标签"`
	Enabled       int       `description:"是否可用，1可用，0禁用"`
}

type ClassifyView struct {
	Id            int             `orm:"column(id);pk"`
	ClassifyName  string          `description:"分类名称"`
	Sort          int             `json:"-"`
	ParentId      int             `description:"父级分类id"`
	CreateTime    time.Time       `description:"创建时间"`
	ModifyTime    time.Time       `description:"修改时间"`
	ClassifyLabel string          `description:"分类标签"`
	Enabled       int             `description:"是否可用，1可用，0禁用"`
	Level         int             `description:"分类层级"`
	HasChild      int             `description:"是否有子分类0：下面没有子分类，1：下面有子分类"`
	Child         []*ClassifyView `description:"子分类"`
}

func GetClassifyList() (items []*ClassifyView, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM classify WHERE enabled=1`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func GetClassifyById(classifyId int) (item *ClassifyView, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM classify WHERE enabled=1 AND id=?`
	err = o.Raw(sql, classifyId).QueryRow(&item)
	return
}

func GetClassifyListByIds(ids []string) (items []*ClassifyView, err error) {
	if len(ids) == 0 {
		return
	}
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM classify WHERE enabled=1 AND id IN (` + utils.GetOrmReplaceHolder(len(ids)) + `)`
	_, err = o.Raw(sql, ids).QueryRows(&items)
	return
}
