package data_manage

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

// ComTradeIndex 联合国商品贸易数据指标表
type ComTradeIndex struct {
	ComTradeId  int       `orm:"column(com_trade_id);pk"`
	IndexCode   string    `description:"联合国商品贸易编码"`
	IndexName   string    `description:"联合国商品名称"`
	IndexNameCn string    `description:"联合国商品名称（中文）"`
	UpdateTime  time.Time `description:"数据最近更新时间"`
	CreateTime  time.Time `description:"创建时间"`
}

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

// GetComTradeIndexByIdList 获取指标数据列表
func GetComTradeIndexByIdList(idList []int) (list []*ComTradeIndex, err error) {
	num := len(idList)
	if num <= 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM com_trade_index WHERE com_trade_id in (` + utils.GetOrmInReplace(num) + `) `
	_, err = o.Raw(sql, idList).QueryRows(&list)
	return
}

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

// ComTradeCodeMapping 联合国商品贸易数据指标编码与分类编码的关系表
type ComTradeCodeMapping struct {
	Code       int       `orm:"column(code);pk"`
	IndexCode  string    `description:"联合国商品贸易编码"`
	Type       int       `description:"1:价格；2：重量"`
	Name       string    `description:"指标名称"`
	CreateTime time.Time `description:"创建时间"`
}

// GetComTradeIndex 根据指标code获取指标信息
func GetComTradeIndex(indexCode string) (item *ComTradeCodeMapping, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM com_trade_code_mapping WHERE code=? `
	err = o.Raw(sql, indexCode).QueryRow(&item)
	return
}
