package future_good

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

// FutureGoodEdbInfo 期货指标表
type FutureGoodEdbInfo struct {
	FutureGoodEdbInfoId int       `orm:"column(future_good_edb_info_id);pk"`
	FutureGoodEdbCode   string    `description:"期货指标code"`
	FutureGoodEdbName   string    `description:"期货指标名称"`
	FutureGoodEdbNameEn string    `description:"期货指标英文名称"`
	ParentId            int       `description:"上级期货id"`
	RegionType          string    `description:"交易所来源，海外还是国内"`
	Exchange            string    `description:"所属交易所"`
	FutureGoodEdbType   int       `description:"指标类型,1:年月是固定的合约；2：只有M+N期的合约，未固定年月"`
	DateSourceId        int       `description:"画图时，日期来源的指标id"`
	Year                int       `description:"所属年份"`
	Month               int       `description:"所属月份"`
	StartDate           string    `description:"起始日期"`
	EndDate             string    `description:"终止日期"`
	MinValue            float64   `description:"最小值"`
	MaxValue            float64   `description:"最大值"`
	LatestValue         float64   `description:"数据最新的值"`
	LatestDate          time.Time `description:"数据最新的日期"`
	ServerUrl           string    `description:"服务器地址"`
	CreateTime          time.Time
	ModifyTime          time.Time
}

// GetFutureGoodEdbInfo 期货指标
func GetFutureGoodEdbInfo(edbInfoId int) (item *FutureGoodEdbInfo, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM future_good_edb_info WHERE future_good_edb_info_id = ? `
	sql += ` ORDER BY future_good_edb_info_id DESC `
	err = o.Raw(sql, edbInfoId).QueryRow(&item)
	return
}

// GetFutureGoodEdbInfoByCode 根据期货code获取 期货指标信息
func GetFutureGoodEdbInfoByCode(futureGoodEdbCode string) (item *FutureGoodEdbInfo, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM future_good_edb_info WHERE future_good_edb_code = ? `
	sql += ` ORDER BY future_good_edb_info_id DESC `
	err = o.Raw(sql, futureGoodEdbCode).QueryRow(&item)
	return
}

// GetFutureGoodEdbInfoList 获取指标数据列表
func GetFutureGoodEdbInfoList(condition string, pars []interface{}) (list []*FutureGoodEdbInfo, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM future_good_edb_info WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY future_good_edb_info_id DESC `
	_, err = o.Raw(sql, pars).QueryRows(&list)
	return
}

// GetAllFutureGoodEdbInfoList 获取指标数据列表
func GetAllFutureGoodEdbInfoList() (list []*FutureGoodEdbInfo, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM future_good_edb_info  ORDER BY future_good_edb_info_id DESC `
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetFutureGoodEdbInfoListByParentId 根据父级ID获取指标数据列表
func GetFutureGoodEdbInfoListByParentId(parentId int) (list []*FutureGoodEdbInfo, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM future_good_edb_info WHERE parent_id = ? or  future_good_edb_info_id = ? ORDER BY future_good_edb_info_id ASC `
	_, err = o.Raw(sql, parentId, parentId).QueryRows(&list)
	return
}

// GetChildFutureGoodEdbInfoListByParentId 根据父级ID获取下面所有的指标数据列表
func GetChildFutureGoodEdbInfoListByParentId(parentId int) (list []*FutureGoodEdbInfo, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM future_good_edb_info WHERE parent_id = ? ORDER BY future_good_edb_info_id ASC `
	_, err = o.Raw(sql, parentId).QueryRows(&list)
	return
}

// AddFutureGoodEdbInfo 添加期货数据库指标
func AddFutureGoodEdbInfo(item *FutureGoodEdbInfo) (err error) {
	o := orm.NewOrmUsingDB("data")
	lastId, err := o.Insert(item)
	if err != nil {
		return
	}
	item.FutureGoodEdbInfoId = int(lastId)
	return
}

// Update 更新指标基础信息
func (FutureGoodEdbInfo *FutureGoodEdbInfo) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Update(FutureGoodEdbInfo, cols...)
	return
}

// FutureGoodEdbInfoGroupListResp 期货指标数据列表数据返回
type FutureGoodEdbInfoGroupListResp struct {
	FutureGoodEdbInfoId     int
	FutureGoodEdbInfoName   string `description:"中文名称"`
	FutureGoodEdbInfoNameEn string `description:"英文名称"`
	Child                   []FutureGoodEdbInfoGroupListResp
}

// GetFutureGoodEdbInfoGroupList 获取分組指标数据列表
func GetFutureGoodEdbInfoGroupList(condition string, pars []interface{}) (list []*FutureGoodEdbInfo, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM future_good_edb_info WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY future_good_edb_info_id DESC `
	_, err = o.Raw(sql, pars).QueryRows(&list)
	return
}

type FutureGoodExchange struct {
	Exchange string `description:"所属交易所"`
}

// GetFutureGoodEdbExchangeList 获取分組指标数据列表
func GetFutureGoodEdbExchangeList(condition string, pars []interface{}) (list []FutureGoodExchange, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT exchange FROM future_good_edb_info WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` GROUP BY exchange `
	_, err = o.Raw(sql, pars).QueryRows(&list)
	return
}

// BarChartInfoReq 柱方图预览请求数据
type BarChartInfoReq struct {
	EdbInfoIdList []BarChartInfoEdbItemReq `description:"指标信息"`
	DateList      []BarChartInfoDateReq    `description:"日期配置"`
	Sort          BarChartInfoSortReq      `description:"排序"`
}

// BarChartInfoEdbItemReq 柱方图预览请求数据（指标相关）
type BarChartInfoEdbItemReq struct {
	EdbInfoId int    `description:"指标ID"`
	Name      string `description:"别名"`
	Source    int    `description:"1：ETA图库；2：商品价格"`
}

// BarChartInfoDateReq 柱方图预览请求数据（日期相关）
type BarChartInfoDateReq struct {
	Type  int    `description:"配置类型"`
	Date  string `description:"固定日期"`
	Value int    `description:"N天的值"`
	Color string `description:"颜色"`
	Name  string `description:"别名"`
}

// BarChartInfoSortReq 柱方图预览请求数据（排序相关）
type BarChartInfoSortReq struct {
	Sort      int `description:"排序类型，0：默认，1：升序，2：降序"`
	DateIndex int `description:"日期数据的下标，从0开始"`
}
