package models

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

type CygxReportHistoryRecord struct {
	Id               int `orm:"column(id);pk"`
	ArticleId        int
	UserId           int
	CreateTime       time.Time
	Mobile           string    `description:"手机号"`
	Email            string    `description:"邮箱"`
	CompanyId        int       `description:"公司id"`
	CompanyName      string    `description:"公司名称"`
	ModifyTime       time.Time `description:"修改时间"`
	ReportType       string    `description:"报告类型，bgjx:报告精选、bzyjhz:本周研究汇总、szjyhz:上周纪要汇总"`
	RealName         string    `description:"用户实际名称"`
	SellerName       string    `description:"所属销售"`
	StopTime         int       `description:"停留时间"`
	OutType          int       `description:"退出方式，1正常退出，2强制关闭"`
	RegisterPlatform int       `description:"来源 1小程序，2：网页"`
}

type CygxReportHistoryRecordLog struct {
	Id               int `orm:"column(id);pk"`
	ArticleId        int
	UserId           int
	CreateTime       time.Time
	Mobile           string    `description:"手机号"`
	Email            string    `description:"邮箱"`
	CompanyId        int       `description:"公司id"`
	CompanyName      string    `description:"公司名称"`
	ModifyTime       time.Time `description:"修改时间"`
	ReportType       string    `description:"报告类型，bgjx:报告精选、bzyjhz:本周研究汇总、szjyhz:上周纪要汇总"`
	RealName         string    `description:"用户实际名称"`
	SellerName       string    `description:"所属销售"`
	StopTime         int       `description:"停留时间"`
	OutType          int       `description:"退出方式，1正常退出，2强制关闭"`
	RegisterPlatform int       `description:"来源 1小程序，2：网页"`
}

// 添加历史信息
func AddCygxReportHistoryRecord(item *CygxReportHistoryRecord) (lastId int64, err error) {
	o := orm.NewOrm()
	item.ModifyTime = time.Now()
	lastId, err = o.Insert(item)
	return
}

// 添加历史信息
func AddCygxReportHistoryRecordLog(item *CygxReportHistoryRecordLog) (lastId int64, err error) {
	o := orm.NewOrm()
	item.ModifyTime = time.Now()
	lastId, err = o.Insert(item)
	return
}

type ListPvResp struct {
	ArticleId int `description:"文章ID"`
	Pv        int `description:"pv"`
}

// 列表
func GetCygxReportHistoryRecordListPv(condition string, pars []interface{}) (items []*ListPvResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT  article_id ,COUNT(*)  as  pv  FROM cygx_report_history_record WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += " GROUP BY article_id "
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// 列表
func GetWxUserRaiCygxReportHistoryRecordCrm16_5() (items []*CygxReportHistoryRecord, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			*
		FROM
			cygx_report_history_record  WHERE report_type IN('bgjx','bzyjhz')  `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type CygxReportSelectionHistoryRecordResp struct {
	ArticleId        int    `description:"文章ID"`
	UserId           int    `description:"用户ID"`
	Mobile           string `description:"手机号"`
	Email            string `description:"邮箱"`
	CompanyId        int    `description:"公司id"`
	CompanyName      string `description:"公司名称"`
	RealName         string `description:"用户实际名称"`
	CreateTime       string `description:"创建时间"`
	RegisterPlatform int    `description:"来源 1小程序，2：网页"`
	RecordId         int    `description:"日志ID"`
}

// 列表
func GetCygxReportSelectionHistoryRecordRespLimit(condition string, pars []interface{}) (items []*CygxReportSelectionHistoryRecordResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			a.article_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_report_selection AS a
			INNER JOIN cygx_report_history_record AS b ON a.article_id = b.article_id   WHERE 1= 1  AND report_type = 'bgjx'  `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
