package models

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

var (
	HomeHeaderTabConfigKey              = "home_header_tab"
	MicroRoadShowListDataRatioConfigKey = "micro_roadshow_list_data_ratio"
	HomeNewListAudioNumKey              = "home_new_list_audio_num"
	MicroRoadshowDefaultImgKey          = "micro_roadshow_default_img"
)

type CygxConfig struct {
	ConfigId    int    `json:"-" orm:"column(config_id);pk"`
	ConfigCode  string `json:"-"`
	ConfigValue string
	Remark      string    `json:"-"`
	CreateTime  time.Time `json:"-"`
}

type KeyWord struct {
	KeyWord string `description:"关键词"`
}

func GetConfigByCode(configCode string) (item *CygxConfig, err error) {
	sql := `SELECT * FROM cygx_config WHERE config_code=? `
	err = orm.NewOrm().Raw(sql, configCode).QueryRow(&item)
	return
}

// 更改配置信息
func UpdateConfigByCode(configValue, countryCode string) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_config SET  config_value= ? WHERE config_code=? `
	_, err = o.Raw(sql, configValue, countryCode).Exec()
	return
}

type ConfigResp struct {
	Item    *CygxConfig
	List    []*KeyWord `description:"图表搜索推荐"`
	ListHot []*KeyWord `description:"热搜关键词"`
}

type ConfigImgListResp struct {
	List []*ConfigImgResp
}

type ConfigImgResp struct {
	Img    string `description:"图片"`
	Height string `description:"高度"`
}

type ConfigImgHbResp struct {
	HbImg     string `description:"海报"`
	ButtonImg string `description:"按钮图片"`
}

// 获取是否展示限免标签
func GetShowSustainable() (count int, err error) {
	o := orm.NewOrm()
	sqlCount := ` SELECT COUNT(1) FROM cygx_config WHERE config_code= 'is_show_sustainable' AND config_value = 1 `
	err = o.Raw(sqlCount).QueryRow(&count)
	return
}

// 通过报告精选来获取最新的推荐搜索词
func GetHotSearch() (permission string, err error) {
	sql := `SELECT
			GROUP_CONCAT( DISTINCT rl.subject_name ORDER BY rl.article_sun_id ASC SEPARATOR ',' ) AS subject_name 
		FROM
			cygx_report_selection_log AS rl 
		WHERE
			rl.article_id = (
			SELECT
				MAX( article_id ) 
			FROM
				cygx_report_selection 
		WHERE
			publish_status = 1)`
	o := orm.NewOrm()
	err = o.Raw(sql).QueryRow(&permission)
	return
}

// HomeHeaderTabList 首页头部导航列表
type HomeHeaderTabList struct {
	Home       []*HomeHeaderTab `description:"首页"`
	SearchPage []*HomeHeaderTab `description:"搜索页"`
}

// HomeHeaderTab 首页头部导航
type HomeHeaderTab struct {
	Id     int    `description:"导航ID"`
	Name   string `description:"导航名称"`
	IsShow bool   `description:"是否展示"`
}

// MicroRoadShowDefaultImgList 微路演行业默认背景图列表
type MicroRoadShowDefaultImgList struct {
	Audio []*MicroRoadShowDefaultImg `description:"音频"`
	Video []*MicroRoadShowDefaultImg `description:"视频"`
}

// MicroRoadShowDefaultImg 微路演行业默认背景图
type MicroRoadShowDefaultImg struct {
	ChartPermissionId   int    `description:"行业ID"`
	ChartPermissionName string `description:"行业名称"`
	ImgUrl              string `description:"背景图"`
	ShareImg            string `description:"分享图"`
}
