package data_manage

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

// ComTradeData 联合国商品贸易数据表
type ComTradeData struct {
	ComTradeDataId     int       `orm:"column(com_trade_data_id);pk"`
	ComTradeId         int       `description:"指标id"`
	IndexCode          string    `description:"联合国商品贸易编码"`
	Flow               string    `description:"贸易流向：X（Export：出口）；M（Import：进口）"`
	ReporterCode       int       `description:"出口国id"`
	ReporterName       string    `description:"出口国名称"`
	PartnerCode        int       `description:"进口国id"`
	PartnerName        string    `description:"进口国名称"`
	Partner2Code       int       `description:"第二进口国id"`
	Partner2Name       string    `description:"第二进口国名称"`
	DateType           int       `description:"日期类型，1：年度，2：月度"`
	DataTime           string    `description:"数据日期"`
	IndexTradeCode     string    `description:"离岸价编码"`
	TradeValue         float64   `description:"离岸价（美元）"`
	IndexNetWeightCode string    `description:"净重编码"`
	NetWeightValue     float64   `description:"净重/公斤"`
	ModifyTime         time.Time `description:"最新更新时间"`
	CreateTime         time.Time `description:"创建时间"`
}

// ComTradeDataListItem 联合国商品贸易数据列表数据
type ComTradeDataListItem struct {
	ComTradeDataId     int       `orm:"column(com_trade_data_id);pk"`
	ComTradeId         int       `description:"指标id"`
	IndexCode          string    `description:"联合国商品贸易编码"`
	IndexNameCn        string    `description:"联合国商品贸易名称（中文）"`
	Flow               string    `description:"贸易流向：X（Export：出口）；M（Import：进口）"`
	ReporterCode       int       `description:"出口国id"`
	ReporterName       string    `description:"出口国名称"`
	PartnerCode        int       `description:"进口国id"`
	PartnerName        string    `description:"进口国名称"`
	Partner2Code       int       `description:"第二进口国id"`
	Partner2Name       string    `description:"第二进口国名称"`
	DateType           int       `description:"日期类型，1：年度，2：月度"`
	DataTime           time.Time `description:"数据日期" json:"-"`
	Date               string    `description:"数据年月份"`
	IndexTradeCode     string    `description:"离岸价编码"`
	TradeValue         float64   `description:"离岸价（美元）"`
	IndexNetWeightCode string    `description:"净重编码"`
	NetWeightValue     float64   `description:"净重/公斤"`
	ModifyTime         time.Time `description:"最新更新时间"`
	CreateTime         time.Time `description:"创建时间"`
}

// ComTradeDataListResp 联合国商品贸易列表数据返回
type ComTradeDataListResp struct {
	Paging         *paging.PagingItem
	List           []*ComTradeDataListItem
	LastUpdateTime string
}

// GetComTradeListCount 获取指标数据汇总数
func GetComTradeListCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT COUNT(1) AS count FROM com_trade_data WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

// GetComTradeList 获取指标数据列表
func GetComTradeList(condition string, pars []interface{}, startSize, pageSize int, orderBy string) (list []*ComTradeDataListItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM com_trade_data WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY ` + orderBy
	sql += `  limit ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
	return
}
