package cygx

import (
	"github.com/beego/beego/v2/client/orm"
	"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:"音频时长"`
	BackgroundImg    string    `description:"封面图片"`
	ShareImg         string    `description:"分享图片"`
	IsByHand         int       `description:"是否手动修改过"`
	FileType         int       `description:"文件类型，1：路演回放，2：调研反馈"`
	CreateTime       time.Time `description:"创建时间"`
}

// ActivityVoiceReq 音频数据
type CygxActivityVoiceReq struct {
	Url             string `description:"音频资源url地址"`
	Name            string `description:"音频名称"`
	PlaySeconds     string `description:"音频时长"`
	ActivityVoiceId int    `description:"活动音频ID"`
	BackgroundImg   string `description:"封面图片"`
	ShareImg        string `description:"分享图片"`
}

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

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

// 获取数量
func GetCygxActivityVoiceCount(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_activity_voice  WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 列表
func GetCygxActivityVoiceReqList(activityId int) (items []*CygxActivityVoiceReq, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT 
			background_img,
			share_img,
			activity_voice_id,
			voice_url AS url,
			voice_name AS name,
			voice_play_seconds AS play_seconds  FROM cygx_activity_voice  WHERE activity_id= ? `
	_, err = o.Raw(sql, activityId).QueryRows(&items)
	return
}

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

// 列表
func GetActivityVoiceListNew(condition string, pars []interface{}) (items []*CygxActivityVoice, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT
	v.*
FROM
	cygx_activity_voice as v  
	INNER JOIN cygx_activity as  a ON a.activity_id = v.activity_id
WHERE
	1= 1 
	AND a.activity_time > '2023-09-22'
	AND  v.voice_name NOT LIKE '%调研反馈%'
	AND  v.voice_name NOT LIKE '%【%'  `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func GetCygxActivityVoiceReqDetail(activityId int) (item *CygxActivityVoice, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT *  FROM cygx_activity_voice  WHERE activity_id= ? `
	err = o.Raw(sql, activityId).QueryRow(&item)
	return
}

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

// 根据音频ID获取详情
func GetCygxActivityVoiceReqDetailByActivityVoiceId(activityVoiceId int) (item *CygxActivityVoice, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT *  FROM cygx_activity_voice  WHERE activity_voice_id= ? `
	err = o.Raw(sql, activityVoiceId).QueryRow(&item)
	return
}

// 根据活动ID获取详情
func GetCygxActivityVoiceReqDetailByActivityId(activityId int) (item *CygxActivityVoice, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT *  FROM cygx_activity_voice  WHERE activity_id= ? `
	err = o.Raw(sql, activityId).QueryRow(&item)
	return
}
