package data_manage

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

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

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

type FenweiSingleDataResp struct {
	FenweiIndexId int
	ClassifyId    int
	IndexCode     string
	IndexName     string
	Frequency     string
	Unit          string
	CreateTime    string
	ModifyTime    string
	Data          []*FenweiSingleData
}

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

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

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

type FenweiIndexDataCountGroup struct {
	IndexCode string
	Count     int
}

func GetFenweiIndexDataCountGroup(indexCodes []string) (items []*FenweiIndexDataCountGroup, err error) {
	if len(indexCodes) <= 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT COUNT(1) AS count, index_code FROM base_from_fenwei_data WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY index_code`
	_, err = o.Raw(sql, indexCodes).QueryRows(&items)
	return
}

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

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

type BaseFromFenweiData struct {
	FenweiDataId  int `orm:"column(fenwei_data_id);pk"`
	FenweiIndexId int
	IndexCode     string
	DataTime      string
	Value         string
	CreateTime    string
	ModifyTime    string
	DataTimestamp int64
}

type BaseFromFenweiIndexSearchItem struct {
	FenweiIndexId int `orm:"column(fenwei_index_id);pk"`
	ClassifyId    int
	IndexCode     string
	IndexName     string
}

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

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

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