package cygx

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:"创建时间"`
}

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

// 活动详情
type CygxActivityVideoListResp struct {
	ActivityId           int    `orm:"column(activity_id);pk";description:"活动ID 等于0新增活动，大于0修改活动"`
	ActivityTypeId       int    `description:"活动类型id"`
	ActivityName         string `description:"活动名称"`
	ActivityTimeText     string `description:"活动时间"`
	ActivityTypeName     string `description:"活动类型名称"`
	ChartPermissionId    int    `description:"行业id"`
	ChartPermissionName  string `description:"行业名称"`
	ChartPermissionNames string `description:"行业名称辅助字段，区分研选子分类"`
	Label                string `description:"标签"`
	TemporaryLabel       string `description:"临时标签"`
	VideoId              int    `orm:"column(video_id);pk"description:"视频id"`
	VideoName            string `description:"视频名称"`
	VideoCounts          int    `description:"播放量"`
	VideoDuration        string `description:"视频时长"`
	CommentNum           int    `description:"留言总数"`
}

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

// 获取数量
func GetActivityVideoCount(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT
				COUNT( 1 ) AS count 
			FROM
				cygx_activity AS art
				INNER JOIN cygx_activity_video AS v ON v.activity_id = art.activity_id 
			WHERE
				1 = 1  `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 列表
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_counts,video_duration,
(SELECT COUNT(1) FROM cygx_article_comment AS h    WHERE h.activity_id=art.activity_id AND  h.industry_id = 0 AND  h.activity_voice_id = 0) AS comment_num
			FROM cygx_activity as art  INNER JOIN cygx_activity_video AS v ON v.activity_id = art.activity_id   WHERE 1= 1  `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// 历史记录
func GetActivityVideoHistoryById(condition string, pars []interface{}) (item []*CygxMicroRoadshowVoiceHistory, err error) {
	o := orm.NewOrm()
	sql := ` SELECT
			h.* 
		FROM
			cygx_activity_video_history AS h
			INNER JOIN cygx_activity_video AS v 
		WHERE
			1 = 1 
			AND v.video_id = h.video_id  `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY  h.create_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

// 详情
func GetCygxActivityVideoReqDetail(activityId int) (item *CygxActivityVideo, err error) {
	o := orm.NewOrm()
	sql := `SELECT *  FROM cygx_activity_video  WHERE activity_id= ? `
	err = o.Raw(sql, activityId).QueryRow(&item)
	return
}

// 列表
func GetCygxActivityVideoReqDetailByVideoId(videoId int) (item *CygxActivityVideo, err error) {
	o := orm.NewOrm()
	sql := `SELECT *  FROM cygx_activity_video  WHERE video_id= ? `
	err = o.Raw(sql, videoId).QueryRow(&item)
	return
}

// 列表
func GetActivityVideoList(condition string, pars []interface{}) (items []*CygxActivityVideoListResp, err error) {
	o := orm.NewOrm()
	sql := ` SELECT  * FROM  cygx_activity_video  WHERE  1=1  `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
