package data_manage

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

type BaseFromTradeShanghaiIndex struct {
	BaseFromTradeShangHaiIndexId int `orm:"column(base_from_trade_shanghai_index_id);pk"`
	Rank                         int
	DealShortName                string
	DealName                     string
	DealCode                     string
	DealValue                    string
	DealChange                   string
	BuyShortName                 string
	BuyName                      string
	BuyCode                      string
	BuyValue                     string
	BuyChange                    string
	SoldShortName                string
	SoldName                     string
	SoldCode                     string
	SoldValue                    string
	SoldChange                   string
	Frequency                    string
	ClassifyName                 string
	ClassifyType                 string
	CreateTime                   time.Time
	ModifyTime                   time.Time
	DataTime                     string
}
type BaseFromTradeCffexIndex struct {
	BaseFromTradeCffexIndexId int `orm:"column(base_from_trade_cffex_index_id);pk"`
	Rank                      int
	DealShortName             string
	DealName                  string
	DealCode                  string
	DealValue                 string
	DealChange                string
	BuyShortName              string
	BuyName                   string
	BuyCode                   string
	BuyValue                  string
	BuyChange                 string
	SoldShortName             string
	SoldName                  string
	SoldCode                  string
	SoldValue                 string
	SoldChange                string
	Frequency                 string
	ClassifyName              string
	ClassifyType              string
	CreateTime                time.Time
	ModifyTime                time.Time
	DataTime                  string
}

type BaseFromTradeIneIndex struct {
	BaseFromTradeIneIndexId int `orm:"column(base_from_trade_ine_index_id);pk"`
	Rank                    int
	DealShortName           string
	DealName                string
	DealCode                string
	DealValue               string
	DealChange              string
	BuyShortName            string
	BuyName                 string
	BuyCode                 string
	BuyValue                string
	BuyChange               string
	SoldShortName           string
	SoldName                string
	SoldCode                string
	SoldValue               string
	SoldChange              string
	Frequency               string
	ClassifyName            string
	ClassifyType            string
	CreateTime              time.Time
	ModifyTime              time.Time
	DataTime                string
}

type BaseFromTradeEicIndex struct {
	BaseFromEicIndexId     int `orm:"column(base_from_eic_index_id);pk"`
	Country                string
	Type                   string
	EicCode                string
	ShortName              string
	Name                   string
	Status                 string
	GasDayStartedOn        string
	GasInStorage           string
	GasInStorageCode       string
	Full                   string
	FullCode               string
	Trend                  string
	TrendCode              string
	Injection              string
	InjectionCode          string
	Withdrawal             string
	WithdrawalCode         string
	WorkingGasVolume       string
	WorkingGasVolumeCode   string
	InjectionCapacity      string
	InjectionCapacityCode  string
	WithdrawalCapacity     string
	WithdrawalCapacityCode string
	Info                   string
	CreateTime             time.Time
	ModifyTime             time.Time
}

func GetBaseFromTradeIndexByParam(exchange, date, classifyName, classifyType string) (list []*BaseFromTradeShanghaiIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	if classifyName == "" {
		sql := "SELECT * FROM base_from_trade_" + exchange + "_index where data_time=?"
		_, err = o.Raw(sql, date).QueryRows(&list)
		return
	} else if classifyType == "" {
		sql := "SELECT * FROM base_from_trade_" + exchange + "_index where data_time=? and classify_name=? ORDER BY classify_type,rank"
		_, err = o.Raw(sql, date, classifyName).QueryRows(&list)
		return
	} else {
		sql := "SELECT * FROM base_from_trade_" + exchange + "_index where data_time=? and classify_name=? and classify_type=?"
		_, err = o.Raw(sql, date, classifyName, classifyType).QueryRows(&list)
		return
	}
}

func GetExchangeClassify(exchange, dataTime string) (classifyName []string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT DISTINCT classify_name FROM base_from_trade_" + exchange + "_index where data_time=? ORDER BY CONVERT(classify_name using gbk) DESC"
	_, err = o.Raw(sql, dataTime).QueryRows(&classifyName)
	return
}

func GetLatestDate(exchange string) (dataTime string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT data_time FROM base_from_trade_" + exchange + "_index ORDER BY data_time desc limit 1"
	err = o.Raw(sql).QueryRow(&dataTime)
	return
}

func GetExchangeClassifyContract(exchange, Classify, dataTime string) (classifyName []*string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT DISTINCT classify_type FROM base_from_trade_" + exchange + "_index where classify_name=? and data_time=?"
	_, err = o.Raw(sql, Classify, dataTime).QueryRows(&classifyName)
	return
}

