package models

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

type CygxChartCollect struct {
	Id          int       `orm:"column(id);pk"`
	ChartId     int       `description:"图表ID"`
	UserId      int       `description:"用户ID"`
	CreateTime  time.Time `description:"创建时间"`
	Mobile      string    `description:"手机号"`
	Email       string    `description:"邮箱"`
	CompanyId   int       `description:"公司id"`
	CompanyName string    `description:"公司名称"`
	RealName    string    `description:"用户实际名称"`
	SellerName  string    `description:"所属销售"`
}
type CygxChartCollectByCygx struct {
	Id          int       `orm:"column(id);pk"`
	ChartId     int       `description:"图表ID"`
	UserId      int       `description:"用户ID"`
	CreateTime  time.Time `description:"创建时间"`
	Mobile      string    `description:"手机号"`
	Email       string    `description:"邮箱"`
	CompanyId   int       `description:"公司id"`
	CompanyName string    `description:"公司名称"`
	RealName    string    `description:"用户实际名称"`
	SellerName  string    `description:"所属销售"`
}

//添加收藏信息
func AddCygxChartCollect(item *CygxChartCollect) (lastId int64, err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	lastId, err = o.Insert(item)
	if err != nil {
		return
	}
	//添加查研观向来源的记录
	itemCygx := new(CygxChartCollectByCygx)
	itemCygx.ChartId = item.ChartId
	itemCygx.UserId = item.UserId
	itemCygx.RealName = item.RealName
	itemCygx.CreateTime = time.Now()
	itemCygx.Mobile = item.Mobile
	itemCygx.Email = item.Email
	itemCygx.CompanyId = item.CompanyId
	itemCygx.CompanyName = item.CompanyName
	lastId, err = o.Insert(itemCygx)
	if err != nil {
		return
	}
	return
}

type ChartCollectResp struct {
	Status       int `description:"1:收藏，2：取消收藏"`
	CollectCount int `description:"收藏总数"`
}

//移除
func RemoveChartCollect(userId, ChartId int) (err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	sql := `DELETE FROM cygx_chart_collect WHERE user_id=? AND chart_id=? `
	_, err = o.Raw(sql, userId, ChartId).Exec()
	if err != nil {
		return
	}
	sql = `DELETE FROM cygx_chart_collect_by_cygx WHERE user_id=? AND chart_id=? `
	_, err = o.Raw(sql, userId, ChartId).Exec()
	return
}

func GetChartCountByUserId(userId, chartID int) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM cygx_chart_collect WHERE user_id=? AND chart_id=? `
	err = orm.NewOrm().Raw(sql, userId, chartID).QueryRow(&count)
	return
}

func GetChartCountByUser(userId int) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM cygx_chart_collect WHERE user_id=?  `
	err = orm.NewOrm().Raw(sql, userId).QueryRow(&count)
	return
}

type CygxChartTop struct {
	Id          int       `orm:"column(id);pk"`
	ChartId     int       `description:"图表ID"`
	UserId      int       `description:"用户ID"`
	CreateTime  time.Time `description:"创建时间"`
	Mobile      string    `description:"手机号"`
	Email       string    `description:"邮箱"`
	CompanyId   int       `description:"公司id"`
	CompanyName string    `description:"公司名称"`
	RealName    string    `description:"用户实际名称"`
	SellerName  string    `description:"所属销售"`
}

//添加置顶信息
func AddCygxChartTop(item *CygxChartTop) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

type ChartTopresp struct {
	Status       int `description:"1:收藏，2：取消收藏"`
	CollectCount int `description:"收藏总数"`
}

func RemoveChartTop(userId, ChartId int) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM cygx_chart_top WHERE user_id=? AND chart_id=? `
	_, err = o.Raw(sql, userId, ChartId).Exec()
	return
}

func GetChartTopCountByUserId(userId, chartID int) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM cygx_chart_top WHERE user_id=? AND chart_id=? `
	err = orm.NewOrm().Raw(sql, userId, chartID).QueryRow(&count)
	return
}

func RemoveChartCollectByMobile(mobile string) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM cygx_chart_collect WHERE mobile IN  (` + mobile + `)`
	_, err = o.Raw(sql).Exec()
	return
}

//批量添加收藏信息
func AddCygxChartCollectList(items []*CygxChartCollect) (lastId int64, err error) {
	o := orm.NewOrm()
	_, err = o.InsertMulti(1, items)
	return
}

//获取列表信息根据手机号分组
func GetCygxChartCollectByMobileList() (items []*CygxChartCollect, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_chart_collect GROUP BY mobile  `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

//修改用户收藏文章的相关信息
func UpdateCygxChartCollect(wxUser *WxUserItem) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_chart_collect SET email=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE mobile=? `
	_, err = o.Raw(sql, wxUser.Email, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Mobile).Exec()
	return
}

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