package smart_report

import (
	"eta/eta_mobile/utils"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
	"html"
	"strings"
	"time"
)

const (
	SmartReportStateWaitPublish = 1
	SmartReportStatePublished   = 2
)

// SmartReport 智能研报
type SmartReport struct {
	SmartReportId       int       `orm:"column(smart_report_id);pk" description:"智能研报ID"`
	ReportCode          string    `description:"报告唯一编码"`
	ClassifyIdFirst     int       `description:"一级分类ID"`
	ClassifyNameFirst   string    `description:"一级分类名称"`
	ClassifyIdSecond    int       `description:"二级分类ID"`
	ClassifyNameSecond  string    `description:"二级分类名称"`
	AddType             int       `description:"新增方式：1-新增报告；2-继承报告"`
	Title               string    `description:"标题"`
	Abstract            string    `description:"摘要"`
	Author              string    `description:"作者"`
	Frequency           string    `description:"频度"`
	Stage               int       `description:"期数"`
	Content             string    `description:"内容"`
	ContentSub          string    `description:"内容前两个章节"`
	ContentStruct       string    `description:"内容组件"`
	VideoUrl            string    `description:"音频文件URL"`
	VideoName           string    `description:"音频文件名称"`
	VideoPlaySeconds    float64   `description:"音频播放时长"`
	VideoSize           string    `description:"音频文件大小，单位M"`
	AdminId             int       `description:"创建者ID"`
	AdminRealName       string    `description:"创建者姓名"`
	State               int       `description:"1:未发布；2:已发布；3-待提交；4-待审批；5-已驳回；6-已通过"`
	LastModifyAdminId   int       `description:"最后更新人ID"`
	LastModifyAdminName string    `description:"最后更新人姓名"`
	ContentModifyTime   time.Time `description:"内容更新时间"`
	Pv                  int       `description:"pv"`
	Uv                  int       `description:"uv"`
	PublishTime         time.Time `description:"发布时间"`
	PrePublishTime      time.Time `description:"预发布时间"`
	PreMsgSend          int       `description:"定时发布后是否推送模版消息：0-否；1-是"`
	MsgIsSend           int       `description:"消息是否已发送：0-否；1-是"`
	MsgSendTime         time.Time `description:"模版消息发送时间"`
	DetailImgUrl        string    `description:"报告详情长图地址"`
	DetailPdfUrl        string    `description:"报告详情PDF地址"`
	DetailImgUrlMobile  string    `description:"报告详情长图地址-手机端"`
	DetailPdfUrlMobile  string    `description:"报告详情PDF地址-手机端"`
	CreateTime          time.Time `description:"创建时间"`
	ModifyTime          time.Time `description:"修改时间"`
	HeadImg             string    `description:"报告头图地址"`
	EndImg              string    `description:"报告尾图地址"`
	CanvasColor         string    `description:"画布颜色"`
	ApproveTime         time.Time `description:"审批时间"`
	ApproveId           int       `description:"审批ID"`
	NeedSplice          int       `description:"0-不需要 1-需要"`
	HeadResourceId      int       `description:"版头资源ID"`
	EndResourceId       int       `description:"版尾资源ID"`
}

func (m *SmartReport) TableName() string {
	return "smart_report"
}

func (m *SmartReport) PrimaryId() string {
	return "smart_report_id"
}

func (m *SmartReport) Create() (err error) {
	o := orm.NewOrmUsingDB("rddp")
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.SmartReportId = int(id)
	return
}

func (m *SmartReport) CreateMulti(items []*SmartReport) (err error) {
	if len(items) == 0 {
		return
	}
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.InsertMulti(len(items), items)
	return
}

func (m *SmartReport) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Update(m, cols...)
	return
}

func (m *SmartReport) Del() (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	_, err = o.Raw(sql, m.SmartReportId).Exec()
	return
}

func (m *SmartReport) MultiDel(menuIds []int) (err error) {
	if len(menuIds) == 0 {
		return
	}
	o := orm.NewOrmUsingDB("rddp")
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
	_, err = o.Raw(sql, menuIds).Exec()
	return
}

