package user_view_statistics

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

// UserViewStatistics 用户报告报告阅读数量统计表
type UserViewStatistics struct {
	Id          int       `orm:"column(id)" description:"自增Id"`
	Mobile      string    `description:"手机号"`
	Email       string    `description:"手机号"`
	RealName    string    `description:"联系人名称"`
	CompanyName string    `description:"客户名称"`
	ViewNum     int       `description:"阅读总数"`
	Date        time.Time `description:"阅读日期"`
	CreateTime  time.Time `description:"添加时间"`
}

// UserViewMobileTotalSlice 根据用户手机号字符串获取用户的浏览数
type UserViewMobileTotalSlice struct {
	Mobile      string `description:"用户手机号"`
	CompanyName string `description:"客户名称"`
	RealName    string `description:"用户名称"`
	Total       int    `description:"总阅读数"`
}

// GetUserViewListByDate 根据日期获取用户阅读数
func GetUserViewListByDate(dayStr string) (list []*UserViewMobileTotalSlice, err error) {
	o := orm.NewOrm()
	sql := `SELECT mobile,sum(view_num) total
			FROM user_view_statistics
			WHERE 1=1 and date >= ? and mobile !="" group by mobile`
	_, err = o.Raw(sql, dayStr).QueryRows(&list)
	return
}

// UserViewStatisticsInfo 根据用户手机号字符串获取用户的浏览数和最晚阅读次数
type UserViewStatisticsInfo struct {
	Mobile       string    `description:"用户手机号"`
	Total        int       `description:"总阅读数"`
	LastViewTime time.Time `description:"用户浏览时间"`
}

// GetUserViewStatisticsByMobile 根据手机号获取联系人的浏览次数
func GetUserViewStatisticsByMobile(mobile string) (item *UserViewStatisticsInfo, err error) {
	o := orm.NewOrm()
	sql := `SELECT mobile,sum(view_num) total,max(last_view_time) last_view_time FROM  user_view_statistics  WHERE mobile = ? `
	err = o.Raw(sql, mobile).QueryRow(&item)
	return
}
