package models

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

type CygxUserInteractionNum struct {
	Id                  int       `orm:"column(id);pk"`
	UserId              int       `description:"用户ID"`
	ArticleHistoryNum   int       `description:"文章浏览数量"`
	ArticleCountNum     int       `description:"文章收藏数量"`
	ChartCountNum       int       `description:"图表收藏"`
	IndustryFllowNum    int       `description:"产业关注数量"`
	DepartmentFollowNum int       `description:"作者关注数量"`
	KeyWordNum          int       `description:"关键词搜索次数"`
	ActivityOnLineNum   int       `description:"线上活动报名次数"`
	ActivityOfficeNum   int       `description:"线下活动报名次数"`
	ActivityNum         int       `description:"活动报名次数"`
	TripNum             int       `description:"专项产业调研数量"`
	RoadshowVideoNum    int       `description:"产业视频播放量"`
	ActivityVideoNum    int       `description:"活动视频播放量"`
	ActivityVoiceNum    int       `description:"活动音频播放量"`
	RoadshowNum         int       `description:"路演播放总数量"`
	TagNum              int       `description:"标签点击次数"`
	CreateTime          time.Time `description:"创建时间"`
	ModifyTime          time.Time `description:"更新时间"`
}

// 添加
func AddCygxUserInteractionNum(item *CygxUserInteractionNum) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

// 修改
func UpdateCygxUserInteractionNum(item *CygxUserInteractionNum) (err error) {
	updateParams := make(map[string]interface{})
	updateParams["UserId"] = item.UserId
	updateParams["ArticleHistoryNum"] = item.ArticleHistoryNum
	updateParams["ArticleCountNum"] = item.ArticleCountNum
	updateParams["ChartCountNum"] = item.ChartCountNum
	updateParams["IndustryFllowNum"] = item.IndustryFllowNum
	updateParams["DepartmentFollowNum"] = item.DepartmentFollowNum
	updateParams["KeyWordNum"] = item.KeyWordNum
	updateParams["ActivityOnLineNum"] = item.ActivityOnLineNum
	updateParams["ActivityOfficeNum"] = item.ActivityOfficeNum
	updateParams["ActivityNum"] = item.ActivityNum
	updateParams["TripNum"] = item.TripNum
	updateParams["RoadshowVideoNum"] = item.RoadshowVideoNum
	updateParams["ActivityVideoNum"] = item.ActivityVideoNum
	updateParams["ActivityVoiceNum"] = item.ActivityVoiceNum
	updateParams["RoadshowNum"] = item.RoadshowNum
	updateParams["TagNum"] = item.TagNum
	updateParams["CreateTime"] = time.Now()
	updateParams["ModifyTime"] = time.Now()
	whereParam := map[string]interface{}{"user_id": item.UserId}
	err = UpdateByExpr(CygxUserInteractionNum{}, whereParam, updateParams)
	return
}

// 获取列表
func GetCygxUserInteractionNumList() (items []*CygxUserInteractionNum, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_user_interaction_num   `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 批量添加用户互动信息
func AddCygxUserInteractionNumList(items []*CygxUserInteractionNum) (lastId int64, err error) {
	o := orm.NewOrm()
	_, err = o.InsertMulti(1, items)
	return
}

// 联系人互动数量
type CygxUserInteractionNumResp struct {
	UserId         int `description:"用户ID"`
	CompanyId      int `description:"公司ID"`
	InteractionNum int `description:"互动数量"`
}

// 用户阅读记录数量统计
func GetCygxArticleHistoryRecordInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_article_and_yanxuan_record AS h
			INNER JOIN cygx_article AS art ON art.article_id = h.source_id 
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1 AND h.source = 'article'   ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户收藏文章数量统计
func GetCygxArticleCollectInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_article_collect AS h
			INNER JOIN cygx_article AS art ON art.article_id = h.article_id
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户收藏图表数量统计
func GetCygxChartCollectInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_chart_collect AS h
			INNER JOIN cygx_chart_all AS art ON art.chart_id = h.chart_id
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户关注产业数量统计
func GetCygxIndustryFllowInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_industry_fllow AS h
			INNER JOIN cygx_industrial_management AS art ON art.industrial_management_id = h.industrial_management_id
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  AND h.type = 1  AND  h.source   IN (0,1,2) ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户搜索关键词数量统计
func GetCygxSearchKeyWordInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_search_key_word AS h
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户活动报名参会数量统计
func GetCygxActivitySignupInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_activity_signup_detail AS h
			INNER JOIN cygx_activity AS art ON art.activity_id = h.activity_id
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户专项调研活动报名参会数量统计
func GetCygxActivitySpecialSignupInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_activity_special_meeting_detail AS h
			INNER JOIN cygx_activity_special AS art ON art.activity_id = h.activity_id
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户路演精华播放数量统计
func GetCygxMicroRoadshowVideoHistoryInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_micro_roadshow_video_history AS h
			INNER JOIN cygx_micro_roadshow_video AS art ON art.video_id = h.video_id
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户活动视频播放数量统计
func GetCygxActivityVideoHistoryInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_activity_video_history AS h
			INNER JOIN cygx_activity_video AS art ON art.activity_id = h.activity_id
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户活动音频播放数量统计
func GetCygxActivityVoiceHistoryInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_activity_voice_history AS h
			INNER JOIN cygx_activity_voice AS art ON art.activity_id = h.activity_id
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户首页标签点击量数量统计
func GetCygxTagHistoryInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_tag_history AS h
			INNER JOIN cygx_tag AS art ON art.tag_id = h.tag_id
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户关注研选作者数量统计
func GetCygxArticleDepartmentFollowInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_article_department_follow AS h
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户收藏研选专栏数量统计
func GetCygxYanxuanSpecialCollectInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_yanxuan_special_collect AS h
			INNER JOIN cygx_yanxuan_special AS art ON art.id = h.yanxuan_special_id
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户关注研选专栏作者数量统计
func GetCygxYanxuanSpecialFollowInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_yanxuan_special_follow AS h
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 用户研选专栏阅读数量数量统计
func GetCygxYanxuanSpecialRecordInteractionNum(condition string) (items []*CygxUserInteractionNumResp, err error) {
	o := orm.NewOrm()
	var sql string
	sql += ` SELECT
			COUNT( 1 ) AS interaction_num,
			h.user_id,
			h.company_id 
		FROM
			cygx_yanxuan_special_record AS h
			INNER JOIN cygx_yanxuan_special AS art ON art.id = h.yanxuan_special_id
			INNER JOIN %s.wx_user AS w ON w.user_id = h.user_id  WHERE 1=1  ` + condition + ` GROUP BY w.user_id `
	databaseName := utils.GetWeeklyDatabase()
	sql = fmt.Sprintf(sql, databaseName)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}