func (m *SmartReport) GetItemById(id int) (item *SmartReport, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

func (m *SmartReport) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *SmartReport, err error) {
	o := orm.NewOrmUsingDB("rddp")
	order := ``
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
	err = o.Raw(sql, pars).QueryRow(&item)
	return
}

func (m *SmartReport) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (m *SmartReport) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SmartReport, err error) {
	o := orm.NewOrmUsingDB("rddp")
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func (m *SmartReport) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*SmartReport, err error) {
	o := orm.NewOrmUsingDB("rddp")
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	// 排序：1:未发布；2:已发布；3-待提交；4-待审批；5-已驳回；6-已通过
	order := `ORDER BY FIELD(state,3,1,4,5,6,2), modify_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func (m *SmartReport) GetMaxStageByClassifyId(classifyId int) (stage int, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := fmt.Sprintf(`SELECT MAX(stage) AS max_stage FROM %s WHERE classify_id_second = ?`, m.TableName())
	err = o.Raw(sql, classifyId).QueryRow(&stage)
	return
}

// SmartReportItem 智能研报信息
type SmartReportItem struct {
	SmartReportId       int     `description:"智能研报ID"`
	ReportCode          string  `description:"报告唯一编码"`
	ClassifyIdFirst     int     `description:"一级分类ID"`
	ClassifyNameFirst   string  `description:"一级分类名称"`
	ClassifyIdSecond    int     `description:"二级分类ID"`
	ClassifyNameSecond  string  `description:"二级分类名称"`
	AddType             int     `description:"新增方式：1-新增报告；2-继承报告"`
	Title               string  `description:"标题"`
	Abstract            string  `description:"摘要"`
	Author              string  `description:"作者"`
	Frequency           string  `description:"频度"`
	Stage               int     `description:"期数"`
	Content             string  `description:"内容"`
	ContentSub          string  `description:"内容前两个章节"`
	ContentStruct       string  `description:"内容组件"`
	VideoUrl            string  `description:"音频文件URL"`
	VideoName           string  `description:"音频文件名称"`
	VideoPlaySeconds    float64 `description:"音频播放时长"`
	VideoSize           string  `description:"音频文件大小，单位M"`
	AdminId             int     `description:"创建者姓名"`
	AdminRealName       string  `description:"创建者姓名"`
	LastModifyAdminId   int     `description:"最后更新人ID"`
	LastModifyAdminName string  `description:"最后更新人姓名"`
	ContentModifyTime   string  `description:"内容更新时间"`
	Pv                  int     `description:"pv"`
	Uv                  int     `description:"uv"`
	State               int     `description:"1:未发布；2:已发布；3-待提交；4-待审批；5-已驳回；6-已通过"`
	PublishTime         string  `description:"发布时间"`
	PrePublishTime      string  `description:"预发布时间"`
	MsgIsSend           int     `description:"消息是否已发送：0-否；1-是"`
	MsgSendTime         string  `description:"模版消息发送时间"`
	DetailImgUrl        string  `description:"报告详情长图地址"`
	DetailPdfUrl        string  `description:"报告详情PDF地址"`
	CreateTime          string  `description:"创建时间"`
	ModifyTime          string  `description:"修改时间"`
	ApproveTime         string  `description:"审批时间"`
	CanEdit             bool    `description:"是否可编辑"`
	Editor              string  `description:"当前编辑人"`
	HeadImg             string  `description:"报告头图地址"`
	EndImg              string  `description:"报告尾图地址"`
	CanvasColor         string  `description:"画布颜色"`
	NeedSplice          int     `description:"0-不需要 1-需要"`
	HeadResourceId      int     `description:"版头资源ID"`
	EndResourceId       int     `description:"版尾资源ID"`
	HeadStyle           string  `description:"版头样式"`
	EndStyle            string  `description:"版尾样式"`
}

// FormatSmartReport2Item 格式化智能研报数据格式
func FormatSmartReport2Item(origin *SmartReport) (item *SmartReportItem) {
	item = new(SmartReportItem)
	if origin == nil {
		return
	}
	item.SmartReportId = origin.SmartReportId
	item.ReportCode = origin.ReportCode
	item.ClassifyIdFirst = origin.ClassifyIdFirst
	item.ClassifyNameFirst = origin.ClassifyNameFirst
	item.ClassifyIdSecond = origin.ClassifyIdSecond
	item.ClassifyNameSecond = origin.ClassifyNameSecond
	item.AddType = origin.AddType
	item.Title = origin.Title
	item.Abstract = origin.Abstract
	item.Author = origin.Author
	item.Frequency = origin.Frequency
	item.Stage = origin.Stage
	item.Content = html.UnescapeString(origin.Content)
	item.ContentSub = html.UnescapeString(origin.ContentSub)
	item.ContentStruct = html.UnescapeString(origin.ContentStruct)
	item.VideoUrl = origin.VideoUrl
	item.VideoName = origin.VideoName
	item.VideoPlaySeconds = origin.VideoPlaySeconds
	item.VideoSize = origin.VideoSize
	item.AdminId = origin.AdminId
	item.AdminRealName = origin.AdminRealName
	item.LastModifyAdminId = origin.LastModifyAdminId
	item.LastModifyAdminName = origin.LastModifyAdminName
	item.ContentModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ContentModifyTime)
	item.Pv = origin.Pv
	item.Uv = origin.Uv
	item.State = origin.State
	item.PublishTime = utils.TimeTransferString(utils.FormatDateTime, origin.PublishTime)
	item.PrePublishTime = utils.TimeTransferString(utils.FormatDateTime, origin.PrePublishTime)
	item.MsgIsSend = origin.MsgIsSend
	item.MsgSendTime = utils.TimeTransferString(utils.FormatDateTime, origin.MsgSendTime)
	item.DetailImgUrl = origin.DetailImgUrl
	item.DetailPdfUrl = origin.DetailPdfUrl
	item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
	item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
	item.HeadImg = origin.HeadImg
	item.EndImg = origin.EndImg
	item.CanvasColor = origin.CanvasColor
	item.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, origin.ApproveTime)
	item.NeedSplice = origin.NeedSplice
	item.HeadResourceId = origin.HeadResourceId
	item.EndResourceId = origin.EndResourceId
	return
}

// SmartReportAddReq 新增智能研报请求体
type SmartReportAddReq struct {
	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:"频度"`
	HeadImg            string `description:"报告头图地址"`
	EndImg             string `description:"报告尾图地址"`
	CanvasColor        string `description:"画布颜色"`
	HeadResourceId     int    `description:"版头资源ID"`
	EndResourceId      int    `description:"版尾资源ID"`
}

