package models

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

// ActivityVoice 活动语音表结构体
type CygxActivityVoice struct {
	ActivityVoiceId  int       `orm:"column(activity_voice_id);pk" description:"活动音频ID"`
	ActivityId       int       ` description:"活动ID"`
	VoiceUrl         string    `description:"音频地址"`
	VoiceName        string    `description:"音频名称"`
	VoicePlaySeconds string    `description:"音频时长"`
	VoiceCounts      int       `description:"播放量"`
	ModifyTime       string    `description:"更新时间"`
	CreateTime       time.Time `description:"创建时间"`
	BackgroundImg    string    `description:"封面图片"`
	ShareImg         string    `description:"分享图片"`
}

// ActivityVoice 活动语音表结构体
type CygxActivityVoiceResp struct {
	ActivityVoiceId     int    `orm:"column(activity_voice_id);pk" description:"活动音频ID"`
	ActivityId          int    ` description:"活动ID"`
	VoiceUrl            string `description:"音频地址"`
	VoiceName           string `description:"音频名称"`
	VoicePlaySeconds    string `description:"音频时长"`
	VoiceCounts         int    `description:"播放量"`
	ModifyTime          string `description:"更新时间"`
	CreateTime          string `description:"创建时间"`
	ChartPermissionId   int    `description:"权限id"`
	ChartPermissionName string `description:"行业名称"`
	ActivityTime        string `description:"活动时间"`
	BackgroundImg       string `description:"封面图片"`
	ShareImg            string `description:"分享图片"`
}

// ActivityVoiceReq 音频数据
type CygxActivityVoiceReq struct {
	ActivityId      int    ` description:"活动ID"`
	ActivityVoiceId int    ` description:"音频ID"`
	Url             string `description:"音频资源url地址"`
	Name            string `description:"音频名称"`
	PlaySeconds     int    `description:"音频时长"`
}

// GetCygxActivityVoiceReqList 获取活动ID的音频
func GetCygxActivityVoiceReqList(activityIds []int) (items []*CygxActivityVoiceReq, err error) {
	lenactivityIds := len(activityIds)
	if lenactivityIds == 0 {
		return
	}
	//活动音频，设置有效时间为30天，失效后该活动就不再支持音频回放。有效期起始时间为活动的开始时间
	//endTime := time.Now().AddDate(0, 0, -30).Format("2006-01-02 15:04:05")
	sql := `SELECT
			v.activity_id,
			v.activity_voice_id,
			v.voice_url AS url,
			v.voice_name AS name,
			v.voice_play_seconds AS play_seconds 
		FROM
			cygx_activity_voice AS v
			INNER JOIN cygx_activity AS a ON a.activity_id = v.activity_id 
		WHERE
			1 = 1  AND  v.is_hide = 0  	 AND  v.activity_id IN (` + utils.GetOrmInReplace(lenactivityIds) + `)  `
	o := orm.NewOrm()
	_, err = o.Raw(sql, activityIds).QueryRows(&items)
	return
}

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

// GetCygxActivityVoiceById 主键获取活动音频
func GetCygxActivityVoiceById(videoId int) (item *CygxActivityVoice, err error) {
	sql := `SELECT * FROM cygx_activity_voice WHERE activity_voice_id = ? LIMIT 1`
	err = orm.NewOrm().Raw(sql, videoId).QueryRow(&item)
	return
}

// 列表
func GetActivityVoiceListAll(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxActivityVideoListResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT art.*  FROM cygx_activity as art  INNER JOIN cygx_activity_voice 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
}

// GetCygxActivityVoiceByActivityId 主键获取活动音频
func GetCygxActivityVoiceByActivityId(activityId int) (item *CygxActivityVoice, err error) {
	sql := `SELECT * FROM cygx_activity_voice WHERE activity_id = ? LIMIT 1`
	err = orm.NewOrm().Raw(sql, activityId).QueryRow(&item)
	return
}

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

// 列表
func GetActivityVoiceList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxActivityVoiceResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT
	v.*,art.chart_permission_id,art.activity_time
FROM
	cygx_activity_voice 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 GetActivityVoiceActivityIds() (activityIds string, err error) {
	sql := `SELECT
				GROUP_CONCAT( DISTINCT a.activity_id SEPARATOR ',' ) AS activityids 
			FROM
				cygx_activity_voice AS a  WHERE  is_hide = 0  `
	o := orm.NewOrm()
	err = o.Raw(sql).QueryRow(&activityIds)
	return
}

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

// 修改
func UpdateCygxActivityVoice(item *CygxActivityVoice) (err error) {
	to := orm.NewOrm()
	updateParams := make(map[string]interface{})
	updateParams["BackgroundImg"] = item.BackgroundImg
	updateParams["ShareImg"] = item.ShareImg
	ptrStructOrTableName := "cygx_activity_voice"
	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
}
