package data_source

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

// BaseFromSci99Index 代表卓创资讯-原始指标表的结构
type BaseFromSci99Index struct {
	BaseFromSciIndexId int       `orm:"column(base_from_sci_index_id);pk"` // 主键，自动递增
	IndexCode          string    // 指标编码
	IndexName          string    // 指标名称
	ClassifyId         int       // 分类Id
	Unit               string    // 单位
	Frequency          string    // 频度
	Describe           string    // 指标描述
	CreateTime         time.Time // 创建时间
	ModifyTime         time.Time // 修改时间
}

// BaseFromSci99Data 代表卓创资讯-原始指标数据表的结构
type BaseFromSci99Data struct {
	BaseFromSciDataId  int       `orm:"column(base_from_sci_data_id);pk"` // 主键，自动递增
	BaseFromSciIndexId int       // 指标id
	IndexCode          string    // 指标编码
	DataTime           string    // 数据日期
	Value              float64   // 数据值
	CreateTime         time.Time // 创建时间
	ModifyTime         time.Time // 修改时间
}

// BaseFromSci99Classify 代表卓创资讯-原始指标分类表的结构
type BaseFromSci99Classify struct {
	BaseFromSciClassifyId int       `orm:"column(base_from_sci_classify_id);pk"` // 主键，自动递增
	ClassifyName          string    // 分类名称
	Sort                  int       // 排序
	CreateTime            time.Time // 创建时间
	ModifyTime            time.Time // 修改时间
}

type BaseFromSci99DataItem struct {
	BaseFromSciDataId  int       `orm:"column(base_from_sci_data_id);pk"` // 主键，自动递增
	BaseFromSciIndexId int       // 指标id
	IndexCode          string    // 指标编码
	DataTime           string    // 数据日期
	Value              float64   // 数据值
	CreateTime         string // 创建时间
	ModifyTime         string // 修改时间
}

type BaseFromSci99IndexList struct {
	BaseFromSciIndexId int       `orm:"column(base_from_sci_index_id);pk"` // 主键，自动递增
	IndexCode          string    // 指标编码
	IndexName          string    // 指标名称
	ClassifyId         int       // 分类Id
	Unit               string    // 单位
	Frequency          string    // 频度
	Describe           string    // 指标描述
	CreateTime         string // 创建时间
	ModifyTime         string // 修改时间
	DataList           []*BaseFromSci99DataItem
	Paging             *paging.PagingItem `description:"分页数据"`
}

// 添加数据
func AddBaseFromSci99Index(item *BaseFromSci99Index) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("data")
	lastId, err = o.Insert(item)
	return
}

func AddBaseFromSci99Classify(item *BaseFromSci99Classify) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("data")
	lastId, err = o.Insert(item)
	return
}

func AddBaseFromSci99DataMulti(item []*BaseFromSci99Data) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.InsertMulti(1000, item)
	return
}

func GetBaseFromSci99Classify() (list []*BaseFromSci99Classify, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_sci99_classify `
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

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


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

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

func GetSci99DataMaxCount(classifyId int) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT MAX(t.num) AS count FROM (
				SELECT COUNT(1) AS num  FROM base_from_icpi_index AS a
				INNER JOIN base_from_icpi_data AS b ON a.base_from_icpi_index_id=b.base_from_icpi_index_id
				WHERE a.base_from_icpi_classify_id=?
				GROUP BY a.base_from_icpi_index_id
			)AS t `
	err = o.Raw(sql, classifyId).QueryRow(&count)
	return
}

func GetSci99IndexDataByCode(indexCode string) (items []*BaseFromSci99DataItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT *  FROM base_from_sci99_data WHERE index_code=? ORDER BY data_time DESC  `
	_, err = o.Raw(sql, indexCode).QueryRows(&items)
	return
}