// SmartReportEditReq 编辑智能研报请求体
type SmartReportEditReq struct {
	SmartReportAddReq
	SmartReportId  int    `description:"智能研报ID"`
	Content        string `description:"内容"`
	ContentStruct  string `description:"内容结构"`
	HeadImg        string `description:"报告头图地址"`
	EndImg         string `description:"报告尾图地址"`
	CanvasColor    string `description:"画布颜色"`
	HeadResourceId int    `description:"版头资源ID"`
	EndResourceId  int    `description:"版尾资源ID"`
}

// SmartReportRemoveReq 删除智能研报请求体
type SmartReportRemoveReq struct {
	SmartReportId int `description:"智能研报ID"`
}

// SmartReportPublishReq 发布智能研报请求体
type SmartReportPublishReq struct {
	SmartReportId int    `description:"智能研报ID"`
	PublishState  int    `description:"1-取消发布; 2-发布"`
	ReportUrl     string `description:"报告Url"`
}

// SmartReportPrePublishReq 预发布智能研报请求体
type SmartReportPrePublishReq struct {
	SmartReportId  int    `description:"智能研报ID"`
	PrePublishTime string `description:"预发布时间"`
	PreMsgSend     int    `description:"定时发布成功后是否立即推送模版消息：0否，1是"`
	ReportUrl      string `description:"报告Url"`
}

