package models

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

// EnglishVideoCover 英文研报视频封面库
type EnglishVideoCover struct {
	Id         int       `orm:"column(id);pk;auto" description:"封面ID"`
	CoverName  string    `description:"封面名称"`
	CoverUrl   string    `description:"封面图地址"`
	IsDeleted  int       `description:"是否已删除 0-否 1-是"`
	CreateTime time.Time `description:"创建时间"`
	ModifyTime time.Time `description:"修改时间"`
	DeleteTime time.Time `description:"删除时间"`
}

func (item *EnglishVideoCover) TableName() string {
	return "english_video_cover"
}

func (item *EnglishVideoCover) Add() (err error) {
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Insert(item)
	return
}

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

// GetEnglishVideoCoverById 主键获取封面
func GetEnglishVideoCoverById(coverId int) (item *EnglishVideoCover, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM english_video_cover WHERE id = ? AND is_deleted = 0 LIMIT 1`
	err = o.Raw(sql, coverId).QueryRow(&item)
	return
}

// GetEnglishVideoCoverPageList 获取封面库分页列表
func GetEnglishVideoCoverPageList(condition string, pars []interface{}, orderRule string, startSize, pageSize int) (total int, list []*EnglishVideoCover, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM english_video_cover WHERE is_deleted = 0 `
	sql += condition
	if orderRule == "" {
		orderRule = ` ORDER BY modify_time DESC`
	}
	sql += orderRule
	totalSQL := `SELECT COUNT(1) total FROM (` + sql + `) z`
	if err = o.Raw(totalSQL, pars).QueryRow(&total); err != nil {
		return
	}
	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
	return
}

// DeleteEnglishVideoCover 删除封面
func DeleteEnglishVideoCover(coverId int) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `UPDATE english_video_cover SET is_deleted = 1, delete_time = NOW() WHERE id = ? LIMIT 1`
	_, err = o.Raw(sql, coverId).Exec()
	return
}


type EnglishVideoCoverListResp struct {
	Paging *paging.PagingItem
	List   []*EnglishVideoCoverItem `description:"列表数据"`
}

type EnglishVideoCoverItem struct {
	Id         int    `description:"封面ID"`
	CoverName  string `description:"封面名称"`
	CoverUrl   string `description:"封面图地址"`
	CreateTime string `description:"提问时间"`
	ModifyTime string `description:"修改时间"`
}

type EnglishVideoCoverSaveReq struct {
	Id        int    `description:"封面ID"`
	CoverName string `description:"封面名称"`
	CoverUrl  string `description:"封面图地址"`
}

type EnglishVideoCoverOptionReq struct {
	Id int `description:"封面图ID"`
}