package models

import (
	"eta/eta_mobile/models/ppt_english"
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
	"time"
)

// PptV2 表
type PptV2 struct {
	PptId         int       `orm:"column(ppt_id);pk;auto" description:"ppt的Id"`
	TemplateType  int       `description:"模版类型"`
	BackgroundImg string    `description:"背景图片"`
	Title         string    `description:"标题"`
	ReportType    string    `description:"报告类型"`
	PptDate       string    `description:"选择日期"`
	Content       string    `description:"ppt内容"`
	PptUrl        string    `description:"ppt下载地址"`
	PptxUrl       string    `description:"pptx下载地址"`
	CreateTime    time.Time `description:"创建时间"`
	ModifyTime    time.Time `description:"修改时间"`
	AdminId       int       `description:"系统用户id"`
	AdminRealName string    `description:"系统用户名称"`
	PptVersion    int8      `description:"是否ppt的旧版本；1：旧的，2：新的"`
	ReportId      int       `description:"关联的报告ID"`
	ReportCode    string    `description:"关联的报告code"`
	IsShare       int8      `description:"是否分享，0：不分享，1：分享"`
	PublishTime   time.Time `description:"发布时间"`
	CoverContent  string    `description:"PPT内容-JSON"`
}

type PptV2Item struct {
	PptId         int       `orm:"column(ppt_id);pk" description:"ppt的Id"`
	TemplateType  int       `description:"模版类型"`
	BackgroundImg string    `description:"背景图片"`
	Title         string    `description:"标题"`
	ReportType    string    `description:"报告类型"`
	PptDate       string    `description:"选择日期"`
	Content       string    `description:"ppt内容"`
	PptUrl        string    `description:"ppt下载地址"`
	PptxUrl       string    `description:"pptx下载地址"`
	CreateTime    time.Time `description:"创建时间"`
	ModifyTime    time.Time `description:"修改时间"`
	AdminId       int       `description:"系统用户id"`
	AdminRealName string    `description:"系统用户名称"`
	PptVersion    int8      `description:"是否ppt的旧版本；1：旧的，2：新的"`
	IsAuth        bool      `description:"true:有操作权限，false:无操作权限"`
	ReportId      int       `description:"报告ID"`
	ReportCode    string    `description:"关联的报告code"`
	IsShare       int8      `description:"是否分享，0：不分享，1：分享"`
	PublishTime   time.Time `description:"发布时间"`
	CoverContent  string    `description:"PPT内容-JSON"`
}

func GetPptV2List(condition string, pars []interface{}, startSize, pageSize int) (items []*PptV2Item, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM ppt_v2 WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	//
	sql += `ORDER BY modify_time DESC LIMIT ?,?`
	//sql += `ORDER BY create_time DESC LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetPptV2ByCondition(condition string, pars []interface{}) (items []*PptV2, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM ppt_v2 WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	//
	sql += `ORDER BY modify_time DESC`
	//sql += `ORDER BY create_time DESC LIMIT ?,?`
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

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

type PptV2ListResp struct {
	List   []*PptV2Item
	Paging *paging.PagingItem `description:"分页数据"`
}

// AddPptV2 新增PPT
func AddPptV2(item *PptV2) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("rddp")
	lastId, err = o.Insert(item)
	return
}

// DeletePptV2 删除ppt
func DeletePptV2(pptId int) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `DELETE FROM ppt_v2 WHERE ppt_id=? `
	_, err = o.Raw(sql, pptId).Exec()
	return
}

type AddPptV2Req struct {
	PptId     int64 `description:"ppt_id"`
	FirstPage struct {
		Title        string `description:"标题"`
		ReportType   string `description:"类型"`
		PptDate      string `description:"日期"`
		ImgUrl       string `description:"图片"`
		BackIndex    int    `description:"背景图片下标"`
		TemplateType int    `description:"模版id"`
	} `description:"首页"`
	Content      string `description:"ppt的json数据"`
	GroupId      int64  `description:"目录id"`
	CoverContent string `description:"封面图内容-JSON数据"`
}

type AddPptV2Resp struct {
	PptId int64 `description:"PptId"`
}

type EditPptV2Req struct {
	PptId     int64 `description:"pptId"`
	FirstPage struct {
		PptPagesId int    `description:"ppt_page_id"`
		Title      string `description:"标题"`
		ReportType string `description:"类型"`
		PptDate    string `description:"日期"`
		ImgUrl     string `description:"图片路径"`
		BackIndex  int    `description:"背景图片下标"`
	}
	ContentPage []ContentPageItems
}

// Update 更新ppt基础信息
func (item *PptV2) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Update(item, cols...)
	return
}

// EditPptV2 编辑ppt
func EditPptV2(item *PptV2) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `UPDATE ppt_v2 SET title = ?,ppt_date= ?, report_type=?,modify_time=NOW()  WHERE ppt_id = ? `
	_, err = o.Raw(sql, item.Title, item.PptDate, item.ReportType, item.PptId).Exec()
	return
}

type DeletePptV2Req struct {
	PptId int `description:"PptId" `
}

// SavePptV2PathReq 保存ppt的下载地址请求
type SavePptV2PathReq struct {
	PptId   int    `description:"PptId" `
	PptxUrl string `description:"PptxUrl" `
}

