package data_manage

import (
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hz_crm_api/utils"
	"time"
)

// BaseFromEiaSteoIndex EiaSteo指标
type BaseFromEiaSteoIndex struct {
	BaseFromEiaSteoIndexId    int       `orm:"column(base_from_eia_steo_index_id);pk"`
	BaseFromEiaSteoClassifyId int       `description:"指标分类id"`
	IndexCode                 string    `description:"指标编码"`
	IndexName                 string    `description:"指标名称（中文名称）"`
	IndexNameOriginal         string    `description:"指标名称（原始名称）"`
	Frequency                 string    `description:"频度"`
	Level                     int       `description:"指标层级（原始层级）"`
	Unit                      string    `description:"单位"`
	Super                     string    `description:"我也不知道是个啥，反正先存起来，万一用到了呢"`
	Precision                 int       `description:"精度，我也不知道会不会用到，反正先存起来，万一用到了呢"`
	LastHistorical            string    `description:"最近的历史记录，我也不知道会不会用到，反正先存起来，万一用到了呢"`
	Description               string    `description:"备注信息，我也不知道会不会用到，反正先存起来，万一用到了呢"`
	IsMappable                int       `description:"是否可映射，我也不知道会不会用到，反正先存起来，万一用到了呢"`
	StartDate                 time.Time `description:"开始日期"`
	EndDate                   time.Time `description:"结束日期"`
	ModifyTime                time.Time `description:"最新更新时间"`
	CreateTime                time.Time `description:"创建时间"`
}

// BaseFromEiaSteoIndexItem EiaSteo指标
type BaseFromEiaSteoIndexItem struct {
	BaseFromEiaSteoIndexId    int    `orm:"column(base_from_eia_steo_index_id);pk"`
	BaseFromEiaSteoClassifyId int    `description:"指标分类id"`
	IndexCode                 string `description:"指标编码"`
	IndexName                 string `description:"指标名称（中文名称）"`
	IndexNameOriginal         string `description:"指标名称（原始名称）"`
	Frequency                 string `description:"频度"`
	Level                     int    `description:"指标层级（原始层级）"`
	Unit                      string `description:"单位"`
	Super                     string `description:"我也不知道是个啥，反正先存起来，万一用到了呢"`
	Precision                 int    `description:"精度，我也不知道会不会用到，反正先存起来，万一用到了呢"`
	LastHistorical            string `description:"最近的历史记录，我也不知道会不会用到，反正先存起来，万一用到了呢"`
	Description               string `description:"备注信息，我也不知道会不会用到，反正先存起来，万一用到了呢"`
	IsMappable                int    `description:"是否可映射，我也不知道会不会用到，反正先存起来，万一用到了呢"`
	StartDate                 string `description:"开始日期"`
	EndDate                   string `description:"结束日期"`
	ModifyTime                string `description:"最新更新时间"`
	CreateTime                string `description:"创建时间"`
}

// BaseFromEiaSteoClassify EiaSteo分类
type BaseFromEiaSteoClassify struct {
	BaseFromEiaSteoClassifyId int       `orm:"column(base_from_eia_steo_classify_id);pk"`
	ClassifyName              string    `description:"分类名称（中文名称）"`
	ClassifyNameOriginal      string    `description:"分类名称（原始名称）"`
	ModifyTime                time.Time `description:"最新更新时间"`
	CreateTime                time.Time `description:"创建时间"`
}

// BaseFromEiaSteoData EiaSteo数据表
type BaseFromEiaSteoData struct {
	BaseFromEiaSteoDataId  int       `orm:"column(base_from_eia_steo_data_id);pk"`
	BaseFromEiaSteoIndexId int       `description:"指标id"`
	IndexCode              string    `description:"指标编码"`
	DataTime               time.Time `description:"数据日期"`
	Value                  float64   `description:"数据值"`
	ModifyTime             time.Time `description:"最新更新时间"`
	CreateTime             time.Time `description:"创建时间"`
}

