package pc

import (
	"fmt"
	"hongze/hongze_yb/global"
	"time"
)

type Report struct {
	Id                 int       `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        string    `description:"发布时间"`
	Stage              int       `description:"期数"`
	MsgIsSend          int       `description:"消息是否已发送，0:否，1:是"`
	Content            string    `description:"内容"`
	VideoUrl           string    `description:"音频文件URL"`
	VideoName          string    `description:"音频文件名称"`
	VideoPlaySeconds   string    `description:"音频播放时长"`
	VideoSize          string    `description:"音频文件大小，单位M"`
	ContentSub         string    `description:"内容前两个章节"`
	ClassifyName       string    `description:"分类名称"`
	HasPermission      int       `description:"报告权限:0:无权限,1:有权限"`
	TitleType          string    `description:"标题类型，FICC或者权益"`
	RaiReportId        int       `description:"权益报告ID"`
}

func GetLatestReportList() (items []*Report, err error) {
	sql := `SELECT * FROM report WHERE state=2 ORDER BY publish_time DESC LIMIT 3 `
	err = global.MYSQL["rddp"].Raw(sql).Scan(&items).Error
	return
}

func GetRecommendList(reportId, reportType, secondId int) (items []*Report, err error) {
	sql := `SELECT * FROM report WHERE state=2 AND id<> %v AND classify_id_second=%v `
	sql = fmt.Sprintf(sql, reportId, secondId)
	if reportType == 1 {
		sql += ` AND classify_name_first='权益研报' `
	} else {
		sql += ` AND classify_name_first<>'权益研报' `
	}
	sql += ` ORDER BY publish_time DESC LIMIT 3`
	err = global.MYSQL["rddp"].Raw(sql).Scan(&items).Error
	return
}

type LatestReport struct {
	Id                 int       `description:"报告Id" json:"_"`
	ClassifyNameFirst  string    `description:"一级分类名称" json:"classify_name_first"`
	ClassifyNameSecond string    `description:"二级分类名称" json:"classify_name_second"`
	ClassifyIdSecond   int       `description:"二级分类id" json:"classify_id_second"`
	Title              string    `description:"标题" json:"title"`
	State              int       `description:"1:未发布，2:已发布" json:"state"`
	PublishTime        time.Time `description:"发布时间" json:"publish_time"`
	Stage              int       `description:"期数" json:"stage"`
	ReportId           int
	RaiReportId        int
}

func GetLatestStage(classifyNames []string) (items []*LatestReport, err error) {
	sql := `SELECT * FROM(SELECT DISTINCT
	classify_name_first,
	id,
	title,
	state,
	publish_time,
	stage 
FROM
	report 
WHERE
	classify_name_first IN  (?) 
AND state IN (2, 6) 
ORDER BY
	publish_time DESC) t GROUP BY t.classify_name_first`
	err = global.MYSQL["rddp"].Raw(sql, classifyNames).Scan(&items).Error
	return
}

type RecommendResp struct {
	ReportId            int
	ReportChapterID     int
	Title               string
	Stage               int
	ClassifyNameFirst   string
	ClassifySecondFirst string
	RaiReportId         int
}

type DetailBannerResp struct {
	ReportId           int
	Stage              int
	VipTitle           string
	Author             string
	ReportAuthor       string
	ImgUrl             string
	ClassifyNameFirst  string
	ClassifyIdFirst    int
	ClassifyNameSecond string
	ClassifyIdSecond   int
	Type               string
	ShowType           uint8
}

type LatestReportBanner struct {
	ReportId           int `description:"报告Id" json:"reportId"`
	ClassifyIdFirst    int
	ClassifyIdSecond   int
	ClassifyNameFirst  string    `description:"一级分类名称" json:"classify_name_first"`
	ClassifyNameSecond string    `description:"二级分类名称" json:"classify_name_second"`
	Title              string    `description:"标题" json:"title"`
	State              int       `description:"1:未发布，2:已发布" json:"state"`
	PublishTime        time.Time `description:"发布时间" json:"publish_time"`
	Stage              int       `description:"期数" json:"stage"`
	VipTitle           string
	Author             string
	ReportAuthor       string
}

// GetRecommendListV2
// @Description: 获取报告的推荐列表
// @author: Roc
// @datetime 2024-06-24 14:25:01
// @param reportId int
// @param reportType int
// @param firstId int
// @param secondId int
// @param thirdId int
// @return items []*Report
// @return err error
func GetRecommendListV2(reportId, reportType, firstId, secondId, thirdId int) (items []*Report, err error) {
	sql := `SELECT id,title,classify_name_first,classify_name_second, stage, rai_report_id FROM report WHERE state=2 AND id<> ? AND classify_id_first=? AND classify_id_second=? AND classify_id_third=? `
	if reportType == 1 {
		sql += ` AND classify_name_first = ? `
	} else {
		sql += ` AND classify_name_first<>? `
	}
	sql += ` ORDER BY publish_time DESC LIMIT 3`
	err = global.MYSQL["rddp"].Raw(sql, reportId, firstId, secondId, thirdId, "权益研报").Scan(&items).Error
	return
}

// 更多推荐展示该二级分类下发布时间在该报告前后的3篇报告，优先往前找，不足3篇时往后找补全，若都不足3篇，则有几篇展示几篇，报告按发布时间降序排，展示报告二级分类和报告标题，不展示期数；
func GetRaiRecommendList(reportId, firstId, secondId, thirdId int, publishTime time.Time) (items []*Report, err error) {
	// 获取发布时间在当前报告之前的报告
	var beforeItems []*Report
	beforeSql := `SELECT id,title,classify_name_first,classify_name_second, stage, rai_report_id FROM report WHERE state=2 AND id<>? AND classify_id_first=? AND classify_id_second=? AND classify_id_third=? 
		AND publish_time <= ? ORDER BY publish_time DESC, id DESC LIMIT 3`
	err = global.MYSQL["rddp"].Raw(beforeSql, reportId, firstId, secondId, thirdId, publishTime).Find(&beforeItems).Error
	if err != nil {
		return
	}

	items = append(items, beforeItems...)

	// 如果前面的报告不足3篇,则获取发布时间在当前报告之后的报告补全
	if len(beforeItems) < 3 {
		var afterItems []*Report
		remainCount := 3 - len(beforeItems)
		afterSql := `SELECT id,title,classify_name_first,classify_name_second, state, rai_report_id FROM report WHERE state=2 AND id<>? AND classify_id_first=? AND classify_id_second=? AND classify_id_third=? 
			AND publish_time > ? ORDER BY publish_time ASC, id ASC LIMIT ?`
		err = global.MYSQL["rddp"].Raw(afterSql, reportId, firstId, secondId, thirdId, publishTime, remainCount).Find(&afterItems).Error
		if err != nil {
			return
		}
		items = append(items, afterItems...)
	}
	return
}
