package models

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

type CygxActivityVideo struct {
	VideoId       int    `orm:"column(video_id);pk"description:"视频id"`
	VideoName     string `description:"视频名称"`
	ActivityId    int    ` description:"活动ID"`
	ModifyDate    string `description:"更新时间"`
	VideoDuration string `description:"视频时长"`
	VideoCounts   int    `description:"播放量"`
	VideoUrl      string `description:"视频地址"`
	CreateTime    string `description:"创建时间"`
	BackgroundImg string `description:"封面图片"`
	ShareImg      string `description:"分享图片"`
}

type CygxActivityVideoResp struct {
	VideoId           int    `orm:"column(video_id);pk"description:"视频id"`
	ChartPermissionId int    `description:"权限id"`
	VideoName         string `description:"视频名称"`
	ActivityId        int    ` description:"活动ID"`
	ModifyDate        string `description:"更新时间"`
	VideoDuration     string `description:"视频时长"`
	VideoCounts       int    `description:"播放量"`
	VideoUrl          string `description:"视频地址"`
	CreateTime        string `description:"创建时间"`
	BackgroundImg     string `description:"封面图片"`
	ShareImg          string `description:"分享图片"`
}

type CygxActivityVideoReq struct {
	VideoName     string `description:"视频名称"`
	VideoDuration string `description:"视频时长"`
	VideoUrl      string `description:"视频地址"`
	ActivityId    int    ` description:"活动ID"`
}

// string `orm:"column(label)";description:"主题"`
// 活动详情
type CygxActivityVideoListResp struct {
	ActivityId    int    `description:"活动ID 等于0新增活动，大于0修改活动"`
	Id            int    `orm:"column(video_id)";description:"视频id"`
	Title         string `orm:"column(video_name)";description:"视频名称"`
	ResourceUrl   string `orm:"column(video_url)";description:"视频地址"`
	VideoDuration string `description:"视频时长"`
	ActivityTime  string `description:"活动时间"`
}

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

// 列表
func GetActivityVideoListAll(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxActivityVideoListResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT art.*,v.video_id,v.video_name,v.video_url,video_duration FROM cygx_activity as art  INNER JOIN cygx_activity_video AS v ON v.activity_id = art.activity_id   WHERE 1= 1  AND  v.is_hide = 0  `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func UpdateActivityVideoCounts(activityId int) (err error) {
	sql := `UPDATE cygx_activity_video SET video_counts = video_counts+1 WHERE activity_id = ?  `
	o := orm.NewOrm()
	_, err = o.Raw(sql, activityId).Exec()
	return
}

// GetCygxActivityVideoByActivityId 获取活动视频
func GetCygxActivityVideoByActivityId(activityId int) (item *CygxActivityVideo, err error) {
	sql := `SELECT * FROM cygx_activity_video WHERE activity_id = ? LIMIT 1 `
	err = orm.NewOrm().Raw(sql, activityId).QueryRow(&item)
	return
}

// 获取数量
func GetCygxActivityVideoCount(activityId int) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_activity_video   WHERE activity_id = ? `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, activityId).QueryRow(&count)
	return
}

// GetCygxActivityVideoReqByActivityId 获取活动视频
func GetCygxActivityVideoReqByActivityId(activityId int) (item *CygxActivityVideoReq, err error) {
	sql := `SELECT * FROM cygx_activity_video WHERE video_id = ? LIMIT 1 `
	err = orm.NewOrm().Raw(sql, activityId).QueryRow(&item)
	return
}

// GetCygxActivityVideoById 获取活动视频
func GetCygxActivityVideoById(videoId int) (item *CygxActivityVideo, err error) {
	sql := `SELECT * FROM cygx_activity_video WHERE video_id = ? LIMIT 1 `
	err = orm.NewOrm().Raw(sql, videoId).QueryRow(&item)
	return
}

// 列表
func GetActivityVideoList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxActivityVideoResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT
	v.*,art.chart_permission_id
FROM
	cygx_activity_video AS v 
	INNER JOIN cygx_activity as art ON art.activity_id = v.activity_id    WHERE 1= 1  `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// 获取所有活动视频ID
func GetActivityVideoActivityIds() (activityIds string, err error) {
	sql := `SELECT
				GROUP_CONCAT( DISTINCT a.activity_id SEPARATOR ',' ) AS activityids 
			FROM
				cygx_activity_video AS a  WHERE  is_hide = 0 `
	o := orm.NewOrm()
	err = o.Raw(sql).QueryRow(&activityIds)
	return
}

type CygxActivityVideoDetailResp struct {
	ActivityId          int    ` description:"活动ID"`
	Id                  int    `description:"视频ID"`
	Title               string `description:"标题"`
	ResourceUrl         string `description:"链接"`
	BackgroundImg       string `description:"背景图"`
	PlaySeconds         int    `description:"音视频时长"`
	ChartPermissionId   int    `description:"行业ID"`
	ChartPermissionName string `description:"行业名称"`
	Type                int    `description:"类型: 1-音频; 2-活动视频； 3-产业视频 、 4-系列问答视频"`
}

// GetCygxActivityVideoById 获取活动视频
func GetCygxActivityVideoByActivityIdInfo(activityId int) (item *CygxActivityVideoDetailResp, err error) {
	sql := `SELECT
			v.video_id AS id,
			v.video_name AS title,
			v.video_url AS resource_url,
			v.video_duration AS play_seconds,
			v.activity_id,
			a.chart_permission_id,
			a.chart_permission_name
		FROM
			cygx_activity_video AS v
			INNER JOIN cygx_activity AS a  ON  v.activity_id = a.activity_id
		WHERE
			 v.activity_id = ? 
			LIMIT 1 `
	err = orm.NewOrm().Raw(sql, activityId).QueryRow(&item)
	return
}

func UpdateActivityVideoCommentNum(activityId int) (err error) {
	sql := `UPDATE cygx_activity_video SET comment_num = comment_num+1 WHERE activity_id = ?  `
	o := orm.NewOrm()
	_, err = o.Raw(sql, activityId).Exec()
	return
}

// 修改
func UpdateCygxActivityVideo(item *CygxActivityVideo) (err error) {
	to := orm.NewOrm()
	updateParams := make(map[string]interface{})
	updateParams["BackgroundImg"] = item.BackgroundImg
	updateParams["ShareImg"] = item.ShareImg
	ptrStructOrTableName := "cygx_activity_video"
	whereParam := map[string]interface{}{"activity_id": item.ActivityId}
	qs := to.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	return
}
