package yb

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

// 研报-用户收藏表
type MyChart struct {
	MyChartId         int       `orm:"column(my_chart_id);pk"`
	MyChartClassifyId int       `orm:"column(my_chart_classify_id)"` //图表分类ID
	ChartInfoId       int       `orm:"column(chart_info_id)"`        //图表ID
	ChartName         string    `orm:"column(chart_name)"`           //图表名称
	UniqueCode        string    `orm:"column(unique_code)"`          //图表唯一编码
	ChartImage        string    `orm:"column(chart_image)"`          //图表图片
	ReportId          int       `orm:"column(report_id)"`            //报告ID(从哪个报告收藏的)
	ReportChapterId   int       `orm:"column(report_chapter_id)"`    //报告章节ID
	Source            int       `orm:"column(source)"`               //操作来源：1-ETA图库；2-ETA表格
	UserId            uint      `orm:"column(user_id)"`              //用户ID
	CompanyId         uint      `orm:"column(company_id)"`           //客户ID
	CompanyStatus     string    `orm:"column(company_status)"`       //客户状态
	CompanyName       string    `orm:"column(company_name)"`         //客户名称
	RealName          string    `orm:"column(real_name)"`            //联系人姓名
	SellerId          int       `orm:"column(seller_id)"`            //客户销售ID
	CreateTime        time.Time `orm:"column(create_time)"`          //创建时间
	ModifyTime        time.Time `orm:"column(modify_time)"`          //修改时间
}
// TableName get sql table name.获取数据库表名
func (l *MyChart) TableName() string {
	return "yb_my_chart"
}

//  联系人收藏统计
type SellerChartCollectCount struct {
	Num      int `json:"num"`
	SellerId int `json:"seller_id"`
}

//  收藏统计
type SellerChartCollectCountListItem struct {
	Num           int
	UserId        int
	RealName      string
	CompanyId     int
	CompanyStatus string
	CompanyName   string
}

// GetSellerChartCollectCountByCompanyStatus 获取销售下的联系人收藏次数
func GetSellerChartCollectCountByCompanyStatus(companyStatus string, startDate, endDate time.Time) (list []*SellerChartCollectCount, err error) {
	o := orm.NewOrm()
	pars := make([]interface{}, 0)
	sql := `SELECT COUNT(*) AS num, seller_id FROM yb_my_chart WHERE company_status =? and create_time >= ? and create_time <= ? GROUP BY seller_id`
	pars = append(pars, companyStatus, startDate, endDate)
	_, err = o.Raw(sql, pars).QueryRows(&list)
	return
}

type SellerChartCollectUserLogReq struct {
	PageSize      int    `description:"每页数据条数"`
	CurrentIndex  int    `description:"当前页页码，从1开始"`
	SortParam     string `description:"排序字段参数，用来排序的字段， 枚举值：'viewTotal':总阅读次数 、 'viewTime':阅读时间 、 'roadShowTotal':累计路演次数 、'expireDay'：到期时间 、 'createTime'：创建时间 、 'formalTime': 转正时间 、 'freezeTime'：冻结时间 、'lossTime'：流失时间" 、'deadline'：距离未完成的任务的截止日期的天数、'ybUserLog': 按照用户的点击量`
	SortType      string `description:"如何排序，是正序还是倒序，枚举值：asc 正序,desc 倒叙 "`
	SellerIds     string `description:"销售ID, 用','拼接 "`
	CompanyStatus string `description:"客户状态"`
	StartDate     string `description:"起始时间"`
	EndDate       string `description:"截止时间"`
}

// GetSellerUserChartCollectCountByCompanyStatus 获取销售下用户图表收藏记录汇总数
func GetSellerUserChartCollectCountByCompanyStatus(companyStatus string, startDate, endDate time.Time, sellerIds string, startSize, pageSize int, orderStr string) (list []*SellerChartCollectCountListItem, total int64, err error) {
	o := orm.NewOrm()
	pars := make([]interface{}, 0)

	sql := `SELECT COUNT(*) AS num, user_id, real_name, company_id, company_status, company_name FROM yb_my_chart WHERE company_status =? and create_time >= ? and create_time <= ? and seller_id in `+sellerIds+` GROUP BY user_id, company_id`
	pars = append(pars, companyStatus, startDate, endDate)

	totalSQL := `SELECT COUNT(1) total FROM (` + sql + `) z `
	err = o.Raw(totalSQL, pars).QueryRow(&total)
	if err != nil {
		return
	}
	if orderStr != "" {
		sql += orderStr
	}
	sql += ` LIMIT ?,?`
	pars = append(pars, startSize, pageSize)
	_, err = o.Raw(sql, pars).QueryRows(&list)
	return
}

type ChartCollectListReq struct {
	PageSize      int    `description:"每页数据条数"`
	CurrentIndex  int    `description:"当前页页码，从1开始"`
	SellerIds     string `description:"销售ID, 用','拼接 "`
	UserId        int    `description:"联系人id"`
	CompanyStatus string `description:"客户历史状态"`
	CompanyId     int    `description:"客户Id"`
	StartDate     string `description:"起始时间"`
	EndDate       string `description:"截止时间"`
}

// GetYbChartByCondition 获取收藏列表
func GetYbChartByCondition(condition string, pars []interface{}, startSize, pageSize int) (list []*MyChart, total int64, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM yb_my_chart WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	totalSQL := `SELECT COUNT(1) total FROM (` + sql + `) z `
	err = o.Raw(totalSQL, pars).QueryRow(&total)
	if err != nil {
		return
	}
	sql += ` order by create_time desc, my_chart_id desc LIMIT ?,?`
	pars = append(pars, startSize, pageSize)
	_, err = o.Raw(sql, pars).QueryRows(&list)
	return
}