package data_source

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

type BaseFromIcpiIndex struct {
	BaseFromIcpiIndexId    int       `orm:"column(base_from_icpi_index_id);pk"`
	BaseFromIcpiClassifyId int       `description:"分类id"`
	IndexCode              string    `description:"指标编码"`
	IndexName              string    `description:"指标名称"`
	Frequency              string    `description:"频度"`
	StartDate              time.Time `description:"开始日期"`
	EndDate                time.Time `description:"结束日期"`
	CreateTime             time.Time `description:"创建时间"`
	ModifyTime             time.Time `description:"修改时间"`
}

type BaseFromIcpiData struct {
	BaseFromIcpiDataId  int       `orm:"column(base_from_icpi_data_id);pk"`
	BaseFromIcpiIndexId int       `description:"指标id"`
	IndexCode           string    `description:"指标编码"`
	DataTime            string    `description:"日期"`
	Value               string    `description:"值"`
	CreateTime          time.Time `description:"创建时间"`
	ModifyTime          time.Time `description:"修改时间"`
}

type BaseFromIcpiClassify struct {
	BaseFromIcpiClassifyId int    `orm:"column(base_from_icpi_classify_id);pk"`
	ClassifyName           string `description:"分类名称"`
	ClassifyNameEn         string `description:"英文名称"`
	ParentId               int    `description:"上级id"`
	CreateTime             string `description:"创建时间"`
	ModifyTime             string `description:"修改时间"`
}

func (obj *BaseFromIcpiIndex) GetBaseFromIcpiClassifyAll() (list []*BaseFromIcpiClassify, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_icpi_classify `
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

type BaseFromIcpiDataView struct {
	BaseFromIcpiDataId  int       `orm:"column(base_from_icpi_data_id);pk"`
	BaseFromIcpiIndexId int       `description:"指标id"`
	IndexCode           string    `description:"指标编码"`
	DataTime            string    `description:"日期"`
	Value               string    `description:"值"`
	CreateTime          time.Time `description:"创建时间"`
	ModifyTime          time.Time `description:"修改时间"`
}

type BaseFromIcpiIndexView struct {
	BaseFromIcpiIndexId    int    `description:"指标id"`
	BaseFromIcpiClassifyId int    `description:"分类id"`
	IndexCode              string `description:"指标编码"`
	IndexName              string `description:"指标名称"`
	Frequency              string `description:"频度"`
	Unit                   string `description:"单位"`
	StartDate              string `description:"开始日期"`
	EndDate                string `description:"结束日期"`
	CreateTime             string `description:"创建时间"`
	ModifyTime             string `description:"修改时间"`
	DataList               []*BaseFromIcpiDataView
	Paging                 *paging.PagingItem `description:"分页数据"`
}

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

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

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

func (obj *BaseFromIcpiIndex) GetIcpiDataMaxCount(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 (obj *BaseFromIcpiIndex) GetIcpiIndexDataByCode(indexCode string) (items []*BaseFromIcpiDataView, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT *  FROM base_from_icpi_data WHERE index_code=? ORDER BY data_time DESC  `
	_, err = o.Raw(sql, indexCode).QueryRows(&items)
	return
}
