package supply_analysis

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

// BaseFromStockPlantData base_from_stock_plant_data 存量装置的指标的计算数据
type BaseFromStockPlantData struct {
	StockPlantDataId int       `orm:"column(stock_plant_data_id);pk"`
	VarietyEdbId     int       `description:"指标id"`
	DataTime         time.Time `description:"数据日期"`
	Value            float64   `description:"数据值"`
	ModifyTime       time.Time `description:"修改时间"`
	CreateTime       time.Time `description:"创建时间"`
	DataTimestamp    int64     `description:"数据日期时间戳"`
}

// GetDataVarietyEdbInfoByVarietyId 根据品种指标id获取所有的数据
func GetDataVarietyEdbInfoByVarietyId(varietyEdbId int) (items []*BaseFromStockPlantData, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_stock_plant_data AS a WHERE a.variety_edb_id = ? ORDER BY data_time desc `
	_, err = o.Raw(sql, varietyEdbId).QueryRows(&items)
	return
}

// GetVarietyEdbData 获取指标数据
func GetVarietyEdbData(varietyEdbId int, startDate, endDate string) (items []*BaseFromStockPlantData, err error) {
	var pars []interface{}
	sql := `SELECT * FROM base_from_stock_plant_data AS a WHERE a.variety_edb_id = ?  `
	if startDate != "" {
		sql += ` AND data_time>=? `
		pars = append(pars, startDate)
	}
	if endDate != "" {
		sql += ` AND data_time<=? `
		pars = append(pars, endDate)
	}

	sql += ` ORDER BY data_time ASC `
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sql, varietyEdbId, pars).QueryRows(&items)
	return
}

func GetVarietyEdbDataListByIdList(varietyEdbIdList []int) (items []*BaseFromStockPlantData, err error) {
	num := len(varietyEdbIdList)
	if num <= 0 {
		return
	}
	sql := `SELECT * FROM  (SELECT * FROM base_from_stock_plant_data AS a WHERE a.variety_edb_id in (` + utils.GetOrmInReplace(num) + `) GROUP BY data_time) d ORDER BY data_time DESC `
	//sql := ` SELECT  DT FROM edbdata WHERE TRADE_CODE IN(` + tradeCode + `)  GROUP BY DT ORDER BY DT DESC `
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sql, varietyEdbIdList).QueryRows(&items)
	return
}