func GetContinentEicDate(date string) (data []*BaseFromTradeEicIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_eic_index where gas_day_started_on=? and type='continent' "
	_, err = o.Raw(sql, date).QueryRows(&data)
	return
}

func GetCountryEicDate(date string) (data []*BaseFromTradeEicIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_eic_index where gas_day_started_on=? and type='country' "
	_, err = o.Raw(sql, date).QueryRows(&data)
	return
}

func GetSSOEicDate(date, country string) (data []*BaseFromTradeEicIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_eic_index where gas_day_started_on=? and country=? and type='SSO' "
	_, err = o.Raw(sql, date, country).QueryRows(&data)
	return
}

func GetStorageEicDate(date, country string) (data []*BaseFromTradeEicIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_eic_index where gas_day_started_on=? and country=? " +
		" and (type='Storage Facility' or type='Storage Group') "
	_, err = o.Raw(sql, date, country).QueryRows(&data)
	return
}

func GetEicDate() (date []*BaseFromTradeEicIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_eic_index ORDER BY gas_day_started_on desc limit 1"
	_, err = o.Raw(sql).QueryRows(&date)
	return
}

func GetEicHistoryDate(code string) (date []*BaseFromTradeEicIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_eic_index WHERE eic_code=? ORDER BY gas_day_started_on desc limit 30"
	_, err = o.Raw(sql, code).QueryRows(&date)
	return
}

func GetEicHistoryDateByDate(code, startDate, endDate string) (date []*BaseFromTradeEicIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_eic_index WHERE eic_code=? and gas_day_started_on between ? and ? ORDER BY gas_day_started_on desc"
	_, err = o.Raw(sql, code, startDate, endDate).QueryRows(&date)
	return
}

type BaseFromCoalmineJsmIndex struct {
	BaseFromCoalmineJsmIndexId int    `orm:"column(base_from_coalmine_jsm_index_id);pk"`
	IndexName                  string `description:"持买单量指标名称"`
	IndexCode                  string `description:"持买单量指标编码"`
	Exchange                   string `description:"样本统计类别"`
	DealValue                  string `description:"成交量"`
	DataTime                   string `description:"数据日期"`
	Source                     string `description:"来源"`
	Province                   string `description:"省份"`
	Description                string `description:"描述"`
	Unit                       string `description:"单位"`
	Frequency                  string `description:"频率"`
	CreateTime                 string `description:"插入时间"`
	ModifyTime                 string `description:"修改时间"`
}

// GetBaseFromCoalmineIndex 查询数据
func GetBaseFromCoalmineIndex(startDate, endDate string) (items []*BaseFromCoalmineJsmIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_jsm_index WHERE data_time between ? and ? `
	_, err = o.Raw(sql, startDate, endDate).QueryRows(&items)
	return
}

type BaseFromCoalmineCompanyIndex struct {
	BaseFromCoalmineCompanyIndexId int    `orm:"column(base_from_coalmine_company_index_id);pk"`
	IndexName                      string `description:"持买单量指标名称"`
	IndexCode                      string `description:"持买单量指标编码"`
	DealValue                      string `description:"成交量"`
	DataTime                       string `description:"数据日期"`
	Source                         string `description:"来源"`
	Province                       string `description:"省份"`
	City                           string `description:"城市"`
	GroupName                      string `description:"集团名称"`
	Unit                           string `description:"单位"`
	Frequency                      string `description:"频率"`
	CreateTime                     string `description:"插入时间"`
	ModifyTime                     string `description:"修改时间"`
}

// GetBaseFromCoalmineCompanyIndex 查询公司指标
func GetBaseFromCoalmineCompanyIndex(startDate, endDate string) (items []*BaseFromCoalmineCompanyIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_company_index WHERE data_time between ? and ? `
	_, err = o.Raw(sql, startDate, endDate).QueryRows(&items)
	return
}

// BaseFromCoalmineFirmIndex 煤矿产量周度公司数据表
type BaseFromCoalmineFirmIndex struct {
	BaseFromCoalmineFirmIndexID int    `orm:"column(base_from_coalmine_firm_index_id);pk"`
	IndexName                   string // 省份/企业名称
	IndexCode                   string // 持买单量指标编码
	DataTime                    string // 指标时间
	DealValue                   string // 数据量
	GroupName                   string // 集团名
	Source                      string // 来源
	Unit                        string // 来源
	Frequency                   string `description:"频率"`
	CreateTime                  string `description:"插入时间"`
	ModifyTime                  string `description:"修改时间"`
}

