package models

import (
	"time"

	"github.com/beego/beego/v2/client/orm"
)

type MyChart struct {
	MyChartId    int       `orm:"pk" description:"我的图表ID"`
	ChartName    string    `description:"图表名称"`   // 图表名称
	UniqueCode   string    `description:"图表唯一编码"` // 图表唯一编码
	ChartImage   string    `description:"图表图片"`   // 图表图片
	ChartInfoId  int       `description:"图表id"`   // 图表图片
	UserId       int       `description:"用户ID"`
	UserRealName string    `description:"用户真实姓名"` // 真实姓名
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time `description:"修改时间"`
}

func (m *MyChart) Insert() (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(m)
	return
}

func GetMyChartCount(userId int, uniqueCode string) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(*) AS count FROM my_chart WHERE user_id=? AND unique_code=?`
	err = o.Raw(sql, userId, uniqueCode).QueryRow(&count)
	return
}

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

func GetMyChartListById(userId int, startSize, pageSize int) (items []*MyChart, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM my_chart WHERE user_id=? ORDER BY create_time LIMIT ?,?`
	_, err = o.Raw(sql, userId, startSize, pageSize).QueryRows(&items)
	return
}

func DeleteMyChart(userId int, uniqueCode string) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM my_chart WHERE user_id=? AND unique_code=?`
	_, err = o.Raw(sql, userId, uniqueCode).Exec()
	return
}

func DeleteMyChartByUserIdAndChartInfoId(userId int, chartInfoId int) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM my_chart WHERE user_id=? AND chart_info_id=?`
	_, err = o.Raw(sql, userId, chartInfoId).Exec()
	return
}