package models

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

type Ppt struct {
	PptId         int       `orm:"column(ppt_id);pk" description:"报告Id"`
	Title         string    `description:"标题"`
	TemplateType  string    `description:"模版类型"`
	PptDate       string    `description:"选择日期"`
	PptUrl        string    `description:"ppt地址"`
	CreateTime    time.Time `description:"创建时间"`
	ModifyTime    time.Time `description:"修改时间"`
	ReportType    string    `description:"报告类型"`
	AdminId       int       `description:"系统用户id"`
	AdminRealName string    `description:"系统用户名称"`
	Version       int       `description:"1:第一版,2:第二版"`
}

type PptItem struct {
	PptId         int       `orm:"column(ppt_id);pk" description:"报告Id"`
	Title         string    `description:"标题"`
	TemplateType  string    `description:"模版类型"`
	PptDate       string    `description:"选择日期"`
	PptUrl        string    `description:"ppt地址"`
	PptxUrl       string    `description:"ppt地址"`
	CreateTime    time.Time `description:"创建时间"`
	ModifyTime    time.Time `description:"修改时间"`
	ReportType    string    `description:"报告类型"`
	AdminId       int       `description:"系统用户id"`
	AdminRealName string    `description:"系统用户名称"`
	IsAuth        bool      `description:"true:有操作权限，false:无操作权限"`
	Version       int       `description:"1:第一版,2:第二版"`
}

type PptImages struct {
	PptImagesId int    `description:"Id"`
	ImageUrl    string `description:"图片地址"`
	ImageType   int    `description:"图片类型,0:首页背景模板"`
}

type PptPages struct {
	PptPagesId int       `orm:"column(ppt_pages_id);pk" description:"报告章节Id"`
	PptId      int64     `description:"ppt_id"`
	Title      string    `description:"标题"`
	ImgUrl     string    `description:"图片路径"`
	CreateTime time.Time `description:"创建时间"`
	ModifyTime time.Time `description:"修改时间"`
	PageCode   string    `description:"编码"`
	Width      int       `description:"宽度"`
	Height     int       `description:"高端"`
	ResourceId string    `description:"来源id，来自上海策略组提供的图表id"`
	Timestamp  int       `description:"时间戳"`
	BackIndex  int       `description:"背景图片下标"`
}

func GetPptList(condition string, pars []interface{}, startSize, pageSize int) (items []*PptItem, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM ppt 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 GetPptListCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT COUNT(1) AS count FROM ppt WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

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

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

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

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

type AddPptReq struct {
	PptId     int64 `description:"ppt_id"`
	FirstPage struct {
		Title      string `description:"标题"`
		ReportType string `description:"类型"`
		PptDate    string `description:"日期"`
		ImgUrl     string `description:"图片"`
		BackIndex  int    `description:"背景图片下标"`
	} `description:"首页"`
	ContentPage []ContentPageItems `description:"内容页"`
}

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

type ContentPageItems struct {
	PptId      int64  `json:"PptId" description:"ppt_id" `
	PptPagesId int    `json:"PptPagesId" description:"章节id，新增时传0"`
	Title      string `json:"Title" description:"标题"`
	ResourceId string `json:"ResourceId" description:"来源id，来自上海策略组提供的图表id"`
	Timestamp  int    `description:"时间戳"`
}

func AddPptPages(item *PptPages) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("rddp")
	lastId, err = o.Insert(item)
	return
}

type EditPptReq 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
}

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

func EditPptPages(item *PptPages) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `UPDATE ppt_pages SET title= ?,  img_url= ?,  modify_time = now(),  resource_id = ? WHERE ppt_pages_id= ? `
	_, err = o.Raw(sql, item.Title, item.ImgUrl, item.ResourceId, item.PptPagesId).Exec()
	return
}

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

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

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

func GetPptPagesById(pptId int) (items []*PptPages, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM ppt_pages WHERE ppt_id=? `
	_, err = o.Raw(sql, pptId).QueryRows(&items)
	return
}

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

type Base64UploadReq struct {
	Img        string `json:"img" description:"图片img base64" `
	PptId      string `json:"title" description:"ppt_id"`
	ResourceId int    `json:"resource_id" description:"来源id，来自上海策略组提供的图表id"`
}

//编辑ppt章节图片
func ModifyPptPages(imgUrl string, pptId, pptPagesId, width, height int) (err error) {
	sql := "UPDATE ppt_pages SET img_url=?,width=?,height=?,modify_time=NOW() WHERE ppt_pages_id=? AND ppt_id=? "
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql, imgUrl, width, height, pptPagesId, pptId).Exec()
	return
}

func GetImages(imageType int) (items []*PptImages, err error) {
	sql := `SELECT * FROM ppt_images WHERE image_type=? `
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql, imageType).QueryRows(&items)
	return
}

type PptImagesResp struct {
	List []*PptImages `description:"ppt背景图片"`
}

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

func GetPptFirstPage(pptId int) (item *PptPages, err error) {
	sql := `SELECT * FROM ppt_pages WHERE ppt_id=? ORDER BY ppt_pages_id ASC LIMIT 1 `
	o := orm.NewOrmUsingDB("rddp")
	err = o.Raw(sql, pptId).QueryRow(&item)
	return
}

func GetPptContentPages(pptId, pptPageId int) (item []*PptPages, err error) {
	sql := `SELECT * FROM ppt_pages WHERE ppt_id=? AND ppt_pages_id<>? ORDER BY ppt_pages_id ASC `
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql, pptId, pptPageId).QueryRows(&item)
	return
}

func EditPptPath(pptId int, pptxPath, pptPath string) (err error) {
	sql := `UPDATE ppt SET pptx_url=?,ppt_url=?,modify_time=NOW() WHERE ppt_id=? `
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql, pptxPath, pptPath, pptId).Exec()
	return
}

type PptPublishRecord struct {
	Id         int `orm:"column(id);pk"`
	PptId      int
	PptUrl     string
	CreateTime time.Time
}

func AddPptPublishRecord(item *PptPublishRecord) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("rddp")
	lastId, err = o.Insert(item)
	return
}

func GetPptPagesContent(pptId int, resourceId string) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM ppt_pages WHERE ppt_id=? AND resource_id=? `
	o := orm.NewOrmUsingDB("rddp")
	err = o.Raw(sql, pptId, resourceId).QueryRow(&count)
	return
}

type Base64UploadBatchReq struct {
	Img        string
	ResourceId string
	PptId      int
	PptPagesId int
}
