package models

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

type CygxArticleCollect struct {
	Id              int `orm:"column(id);pk"`
	ArticleId       int
	ActivityVoiceId int
	ActivityVideoId int
	VideoId         int
	UserId          int
	CreateTime      time.Time
	ModifyTime      time.Time `description:"更新时间"`
	Mobile          string    `description:"手机号"`
	Email           string    `description:"邮箱"`
	CompanyId       int       `description:"公司id"`
	CompanyName     string    `description:"公司名称"`
	RealName        string    `description:"用户实际名称"`
}

// 添加收藏信息
func AddCygxArticleCollect(item *CygxArticleCollect) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

type ArticleCollectReq struct {
	ArticleId int `description:"报告id"`
}

type ArticleCollectResp struct {
	Status       int `description:"1:收藏，2：取消收藏"`
	CollectCount int `description:"收藏总数"`
}

func RemoveArticleCollect(userId, articleId int) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM cygx_article_collect WHERE user_id=? AND article_id=? `
	_, err = o.Raw(sql, userId, articleId).Exec()
	return
}

func GetArticleCollectUsersCount(articleId int) (count int, err error) {
	sql := `SELECT COUNT(user_id) AS count FROM cygx_article_collect WHERE article_id=? `
	err = orm.NewOrm().Raw(sql, articleId).QueryRow(&count)
	return
}

func GetArticleCollectCount(userId, articleId int) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM cygx_article_collect WHERE user_id=? AND article_id=? `
	err = orm.NewOrm().Raw(sql, userId, articleId).QueryRow(&count)
	return
}

type ArticleCollectList struct {
	Id              int `orm:"column(id);pk"`
	ArticleId       int
	UserId          int
	CreateTime      time.Time
	Title           string `description:"标题"`
	TitleEn         string `description:"英文标题 "`
	UpdateFrequency string `description:"更新周期"`
	CreateDate      string `description:"创建时间"`
	PublishDate     string `description:"发布时间"`
	Body            string `description:"内容"`
	Abstract        string `description:"摘要"`
	CategoryName    string `description:"一级分类"`
	SubCategoryName string `description:"二级分类"`
}

func GetCygxArticleCollectList(condition string) (items []*CygxArticleCollect, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_article_collect  WHERE 1 =1  AND article_id > 0  ` + condition + ` GROUP BY user_id  `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// GetCygxArticleCollectByUser 根据用户ID获取所有文章收藏
func GetCygxArticleCollectByUser(userId int) (items []*CygxArticleCollect, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_article_collect  WHERE 1 =1  AND article_id > 0 AND  user_id =?  `
	_, err = o.Raw(sql, userId).QueryRows(&items)
	return
}

// 修改用户收藏文章的相关信息
func UpdateCygxArticleCollect(wxUser *WxUserItem) (err error) {
	o := orm.NewOrm()
	var sql string
	if wxUser.Mobile != "" {
		sql = `UPDATE cygx_article_collect 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_article_collect SET user_id=?,company_id=?,company_name=?,mobile=?,real_name=? WHERE email=? `
		_, err = o.Raw(sql, wxUser.UserId, wxUser.CompanyId, wxUser.CompanyName, wxUser.Mobile, wxUser.RealName, wxUser.Email).Exec()
	}
	return
}

type CygxArticleCollectCountRep struct {
	ArticleId int `description:"文章ID"`
	Num       int `description:"数量"`
}

// 获取文章被收藏的数量
func GetUserArticleCollectList() (items []*CygxArticleCollectCountRep, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			COUNT( 1 ) AS num,
			f.article_id 
		FROM
			cygx_article_collect AS f
			INNER JOIN cygx_article AS a ON a.article_id = f.article_id 
		WHERE
			1 = 1 
		GROUP BY
			f.article_id 
		ORDER BY
			num DESC 
			LIMIT 30 `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 修改文章收藏的数量
func UpdateArticleCollectCountNum(num, articleId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_article SET user_collection_num = ? WHERE article_id = ?`
	_, err = o.Raw(sql, num, articleId).Exec()
	return
}

type CygxArticleNum struct {
	ArticleId  int  `description:"文章ID"`
	IsCollect  bool `description:"本人是否收藏"`
	Pv         int  `description:"PV"`
	CollectNum int  `description:"收藏人数"`
}

