package cross_variety

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

// ChartVarietyMapping
// @Description: 图表与品种的关系表
type ChartVarietyMapping struct {
	Id             int       `orm:"column(id);pk"`
	ChartInfoId    int       `description:"图表id"`
	ChartVarietyId int       `description:"品种id"`
	ModifyTime     time.Time `description:"修改时间"`
	CreateTime     time.Time `description:"创建时间"`
}

// GetChartVarietyMappingList
// @Description: 获取图表与品种的关系列表表
// @author: Roc
// @datetime 2023-11-24 15:22:36
// @param chartInfoId int
// @return items []*ChartVarietyMapping
// @return err error
func GetChartVarietyMappingList(chartInfoId int) (items []*ChartVarietyMapping, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM chart_variety_mapping WHERE chart_info_id = ? `
	_, err = o.Raw(sql, chartInfoId).QueryRows(&items)
	return
}

// GetCountChartByVarietyId
// @Description: 根据品种id获取引用该标签的图表数量
// @author: Roc
// @datetime 2023-11-27 10:41:46
// @param tagId int
// @return total int64
// @return err error
func GetCountChartByVarietyId(varietyId int) (total int64, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT count(1) total FROM chart_info AS a JOIN
    chart_variety_mapping AS b on a.chart_info_id = b.chart_info_id 
    WHERE b.chart_variety_id = ? `
	err = o.Raw(sql, varietyId).QueryRow(&total)

	return
}

// GetChartVarietyMappingListByChartInfoIdList
// @Description: 根据图表id列表获取图表与品种的关系列表
// @author: Roc
// @datetime 2023-11-24 15:22:36
// @param chartInfoId int
// @return items []*ChartVarietyMapping
// @return err error
func GetChartVarietyMappingListByChartInfoIdList(chartInfoIdList []int) (items []*ChartVarietyMapping, err error) {
	num := len(chartInfoIdList)
	if num <= 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM chart_variety_mapping WHERE chart_info_id in (` + utils.GetOrmInReplace(num) + `) `
	_, err = o.Raw(sql, chartInfoIdList).QueryRows(&items)

	return
}