// SmartReportSaveContentReq 保存草稿请求体
type SmartReportSaveContentReq struct {
	SmartReportId  int    `description:"智能研报ID"`
	Content        string `description:"内容"`
	ContentStruct  string `description:"内容结构"`
	NoChange       int    `description:"内容是否未改变：1：内容未改变"`
	HeadImg        string `description:"报告头图地址"`
	EndImg         string `description:"报告尾图地址"`
	CanvasColor    string `description:"画布颜色"`
	HeadResourceId int    `description:"版头资源ID"`
	EndResourceId  int    `description:"版尾资源ID"`
}

// SmartReportSaveContentResp 保存草稿响应体
type SmartReportSaveContentResp struct {
	SmartReportId int `description:"智能研报ID"`
}

// SmartReportSendMsgReq 消息推送请求体
type SmartReportSendMsgReq struct {
	SmartReportId int `description:"智能研报ID"`
}

// SmartReportMarkEditReq 标记编辑英文研报的请求数据
type SmartReportMarkEditReq struct {
	SmartReportId int `description:"智能研报ID"`
	Status        int `description:"标记状态: 1-编辑中; 2-编辑完成"`
}

// SmartReportListResp 智能研报
type SmartReportListResp struct {
	List   []*SmartReportItem
	Paging *paging.PagingItem `description:"分页数据"`
}

// ElasticSmartReport 智能研报es
type ElasticSmartReport struct {
	SmartReportId      int    `description:"智能研报ID"`
	Title              string `description:"标题"`
	Abstract           string `description:"摘要"`
	BodyContent        string `description:"内容"`
	PublishTime        string `description:"发布时间"`
	PublishState       int    `description:"发布状态 1-未发布 2-已发布"`
	Author             string `description:"作者"`
	ClassifyIdFirst    int    `description:"一级分类ID"`
	ClassifyNameFirst  string `description:"一级分类名称"`
	ClassifyIdSecond   int    `description:"二级分类ID"`
	ClassifyNameSecond string `description:"二级分类名称"`
	StageStr           string `description:"报告期数"`
	Frequency          string `description:"频度"`
}

// Report2ImgQueueReq 报告详情生成长图队列请求体
type Report2ImgQueueReq struct {
	ReportType int    `description:"报告类型: 1-研报; 2-智能研报"`
	ReportCode string `description:"报告唯一编码"`
}

// UpdateSmartReportsStateByCond 批量更新报告状态
func UpdateSmartReportsStateByCond(classifyFirstId, classifySecondId, oldState, newState int) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	cond := ``
	if classifyFirstId > 0 {
		cond += fmt.Sprintf(` AND classify_id_first = %d`, classifyFirstId)
	}
	if classifySecondId > 0 {
		cond += fmt.Sprintf(` AND classify_id_second = %d`, classifySecondId)
	}
	sql := fmt.Sprintf(`UPDATE smart_report SET state = ?, pre_publish_time = NULL WHERE state = ? %s`, cond)
	_, err = o.Raw(sql, newState, oldState).Exec()
	return
}

// UpdateSmartReportsStateBySecondIds 批量更新二级分类报告状态
func UpdateSmartReportsStateBySecondIds(oldState, newState int, secondIds []int) (err error) {
	if len(secondIds) <= 0 {
		return
	}
	o := orm.NewOrmUsingDB("rddp")
	// (有审批流的)未发布->待提交
	sql := fmt.Sprintf(`UPDATE smart_report SET state = ?, pre_publish_time = NULL WHERE state = ? AND classify_id_second IN (%s)`, utils.GetOrmInReplace(len(secondIds)))
	_, err = o.Raw(sql, newState, oldState, secondIds).Exec()
	if err != nil {
		return
	}
	// (无审批流的)待提交->未发布
	sql = fmt.Sprintf(`UPDATE smart_report SET state = ?, pre_publish_time = NULL WHERE state = ? AND classify_id_second NOT IN (%s)`, utils.GetOrmInReplace(len(secondIds)))
	_, err = o.Raw(sql, oldState, newState, secondIds).Exec()
	return
}

// UpdatePdfUrlSmartReportById 清空pdf相关字段
func UpdatePdfUrlSmartReportById(reportId int) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `UPDATE smart_report SET detail_img_url = '',detail_pdf_url='',modify_time=NOW() WHERE smart_report_id = ? `
	_, err = o.Raw(sql, reportId).Exec()
	return
}
