package yb

import (
	"github.com/beego/beego/v2/client/orm"
	"hongze/hz_crm_api/utils"
	"time"
)

// CommunityVideo 视频社区
type CommunityVideo struct {
	CommunityVideoId  int       `orm:"column(community_video_id);pk" description:"问题ID"`
	Title             string    `description:"视频标题"`
	VarietyTagId      int       `description:"标签ID"`
	VarietyTagName    string    `description:"标签名称"`
	CoverImgUrl       string    `description:"封面图地址"`
	VideoUrl          string    `description:"视频地址"`
	VideoSeconds      string    `description:"视频时长，单位秒"`
	PublishState      int       `description:"发布状态：0-待发布 1-已发布"`
	SendThsState      int       `description:"客群消息推送状态：0-待推送 1-已推送"`
	SendTemplateState int       `description:"模板消息推送状态：0-待推送 1-已推送"`
	IsDeleted         int       `description:"是否已删除 0-否 1-是"`
	PublishTime       time.Time `description:"发布时间"`
	SendThsTime       time.Time `description:"推送客群时间"`
	SendTemplateTime  time.Time `description:"推送模板消息时间"`
	CreateTime        time.Time `description:"提问时间"`
	ModifyTime        time.Time `description:"修改时间"`
	DeleteTime        time.Time `description:"删除时间"`
}
type CommunityVideoMore struct {
	CommunityVideo
	ClickNum int `description:"点击量"`
}

func (item *CommunityVideo) TableName() string {
	return "yb_community_video"
}

func (item *CommunityVideo) Add() (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	if err != nil {
		return
	}
	item.CommunityVideoId = int(lastId)
	return
}

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

// GetVideoById 主键获取视频
func GetVideoById(videoId int) (item *CommunityVideo, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM yb_community_video WHERE community_video_id = ? AND is_deleted = 0 LIMIT 1`
	err = o.Raw(sql, videoId).QueryRow(&item)
	return
}

// GetCommunityVideoList 获取视频列表
func GetCommunityVideoList(condition string, pars []interface{}, order string, startSize, pageSize int) (total int, list []*CommunityVideoMore, err error) {
	o := orm.NewOrm()
	sql := `SELECT
	v.*,
	l.click_num
FROM
	yb_community_video v
	LEFT JOIN ( SELECT count( * ) AS click_num, community_video_id FROM yb_community_video_play_log WHERE type=1 GROUP BY community_video_id ) AS l ON v.community_video_id = l.community_video_id 
WHERE
	v.is_deleted = 0 `
	sql += condition
	if order != "" {
		sql += order
	} else {
		sql += ` ORDER BY v.modify_time DESC`
	}
	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
}

// DeleteVideo 删除视频
func DeleteVideo(videoId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE yb_community_video SET is_deleted = 1, delete_time = NOW() WHERE community_video_id = ? LIMIT 1`
	_, err = o.Raw(sql, videoId).Exec()
	return
}

// GetCommunityVideoListByIds 根据IDs获取视频列表
func GetCommunityVideoListByIds(videoIds []int) (list []*CommunityVideo, err error) {
	arrLen := len(videoIds)
	if arrLen == 0 {
		return
	}
	o := orm.NewOrm()
	sql := `SELECT * FROM yb_community_video WHERE community_video_id IN (` + utils.GetOrmInReplace(arrLen) + `) AND is_deleted = 0`
	_, err = o.Raw(sql, videoIds).QueryRows(&list)
	return
}
