package report_chapter

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

// ReportChapter 报告章节
type ReportChapter struct {
	ReportChapterId   int       `orm:"column(report_chapter_id);pk" description:"报告章节ID"`
	ReportId          int       `description:"报告ID"`
	ReportType        string    `description:"报告类型 day-晨报 week-周报"`
	ClassifyIdFirst   int       `description:"一级分类id"`
	ClassifyNameFirst string    `description:"一级分类名称"`
	TypeId            int       `description:"品种ID"`
	TypeName          string    `description:"品种名称"`
	Title             string    `description:"标题"`
	Abstract          string    `description:"摘要"`
	AddType           int       `description:"新增方式：1:新增报告，2:继承报告"`
	Author            string    `description:"作者"`
	Content           string    `description:"内容"`
	ContentSub        string    `description:"内容前两个章节"`
	Stage             int       `description:"期数"`
	Trend             string    `description:"趋势观点"`
	Sort              int       `description:"排序: 数值越小越靠前"`
	IsEdit            int       `description:"是否已编辑 0-待编辑 1-已编辑"`
	PublishState      int       `description:"发布状态 1-待发布，2-已发布"`
	PublishTime       time.Time `description:"发布时间"`
	VideoUrl          string    `description:"音频文件URL"`
	VideoName         string    `description:"音频文件名称"`
	VideoPlaySeconds  string    `description:"音频播放时长"`
	VideoSize         string    `description:"音频文件大小，单位M"`
	CreateTime        string    `description:"创建时间"`
	ModifyTime        time.Time `description:"修改时间"`
}

// GetListByReportId 根据报告ID获取章节列表
func GetListByReportId(reportId int, classifyNameFirst string) (list []*ReportChapter, err error) {
	var where string
	if classifyNameFirst == "周报" {
		where = "report_id = ? AND is_edit = 1 AND publish_state = 2"
	} else {
		where = "report_id = ? AND publish_state = 2"
	}

	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM report_chapter WHERE   ` + where
	sql += ` order by sort asc,report_chapter_id asc  `
	_, err = o.Raw(sql, reportId).QueryRows(&list)
	return
}

// GetContentById 根据ID获取章节详情
func GetContentById(id int, typeIds []int) (info *ReportChapter, err error) {
	o := orm.NewOrmUsingDB("rddp")
	lenNum := len(typeIds)
	if lenNum <= 0 {
		return
	}
	sql := `SELECT * FROM report_chapter WHERE  report_chapter_id = ? and type_id in (` + utils.GetOrmInReplace(lenNum) + `) AND publish_state = 2 `
	err = o.Raw(sql, id, typeIds).QueryRow(&info)
	return
}

// GetListByReportIdTypeIds 根据报告ID、章节类型ID获取章节列表
func GetListByReportIdTypeIds(reportId int, typeIds []int, classifyNameFirst string) (list []*ReportChapter, err error) {
	lenNum := len(typeIds)
	if lenNum <= 0 {
		return
	}
	var where string
	if classifyNameFirst == "周报" {
		where = ` report_id = ? AND type_id in (` + utils.GetOrmInReplace(lenNum) + `) AND is_edit = 1 AND publish_state = 2 `
	} else {
		where = ` report_id = ? AND type_id in (` + utils.GetOrmInReplace(lenNum) + `) AND publish_state = 2 `
	}

	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM report_chapter WHERE ` + where
	sql += ` order by sort asc,report_chapter_id asc  `
	_, err = o.Raw(sql, reportId, typeIds).QueryRows(&list)
	return
}

// GetTypeIdById 根据ID获取章节类型
func GetTypeIdById(id int) (info *ReportChapter, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM report_chapter WHERE  report_chapter_id = ? AND publish_state = 2 `
	err = o.Raw(sql, id).QueryRow(&info)
	return
}