func GetPptV2ById(pptId int) (item *PptV2, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM ppt_v2 WHERE 1=1 AND ppt_id=? `
	err = o.Raw(sql, pptId).QueryRow(&item)
	return
}

func GetPptV2ByIds(pptIds []int) (list []*PptV2, err error) {
	_, err = orm.NewOrmUsingDB("rddp").
		QueryTable("ppt_v2").
		Filter("ppt_id__in", pptIds).
		All(&list)
	return
}

func GetPptV2ByTitle(title string) (item *PptV2, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM ppt_v2 WHERE 1=1 AND title=? `
	err = o.Raw(sql, title).QueryRow(&item)
	return
}

type PptV2DetailResp struct {
	PptPages []*PptPages `description:"ppt详情数据"`
	Ppt      *Ppt        `description:"ppt数据"`
}

type PptV2PublishReq struct {
	PptId        int `description:"PptId"`
	ScreenHeight int `description:"屏幕宽高"`
}

// EditPptV2Path 编辑Ppt的下载地址
func EditPptV2Path(pptId int, pptxPath string) (err error) {
	sql := `UPDATE ppt_v2 SET pptx_url=?,modify_time=NOW(),publish_time=NOW() WHERE ppt_id=? `
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql, pptxPath, pptId).Exec()
	return
}

// AddPptV2PublishRecord 发布记录表
type PptV2PublishRecord struct {
	Id         int `orm:"column(id);pk"`
	PptId      int
	PptUrl     string
	CreateTime time.Time
}

// AddPptV2PublishRecord 添加发布记录，主要是记录每次发布的时候，当前的ppt
func AddPptV2PublishRecord(item *PptV2PublishRecord) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("rddp")
	lastId, err = o.Insert(item)
	return
}

// PptV2SaveLog ppt记录表
type PptV2SaveLog struct {
	Id            int       `orm:"column(id);pk" description:"自增Id"`
	PptId         int       `orm:"column(ppt_id)" description:"ppt的Id"`
	TemplateType  int       `description:"模版类型"`
	BackgroundImg string    `description:"背景图片"`
	Title         string    `description:"标题"`
	ReportType    string    `description:"报告类型"`
	PptDate       string    `description:"选择日期"`
	Content       string    `description:"ppt内容"`
	AdminId       int       `description:"系统用户id"`
	AdminRealName string    `description:"系统用户名称"`
	CreateTime    time.Time `description:"创建时间"`
	CoverContent  string    `description:"PPT内容-JSON"`
}

// AddPptV2SaveLog 新增PPT日志
func AddPptV2SaveLog(item *PptV2SaveLog) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("rddp")
	lastId, err = o.Insert(item)
	return
}

type PptV2ConfigResp struct {
	PptNum   int `description:"ppt数量" json:"ppt_num"`
	ChartNum int `description:"图片数量" json:"chart_num"`
}

type PPT2ReportReq struct {
	PptId            int    `description:"PPT主键"`
	ClassifyIdSecond int    `description:"报告二级分类ID"`
	Title            string `description:"标题"`
}

// AddPptV2Multi 批量新增ppt
func AddPptV2Multi(list []*PptV2) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.InsertMulti(1, list)
	return
}

// GetAllPptTitle 获取当前目录的所有名称
func GetAllPptTitle() (names []string, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := "select title from ppt_v2"
	_, err = o.Raw(sql).QueryRows(&names)
	return
}

// PPT2ReportResp PPT转报告响应体
type PPT2ReportResp struct {
	ReportId   int    `description:"关联的报告ID" json:"report_id"`
	ReportCode string `description:"关联的报告编码" json:"report_code"`
}

// GetPptV2ByReportId 通过关联的报告ID获取PPT
func GetPptV2ByReportId(reportId int) (item *PptV2, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM ppt_v2 WHERE 1=1 AND report_id=? LIMIT 1`
	err = o.Raw(sql, reportId).QueryRow(&item)
	return
}

// GetSharePptV2 获取公开分享的ppt
func GetSharePptV2(IsNewPpt int) (items []*PptV2, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM ppt_v2 WHERE 1=1 AND is_share = 1 `

	if IsNewPpt > 0 {
		sql += ` AND ppt_version = 2 `
	}
	sql += `ORDER BY create_time DESC`
	//sql += `ORDER BY create_time DESC LIMIT ?,?`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// GetAllPptV2List 根据条件获取所有的Ppt
func GetAllPptV2List(condition string, pars []interface{}) (items []*PptV2Item, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM ppt_v2 WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	//
	sql += `ORDER BY modify_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// GetSyncReportPptList 获取所有已经同步了报告的ppt
func GetSyncReportPptList() (list []*PptV2, err error) {
	sql := `SELECT * FROM ppt_v2 WHERE report_id>0 `
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

type EnPPT2CnReq struct {
	PptId   int `description:"英文PPT主键"`
	GroupId int `description:"目录ID"`
}

type BatchEnPPT2CnReq struct {
	PptIds  string `description:"英文PPT主键s"`
	GroupId int    `description:"目录ID"`
}

func GetPptV2ByTitleAndId(title string, adminId int) (item *PptV2, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM ppt_v2 WHERE 1=1 AND title=? AND admin_id=? `
	err = o.Raw(sql, title, adminId).QueryRow(&item)
	return
}

// PPTDetailResp PPT详情响应体
type PPTDetailResp struct {
	*PptV2
	Editor ppt_english.PPTEditingCache `description:"编辑人信息"`
}