// 查询指标
func GetBaseFromCoalmineFirmIndex(dataTime string) (items []*BaseFromCoalmineFirmIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_firm_index WHERE data_time LIKE  `
	sql = sql + "'" + dataTime + "%" + "'"
	_, err = o.Raw(sql).QueryRows(&items)
	fmt.Println(sql)
	return
}

// BaseFromCoalmineCoastalIndex 沿海八省动力煤用户供耗存数据指标表
type BaseFromCoalmineCoastalIndex struct {
	BaseFromCoalmineCoastalIndexID int    `orm:"column(base_from_coalmine_coastal_index_id);pk"`
	IndexName                      string // 省份/企业名称
	IndexCode                      string // 持买单量指标编码
	DataTime                       string // 指标时间
	DealValue                      string // 数据量
	GroupName                      string // 地区
	Source                         string // 来源
	Unit                           string //单位
	Frequency                      string `description:"频率"`
	CreateTime                     string `description:"插入时间"`
	ModifyTime                     string `description:"修改时间"`
}

// 查询指标
func GetBaseFromCoalmineCoastalIndex(startDate, endDate string) (items []*BaseFromCoalmineCoastalIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_coastal_index WHERE data_time between ? and ? `
	_, err = o.Raw(sql, startDate, endDate).QueryRows(&items)
	return
}

// BaseFromCoalmineInlandIndex 内陆动力煤用户供耗存数据指标表
type BaseFromCoalmineInlandIndex struct {
	BaseFromCoalmineInlandIndexID int    `orm:"column(base_from_coalmine_inland_index_id);pk"`
	IndexName                     string // 省份/企业名称
	IndexCode                     string // 持买单量指标编码
	DataTime                      string // 指标时间
	DealValue                     string // 数据量
	GroupName                     string // 地区
	Source                        string // 来源
	Unit                          string // 来源
	Frequency                     string `description:"频率"`
	CreateTime                    string `description:"插入时间"`
	ModifyTime                    string `description:"修改时间"`
}

// 查询指标
func GetBaseFromCoalmineInlandIndex(startDate, endDate string) (items []*BaseFromCoalmineInlandIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_inland_index WHERE data_time between ? and ? `
	_, err = o.Raw(sql, startDate, endDate).QueryRows(&items)
	return
}

type BaseFromCoalmineClassifyItem struct {
	ClassifyId   int
	ClassifyName string
	Child        []CoalChild
}

type CoalChild struct {
	ClassifyId   int
	ClassifyName string
}

type CoalmineDataResp struct {
	IndexName  string
	IndexCode  string // 持买单量指标编码
	Unit       string // 来源
	Frequency  string
	ModifyTime string
	DataList   []DataList
}

type CoalmineDataPageResp struct {
	IndexName  string
	IndexCode  string // 持买单量指标编码
	Unit       string // 来源
	Frequency  string
	ModifyTime string
	Paging     *paging.PagingItem `description:"分页数据"`
	DataList   []DataList
}

type CoalmineSingalDataResp struct {
	IndexName  string
	IndexCode  string // 持买单量指标编码
	Unit       string // 来源
	GroupId    int    // 二级id
	GroupName  string // 二级名称
	Frequency  string
	ModifyTime string
	ClassifyId int
	DataList   []DataList
}

type DataList struct {
	Value    string
	DataTime string
}

type FrequencyResp struct {
	Frequency int
}

// GetFrequencyFromCoal 获取指标信息
func GetFrequencyFromCoal(suffix string) (list *string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT DISTINCT frequency FROM base_from_coalmine_%s `
	sql = fmt.Sprintf(sql, suffix)
	err = o.Raw(sql).QueryRow(&list)
	return
}

