package models

import (
	"fmt"
	"hongze/hongze_task/utils"
	"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         string    `description:"创建时间"`
	ModifyTime         time.Time `description:"修改时间"`
	State              int       `description:"1:未发布,2:已发布"`
	PublishTime        time.Time `description:"发布时间"`
	Stage              int       `description:"期数"`
	MsgIsSend          int       `description:"消息是否已发送,0:否,1:是"`
	Content            string    `description:"内容"`
	VideoUrl           string    `description:"音频文件URL"`
	VideoName          string    `description:"音频文件名称"`
	VideoPlaySeconds   string    `description:"音频播放时长"`
	ContentSub         string    `description:"内容前两个章节"`
	HasChapter         int       `description:"是否有章节 0-否 1-是"`
	ChapterType        string    `description:"章节类型 day-晨报 week-周报"`
	OldReportId        int       `description:"research_report表ID(后续一两个版本过渡需要,之后可移除)"`
}

func GetReportById(reportId int) (item *Report, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM report WHERE id=?`
	err = o.Raw(sql, reportId).QueryRow(&item)
	return
}

func GetReport() (items []*Report, err error) {
	sql := `SELECT * FROM report ORDER BY id ASC `
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func ModifyReportContentSub(id int, contentSub string) (err error) {
	sql := `UPDATE report SET content_sub=? WHERE id=? `
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql, contentSub, id).Exec()
	return
}

func GetReportLimit() (items []*Report, err error) {
	sql := `SELECT * FROM report WHERE state=2 ORDER BY id DESC LIMIT 50 `
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func EditReportContent(reportId int, content, contentSub string) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` UPDATE report SET content=?,content_sub=?,modify_time=NOW() WHERE id=? `
	_, err = o.Raw(sql, content, contentSub, reportId).Exec()
	return
}

//删除报告日志记录-保留3个月
func DeleteReportSaveLog() {
	startDateTime := time.Now().AddDate(0, -3, 0).Format(utils.FormatDateTime)
	fmt.Println(startDateTime)
}

func EditReportContentHtml(reportId int, content string) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` UPDATE report SET content=?,modify_time=NOW() WHERE id=? `
	_, err = o.Raw(sql, content, reportId).Exec()
	return
}