package report

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

type Report struct {
	Id                 int       `orm:"column(id)" description:"报告Id"`
	AddType            int       `description:"新增方式：1:新增报告，2:继承报告"`
	ClassifyIdFirst    int       `description:"一级分类id"`
	ClassifyNameFirst  string    `description:"一级分类名称"`
	ClassifyIdSecond   int       `description:"二级分类id"`
	ClassifyNameSecond string    `description:"二级分类名称"`
	Title              string    `description:"标题"`
	Abstract           string    `description:"摘要"`
	Author             string    `description:"作者"`
	Frequency          string    `description:"频度"`
	CreateTime         time.Time `description:"创建时间"`
	ModifyTime         time.Time `description:"修改时间"`
	State              int       `description:"1:未发布，2:已发布"`
	PublishTime        time.Time `description:"发布时间"`
	Stage              int       `description:"期数"`
	MsgIsSend          int       `description:"消息是否已发送，0:否，1:是"`
	ThsMsgIsSend       int       `description:"客户群消息是否已发送，0:否，1:是"`
	Content            string    `description:"内容"`
	VideoUrl           string    `description:"音频文件URL"`
	VideoName          string    `description:"音频文件名称"`
	VideoPlaySeconds   string    `description:"音频播放时长"`
	VideoSize          string    `description:"音频文件大小，单位M"`
	ContentSub         string    `description:"内容前两个章节"`
	ReportVersion      int       `description:"1:旧版,2:新版"`
	HasChapter         int       `description:"是否有章节 0-否 1-是"`
	ChapterType        string    `description:"章节类型 day-晨报 week-周报"`
	OldReportId        int       `description:"research_report表ID(后续一两个版本过渡需要,之后可移除)"`
}

// ReportList 报告列表
type ReportList struct {
	Id         int       `description:"报告Id"`
	Title      string    `description:"标题"`
	Author     string    `description:"作者"`
	Stage      int       `description:"期数" json:"stage"`
	CreateTime time.Time `description:"创建时间" json:"create_time"`
	HttpUrl    string    `description:"跳转地址" json:"http_url"`
}

// GetReportList 获取报告列表
func GetReportList(condition string, pars []interface{}, startSize, pageSize int) (total int, items []*ReportList, err error) {
	o := orm.NewOrmUsingDB("rddp")
	// 汇总数据
	totalSql := `SELECT count(1) total FROM report WHERE 1=1`
	//报告列表
	sql := `SELECT id,title,author,stage,create_time FROM report WHERE 1=1  `
	if condition != "" {
		totalSql += condition
		sql += condition
	}

	err = o.Raw(totalSql, pars).QueryRow(&total)
	if err != nil {
		return
	}

	sql += `ORDER BY modify_time DESC LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// GetById 根据报告id获取报告详情
func GetById(id int) (item *Report, err error) {
	o := orm.NewOrmUsingDB("rddp")
	//报告列表
	sql := `SELECT * FROM report WHERE id = ?  `
	err = o.Raw(sql, id).QueryRow(&item)
	return
}
