package models

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

type ReportChapterType struct {
	ReportChapterTypeId    int       `orm:"column(report_chapter_type_id);pk" description:"报告章节类型id"`
	ReportChapterTypeKey   string    `description:"章节key"`
	ReportChapterTypeThumb string    `description:"H5展示的图片"`
	BannerUrl              string    `description:"banner显示图片"`
	ReportChapterTypeName  string    `description:"报告章节类型名称"`
	Sort                   int       `description:"排序字段"`
	Enabled                int       `description:"启禁用状态"`
	CreatedTime            time.Time `description:"创建时间"`
	LastUpdatedTime        time.Time `description:"更新时间"`
	ResearchType           string    `description:"研报类型"`
	SelectedImage          string    `description:"选中时的图片"`
	UnselectedImage        string    `description:"没选中时的图片"`
	PcSelectedImage        string    `description:"PC-选中的图片"`
	PcUnselectedImage      string    `description:"PC-未选中的图片"`
	EditImgUrl             string    `description:"管理后台编辑时选用的图"`
	TickerTitle            string    `description:"指标列的标题"`
	IsShow                 int       `description:"是否显示(研报小程序端根据此字段判断)"`
	PauseStartTime         string    `description:"暂停开始日期"`
	PauseEndTime           string    `description:"暂停结束日期"`
	IsSet                  int       `description:"是否设置:0为设置,1已设置"`
	YbIconUrl              string    `description:"研报小程序icon"`
	YbBottomIcon           string    `description:"研报小程序详情底部icon"`
}

// GetReportChapterTypeById 获取章节类型
func GetReportChapterTypeById(reportChapterTypeId int) (item *ReportChapterType, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM report_chapter_type WHERE report_chapter_type_id = ? `
	err = o.Raw(sql, reportChapterTypeId).QueryRow(&item)
	return
}

// GetReportChapterTypeList 获取章节类型列表
func GetReportChapterTypeList() (list []*ReportChapterType, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM report_chapter_type WHERE enabled = 1 `
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetReportChapterTypeListByResearchType 通过报告类型获取章节类型列表
func GetReportChapterTypeListByResearchType(researchType string) (list []*ReportChapterType, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM report_chapter_type WHERE research_type = ? AND enabled = 1`
	_, err = o.Raw(sql, researchType).QueryRows(&list)
	return
}

// GetAllReportChapterTypeListByResearchType 通过报告类型获取章节类型列表
func GetAllReportChapterTypeListByResearchType(researchType string) (list []*ReportChapterType, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM report_chapter_type WHERE research_type = ?`
	_, err = o.Raw(sql, researchType).QueryRows(&list)
	return
}

// GetEnableReportChapterTypeList 获取未暂停的章节类型列表
func GetEnableReportChapterTypeList(researchType string) (list []*ReportChapterType, err error) {
	o := orm.NewOrm()
	sql := ` SELECT
				*
			FROM
				report_chapter_type
			WHERE
				enabled = 1
			AND research_type = ?
			AND (
				(
					ISNULL(pause_start_time) AND ISNULL(pause_end_time)
				)
				OR (
					NOW() NOT BETWEEN pause_start_time AND pause_end_time
				)
			) `
	_, err = o.Raw(sql, researchType).QueryRows(&list)
	return
}

// StopUpdateReportChapterTypeResp 停止更新的报告分类列表
type StopUpdateReportChapterTypeResp struct {
	StopDay     []*ReportChapterType `description:"暂时停更晨报"`
	StopWeek    []*ReportChapterType `description:"暂时停更周报"`
	DisableDay  []*ReportChapterType `description:"永久停更晨报"`
	DisableWeek []*ReportChapterType `description:"永久停更周报"`
}

// GetStopUpdateReportChapterTypeListByResearchType 获取暂停更新章节类型列表
func GetStopUpdateReportChapterTypeListByResearchType() (list []*ReportChapterType, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM report_chapter_type WHERE is_set = 1 AND pause_end_time >= ? AND enabled = 1 `
	_, err = o.Raw(sql, time.Now().Format(utils.FormatDate)).QueryRows(&list)
	return
}

// GetDisableUpdateReportChapterTypeListByResearchType 获取停止更新的章节类型列表
func GetDisableUpdateReportChapterTypeListByResearchType() (list []*ReportChapterType, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM report_chapter_type WHERE enabled = 0`
	_, err = o.Raw(sql).QueryRows(&list)
	return
}