// 查询数据
func GetBaseFromCoalmineIndexByFrequency(frequency, groupName string) (items []*BaseFromCoalmineJsmIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_jsm_index WHERE frequency=? AND province=?`
	_, err = o.Raw(sql, frequency, groupName).QueryRows(&items)
	return
}

// 查询数据
func GetGroupNameFromCoalmineIndex(suffix string) (items []*string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT DISTINCT group_name FROM base_from_coalmine_%s `
	sql = fmt.Sprintf(sql, suffix)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 查询数据
func GetProvinceFromCoalmineIndex(suffix string) (items []*string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT DISTINCT province FROM base_from_coalmine_%s `
	sql = fmt.Sprintf(sql, suffix)
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 查询数据
func GetClassifyCoalmineIndexByGroupName(groupName string) (items []*string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT DISTINCT index_code FROM base_from_coalmine_jsm_index WHERE province=? `
	_, err = o.Raw(sql, groupName).QueryRows(&items)
	return
}

// 查询数据
func GetPageFromCoalmineIndexByFrequency(frequency, classify string, startSize, pageSize int) (items []*BaseFromCoalmineJsmIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_jsm_index WHERE frequency=? AND index_code=? ORDER BY data_time DESC LIMIT ?,?  `
	_, err = o.Raw(sql, frequency, classify, startSize, pageSize).QueryRows(&items)
	return
}

// 查询数据
func GetCountFromJsm(indexCode string) (item int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT COUNT(1) FROM base_from_coalmine_jsm_index WHERE index_code=? `
	err = o.Raw(sql, indexCode).QueryRow(&item)
	return
}

func GetClassifyCompanyByGroupName(groupName string) (items []*string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT DISTINCT index_code FROM base_from_coalmine_company_index WHERE group_name=? `
	_, err = o.Raw(sql, groupName).QueryRows(&items)
	return
}

func GetClassifyJsmByGroupName(groupName string) (items []*string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT DISTINCT index_code FROM base_from_coalmine_jsm_index WHERE province=? `
	_, err = o.Raw(sql, groupName).QueryRows(&items)
	return
}

// 查询公司指标
func GetPageFromCoalmineCompanyIndexByFrequency(frequency, classify string, startSize, pageSize int) (items []*BaseFromCoalmineCompanyIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_company_index WHERE frequency=? AND index_code=? ORDER BY data_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, frequency, classify, startSize, pageSize).QueryRows(&items)
	return
}

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

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

func GetClassifyFirmByGroupName(groupName string) (items []*string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT DISTINCT index_code FROM base_from_coalmine_firm_index WHERE group_name=? `
	_, err = o.Raw(sql, groupName).QueryRows(&items)
	return
}

// 查询指标
func GetPageFromCoalmineFirmIndexByFrequency(frequency, classify string, startSize, pageSize int) (items []*BaseFromCoalmineFirmIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_firm_index WHERE frequency=? AND index_code=? ORDER BY data_time DESC LIMIT ?,?  `
	_, err = o.Raw(sql, frequency, classify, startSize, pageSize).QueryRows(&items)
	return
}

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

func GetClassifyCoastalByGroupName(groupName string) (items []*string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT DISTINCT index_code FROM base_from_coalmine_coastal_index WHERE group_name=? `
	_, err = o.Raw(sql, groupName).QueryRows(&items)
	return
}

// 查询指标
func GetPageFromCoalmineCoastalIndexByFrequency(frequency, classify string, startSize, pageSize int) (items []*BaseFromCoalmineCoastalIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_coastal_index WHERE frequency=? AND index_code=? ORDER BY data_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, frequency, classify, startSize, pageSize).QueryRows(&items)
	return
}

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

func GetClassifyInlandByGroupName(groupName string) (items []*string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT DISTINCT index_code FROM base_from_coalmine_inland_index WHERE group_name=? `
	_, err = o.Raw(sql, groupName).QueryRows(&items)
	return
}

// 查询指标
func GetPageFromCoalmineInlandIndexByFrequency(frequency, classify string, startSize, pageSize int) (items []*BaseFromCoalmineInlandIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_inland_index WHERE frequency=? AND index_code=? ORDER BY data_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, frequency, classify, startSize, pageSize).QueryRows(&items)
	return
}

// 查询公司指标
func GetBaseFromCoalmineCompanyIndexByFrequency(frequency, groupName string) (items []*BaseFromCoalmineCompanyIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_company_index WHERE frequency=? AND group_name=? `
	_, err = o.Raw(sql, frequency, groupName).QueryRows(&items)
	return
}

// 查询指标
func GetBaseFromCoalmineFirmIndexByFrequency(frequency, groupName string) (items []*BaseFromCoalmineFirmIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_firm_index WHERE frequency=? AND group_name=? `
	_, err = o.Raw(sql, frequency, groupName).QueryRows(&items)
	return
}

// 查询指标
func GetBaseFromCoalmineCoastalIndexByFrequency(frequency, groupName string) (items []*BaseFromCoalmineCoastalIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_coastal_index WHERE frequency=? AND group_name=? `
	_, err = o.Raw(sql, frequency, groupName).QueryRows(&items)
	return
}

// 查询指标
func GetBaseFromCoalmineInlandIndexByFrequency(frequency, groupName string) (items []*BaseFromCoalmineInlandIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_inland_index WHERE frequency=? AND group_name=? `
	_, err = o.Raw(sql, frequency, groupName).QueryRows(&items)
	return
}

