package models

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

type CygxYanxuanSpecialRecord struct {
	CygxYanxuanSpecialRecordId int       `orm:"column(cygx_yanxuan_special_record_id);pk"`
	UserId                     int       // 用户ID
	Mobile                     string    // 手机号
	Email                      string    // 邮箱
	CompanyId                  int       // 公司ID
	CompanyName                string    // 公司名称
	RealName                   string    // 用户实际名称
	SellerName                 string    // 所属销售
	CreateTime                 time.Time // 创建时间
	ModifyTime                 time.Time // 修改时间
	RegisterPlatform           int       // 来源 1小程序,2:网页
	YanxuanSpecialId           int       // cygx_yanxuan_special 表主键ID
	StopTime                   int       // 停留时间
	PermissionCode             int       // 权限状态,1:有权限,0:无权限
}

type CygxYanxuanSpecialRecordResp struct {
	CygxYanxuanSpecialRecordId int       `orm:"column(cygx_yanxuan_special_record_id);pk"`
	UserId                     int       // 用户ID
	Mobile                     string    // 手机号
	Email                      string    // 邮箱
	CompanyId                  int       // 公司ID
	CompanyName                string    // 公司名称
	RealName                   string    // 用户实际名称
	SellerName                 string    // 所属销售
	CreateTime                 string    // 创建时间
	ModifyTime                 time.Time // 修改时间
	RegisterPlatform           int       // 来源 1小程序,2:网页
	YanxuanSpecialId           int       // cygx_yanxuan_special 表主键ID
	StopTime                   int       // 停留时间
}

func AddCygxYanxuanSpecialRecord(item *CygxYanxuanSpecialRecord) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

type AddCygxYanxuanSpecialRecordReq struct {
	SpecialId int `description:"专栏文章id"`
	StopTime  int `description:"停留时间"`
}

// 获取数量
func GetCygxYanxuanSpecialRecordCount(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_yanxuan_special_record as art  WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 获取数量
func GetCygxYanxuanSpecialRecordCountGroup(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT	 count(*) AS count FROM
	( SELECT COUNT(*) FROM cygx_yanxuan_special_record AS art WHERE 1 = 1 ` + condition + ` GROUP BY user_id ) f `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 判断一个用户是否阅读过 某一篇研选专栏
func GetCygxYanxuanSpecialRecordCountByUser(userId, yanxuanSpecialId int) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_yanxuan_special_record as a  WHERE user_id= ?    AND yanxuan_special_id  = ? `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, userId, yanxuanSpecialId).QueryRow(&count)
	return
}

// 详细日志记录,不过滤时长小于 3 秒的那种
type CygxYanxuanSpecialRecordLog struct {
	CygxYanxuanSpecialRecordId int       `orm:"column(cygx_yanxuan_special_record_id);pk"`
	UserId                     int       // 用户ID
	Mobile                     string    // 手机号
	Email                      string    // 邮箱
	CompanyId                  int       // 公司ID
	CompanyName                string    // 公司名称
	RealName                   string    // 用户实际名称
	SellerName                 string    // 所属销售
	CreateTime                 time.Time // 创建时间
	ModifyTime                 time.Time // 修改时间
	RegisterPlatform           int       // 来源 1小程序,2:网页
	YanxuanSpecialId           int       // cygx_yanxuan_special 表主键ID
	StopTime                   int       // 停留时间
	PermissionCode             int       // 权限状态,1:有权限,0:无权限
}

func AddCygxYanxuanSpecialRecordLog(item *CygxYanxuanSpecialRecordLog) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

// 列表
func GetCygxYanxuanSpecialRecordRespList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxYanxuanSpecialRecordResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_yanxuan_special_record as art WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?  `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type LisYanxuanSpecialRecordPvResp struct {
	YanxuanSpecialId int `description:"文章ID"`
	Pv               int `description:"pv"`
	Uv               int `description:"pv"`
}

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