package data_manage

import (
	"eta/eta_api/global"
	"eta/eta_api/utils"
	"fmt"
	"gorm.io/gorm"
)

type GzData struct {
	InputValue string `orm:"column(DATA_VALUE)" description:"日期"`
	DataTime   string `orm:"column(DATA_DATE)" description:"值"`
}

func GetEdbDataGzMaxOrMinDate(edbCode string) (minDate, maxDate string, err error) {
	//o := orm.NewOrmUsingDB("data")
	sql := ` SELECT MIN(data_time) AS minDate,MAX(data_time) AS maxDate FROM edb_data_gz WHERE edb_code=? `
	//err = o.Raw(sql, edbCode).QueryRow(&minDate, &maxDate)
	var maxAndMinDate MaxAndMinDate
	err = global.DbMap[utils.DbNameIndex].Raw(sql, edbCode).First(&maxAndMinDate).Error
	if err != nil {
		return
	}
	minDate = maxAndMinDate.MinDate.Format(utils.FormatDate)
	maxDate = maxAndMinDate.MaxDate.Format(utils.FormatDate)

	return
}

type GzIndexView struct {
	BaseFromTradeGuangzhouIndexId    int     `description:"指标id"`
	BaseFromTradeGuangzhouClassifyId int     `description:"分类id"`
	IndexCode                        string  `description:"指标编码"`
	IndexName                        string  `description:"指标名称"`
	Frequency                        string  `description:"频率"`
	Unit                             string  `description:"单位"`
	StartDate                        string  `description:"开始日期"`
	EndDate                          string  `description:"结束日期"`
	Value                            float64 `description:"数据"`
}

// AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
func (m *GzIndexView) AfterFind(db *gorm.DB) (err error) {
	m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
	m.EndDate = utils.GormDateStrToDateStr(m.EndDate)

	return
}
func (m *GzIndexView) ConvDateTimeStr() {
	m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
	m.EndDate = utils.GormDateStrToDateStr(m.EndDate)

	return
}

// GetBaseInfoFromShByIndexCode 获取指标信息
func GetBaseInfoFromGzByIndexCode(indexCode string) (item *GzIndexView, err error) {
	sql := `SELECT * FROM base_from_trade_guangzhou_index WHERE index_code=? `
	sql = fmt.Sprintf(sql)
	err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).First(&item).Error
	if err != nil {
		return
	}
	item.ConvDateTimeStr()

	return
}