// 查询数据
func GetBaseFromCoalmineIndexByCode(indexCode string) (items []*BaseFromCoalmineJsmIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_jsm_index WHERE index_code=?`
	_, err = o.Raw(sql, indexCode).QueryRows(&items)
	return
}

// 查询公司指标
func GetBaseFromCoalmineCompanyIndexByCode(indexCode string) (items []*BaseFromCoalmineCompanyIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_company_index WHERE index_code=? `
	_, err = o.Raw(sql, indexCode).QueryRows(&items)
	return
}

// 查询指标
func GetBaseFromCoalmineFirmIndexByCode(indexCode string) (items []*BaseFromCoalmineFirmIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_firm_index WHERE index_code=? `
	_, err = o.Raw(sql, indexCode).QueryRows(&items)
	return
}

// 查询指标
func GetBaseFromCoalmineCoastalIndexByCode(indexCode string) (items []*BaseFromCoalmineCoastalIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_coastal_index WHERE index_code=? `
	_, err = o.Raw(sql, indexCode).QueryRows(&items)
	return
}

// 查询指标
func GetBaseFromCoalmineInlandIndexByCode(indexCode string) (items []*BaseFromCoalmineInlandIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_coalmine_inland_index WHERE index_code=? `
	_, err = o.Raw(sql, indexCode).QueryRows(&items)
	return
}

type BaseFromTradeEicIndexV2 struct {
	BaseFromEicIndexId     int `orm:"column(base_from_eic_index_id);pk"`
	Type                   string
	EicCode                string
	Name                   string
	Status                 string
	GasDayStart            string
	GasInStorage           float64
	GasInStorageCode       string
	Consumption            string
	ConsumptionCode        string
	ConsumptionFull        string
	ConsumptionFullCode    string
	Full                   float64
	FullCode               string
	Trend                  float64
	TrendCode              string
	Injection              float64
	InjectionCode          string
	Withdrawal             float64
	WithdrawalCode         string
	WorkingGasVolume       float64
	WorkingGasVolumeCode   string
	InjectionCapacity      float64
	InjectionCapacityCode  string
	WithdrawalCapacity     float64
	WithdrawalCapacityCode string
	Info                   string
	Parent                 string
	CreateTime             time.Time
	ModifyTime             time.Time
	Children               []BaseFromTradeEicIndexV2
}

func GetEicDateV2() (date []*BaseFromTradeEicIndexV2, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_eic_index_v2 ORDER BY gas_day_start desc limit 1"
	_, err = o.Raw(sql).QueryRows(&date)
	return
}

func GetEicDataV2(date string) (data []*BaseFromTradeEicIndexV2, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_eic_index_v2 where gas_day_start=? "
	_, err = o.Raw(sql, date).QueryRows(&data)
	return
}

func GetEicHistoryDateByDateV2(code, startDate, endDate string) (date []*BaseFromTradeEicIndexV2, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_eic_index_v2 WHERE eic_code=? and gas_day_start between ? and ? ORDER BY gas_day_start desc"
	_, err = o.Raw(sql, code, startDate, endDate).QueryRows(&date)
	return
}

func GetEicHistoryDateV2(code string) (date []*BaseFromTradeEicIndexV2, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_eic_index_v2 WHERE eic_code=? ORDER BY gas_day_start desc limit 30"
	_, err = o.Raw(sql, code).QueryRows(&date)
	return
}

func GetContinentAndCountryEicDateV2(date string) (data []*BaseFromTradeEicIndexV2, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_eic_index_v2 where gas_day_start=? and (type='continent' OR type='country') "
	_, err = o.Raw(sql, date).QueryRows(&data)
	return
}

func GetSSOAndFacEicDateV2(date, name string) (data []*BaseFromTradeEicIndexV2, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_eic_index_v2 where gas_day_start=? and (type='sso' OR type='fac') "
	_, err = o.Raw(sql, date).QueryRows(&data)
	return
}

func GetBaseFromTradeIndexByDate(exchange, startDate, endDate string) (list []*BaseFromTradeShanghaiIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_" + exchange + "_index where rank < 50 and data_time between ? and ? order by data_time asc"
	_, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
	return
}

func GetFirstBaseFromTradeIndexByDate(exchange string) (item *BaseFromTradeShanghaiIndex, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := "SELECT * FROM base_from_trade_" + exchange + "_index where rank < 50 order by data_time asc"
	err = o.Raw(sql).QueryRow(&item)
	return
}

type BaseFromCoalmineClassify struct {
	BaseFromCoalmineClassifyId int    `orm:"column(base_from_coalmine_classify_id);pk"`
	ClassifyName               string // 分类名称
	Suffix                     string // 表名后缀
	CreateTime                 time.Time
}

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