package models

import (
	"eta/eta_mini_crm_ht/utils"
	"time"

	"github.com/beego/beego/v2/client/orm"
)

type ReportPdf struct {
	ReportPdfId        int       `orm:"pk" description:"id"`
	PdfUrl             string    `description:"pdf文件URL"`
	PdfName            string    `description:"pdf文件名称"`
	Title              string    `description:"pdf文件标题"`
	Author             string    `description:"作者"`
	Abstract           string    `description:"摘要"`
	ClassifyIdFirst    int       `description:"一级分类id"`
	ClassifyNameFirst  string    `description:"一级分类名称"`
	ClassifyIdSecond   int       `description:"二级分类id"`
	ClassifyNameSecond string    `description:"二级分类名称"`
	ClassifyIdThird    int       `description:"三级分类id"`
	ClassifyNameThird  string    `description:"三级分类名称"`
	Stage              int       `description:"期数"`
	PublishTime        time.Time `description:"发布时间"`
	ModifyTime         time.Time `description:"更新时间"`
	Pv                 int       `description:"pv"`
	Uv                 int       `description:"uv"`
	SysUserId          int       `description:"创建人id"`
	SysRealName        string    `description:"创建人姓名"`
	State              int       `description:"状态"`
}

type ReportPdfView struct {
	ReportPdfId        int       `orm:"pk" description:"id"`
	PdfUrl             string    `description:"pdf文件URL"`
	PdfName            string    `description:"pdf文件名称"`
	Title              string    `description:"pdf文件标题"`
	Author             string    `description:"作者"`
	Abstract           string    `description:"摘要"`
	ClassifyIdFirst    int       `description:"一级分类id"`
	ClassifyNameFirst  string    `description:"一级分类名称"`
	ClassifyIdSecond   int       `description:"二级分类id"`
	ClassifyNameSecond string    `description:"二级分类名称"`
	ClassifyIdThird    int       `description:"三级分类id"`
	ClassifyNameThird  string    `description:"三级分类名称"`
	Stage              int       `description:"期数"`
	PublishTime        string    `description:"发布时间"`
	ModifyTime         time.Time `description:"更新时间"`
	Pv                 int       `description:"pv"`
	Uv                 int       `description:"uv"`
	SysUserId          int       `description:"创建人id"`
	SysRealName        string    `description:"创建人姓名"`
	State              int       `description:"状态"`
}

func (r *ReportPdf) Insert() (insertId int64, err error) {
	o := orm.NewOrm()
	// 计算研报期数
	sql := `SELECT MAX(stage) + 1 AS count FROM report_pdf WHERE classify_id_second=?`
	err = o.Raw(sql, r.ClassifyIdSecond).QueryRow(&r.Stage)
	if r.Stage == 0 {
		r.Stage = 1
	}
	if err != nil {
		return
	}
	insertId, err = o.Insert(r)
	return
}

func (r *ReportPdf) ToView() (item *ReportPdfView) {
	item = new(ReportPdfView)
	item.ReportPdfId = r.ReportPdfId
	item.PdfUrl = r.PdfUrl
	item.PdfName = r.PdfName
	item.Title = r.Title
	item.Author = r.Author
	item.Abstract = r.Abstract
	item.ClassifyIdFirst = r.ClassifyIdFirst
	item.ClassifyNameFirst = r.ClassifyNameFirst
	item.ClassifyIdSecond = r.ClassifyIdSecond
	item.ClassifyNameSecond = r.ClassifyNameSecond
	item.Stage = r.Stage
	item.State = r.State
	item.PublishTime = r.PublishTime.Format(utils.FormatDateTime)
	item.ModifyTime = r.ModifyTime
	item.Pv = r.Pv
	item.Uv = r.Uv
	item.SysUserId = r.SysUserId
	item.SysRealName = r.SysRealName
	return

}

func (r *ReportPdf) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(r, cols...)
	return
}

func (r *ReportPdf) Delete() (err error) {
	o := orm.NewOrm()
	_, err = o.Delete(r)
	return
}

func GetReportPdfCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(*) AS count FROM report_pdf WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func GetReportPdfByCondition(condition, sortCondition string, pars []interface{}, startPage, pageSize int) (items []*ReportPdf, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM report_pdf WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	if sortCondition != "" {
		sql += sortCondition // 排序
	}
	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, pars, startPage, pageSize).QueryRows(&items)
	return
}

func GetReportPdfById(id int) (item *ReportPdf, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM report_pdf WHERE report_pdf_id=?`
	err = o.Raw(sql, id).QueryRow(&item)
	return
}
