package cygx

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

type CygxMicroRoadshowVideo struct {
	VideoId             int       `orm:"column(video_id);pk"description:"微路演视频id"`
	VideoName           string    `description:"视频名称"`
	ChartPermissionId   int       `description:"行业Id"`
	ChartPermissionName string    `description:"行业名称"`
	IndustryId          int       `description:"产业id"`
	IndustryName        string    `description:"产业名称"`
	PublishStatus       int       `description:"发布状态 1发布 0没有"`
	ModifyDate          string    `description:"更新时间"`
	PublishDate         time.Time `description:"发布时间"`
	VideoDuration       string    `description:"视频时长"`
	VideoCounts         int       `description:"播放量"`
	VideoUrl            string    `description:"视频地址"`
	CreateTime          string    `description:"创建时间"`
	ImgUrl              string    `description:"视频封面图"`
	ShareImgUrl         string    `description:"视频分享图"`
	DetailImgUrl        string    `description:"产业详情页背景图"`
	CommentNum          int       `description:"留言总数"`
	IsSendWxMsg         int       `description:"是否推送过微信模板消息，1是，0：否"`
}

type CygxMicroRoadshowVideoResp struct {
	VideoId             int    `orm:"column(video_id);pk"description:"微路演视频id"`
	VideoName           string `description:"视频名称"`
	ChartPermissionId   int    `description:"行业Id"`
	ChartPermissionName string `description:"行业名称"`
	IndustryId          int    `description:"产业id"`
	IndustryName        string `description:"产业名称"`
	PublishStatus       int    `description:"发布状态 1发布 0没有"`
	ModifyDate          string `description:"更新时间"`
	PublishDate         string `description:"发布时间"`
	VideoDuration       string `description:"视频时长"`
	VideoCounts         int    `description:"播放量"`
	VideoUrl            string `description:"视频地址"`
	CreateTime          string `description:"创建时间"`
	ImgUrl              string `description:"视频封面图"`
	ShareImgUrl         string `description:"视频分享图"`
	DetailImgUrl        string `description:"产业详情页背景图"`
	CommentNum          int    `description:"留言总数"`
}

type MicroRoadshowVideoListResp struct {
	Paging *paging.PagingItem
	List   []*CygxMicroRoadshowVideoResp
}

func GetMicroRoadshowVideoList(condition, sortStr string, pars []interface{}, startSize, pageSize int) (items []*CygxMicroRoadshowVideo, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT *,(SELECT COUNT(1) FROM cygx_article_comment AS h    WHERE h.industry_id=v.industry_id AND  h.activity_id = 0 ) AS comment_num FROM cygx_micro_roadshow_video as v  WHERE 1=1 `
	if condition != "" {
		sql += condition
	}

	if sortStr == "" {
		sortStr = "ORDER BY publish_date DESC "
	}

	sql += sortStr + ` LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetCygxMicroRoadshowVideoList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxMicroRoadshowVideo, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT * FROM cygx_micro_roadshow_video as v  WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetMicroRoadshowVideoListCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT COUNT(1) AS count FROM cygx_micro_roadshow_video WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

// 视频保存请求参数
type VideoAddReq struct {
	VideoId           int    `description:"视频Id"`
	VideoName         string `description:"视频标题"`
	VideoUrl          string `description:"视频地址"`
	VideoDuration     string `description:"视频时长"`
	ChartPermissionId int    `description:"行业id"`
	IndustryId        int    `description:"产业id"`
	ImgUrl            string `description:"视频封面图"`
	ShareImgUrl       string `description:"视频分享图"`
	DetailImgUrl      string `description:"产业详情页背景图"`
	PublishDate       string `description:"发布时间"`
	PublishOrSave     int    `description:"保存或发布 1发布 0保存"`
}

func GetMicroRoadshowVideoByTitle(title string) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT COUNT(1) AS count FROM cygx_micro_roadshow_video WHERE video_name =? `
	err = o.Raw(sql, title).QueryRow(&count)
	return
}

// 新增视频
func AddMicroRoadshowVideo(item *CygxMicroRoadshowVideo) (newId int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	newId, err = o.Insert(item)
	return
}

// 视频编辑请求参数
type VideoEditReq struct {
	VideoId             int    `description:"视频id"`
	VideoName           string `description:"视频标题"`
	VideoUrl            string `description:"视频地址"`
	VideoDuration       string `description:"视频时长"`
	ChartPermissionId   int    `description:"行业Id"`
	ChartPermissionName string `description:"行业名称"`
	IndustryId          int    `description:"产业id"`
	IndustryName        string `description:"产业名称"`
	PublishDate         string `description:"发布时间"`
}

// 新增视频
func EditVideo(item *CygxMicroRoadshowVideo) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := " UPDATE cygx_micro_roadshow_video SET " +
		" video_url = ?, video_name = ?,chart_permission_id=?, chart_permission_name=?, industry_id =?, industry_name =?, " +
		" modify_date=?, publish_date=?, video_duration=?, img_url=?, share_img_url=?, detail_img_url=?, publish_status=? WHERE video_id = ? "
	_, err = o.Raw(sql, item.VideoUrl, item.VideoName, item.ChartPermissionId, item.ChartPermissionName,
		item.IndustryId, item.IndustryName, item.ModifyDate, item.PublishDate, item.VideoDuration, item.ImgUrl, item.ShareImgUrl, item.DetailImgUrl, item.PublishStatus, item.VideoId).Exec()
	return
}

// 修改
func UpdateCygxMicroRoadshowVideo(item *CygxMicroRoadshowVideo) (err error) {
	to := orm.NewOrmUsingDB("hz_cygx")
	updateParams := make(map[string]interface{})
	updateParams["ShareImgUrl"] = item.ShareImgUrl
	ptrStructOrTableName := "cygx_micro_roadshow_video"
	whereParam := map[string]interface{}{"video_id": item.VideoId}
	qs := to.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	return
}

func PublishVideoCancel(videoId, publishOrCancle int) (err error) {

	if publishOrCancle == 0 {
		publishOrCancle = 3 // 取消发布状态
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_micro_roadshow_video SET publish_status=?,modify_date=NOW() WHERE video_id =?`
	_, err = o.Raw(sql, publishOrCancle, videoId).Exec()
	return
}

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

