package models

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

type CygxActivityVoiceHistory struct {
	Id               int       `orm:"column(id);pk"`
	ActivityId       int       `description:"活动ID"`
	UserId           int       `description:"用户ID"`
	CreateTime       time.Time `description:"创建时间"`
	Mobile           string    `description:"手机号"`
	Email            string    `description:"邮箱"`
	CompanyId        int       `description:"公司id"`
	CompanyName      string    `description:"公司名称"`
	RealName         string    `description:"用户实际名称"`
	SellerName       string    `description:"所属销售"`
	PlaySeconds      string    `description:"播放时间 单位s"`
	ModifyTime       time.Time `description:"视频修改时间"`
	RegisterPlatform int       // 来源 1小程序，2：网页
}

// 添加
func AddCygxActivityVoiceHistory(item *CygxActivityVoiceHistory) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(item)
	return
}

func GetLastCygxActivityVoiceHistory(activityId, userId int) (item *CygxActivityVoiceHistory, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM cygx_activity_voice_history WHERE activity_id=?  AND user_id=? AND register_platform = 1 ORDER BY create_time DESC limit 1 `
	err = o.Raw(sql, activityId, userId).QueryRow(&item)
	return
}

func UpdateLastCygxActivityVoiceHistory(playSeconds string, lastId int) (err error) {
	o := orm.NewOrm()
	sql := ` UPDATE cygx_activity_voice_history SET play_seconds = play_seconds +? WHERE id=? `
	_, err = o.Raw(sql, playSeconds, lastId).Exec()
	return
}

// 获取列表信息根据手机号分组
func GetActivityVoiceHistoryByMobileList(condition string) (items []*CygxActivityVoiceHistory, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_activity_voice_history  WHERE 1 =1  ` + condition + `  GROUP BY user_id  `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 修改用户浏览活动音频的相关信息
func UpdateCygxActivityVoiceHistory(wxUser *WxUserItem) (err error) {
	o := orm.NewOrm()
	var sql string
	if wxUser.Mobile != "" {
		sql = `UPDATE cygx_activity_voice_history SET email=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE mobile=? `
		_, err = o.Raw(sql, wxUser.Email, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Mobile).Exec()
	} else if wxUser.Email != "" {
		sql = `UPDATE cygx_activity_voice_history SET mobile=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE email=? `
		_, err = o.Raw(sql, wxUser.Mobile, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Email).Exec()
	}
	return
}

// 获取列表信息根据手机号分组
func GetActivityVoiceHistoryByMobileListCRM_16_5Init(condition string) (items []*CygxActivityVoiceHistory, err error) {
	o := orm.NewOrm()
	sql := `SELECT mobile,activity_id ,MAX(create_time)  as  create_time  FROM cygx_activity_voice_history  WHERE 1 =1  ` + condition + `  GROUP BY mobile ,activity_id `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type CygxActivityVoiceHistoryResp struct {
	ActivityVoiceId  int    `orm:"column(activity_voice_id);pk" description:"活动音频ID"`
	VoiceName        string `description:"音频名称"`
	UserId           int    `description:"用户ID"`
	Mobile           string `description:"手机号"`
	Email            string `description:"邮箱"`
	CompanyId        int    `description:"公司id"`
	CompanyName      string `description:"公司名称"`
	RealName         string `description:"用户实际名称"`
	CreateTime       string `description:"创建时间"`
	ActivityId       int    `description:"活动ID"`
	RegisterPlatform int    `description:"来源 1小程序，2：网页"`
	RecordId         int    `description:"日志ID"`
}

// 列表
func GetCygxActivityVoiceHistoryListNoLimit(condition string, pars []interface{}) (items []*CygxActivityVoiceHistoryResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			a.voice_name,
			a.activity_voice_id,
			a.activity_id,
			b.user_id,
			b.id as record_id,
			b.real_name,
			b.mobile,
			b.email,
			b.company_id,
			b.company_name,
			b.create_time 
		FROM
			cygx_activity_voice AS a
			INNER JOIN cygx_activity_voice_history AS b ON a.activity_voice_id = b.activity_voice_id    WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
