package classify

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

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:"修改时间"`
	Abstract          string    `description:"栏目简介"`
	Descript          string    `description:"分享描述"`
	ReportAuthor      string    `description:"栏目作者"`
	AuthorDescript    string    `description:"作者简介"`
	ColumnImgUrl      string    `description:"栏目配图"`
	HeadImgUrl        string    `description:"头部banner"`
	AvatarImgUrl      string    `description:"头像"`
	ReportImgUrl      string    `description:"报告配图"`
	HomeImgUrl        string    `description:"首页配图"`
	ClassifyLabel     string    `description:"分类标签"`
	ShowType          int       `description:"展示类型：1-列表 2-专栏"`
	HasTeleconference int       `description:"是否有电话会：0-否 1-是"`
	VipTitle          string    `description:"研究员头衔"`
	IsShow            int       `description:"是否在小程序显示：1-显示 0-隐藏"`
	YbFiccSort        int       `description:"小程序FICC页排序"`
	YbFiccIcon        string    `description:"小程序FICC页icon"`
	YbFiccPcIcon      string    `description:"小程序PC端FICC页背景图"`
	YbIconUrl         string    `description:"小程序已购页icon"`
	YbBgUrl           string    `description:"小程序已购详情背景图"`
	YbListImg         string    `description:"小程序研报列表封面图"`
}

// ClassifyList 分类列表
type ClassifyList struct {
	Id           int    `orm:"column(id);pk"`
	ClassifyName string `description:"分类名称"`
	Sort         int    `description:"排序" json:"-"`
	ParentId     int    `description:"父级分类id"`
	ShowType     int    `description:"展示类型：1-列表 2-专栏" json:"-"`
	Child        []*ClassifyList
}

// GetClassify 获取分类详情
func GetClassify(classifyId int) (item *Classify, err error) {
	sql := `SELECT * FROM classify WHERE id=? `
	o := orm.NewOrmUsingDB("rddp")
	err = o.Raw(sql, classifyId).QueryRow(&item)
	return
}

// GetClassifyList 获取一级分类列表
func GetClassifyList() (items []*ClassifyList, err error) {
	sql := `SELECT id,classify_name,parent_id,show_type,sort FROM classify WHERE parent_id=0 AND classify_name != '权益研报' AND is_show=1 ORDER BY sort ASC,create_time ASC`
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// GetClassifyListByParentId 根据父级id获取当前下级的分类列表
func GetClassifyListByParentId(parentId int) (items []*ClassifyList, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT id,classify_name,parent_id,show_type,sort FROM classify WHERE parent_id=? AND is_show=1 ORDER BY create_time ASC `
	_, err = o.Raw(sql, parentId).QueryRows(&items)
	return
}
