package data_manage

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

type BaseFromYongyiIndex struct {
	YongyiIndexId int `orm:"column(yongyi_index_id);pk"`
	ClassifyId    int
	IndexCode     string
	IndexName     string
	Frequency     string
	Unit          string
	Sort          int
	CreateTime    time.Time
	ModifyTime    time.Time
}

type BaseFromYongyiIndexList struct {
	YongyiIndexId int `orm:"column(yongyi_index_id);pk"`
	ClassifyId    int
	Interface     string
	IndexCode     string
	IndexName     string
	Frequency     string
	Unit          string
	Sort          int
	CreateTime    string
	ModifyTime    string
	DataList      []*BaseFromYongyiData
	Paging        *paging.PagingItem `description:"分页数据"`
}

type YongyiSingleDataResp struct {
	YongyiIndexId int
	ClassifyId    int
	IndexCode     string
	IndexName     string
	Frequency     string
	Unit          string
	StartTime     string
	CreateTime    string
	ModifyTime    string
	Data          []*YongyiSingleData
}

type YongyiSingleData struct {
	Value    string `orm:"column(value)" description:"日期"`
	DataTime string `orm:"column(data_time)" description:"值"`
}

func GetYongyiIndex(condition string, pars interface{}) (items []*BaseFromYongyiIndexList, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM base_from_yongyi_index WHERE 1=1  `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY sort ASC, yongyi_index_id asc`
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func GetYongyiIndexDataCount(indexCode string) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT COUNT(1) AS count  FROM base_from_yongyi_data WHERE index_code=? `
	err = o.Raw(sql, indexCode).QueryRow(&count)
	return
}

func GetYongyiIndexData(indexCode string, startSize, pageSize int) (items []*BaseFromYongyiData, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT *  FROM base_from_yongyi_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, indexCode, startSize, pageSize).QueryRows(&items)
	return
}

func GetYongyiIndexDataByCodes(indexCode []string) (items []*BaseFromYongyiData, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT *  FROM base_from_yongyi_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCode)) + `) ORDER BY data_time DESC  `
	_, err = o.Raw(sql, indexCode).QueryRows(&items)
	return
}

type BaseFromYongyiData struct {
	YongyiDataId  int `orm:"column(yongyi_data_id);pk"`
	YongyiIndexId int
	IndexCode     string
	DataTime      string
	Value         string
	CreateTime    string
	ModifyTime    string
	DataTimestamp int64
}

type BaseFromYongyiIndexSearchItem struct {
	YongyiIndexId int `orm:"column(yongyi_index_id);pk"`
	ClassifyId    int
	IndexCode     string
	IndexName     string
}

// GetYongyiItemList 模糊查询Yongyi数据库指标列表
func GetYongyiItemList(condition string) (items []*BaseFromYongyiIndexSearchItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_yongyi_index  WHERE 1=1"
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func GetYongyiIndexDataByCode(indexCode string) (list []*BaseFromYongyiData, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_yongyi_data WHERE index_code=? `
	_, err = o.Raw(sql, indexCode).QueryRows(&list)
	return
}

func GetBaseFromYongyiIndexByIndexCode(indexCode string) (list *BaseFromYongyiIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM base_from_yongyi_index WHERE index_code=? `
	err = o.Raw(sql, indexCode).QueryRow(&list)
	return
}

type BaseFromYongyiIndexType struct {
	Type2 string `orm:"column(type_2)"`
	Type3 string `orm:"column(type_3)"`
}

// Update 更新Yongyi指标基础信息
func (item *BaseFromYongyiIndex) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Update(item, cols...)
	return
}

// EditYongyiIndexInfoResp 新增指标的返回
type EditYongyiIndexInfoResp struct {
	YongyiIndexId int    `description:"指标ID"`
	IndexCode     string `description:"指标code"`
}
