package supply_analysis

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

// VarietyEdbInfo variety_edb_info 品种指标表
type VarietyEdbInfo struct {
	VarietyEdbId int       `orm:"column(variety_edb_id);pk"`
	VarietyId    int       `description:"品种id"`
	EdbName      string    `description:"品种指标名称"`
	Frequency    string    `description:"频度"`
	StartDate    time.Time `description:"开始日期"`
	EndDate      time.Time `description:"结束日期"`
	LatestValue  float64   `description:"最新值"`
	EdbInfoId    int       `description:"指标id"`
	EdbCode      string    `description:"品种编码"`
	Source       int       `description:"来源，1：影响周度产量；2：周度产量变动；3：影响月度产量；4：月度产量变动"`
	ModifyTime   time.Time `description:"更新时间"`
	CreateTime   time.Time `description:"添加时间"`
}

// GetAllVarietyEdbInfoByVarietyId 根据品种id获取所有的指标
func GetAllVarietyEdbInfoByVarietyId(varietyId int) (items []*VarietyEdbInfo, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM variety_edb_info AS a WHERE a.variety_id = ? ORDER BY a.variety_edb_id ASC `
	_, err = o.Raw(sql, varietyId).QueryRows(&items)
	return
}

// GetVarietyEdbInfoByEdbInfoId 根据ETA指标库中的指标id获取指标
func GetVarietyEdbInfoByEdbInfoId(varietyId int) (item *VarietyEdbInfo, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM variety_edb_info AS a WHERE a.edb_info_id = ? ORDER BY a.variety_edb_id ASC `
	err = o.Raw(sql, varietyId).QueryRow(&item)

	return
}

// BatchCreateVarietyEdbInfo 批量插入
func BatchCreateVarietyEdbInfo(list []*VarietyEdbInfo) (err error) {
	num := len(list)
	if num <= 0 {
		return
	}

	o := orm.NewOrm()
	_, err = o.InsertMulti(num, list)

	return
}

// GetCountVarietyEdbByVarietyId 根据品种id获取添加到已经生成的指标数量（不管是否加入到ETA指标库）
func GetCountVarietyEdbByVarietyId(varietyId int) (total int, err error) {
	o := orm.NewOrm()
	sql := `SELECT count(1) total FROM variety_edb_info AS a WHERE a.variety_id = ?`
	err = o.Raw(sql, varietyId).QueryRow(&total)
	return
}

// GetCountVarietyEdbInfoByVarietyId 根据品种id获取添加到ETA指标库的指标数量
func GetCountVarietyEdbInfoByVarietyId(varietyId int) (total int, err error) {
	o := orm.NewOrm()
	sql := `SELECT count(1) total FROM variety_edb_info AS a
			JOIN edb_info b on a.edb_info_id=b.edb_info_id
			WHERE a.variety_id = ? and a.edb_info_id >0`
	err = o.Raw(sql, varietyId).QueryRow(&total)
	return
}

// VarietyEdbInfoItem 品种指标表（展示使用）
type VarietyEdbInfoItem struct {
	VarietyEdbId int                  `orm:"column(variety_edb_id);pk"`
	VarietyId    int                  `description:"品种id"`
	EdbName      string               `description:"品种指标名称"`
	Frequency    string               `description:"频度"`
	StartDate    string               `description:"开始日期"`
	EndDate      string               `description:"结束日期"`
	EdbInfoId    int                  `description:"指标id" json:"-"`
	EdbCode      string               `description:"品种编码"`
	ClassifyName string               `description:"分类名称"`
	Source       int                  `description:"来源，1：影响周度产量；2：周度产量变动；3：影响月度产量；4：月度产量变动" json:"-"`
	ModifyTime   string               `description:"最近一次更新时间"`
	CreateTime   string               `description:"添加时间"`
	Button       VarietyEdbInfoButton `description:"操作按钮权限"`
}

type VarietyEdbInfoButton struct {
	Copy bool `description:"复制数据权限"`
	Show bool `description:"查看数据权限"`
	Add  bool `description:"添加到指标库"`
}

// GetAllVarietyEdbInfoItemByVarietyId 根据品种id获取所有的指标
func GetAllVarietyEdbInfoItemByVarietyId(varietyId int) (items []*VarietyEdbInfoItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM variety_edb_info AS a WHERE a.variety_id = ? ORDER BY a.variety_edb_id ASC `
	_, err = o.Raw(sql, varietyId).QueryRows(&items)
	return
}
