package data_source

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

type BaseFromTradeGuangzhouClassify struct {
	BaseFromTradeGuangzhouClassifyId int       `orm:"column(base_from_trade_guangzhou_classify_id);pk"`
	ClassifyName                     string    `description:"分类名称"`
	ClassifyCode                     string    `description:"分类编码"`
	ParentId                         int       `description:"分类父级id"`
	ModifyTime                       time.Time `description:"修改时间"`
	CreateTime                       time.Time `description:"创建时间"`
}

type BaseFromTradeGuangzhouIndex struct {
	BaseFromTradeGuangzhouIndexId    int       `orm:"column(base_from_trade_guangzhou_index_id);pk"`
	BaseFromTradeGuangzhouClassifyId int       `description:"分类id"`
	IndexCode                        string    `description:"指标编码"`
	IndexName                        string    `description:"指标名称"`
	Frequency                        string    `description:"频率"`
	Unit                             string    `description:"单位"`
	StartDate                        string    `description:"开始日期"`
	EndDate                          string    `description:"结束日期"`
	CreateTime                       time.Time `description:"创建日期"`
	ModifyTime                       time.Time `description:"修改日期"`
}

type BaseFromTradeGuangzhouData struct {
	BaseFromTradeGuangzhouDataId  int       `orm:"column(base_from_trade_guangzhou_data_id);pk"`
	BaseFromTradeGuangzhouIndexId int       `description:"指标id"`
	IndexCode                     string    `description:"指标编码"`
	DataTime                      string    `description:"数据日期"`
	Value                         float64   `description:"数据值"`
	QtySub                        float64   `description:"增减"`
	CreateTime                    time.Time `description:"创建日期"`
	ModifyTime                    time.Time `description:"修改日期"`
}

type BaseFromTradeGuangzhouClassifyView struct {
	BaseFromTradeGuangzhouClassifyId int    `orm:"column(base_from_trade_guangzhou_classify_id);pk"`
	ClassifyName                     string `description:"分类名称"`
	ClassifyCode                     string `description:"分类编码"`
	ParentId                         int    `description:"分类父级id"`
	Children                         []*BaseFromTradeGuangzhouClassifyView
}

type BaseFromTradeGuangzhouContract struct {
	BaseFromTradeGuangzhouContractId int    `orm:"column(base_from_trade_guangzhou_contract_id);pk"`
	BaseFromTradeGuangzhouClassifyId int    `description:"分类id"`
	ClassifyCode                     string `description:"分类编码"`
	Contract                         string `description:"合约编码"`
	TradeDate                        string `description:"合约日期"`
}

func (obj *BaseFromTradeGuangzhouIndex) GetBaseFromTradeGuangzhouClassifyAll() (list []*BaseFromTradeGuangzhouClassifyView, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM base_from_trade_guangzhou_classify`
	_, err = o.Raw(sql).QueryRows(&list)
	return list, err
}

func (obj *BaseFromTradeGuangzhouIndex) GetBaseFromTradeGuangzhouMaxDate() (max_date string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT MAX(a.end_date) AS max_date FROM base_from_trade_guangzhou_index AS a `
	err = o.Raw(sql).QueryRow(&max_date)
	return max_date, err
}

func (obj *BaseFromTradeGuangzhouIndex) GetBaseFromTradeGuangzhouContract(classifyId int, tradeDate string) (list []*BaseFromTradeGuangzhouContract, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM base_from_trade_guangzhou_contract AS a WHERE base_from_trade_guangzhou_classify_id=? AND trade_date=? `
	_, err = o.Raw(sql, classifyId, tradeDate).QueryRows(&list)
	return
}

type BaseFromTradeGuangzhouIndexView struct {
	BaseFromTradeGuangzhouIndexId    int     `description:"指标id"`
	BaseFromTradeGuangzhouClassifyId int     `description:"分类id"`
	IndexCode                        string  `description:"指标编码"`
	IndexName                        string  `description:"指标名称"`
	Frequency                        string  `description:"频率"`
	Unit                             string  `description:"单位"`
	StartDate                        string  `description:"开始日期"`
	EndDate                          string  `description:"结束日期"`
	Value                            float64 `description:"数据"`
}

func (obj *BaseFromTradeGuangzhouIndex) GetBaseFromTradeGuangzhouIndex(condition string, pars []interface{}, baseFromTradeGuangzhouContractId int) (list []*BaseFromTradeGuangzhouIndexView, err error) {
	o := orm.NewOrmUsingDB("data")
	if baseFromTradeGuangzhouContractId <= 0 {
		sql := ` SELECT b.base_from_trade_guangzhou_index_id,b.index_code,b.index_name,b.unit,b.frequency,b.start_date,b.end_date,a.value,c.base_from_trade_guangzhou_contract_id 
 FROM base_from_trade_guangzhou_data AS a
INNER JOIN base_from_trade_guangzhou_index AS b ON a.base_from_trade_guangzhou_index_id=b.base_from_trade_guangzhou_index_id
LEFT JOIN base_from_trade_guangzhou_contract AS c ON b.base_from_trade_guangzhou_classify_id=c.base_from_trade_guangzhou_classify_id
WHERE `
		if condition != "" {
			sql += condition
		}
		sql += ` ORDER BY a.index_code ASC `
		_, err = o.Raw(sql, pars).QueryRows(&list)
		return
	} else {
		condition += ` AND a.base_from_trade_guangzhou_contract_id=? `
		pars = append(pars, baseFromTradeGuangzhouContractId)

		sql := ` SELECT a.*,b.value 
 FROM base_from_trade_guangzhou_index AS a
INNER JOIN base_from_trade_guangzhou_data AS b ON a.base_from_trade_guangzhou_index_id=b.base_from_trade_guangzhou_index_id
WHERE `

		if condition != "" {
			sql += condition
		}
		sql += ` ORDER BY a.index_code ASC `
		_, err = o.Raw(sql, pars).QueryRows(&list)
		return
	}
}

func (obj *BaseFromTradeGuangzhouIndex) GetBaseFromTradeGuangzhouIndexDetail(condition string, pars []interface{}) (list []*BaseFromTradeGuangzhouIndexView, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT a.*
 FROM base_from_trade_guangzhou_index AS a
WHERE `

	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY a.index_code ASC `
	_, err = o.Raw(sql, pars).QueryRows(&list)
	return
}
