package models

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

type CygxCompanyInteractionNum struct {
	Id                       int       `orm:"column(id);pk"`
	CompanyId                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:"标签点击次数"`
	YanxuanSpecialNum        int       `description:"研选专栏阅读数量"`
	YanxuanSpecialCollectNum int       `description:"研选专栏收藏数量"`
	YanxuanSpecialFollowNum  int       `description:"研选专栏作者关注数量"`
	CreateTime               time.Time `description:"创建时间"`
	ModifyTime               time.Time `description:"更新时间"`
}

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

// 修改
func UpdateCygxCompanyInteractionNum(item *CygxCompanyInteractionNum) (err error) {
	updateParams := make(map[string]interface{})
	updateParams["CompanyId"] = item.CompanyId
	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{}{"company_id": item.CompanyId}
	err = UpdateByExpr(CygxCompanyInteractionNum{}, whereParam, updateParams)
	return
}

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

// 批量添加企业互动信息
func AddCygxCompanyInteractionNumList(items []*CygxCompanyInteractionNum) (lastId int64, err error) {
	o := orm.NewOrm()
	_, err = o.InsertMulti(len(items), items)
	return
}

// UpdateActivitySignupDetailMulti 批量修改
func UpdateCygxCompanyInteractionNumMulti(items []*CygxCompanyInteractionNum) (err error) {
	o := orm.NewOrm()
	p, err := o.Raw(` UPDATE cygx_company_interaction_num SET 
							article_history_num = ?,
							article_count_num = ?,
							industry_fllow_num = ?,
							department_follow_num = ?,
							key_word_num = ?,
							activity_num = ?,
							chart_count_num = ?,
							trip_num = ?,
							roadshow_video_num = ?,
							activity_video_num = ?,
							activity_voice_num = ?,
							roadshow_num = ?,
							tag_num = ?
							WHERE company_id = ? `).Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(
			v.ArticleHistoryNum,
			v.ArticleCountNum,
			v.ChartCountNum,
			v.IndustryFllowNum,
			v.DepartmentFollowNum,
			v.KeyWordNum,
			v.ActivityNum,
			v.TripNum,
			v.RoadshowVideoNum,
			v.ActivityVideoNum,
			v.ActivityVoiceNum,
			v.RoadshowNum,
			v.TagNum,
			v.CompanyId)
		if err != nil {
			return
		}
	}
	return
}

// 企业互动量
type CompanyInteractionNumReq struct {
	CompanyId            int `description:"用户ID"`
	InteractionNum       int `description:"互动总量"`
	InteractionNumSeller int `description:"销售可以查看的互动总量"`
}

// UpdateCompanyInteractionNumMulti 批量修改客户互动量
func UpdateCompanyInteractionNumMulti(items []*CompanyInteractionNumReq) (err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	p, err := o.Raw(` UPDATE company SET interaction_num = ? ,  interaction_num_seller = ?  WHERE company_id=? `).Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range items {
		_, err = p.Exec(
			v.InteractionNum,
			v.InteractionNumSeller,
			v.CompanyId)
		if err != nil {
			return
		}
	}
	return
}
