package models

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

type ReportChapterTicker struct {
	Id              int       `orm:"column(id);pk" description:"主键ID"`
	ReportChapterId int       `description:"报告章节ID"`
	Sort            int       `description:"排序"`
	Ticker          string    `description:"ticker"`
	CreateTime      time.Time `description:"创建时间"`
	UpdateTime      time.Time `description:"更新时间"`
}

// 新增章节ticker
func InsertChapterTicker(tickerInfo *ReportChapterTicker) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("rddp")
	lastId, err = o.Insert(tickerInfo)
	return
}

// GetTickerListByReportChapterId 根据章节ID获取ticker列表
func GetTickerListByReportChapterId(reportChapterId int) (list []*ReportChapterTicker, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT * FROM report_chapter_ticker WHERE report_chapter_id = ? ORDER BY sort ASC `
	_, err = o.Raw(sql, reportChapterId).QueryRows(&list)

	return
}

// ClearReportChapterTicker 清空章节ticker
func ClearReportChapterTicker(reportChapterId int) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` DELETE FROM report_chapter_ticker WHERE report_chapter_id = ? `
	_, err = o.Raw(sql, reportChapterId).Exec()

	return
}

// DailyBaseColumn 基础列
type DailyBaseColumn struct {
	BaseColumnId          int       `orm:"column(base_column_id)" description:"主键ID"`
	TableName             string    `description:"表名"`
	BaseColumnName        string    `description:"列名称"`
	BaseColumnChapterType string    `description:"列类型"`
	BaseColumnTicker      string    `description:"指标"`
	Enabled               int       `description:"状态"`
	CreatedTime           time.Time `description:"创建时间"`
	LastUpdatedTime       time.Time `description:"更新时间"`
	Freq                  string    `description:"频率 D-日度 W-周度 M-月度"`
	Catalog               string    `description:"分类名称"`
	ReportChapterTypeId   int       `description:"分类ID"`
	Selected              int       `description:"选中状态 0-未选中 1-已选中"`
}

// GetDailyBaseColumnList 获取基础列列表
func GetDailyBaseColumnList(keyword string, typeId int) (list []*DailyBaseColumn, err error) {
	o := orm.NewOrmUsingDB("weekly")
	sql := ` SELECT * FROM daily_base_column WHERE 1 = 1 `
	pars := make([]interface{}, 0)
	if keyword != "" {
		keyword = "%" + keyword + "%"
		pars = append(pars, keyword)
		sql += ` AND base_column_name like ? `
	}
	pars = append(pars, typeId)
	sql += ` AND report_chapter_type_id = ? `
	_, err = o.Raw(sql, pars).QueryRows(&list)

	return
}
