package cygx

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

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:"视频地址"`
	BackgroundImg string `description:"封面图片"`
	ShareImg      string `description:"分享图片"`
	IsByHand      int    `description:"是否手动修改过"`
	FileType      int    `description:"文件类型，1：路演回放，2：调研反馈"`
	CreateTime    string `description:"创建时间"`
}

type CygxActivityVideoReq struct {
	VideoName     string `description:"视频名称"`
	VideoDuration string `description:"视频时长"`
	VideoUrl      string `description:"视频地址"`
	BackgroundImg string `description:"封面图片"`
	ShareImg      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:"留言总数"`
	VideoUrl             string `description:"视频地址"`
	FileName             string `description:"文件类型名称"`
}

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

// 添加
func AddCygxActivityVideo(item *CygxActivityVideo) (newId int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	newId, err = o.Insert(item)
	return
}

// 修改
func UpdateCygxActivityVideo(item *CygxActivityVideo) (err error) {
	to := orm.NewOrmUsingDB("hz_cygx")
	updateParams := make(map[string]interface{})
	updateParams["VideoName"] = item.VideoName
	updateParams["VideoDuration"] = item.VideoDuration
	updateParams["VideoUrl"] = item.VideoUrl
	updateParams["BackgroundImg"] = item.BackgroundImg
	updateParams["ShareImg"] = item.ShareImg
	updateParams["FileType"] = item.FileType
	updateParams["ModifyDate"] = time.Now()
	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
}

// 获取数量
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.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

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

// 列表
func GetActivityVideoListAll(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxActivityVideoListResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	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.NewOrmUsingDB("hz_cygx")
	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.NewOrmUsingDB("hz_cygx")
	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.NewOrmUsingDB("hz_cygx")
	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.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT  * FROM  cygx_activity_video  WHERE  1=1  `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

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

// MicroRoadShowDefaultImgList 微路演行业默认背景图列表
type MicroRoadShowDefaultImgList struct {
	Audio []*MicroRoadShowDefaultImg `description:"音频"`
	Video []*MicroRoadShowDefaultImg `description:"视频"`
}

// MicroRoadShowDefaultImg 微路演行业默认背景图
type MicroRoadShowDefaultImg struct {
	ChartPermissionId   int    `description:"行业ID"`
	ChartPermissionName string `description:"行业名称"`
	ImgUrl              string `description:"背景图"`
	ShareImg            string `description:"分享图"`
}

// MicroRoadShowDefaultImgList 微路演行业默认背景图列表
type ActivityVideoDefaultImgList struct {
	Audio []*ActivityVideoDefaultImg `description:"音频"`
	Video []*ActivityVideoDefaultImg `description:"视频"`
}

type ActivityVideoDefaultImg struct {
	ChartPermissionId int `description:"行业ID"`
	List              []*MicroRoadShowDefaultImg
}

type ActivityVideoDefaultImgListResp struct {
	List []*MicroRoadShowDefaultImg
}

type ActivityVideoDefaultImgResp struct {
	BackgroundImg string `description:"封面图片"`
	ShareImg      string `description:"分享图"`
}

// 删除数据
func DeleteCygxActivityVideo(activityId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` DELETE FROM cygx_activity_video WHERE  activity_id= ? `
	_, err = o.Raw(sql, activityId).Exec()
	return
}

func Updatevideo_url(video_url string, video_id int) (err error) {
	sql := `UPDATE cygx_activity_video SET video_url =? WHERE video_id=? `
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Raw(sql, video_url, video_id).Exec()
	return
}

// 获取数量
func GetActivityVoiceAndVideoList(condition, conditionVoice, conditionVideo, conditionOrder string, pars []interface{}, startSize, pageSize int) (items []*CygxActivityVideoListResp, total int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` 	
		SELECT
		art.activity_id,
		art.activity_time,
		art.activity_name,
		art.activity_type_name,
		art.chart_permission_name,
		art.activity_time_text,
		v.activity_voice_id as video_id,
		v.voice_name as video_name,
		v.voice_counts as video_counts,
		v.comment_num as comment_num,
		v.voice_play_seconds as  video_duration,
		v.voice_url as  video_url
	FROM
		cygx_activity AS art
		INNER JOIN cygx_activity_voice AS v ON v.activity_id = art.activity_id 
	WHERE
		1 = 1 ` + condition + conditionVoice

	sql += `	UNION ALL`
	sql += ` SELECT
		art.activity_id,
		art.activity_time,
		art.activity_name,
		art.activity_type_name,
		art.chart_permission_name,
		art.activity_time_text,
		v.video_id,
		v.video_name,
		v.video_counts,
		v.comment_num as comment_num,
		v.video_duration,
		v.video_url
	FROM
		cygx_activity AS art
		INNER JOIN cygx_activity_video AS v ON v.activity_id = art.activity_id 
	WHERE
		1 = 1  ` + condition + conditionVideo + conditionOrder
	totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z `
	err = o.Raw(totalSql, pars, pars).QueryRow(&total)
	if err != nil {
		return
	}
	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, pars, pars, startSize, pageSize).QueryRows(&items)
	return
}
