package models

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

type CygxZhouqiArticleMap struct {
	CategoryId        int    `description:"产业Id"`
	ParentId          int    `description:"父级ID"`
	MatchTypeName     string `description:"行业名称"`
	SeriesName        string `description:"系列名称"`
	ArticleUpdateTime string `description:"对应文章的更新时间"`
}

type CygxZhouqiArticleDetail struct {
	FiccReportId string `description:"Ficc研报单一篇报告，多个地方展示配置项"`
}

// 获取周期下面的一级分类
func GetCygxZhouqiArticleMapFirst() (items []*CygxZhouqiArticleMap, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			category_id,
			parent_id,
			match_type_name,
			series_name,
			article_update_time 
		FROM
			cygx_zhouqi_article_map 
		WHERE
			hidden = 0 
			AND parent_id = 0 
		ORDER BY
			sort DESC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 根据父级分类ID获取下面的子分类
func GetCygxZhouqiArticleMapByParentId(parentId int) (items []*CygxZhouqiArticleMap, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			category_id,
			match_type_name,
			series_name,
			article_update_time 
		FROM
			cygx_zhouqi_article_map 
		WHERE
			parent_id = ?  `
	_, err = o.Raw(sql, parentId).QueryRows(&items)
	return
}

// 根据名称获取详情
func GetCygxZhouqiArticleMapByMatchTypeName(matchTypeName string) (item *CygxZhouqiArticleMap, err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `SELECT a.* FROM cygx_zhouqi_article_map AS a WHERE a.match_type_name=? AND  parent_id > 0  `
	err = o.Raw(sql, matchTypeName).QueryRow(&item)
	return
}

// 更新对应分类所关联的最新文章的时间
func UpdateCygxZhouqiArticleMapTime(articleUpdateTime string, categoryId int) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_zhouqi_article_map SET hidden=0,article_update_time=? WHERE category_id = ? `
	_, err = o.Raw(sql, articleUpdateTime, categoryId).Exec()
	return
}

// 通过纪要ID获取详情
func GetCygxZhouqiArticleMapInfoById(categoryId int) (item *CygxZhouqiArticleDetail, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_zhouqi_article_map  WHERE category_id=?  `
	err = o.Raw(sql, categoryId).QueryRow(&item)
	return
}