func GetMicroRoadshowVideoHistoryByIdPage(condition string, pars []interface{}, startSize, pageSize int) (item []*CygxMicroRoadshowVideoHistory, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT * FROM cygx_micro_roadshow_video_history WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY  create_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
	return
}

func GetMicroRoadshowVideoHistoryById(condition string, pars []interface{}) (item []*CygxMicroRoadshowVideoHistory, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT * FROM cygx_micro_roadshow_video_history WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY  create_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

//type CygxMicroRoadshowVoice struct {
//	ActivityVoiceId     int    `orm:"column(activity_voice_id);pk"description:"微路演视频id"`
//	ActivityId          string `description:"活动Id"`
//	VoiceName           string `description:"音频名称"`
//	ChartPermissionId   int    `description:"行业Id"`
//	ChartPermissionName string `description:"行业名称"`
//	IndustryId          int    `description:"产业id"`
//	IndustryName        string `description:"产业名称"`
//	ModifyDate          string `description:"更新时间"`
//	PublishDate         string `description:"发布时间"`
//	VoicePlaySeconds    string `description:"音频时长"`
//	VoiceCounts         int    `description:"播放量"`
//	VoiceUrl            string `description:"音频地址"`
//	CreateTime          string `description:"创建时间"`
//}

type CygxMicroRoadshowVoice struct {
	ActivityVoiceId     int       `description:"微路演音频id"`
	ActivityId          string    `description:"活动Id"`
	VoiceName           string    `description:"音频名称"`
	ChartPermissionId   int       `description:"行业Id"`
	ChartPermissionName string    `description:"行业名称"`
	IndustryId          int       `description:"产业id"`
	IndustryName        string    `description:"产业名称"`
	ModifyTime          string    `description:"更新时间"`
	PublishDate         string    `description:"发布时间"`
	VoicePlaySeconds    string    `description:"音频时长"`
	VoiceCounts         int       `description:"播放量"`
	VoiceUrl            string    `description:"音频地址"`
	CreateTime          string    `description:"创建时间"`
	Label               string    `description:"活动标签"`
	ActivityTypeId      string    `description:"活动类型Id"`
	ActivityTypeName    string    `description:"活动类型名称"`
	ActivityTime        time.Time `description:"活动类型时间"`
	ActivityTimeText    string    `description:"活动类型时间文字"`
	CommentNum          int       `description:"留言总数"`
}

type CygxMicroRoadshowVoiceRsep struct {
	ActivityVoiceId     int    `description:"微路演音频id"`
	ActivityId          string `description:"活动Id"`
	VoiceName           string `description:"音频名称"`
	ChartPermissionId   int    `description:"行业Id"`
	ChartPermissionName string `description:"行业名称"`
	IndustryId          int    `description:"产业id"`
	IndustryName        string `description:"产业名称"`
	ModifyTime          string `description:"更新时间"`
	PublishDate         string `description:"发布时间"`
	VoicePlaySeconds    string `description:"音频时长"`
	VoiceCounts         int    `description:"播放量"`
	VoiceUrl            string `description:"音频地址"`
	CreateTime          string `description:"创建时间"`
	Label               string `description:"活动标签"`
	ActivityTypeId      string `description:"活动类型Id"`
	ActivityTypeName    string `description:"活动类型名称"`
	ActivityTime        string `description:"活动类型时间"`
	ActivityTimeText    string `description:"活动类型时间文字"`
	CommentNum          int    `description:"留言总数"`
}

type MicroRoadshowVoiceListResp struct {
	Paging *paging.PagingItem
	List   []*CygxMicroRoadshowVoiceRsep
}

func GetMicroRoadshowVoiceList(condition, sortStr string, pars []interface{}, startSize, pageSize int) (items []*CygxMicroRoadshowVoice, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT
	v.*,
	a.label,
	a.activity_type_id,
	a.activity_type_name,
	a.activity_time,
	a.activity_time_text,
	(SELECT COUNT(1) FROM cygx_article_comment AS h    WHERE h.activity_id=a.activity_id AND  h.industry_id = 0 AND h.video_id = 0) AS comment_num,
	a.chart_permission_name 
FROM
	cygx_activity_voice AS v
	INNER JOIN cygx_activity AS a 
WHERE
	v.activity_id = a.activity_id `
	if condition != "" {
		sql += condition
	}

	if sortStr == "" {
		sortStr = "ORDER BY  a.activity_time DESC "
	}

	sql += sortStr + ` LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetMicroRoadshowVoiceListCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
	COUNT(1) AS count
FROM
	cygx_activity_voice AS v
	INNER JOIN cygx_activity AS a 
WHERE
	v.activity_id = a.activity_id `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

type PermissionNameAndIndustryName struct {
	PermissionName string
	PermissionId   int
	IndustryName   string
}

func GetPermissionNameAndIndustryNameByIndustryId(industryId int) (item *PermissionNameAndIndustryName, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
			cm.industry_name as industry_name,
			cm.chart_permission_id AS permission_id
		FROM
			 cygx_industrial_management AS cm 
		WHERE
		1=1 
			AND cm.industrial_management_id = ? `

	err = o.Raw(sql, industryId).QueryRow(&item)
	return
}

type MicroRoadshowVideoHistoryListResp struct {
	Paging *paging.PagingItem
	List   []*CygxMicroRoadshowVideoHistory
}

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

func GetMicroRoadshowVoiceHistoryById(condition string, pars []interface{}) (item []*CygxMicroRoadshowVoiceHistory, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` SELECT
	h.* 
FROM
	cygx_activity_voice_history AS h
	INNER JOIN cygx_activity_voice AS v 
WHERE
	1 = 1 
	AND v.activity_id = h.activity_id 
	 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY  create_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

func GetPermissionNameAndIndustryNameByChartPermissionId(ChartPermissionId int) (item *PermissionNameAndIndustryName, err error) {
	o := orm.NewOrm()
	sql := `SELECT
	chart_permission_id AS permission_id, 
	chart_permission_name AS permission_name
FROM
	chart_permission 
WHERE
	chart_permission_id = ? `

	err = o.Raw(sql, ChartPermissionId).QueryRow(&item)
	return
}

func GetMicroRoadshowVideoByIndustryId(industryId int) (item *CygxMicroRoadshowVideo, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * from cygx_micro_roadshow_video where industry_id = ? `
	err = o.Raw(sql, industryId).QueryRow(&item)
	return
}

func GetMicroRoadshowVideoByVideoId(videoId int) (item *CygxMicroRoadshowVideo, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * from cygx_micro_roadshow_video where video_id = ? `
	err = o.Raw(sql, videoId).QueryRow(&item)
	return
}

// 修改是否推送过模板消息的状态
func UpdateIsSendWxMsg(videoId, isSendWxMsg int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_micro_roadshow_video SET is_send_wx_msg=?  WHERE video_id =?`
	_, err = o.Raw(sql, isSendWxMsg, videoId).Exec()
	return
}

func GetMicroRoadshowVoiceById(activityId int) (item *CygxMicroRoadshowVoice, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
	v.*,
	a.label,
	a.activity_type_id,
	a.activity_type_name,
	a.activity_time,
	a.activity_time_text,
	a.chart_permission_name 
FROM
	cygx_activity_voice AS v
	INNER JOIN cygx_activity AS a 
WHERE
	v.activity_id = a.activity_id AND v.activity_id = ?`
	err = o.Raw(sql, activityId).QueryRow(&item)
	return
}
