package cross_variety

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

// ChartVariety
// @Description: chart_variety 图表品种表
type ChartVariety struct {
	ChartVarietyId     int       `orm:"column(chart_variety_id);pk"`
	ChartVarietyName   string    `description:"品种名称"`
	ChartVarietyNameEn string    `description:"品种名称（英文）"`
	SysUserId          int       `description:"创建人id"`
	SysUserRealName    string    `description:"创建人姓名"`
	ModifyTime         time.Time `description:"修改时间"`
	CreateTime         time.Time `description:"创建时间"`
}

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

	return
}

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

	return
}

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

	item.ChartVarietyId = int(lastId)

	return
}

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

	return
}

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

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

	return
}

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

	return
}