// GetArticleCollectNum 根据文章ID获取收藏数量的列表
func GetArticleCollectNum(articleId []string, uid int) (items []*CygxArticleNum, err error) {
	lenarticleId := len(articleId)
	if lenarticleId == 0 {
		return
	}
	sql := `SELECT
			a.article_id,
			( SELECT count( 1 ) FROM cygx_article_history_record_newpv AS h WHERE h.article_id = a.article_id ) AS pv,
			( SELECT count( 1 ) FROM cygx_article_collect AS ac   WHERE ac.article_id = a.article_id  ) AS collect_num, 
			( SELECT count( 1 ) FROM cygx_article_collect AS ac   WHERE ac.article_id = a.article_id AND DATE_SUB( CURDATE(), INTERVAL 30 DAY ) <= date( ac.create_time )  ) AS collect_num_order, 
			( SELECT count( 1 ) FROM cygx_article_collect AS ac WHERE ac.article_id = a.article_id  AND user_id = ? ) AS is_collect
		FROM
			cygx_article AS a WHERE  1 = 1  AND  article_id IN  (` + utils.GetOrmInReplace(lenarticleId) + `) `
	o := orm.NewOrm()
	_, err = o.Raw(sql, uid, articleId).QueryRows(&items)
	return
}

// GetArticleCollectNum 根据文章ID获取收藏数量的列表
func GetArticleCollectListNum(articleIds []int) (items []*CygxArticleNum, err error) {
	lenArr := len(articleIds)
	if lenArr == 0 {
		return
	}
	sql := `SELECT  COUNT(1) as collect_num , article_id  FROM cygx_article_collect  WHERE   article_id IN  (` + utils.GetOrmInReplace(lenArr) + `)   GROUP BY article_id `
	o := orm.NewOrm()
	_, err = o.Raw(sql, articleIds).QueryRows(&items)
	return
}

type MicroRoadshowCollectList struct {
	AudioIds         string
	VideoIds         string
	ActivityVideoIds string
}

func GetVideoCollectCount(userId, videoId int) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM cygx_article_collect WHERE user_id=? AND video_id=? `
	err = orm.NewOrm().Raw(sql, userId, videoId).QueryRow(&count)
	return
}

func RemoveVideoCollect(userId, videoId int) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM cygx_article_collect WHERE user_id=? AND video_id=? `
	_, err = o.Raw(sql, userId, videoId).Exec()
	return
}

func GetVideoCollectUsersCount(videoId int) (count int, err error) {
	sql := `SELECT COUNT(user_id) AS count FROM cygx_article_collect WHERE video_id=? `
	err = orm.NewOrm().Raw(sql, videoId).QueryRow(&count)
	return
}

func GetVoiceCollectCount(userId, voiceId int) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM cygx_article_collect WHERE user_id=? AND activity_voice_id=? `
	err = orm.NewOrm().Raw(sql, userId, voiceId).QueryRow(&count)
	return
}

func RemoveVoiceCollect(userId, voiceId int) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM cygx_article_collect WHERE user_id=? AND activity_voice_id=? `
	_, err = o.Raw(sql, userId, voiceId).Exec()
	return
}

func GetVoiceCollectUsersCount(voiceId int) (count int, err error) {
	sql := `SELECT COUNT(user_id) AS count FROM cygx_article_collect WHERE activity_voice_id=? `
	err = orm.NewOrm().Raw(sql, voiceId).QueryRow(&count)
	return
}

func GetActivityVideoCollectCount(userId, activityVideoId int) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM cygx_article_collect WHERE user_id=? AND activity_video_id=? `
	err = orm.NewOrm().Raw(sql, userId, activityVideoId).QueryRow(&count)
	return
}

func RemoveActivityVideoCollect(userId, activityVideoId int) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM cygx_article_collect WHERE user_id=? AND activity_video_id=? `
	_, err = o.Raw(sql, userId, activityVideoId).Exec()
	return
}

func GetActivityVideoCollectUsersCount(videoId int) (count int, err error) {
	sql := `SELECT COUNT(user_id) AS count FROM cygx_article_collect WHERE activity_video_id=? `
	err = orm.NewOrm().Raw(sql, videoId).QueryRow(&count)
	return
}

func GetActivityVoiceCollectCount(userId, activityId int) (count int, err error) {
	sql := `SELECT
	COUNT( 1 ) AS count 
FROM
	cygx_article_collect AS c
	INNER JOIN cygx_activity_voice AS vc 
WHERE
	c.user_id =? 
	AND vc.activity_id =? 
	AND c.activity_voice_id = vc.activity_voice_id`
	err = orm.NewOrm().Raw(sql, userId, activityId).QueryRow(&count)

	return
}

func GetActivityVideoCollectCountByActivityId(userId, activityId int) (count int, err error) {
	sql := `SELECT
	COUNT( 1 ) AS count 
FROM
	cygx_article_collect AS c
	INNER JOIN cygx_activity_video AS vd 
WHERE
	c.user_id =? 
	AND vd.activity_id =? 
	AND c.activity_video_id = vd.video_id`
	err = orm.NewOrm().Raw(sql, userId, activityId).QueryRow(&count)
	return
}
