package cross_variety

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

// ChartTag
// @Description: chart_tag 图表标签表
type ChartTag struct {
	ChartTagId      int       `orm:"column(chart_tag_id);pk"`
	ChartTagName    string    `description:"标签名称"`
	ChartTagNameEn  string    `description:"标签名称（英文）"`
	SysUserId       int       `description:"创建人id"`
	SysUserRealName string    `description:"创建人姓名"`
	ModifyTime      time.Time `description:"修改时间"`
	CreateTime      time.Time `description:"创建时间"`
}

// GetTagById
// @Description: 根据标签id获取标签详情
// @author: Roc
// @datetime 2023-11-21 14:55:31
// @param id int
// @return item *ChartTag
// @return err error
func GetTagById(id int) (item *ChartTag, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM chart_tag WHERE chart_tag_id = ?`
	err = o.Raw(sql, id).QueryRow(&item)

	return
}

// GetTagByName
// @Description: 根据标签名称获取标签详情
// @author: Roc
// @datetime 2023-11-21 14:55:20
// @param name string
// @return item *ChartTag
// @return err error
func GetTagByName(name string) (item *ChartTag, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM chart_tag WHERE chart_tag_name = ?`
	err = o.Raw(sql, name).QueryRow(&item)

	return
}

// AddTag
// @Description: 添加标签
// @author: Roc
// @datetime 2023-11-21 14:52:56
// @param item *ChartTag
// @return err error
func AddTag(item *ChartTag) (err error) {
	o := orm.NewOrmUsingDB("data")
	lastId, err := o.Insert(item)
	if err != nil {
		return
	}

	item.ChartTagId = int(lastId)

	return
}

// GetTagList
// @Description: 获取所有标签列表
// @author: Roc
// @datetime 2023-11-22 10:44:35
// @return items []*ChartTag
// @return err error
func GetTagList() (items []*ChartTag, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM chart_tag WHERE 1 = 1 `
	_, err = o.Raw(sql).QueryRows(&items)

	return
}

// Update
// @Description: 更新标签
// @author: Roc
// @receiver item
// @datetime 2023-11-21 15:15:17
// @param updateColList []string
// @return err error
func (item *ChartTag) Update(updateColList []string) (err error) {
	to := orm.NewOrmUsingDB("data")
	_, err = to.Update(item, updateColList...)

	return
}

// Delete 删除
func (item *ChartTag) Delete() (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Delete(item)
	return
}

// GetTagListByIdList
// @Description: 根据ID列表获取标签列表
// @author: Roc
// @datetime 2023-11-23 17:56:39
// @param idList []int
// @return items []*ChartTag
// @return err error
func GetTagListByIdList(idList []int) (items []*ChartTag, err error) {
	num := len(idList)
	if num <= 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM chart_tag WHERE 1 = 1 AND chart_tag_id in (` + utils.GetOrmInReplace(num) + `)`
	_, err = o.Raw(sql, idList).QueryRows(&items)

	return
}

// ChartTagItem
// @Description: chart_tag 图表标签表
type ChartTagItem struct {
	ChartTagId      int       `orm:"column(chart_tag_id);pk"`
	ChartTagName    string    `description:"标签名称"`
	ChartTagNameEn  string    `description:"标签名称（英文）"`
	SysUserId       int       `description:"创建人id"`
	VarietyTotal    int       `description:"配置关联品种的数量"`
	SysUserRealName string    `description:"创建人姓名"`
	ModifyTime      time.Time `description:"修改时间"`
	CreateTime      time.Time `description:"创建时间"`
}

// GetTagItemList
// @Description: 获取所有标签列表（列表页用到的）
// @author: Roc
// @datetime 2023-11-22 10:44:35
// @return items []*ChartTag
// @return err error
func GetTagItemList() (items []*ChartTagItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM chart_tag WHERE 1 = 1 `
	_, err = o.Raw(sql).QueryRows(&items)

	return
}