// BaseFromEiaSteoDataItem EiaSteo数据
type BaseFromEiaSteoDataItem struct {
	BaseFromEiaSteoDataId  int     `orm:"column(base_from_eia_steo_data_id);pk"`
	BaseFromEiaSteoIndexId int     `description:"指标id"`
	IndexCode              string  `description:"指标编码"`
	DataTime               string  `description:"数据日期"`
	Value                  float64 `description:"数据值"`
	ModifyTime             string  `description:"最新更新时间"`
	CreateTime             string  `description:"创建时间"`
}

// GetEiaSteoClassifyList 获取分类列表
func GetEiaSteoClassifyList() (items []*BaseFromEiaSteoClassify, err error) {
	sql := ` SELECT * FROM base_from_eia_steo_classify  ORDER BY base_from_eia_steo_classify_id ASC `
	o := orm.NewOrmUsingDB("data")
	o.Raw(sql).QueryRows(&items)
	return
}

type EiaSteoIndexListResp struct {
	BaseFromEiaSteoIndexId    int                `orm:"column(base_from_eia_steo_index_id);pk"`
	BaseFromEiaSteoClassifyId int                `description:"指标分类id"`
	IndexCode                 string             `description:"指标编码"`
	IndexName                 string             `description:"指标名称（中文名称）"`
	Unit                      string             `description:"单位"`
	Frequency                 string             `description:"频度"`
	StartDate                 string             `description:"开始日期"`
	EndDate                   string             `description:"结束日期"`
	ModifyTime                string             `description:"最新更新时间"`
	CreateTime                string             `description:"创建时间"`
	Paging                    *paging.PagingItem `description:"分页数据"`
	DataList                  []*BaseFromEiaSteoDataItem
}

// GetEiaSteoIndexList 获取指标列表
func GetEiaSteoIndexList(condition string, pars []interface{}) (items []*BaseFromEiaSteoIndex, err error) {
	sql := ` SELECT *  FROM base_from_eia_steo_index WHERE 1=1  `
	sql += condition
	sql += ` ORDER BY base_from_eia_steo_index_id ASC `
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// GetEiaSteoIndexDataCount 获取指标数据总数
func GetEiaSteoIndexDataCount(indexCode string) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT COUNT(1) AS count FROM base_from_eia_steo_data where index_code = ? `
	err = o.Raw(sql, indexCode).QueryRow(&count)
	return
}

// GetEiaSteoIndexDataList 获取指标数据总数列表
func GetEiaSteoIndexDataList(indexCode string, startSize, pageSize int) (items []*BaseFromEiaSteoDataItem, err error) {
	//sql := ` SELECT *  FROM mb_index_main_data WHERE INDEX_CODE=? ORDER BY DATA_DATE DESC,PUBLISH_TIME DESC LIMIT ?,?`
	sql := ` SELECT * FROM base_from_eia_steo_data AS a WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sql, indexCode, startSize, pageSize).QueryRows(&items)
	return
}

// GetEiaSteoItemList 模糊查询EiaSteo数据库指标列表
func GetEiaSteoItemList(keyword string) (items []*BaseFromEiaSteoIndexItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_eia_steo_index WHERE index_name LIKE ?  OR index_code like ?"
	_, err = o.Raw(sql, `%`+keyword+`%`, `%`+keyword+`%`).QueryRows(&items)
	return
}

// GetEiaSteoDateList 根据指标code获取日期数据
func GetEiaSteoDateList(indexCodeList []string) (items []*BaseFromEiaSteoDataItem, err error) {
	num := len(indexCodeList)
	if num <= 0 {
		return
	}

	sql := ` SELECT data_time FROM base_from_eia_steo_data WHERE index_code IN(` + utils.GetOrmInReplace(num) + `)  GROUP BY data_time ORDER BY data_time DESC `
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sql, indexCodeList).QueryRows(&items)

	return
}

// GetBaseFromEiaSteoIndexByCode 根据指标code获取指标信息
func GetBaseFromEiaSteoIndexByCode(indexCode string) (item *BaseFromEiaSteoIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_eia_steo_index WHERE index_code=? `
	err = o.Raw(sql, indexCode).QueryRow(&item)
	return
}
