|
@@ -4,10 +4,68 @@ import (
|
|
"eta/eta_api/utils"
|
|
"eta/eta_api/utils"
|
|
"fmt"
|
|
"fmt"
|
|
"github.com/beego/beego/v2/client/orm"
|
|
"github.com/beego/beego/v2/client/orm"
|
|
|
|
+ "strings"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
-// 上期能源持仓榜单表
|
|
|
|
|
|
+const (
|
|
|
|
+ TradeDataTypeNull = 0 // 无值
|
|
|
|
+ TradeDataTypeOrigin = 1 // 原始值
|
|
|
|
+ TradeDataTypeCalculate = 2 // 推算值
|
|
|
|
+
|
|
|
|
+ WarehouseBuyChartType = 1 // 多单图
|
|
|
|
+ WarehouseSoldChartType = 2 // 空单图
|
|
|
|
+ WarehousePureBuyChartType = 3 // 净多单图
|
|
|
|
+
|
|
|
|
+ WarehouseDefaultUnit = "手"
|
|
|
|
+ WarehouseDefaultFrequency = "日度"
|
|
|
|
+
|
|
|
|
+ GuangZhouTopCompanyAliasName = "日成交持仓排名" // 广期所TOP20对应的公司名称
|
|
|
|
+ GuangZhouSeatNameBuy = "持买单量" // 广期所指标名称中的多单名称
|
|
|
|
+ GuangZhouSeatNameSold = "持卖单量" // 广期所指标名称中的空单名称
|
|
|
|
+ GuangZhouTopSeatNameBuy = "持买单量总计" // 广期所指标名称中的TOP20多单名称
|
|
|
|
+ GuangZhouTopSeatNameSold = "持卖单量总计" // 广期所指标名称中的TOP20空单名称
|
|
|
|
+ GuangZhouTopSeatNameDeal = "成交量总计" // 广期所指标名称中的TOP20成交量名称
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+const (
|
|
|
|
+ TradeExchangeDalian = "dalian"
|
|
|
|
+ TradeExchangeZhengzhou = "zhengzhou"
|
|
|
|
+ TradeExchangeGuangzhou = "guangzhou"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+var WarehouseTypeSuffixNames = map[int]string{
|
|
|
|
+ WarehouseBuyChartType: "席位多单",
|
|
|
|
+ WarehouseSoldChartType: "席位空单",
|
|
|
|
+ WarehousePureBuyChartType: "席位净多单",
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GuangzhouSeatNameValType 广期所数据名称对应的席位方向
|
|
|
|
+var GuangzhouSeatNameValType = map[string]int{
|
|
|
|
+ GuangZhouSeatNameBuy: 1,
|
|
|
|
+ GuangZhouSeatNameSold: 2,
|
|
|
|
+ GuangZhouTopSeatNameBuy: 1,
|
|
|
|
+ GuangZhouTopSeatNameSold: 2,
|
|
|
|
+ GuangZhouTopSeatNameDeal: 3,
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 合约查询方式
|
|
|
|
+var (
|
|
|
|
+ ContractQueryTypeTop = 1 // 主力合约
|
|
|
|
+ ContractQueryTypeTop2 = 2 // 成交量前2
|
|
|
|
+ ContractQueryTypeTop3 = 3 // 成交量前3
|
|
|
|
+ ContractQueryTypeAll = 4 // 所有合约(多个)
|
|
|
|
+ ContractQueryTypeTotal = 5 // 合约加总(1个)
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+// 合约方向
|
|
|
|
+var (
|
|
|
|
+ ContractPositionBuy = 1 // 多单
|
|
|
|
+ ContractPositionSold = 2 // 空单
|
|
|
|
+ ContractPositionPureBuy = 3 // 净多单
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+// TradePositionTop 持仓榜单
|
|
type TradePositionTop struct {
|
|
type TradePositionTop struct {
|
|
Id uint64 `gorm:"primaryKey;column:id" json:"id"`
|
|
Id uint64 `gorm:"primaryKey;column:id" json:"id"`
|
|
ClassifyName string `gorm:"column:classify_name" json:"classify_name"` //分类名称
|
|
ClassifyName string `gorm:"column:classify_name" json:"classify_name"` //分类名称
|
|
@@ -173,220 +231,90 @@ type OriginTradeData struct {
|
|
ValType int `description:"数据类型: 1-多单; 2-空单"`
|
|
ValType int `description:"数据类型: 1-多单; 2-空单"`
|
|
}
|
|
}
|
|
|
|
|
|
-// GetTradeDataByClassifyAndCompany 根据品种和公司名称获取持仓数据
|
|
|
|
-func GetTradeDataByClassifyAndCompany(exchange, classifyName string, contracts, companies []string) (items []*OriginTradeData, err error) {
|
|
|
|
|
|
+// BaseFromTradeCommonIndex 郑商所/大商所/上期所/上期能源指标表通用字段
|
|
|
|
+type BaseFromTradeCommonIndex struct {
|
|
|
|
+ Rank int `description:"排名"`
|
|
|
|
+ DealShortName string `description:"成交量公司简称"`
|
|
|
|
+ DealName string `description:"成交量指标名称"`
|
|
|
|
+ DealCode string `description:"成交量指标编码"`
|
|
|
|
+ DealValue int `description:"成交量"`
|
|
|
|
+ DealChange int `description:"成交变化量"`
|
|
|
|
+ BuyShortName string `description:"持买单量公司简称"`
|
|
|
|
+ BuyName string `description:"持买单量指标名称"`
|
|
|
|
+ BuyCode string `description:"持买单量指标编码"`
|
|
|
|
+ BuyValue int `description:"持买单量"`
|
|
|
|
+ BuyChange int `description:"持买单量变化量"`
|
|
|
|
+ SoldShortName string `description:"持卖单量公司简称"`
|
|
|
|
+ SoldName string `description:"持卖单量指标名称"`
|
|
|
|
+ SoldCode string `description:"持卖单量指标编码"`
|
|
|
|
+ SoldValue int `description:"持卖单量"`
|
|
|
|
+ SoldChange int `description:"持卖单变化量"`
|
|
|
|
+ Frequency string `description:"频度"`
|
|
|
|
+ ClassifyName string `description:"品种"`
|
|
|
|
+ ClassifyType string `description:"合约"`
|
|
|
|
+ CreateTime time.Time `description:"创建时间"`
|
|
|
|
+ ModifyTime time.Time `description:"更新时间"`
|
|
|
|
+ DataTime time.Time `description:"数据日期"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetTradeDataByContracts 根据合约获取持仓数据
|
|
|
|
+func GetTradeDataByContracts(exchange string, classifyNames, contracts []string, startDate, endDate time.Time) (items []*BaseFromTradeCommonIndex, err error) {
|
|
if exchange == "" {
|
|
if exchange == "" {
|
|
err = fmt.Errorf("数据表名称有误")
|
|
err = fmt.Errorf("数据表名称有误")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- if len(contracts) == 0 || len(companies) == 0 {
|
|
|
|
- return
|
|
|
|
|
|
+ var cond string
|
|
|
|
+ var pars []interface{}
|
|
|
|
+ if len(classifyNames) > 0 {
|
|
|
|
+ cond += fmt.Sprintf(` AND classify_name IN (%s)`, utils.GetOrmInReplace(len(classifyNames)))
|
|
|
|
+ pars = append(pars, classifyNames)
|
|
}
|
|
}
|
|
- condBuy := fmt.Sprintf(`classify_name = ? AND classify_type IN (%s)`, utils.GetOrmInReplace(len(contracts)))
|
|
|
|
- parsBuy := make([]interface{}, 0)
|
|
|
|
- parsBuy = append(parsBuy, classifyName, contracts)
|
|
|
|
-
|
|
|
|
- condSold := fmt.Sprintf(`classify_name = ? AND classify_type IN (%s)`, utils.GetOrmInReplace(len(contracts)))
|
|
|
|
- parsSold := make([]interface{}, 0)
|
|
|
|
- parsSold = append(parsSold, classifyName, contracts)
|
|
|
|
-
|
|
|
|
- // 是否含有TOP20
|
|
|
|
- var hasTop bool
|
|
|
|
- var condCompanies []string
|
|
|
|
- for _, v := range companies {
|
|
|
|
- if v == TradeFuturesCompanyTop20 {
|
|
|
|
- hasTop = true
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- condCompanies = append(condCompanies, v)
|
|
|
|
|
|
+ if len(contracts) > 0 {
|
|
|
|
+ cond += fmt.Sprintf(` AND classify_type IN (%s)`, utils.GetOrmInReplace(len(contracts)))
|
|
|
|
+ pars = append(pars, contracts)
|
|
}
|
|
}
|
|
- if !hasTop {
|
|
|
|
- if len(condCompanies) == 0 {
|
|
|
|
- err = fmt.Errorf("查询条件-期货公司异常")
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- condBuy += fmt.Sprintf(` AND buy_short_name IN (%s)`, utils.GetOrmInReplace(len(condCompanies)))
|
|
|
|
- parsBuy = append(parsBuy, condCompanies)
|
|
|
|
- condSold += fmt.Sprintf(` AND sold_short_name IN (%s)`, utils.GetOrmInReplace(len(condCompanies)))
|
|
|
|
- parsSold = append(parsSold, condCompanies)
|
|
|
|
- } else {
|
|
|
|
- // 这里rank=0或者999是因为大商所的数据并不只有999
|
|
|
|
- if len(condCompanies) > 0 {
|
|
|
|
- condBuy += fmt.Sprintf(` AND (rank = 999 OR rank = 0 OR buy_short_name IN (%s))`, utils.GetOrmInReplace(len(condCompanies)))
|
|
|
|
- condSold += fmt.Sprintf(` AND (rank = 999 OR rank = 0 OR sold_short_name IN (%s))`, utils.GetOrmInReplace(len(condCompanies)))
|
|
|
|
- parsBuy = append(parsBuy, condCompanies)
|
|
|
|
- parsSold = append(parsSold, condCompanies)
|
|
|
|
- } else {
|
|
|
|
- condBuy += ` AND (rank = 999 OR rank = 0)`
|
|
|
|
- condSold += ` AND (rank = 999 OR rank = 0)`
|
|
|
|
- }
|
|
|
|
|
|
+ if !startDate.IsZero() && !endDate.IsZero() {
|
|
|
|
+ cond += ` AND (data_time BETWEEN ? AND ?)`
|
|
|
|
+ pars = append(pars, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ fields := []string{"rank", "buy_short_name", "buy_value", "buy_change", "sold_short_name", "sold_value", "sold_change", "classify_name", "classify_type", "data_time"}
|
|
tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
|
|
tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
|
|
- sql := `SELECT
|
|
|
|
- rank,
|
|
|
|
- buy_short_name AS company_name,
|
|
|
|
- buy_value AS val,
|
|
|
|
- buy_change AS val_change,
|
|
|
|
- classify_name,
|
|
|
|
- classify_type,
|
|
|
|
- data_time,
|
|
|
|
- 1 AS val_type
|
|
|
|
- FROM
|
|
|
|
- %s
|
|
|
|
- WHERE
|
|
|
|
- %s
|
|
|
|
- UNION ALL
|
|
|
|
- (
|
|
|
|
- SELECT
|
|
|
|
- rank,
|
|
|
|
- sold_short_name,
|
|
|
|
- sold_value,
|
|
|
|
- sold_change,
|
|
|
|
- classify_name,
|
|
|
|
- classify_type,
|
|
|
|
- data_time,
|
|
|
|
- 2 AS val_type
|
|
|
|
- FROM
|
|
|
|
- %s
|
|
|
|
- WHERE
|
|
|
|
- %s
|
|
|
|
- )`
|
|
|
|
- sql = fmt.Sprintf(sql, tableName, condBuy, tableName, condSold)
|
|
|
|
- o := orm.NewOrmUsingDB("data")
|
|
|
|
- _, err = o.Raw(sql, parsBuy, parsSold).QueryRows(&items)
|
|
|
|
|
|
+ sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s ORDER BY data_time DESC`, strings.Join(fields, ","), tableName, cond)
|
|
|
|
+ _, err = orm.NewOrmUsingDB("data").Raw(sql, pars).QueryRows(&items)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-// GetTradeZhengzhouDataByClassifyAndCompany 郑商所-根据品种和公司名称获取持仓数据
|
|
|
|
-func GetTradeZhengzhouDataByClassifyAndCompany(exchange string, contracts, companies []string) (items []*OriginTradeData, err error) {
|
|
|
|
- if exchange == "" {
|
|
|
|
- err = fmt.Errorf("数据表名称有误")
|
|
|
|
- return
|
|
|
|
|
|
+// GetZhengzhouTradeDataByContracts 郑商所-根据合约获取持仓数据
|
|
|
|
+func GetZhengzhouTradeDataByContracts(classifyNames []string, startDate, endDate time.Time) (items []*BaseFromTradeCommonIndex, err error) {
|
|
|
|
+ var cond string
|
|
|
|
+ var pars []interface{}
|
|
|
|
+ if len(classifyNames) > 0 {
|
|
|
|
+ cond += fmt.Sprintf(` AND classify_name IN (%s)`, utils.GetOrmInReplace(len(classifyNames)))
|
|
|
|
+ pars = append(pars, classifyNames)
|
|
}
|
|
}
|
|
- if len(contracts) == 0 || len(companies) == 0 {
|
|
|
|
- return
|
|
|
|
|
|
+ if !startDate.IsZero() && !endDate.IsZero() {
|
|
|
|
+ cond += ` AND (data_time BETWEEN ? AND ?)`
|
|
|
|
+ pars = append(pars, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
|
|
}
|
|
}
|
|
- condBuy := fmt.Sprintf(`classify_name IN (%s)`, utils.GetOrmInReplace(len(contracts)))
|
|
|
|
- parsBuy := make([]interface{}, 0)
|
|
|
|
- parsBuy = append(parsBuy, contracts)
|
|
|
|
-
|
|
|
|
- condSold := fmt.Sprintf(`classify_name IN (%s)`, utils.GetOrmInReplace(len(contracts)))
|
|
|
|
- parsSold := make([]interface{}, 0)
|
|
|
|
- parsSold = append(parsSold, contracts)
|
|
|
|
-
|
|
|
|
- // 是否含有TOP20
|
|
|
|
- var hasTop bool
|
|
|
|
- var condCompanies []string
|
|
|
|
- for _, v := range companies {
|
|
|
|
- if v == TradeFuturesCompanyTop20 {
|
|
|
|
- hasTop = true
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- condCompanies = append(condCompanies, v)
|
|
|
|
- }
|
|
|
|
- if !hasTop {
|
|
|
|
- if len(condCompanies) == 0 {
|
|
|
|
- err = fmt.Errorf("查询条件-期货公司异常")
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- condBuy += fmt.Sprintf(` AND buy_short_name IN (%s)`, utils.GetOrmInReplace(len(condCompanies)))
|
|
|
|
- parsBuy = append(parsBuy, condCompanies)
|
|
|
|
- condSold += fmt.Sprintf(` AND sold_short_name IN (%s)`, utils.GetOrmInReplace(len(condCompanies)))
|
|
|
|
- parsSold = append(parsSold, condCompanies)
|
|
|
|
- } else {
|
|
|
|
- if len(condCompanies) > 0 {
|
|
|
|
- condBuy += fmt.Sprintf(` AND (rank = 999 OR buy_short_name IN (%s))`, utils.GetOrmInReplace(len(condCompanies)))
|
|
|
|
- condSold += fmt.Sprintf(` AND (rank = 999 OR sold_short_name IN (%s))`, utils.GetOrmInReplace(len(condCompanies)))
|
|
|
|
- parsBuy = append(parsBuy, condCompanies)
|
|
|
|
- parsSold = append(parsSold, condCompanies)
|
|
|
|
- } else {
|
|
|
|
- condBuy += ` AND rank = 999`
|
|
|
|
- condSold += ` AND rank = 999`
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
|
|
|
|
- sql := `SELECT
|
|
|
|
- rank,
|
|
|
|
- buy_short_name AS company_name,
|
|
|
|
- buy_value AS val,
|
|
|
|
- buy_change AS val_change,
|
|
|
|
- classify_name AS classify_type,
|
|
|
|
- data_time,
|
|
|
|
- 1 AS val_type
|
|
|
|
- FROM
|
|
|
|
- %s
|
|
|
|
- WHERE
|
|
|
|
- %s
|
|
|
|
- UNION ALL
|
|
|
|
- (
|
|
|
|
- SELECT
|
|
|
|
- rank,
|
|
|
|
- sold_short_name,
|
|
|
|
- sold_value,
|
|
|
|
- sold_change,
|
|
|
|
- classify_name AS classify_type,
|
|
|
|
- data_time,
|
|
|
|
- 2 AS val_type
|
|
|
|
- FROM
|
|
|
|
- %s
|
|
|
|
- WHERE
|
|
|
|
- %s
|
|
|
|
- )`
|
|
|
|
- sql = fmt.Sprintf(sql, tableName, condBuy, tableName, condSold)
|
|
|
|
- o := orm.NewOrmUsingDB("data")
|
|
|
|
- _, err = o.Raw(sql, parsBuy, parsSold).QueryRows(&items)
|
|
|
|
|
|
+ // ps.classify_name实为合约代码
|
|
|
|
+ fields := []string{"rank", "buy_short_name", "buy_value", "buy_change", "sold_short_name", "sold_value", "sold_change", "classify_name AS classify_type", "data_time"}
|
|
|
|
+ sql := fmt.Sprintf(`SELECT %s FROM base_from_trade_zhengzhou_index WHERE 1=1 %s ORDER BY data_time DESC`, strings.Join(fields, ","), cond)
|
|
|
|
+ _, err = orm.NewOrmUsingDB("data").Raw(sql, pars).QueryRows(&items)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
// ContractCompanyTradeData [合约-期货公司]持仓数据
|
|
// ContractCompanyTradeData [合约-期货公司]持仓数据
|
|
type ContractCompanyTradeData struct {
|
|
type ContractCompanyTradeData struct {
|
|
- CompanyName string `description:"期货公司名称"`
|
|
|
|
|
|
+ Exchange string `description:"交易所"`
|
|
|
|
+ ClassifyName string `description:"品种"`
|
|
ClassifyType string `description:"合约代码"`
|
|
ClassifyType string `description:"合约代码"`
|
|
|
|
+ CompanyName string `description:"期货公司名称"`
|
|
|
|
+ IsTotal bool `description:"是否为合约加总"`
|
|
StartDate time.Time `description:"数据开始日期"`
|
|
StartDate time.Time `description:"数据开始日期"`
|
|
EndDate time.Time `description:"数据结束日期"`
|
|
EndDate time.Time `description:"数据结束日期"`
|
|
DataList []*ContractCompanyTradeDataList `description:"数据序列"`
|
|
DataList []*ContractCompanyTradeDataList `description:"数据序列"`
|
|
}
|
|
}
|
|
|
|
|
|
-const (
|
|
|
|
- TradeDataTypeNull = 0 // 无值
|
|
|
|
- TradeDataTypeOrigin = 1 // 原始值
|
|
|
|
- TradeDataTypeCalculate = 2 // 推算值
|
|
|
|
-
|
|
|
|
- WarehouseBuyChartType = 1 // 多单图
|
|
|
|
- WarehouseSoldChartType = 2 // 空单图
|
|
|
|
- WarehousePureBuyChartType = 3 // 净多单图
|
|
|
|
-
|
|
|
|
- WarehouseDefaultUnit = "手"
|
|
|
|
- WarehouseDefaultFrequency = "日度"
|
|
|
|
-
|
|
|
|
- GuangZhouTopCompanyAliasName = "日成交持仓排名" // 广期所TOP20对应的公司名称
|
|
|
|
- GuangZhouSeatNameBuy = "持买单量" // 广期所指标名称中的多单名称
|
|
|
|
- GuangZhouSeatNameSold = "持卖单量" // 广期所指标名称中的空单名称
|
|
|
|
- GuangZhouTopSeatNameBuy = "持买单量总计" // 广期所指标名称中的TOP20多单名称
|
|
|
|
- GuangZhouTopSeatNameSold = "持卖单量总计" // 广期所指标名称中的TOP20空单名称
|
|
|
|
-)
|
|
|
|
-
|
|
|
|
-const (
|
|
|
|
- TradeExchangeZhengzhou = "zhengzhou"
|
|
|
|
- TradeExchangeGuangzhou = "guangzhou"
|
|
|
|
-)
|
|
|
|
-
|
|
|
|
-var WarehouseTypeSuffixNames = map[int]string{
|
|
|
|
- WarehouseBuyChartType: "席位多单",
|
|
|
|
- WarehouseSoldChartType: "席位空单",
|
|
|
|
- WarehousePureBuyChartType: "席位净多单",
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// GuangzhouSeatNameValType 广期所数据名称对应的席位方向
|
|
|
|
-var GuangzhouSeatNameValType = map[string]int{
|
|
|
|
- GuangZhouSeatNameBuy: 1,
|
|
|
|
- GuangZhouSeatNameSold: 2,
|
|
|
|
- GuangZhouTopSeatNameBuy: 1,
|
|
|
|
- GuangZhouTopSeatNameSold: 2,
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
// ContractCompanyTradeDataList [合约-期货公司]持仓数据详情
|
|
// ContractCompanyTradeDataList [合约-期货公司]持仓数据详情
|
|
type ContractCompanyTradeDataList struct {
|
|
type ContractCompanyTradeDataList struct {
|
|
Date time.Time `description:"数据日期"`
|
|
Date time.Time `description:"数据日期"`
|
|
@@ -404,139 +332,6 @@ type ContractCompanyTradeDataList struct {
|
|
PureBuyChangeType int `description:"净多单持仓增减类型: 0-无值; 1-原始值; 2-推算值"`
|
|
PureBuyChangeType int `description:"净多单持仓增减类型: 0-无值; 1-原始值; 2-推算值"`
|
|
}
|
|
}
|
|
|
|
|
|
-// GetLastTradeDataByClassify 获取[合约]末位多空单数据
|
|
|
|
-func GetLastTradeDataByClassify(exchange, classifyName string, contracts []string) (items []*OriginTradeData, err error) {
|
|
|
|
- if exchange == "" {
|
|
|
|
- err = fmt.Errorf("数据表名称有误")
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- if len(contracts) == 0 {
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- contractReplacer := utils.GetOrmInReplace(len(contracts))
|
|
|
|
-
|
|
|
|
- tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
|
|
|
|
- sql := `SELECT
|
|
|
|
- tpt.rank,
|
|
|
|
- tpt.buy_short_name AS company_name,
|
|
|
|
- tpt.buy_value AS val,
|
|
|
|
- tpt.buy_change AS val_change,
|
|
|
|
- tpt.classify_name,
|
|
|
|
- tpt.classify_type,
|
|
|
|
- tpt.data_time,
|
|
|
|
- 1 AS val_type
|
|
|
|
- FROM
|
|
|
|
- %s tpt
|
|
|
|
- JOIN
|
|
|
|
- (
|
|
|
|
- SELECT
|
|
|
|
- data_time, classify_type, MAX(rank) AS max_rank
|
|
|
|
- FROM
|
|
|
|
- %s
|
|
|
|
- WHERE
|
|
|
|
- classify_name = ? AND classify_type IN (%s) AND buy_short_name <> ''
|
|
|
|
- GROUP BY
|
|
|
|
- data_time,
|
|
|
|
- classify_type
|
|
|
|
- ) sub
|
|
|
|
- ON
|
|
|
|
- tpt.data_time = sub.data_time AND tpt.classify_type = sub.classify_type AND tpt.rank = sub.max_rank
|
|
|
|
- WHERE
|
|
|
|
- tpt.classify_name = ? AND tpt.classify_type IN (%s)
|
|
|
|
- UNION ALL
|
|
|
|
- (
|
|
|
|
- SELECT
|
|
|
|
- tpt.rank, tpt.sold_short_name, tpt.sold_value, tpt.sold_change, tpt.classify_name, tpt.classify_type, tpt.data_time, 2 AS val_type
|
|
|
|
- FROM
|
|
|
|
- %s tpt
|
|
|
|
- JOIN
|
|
|
|
- (
|
|
|
|
- SELECT
|
|
|
|
- data_time, classify_type, MAX(rank) AS max_rank
|
|
|
|
- FROM
|
|
|
|
- %s
|
|
|
|
- WHERE
|
|
|
|
- classify_name = ? AND classify_type IN (%s) AND sold_short_name <> ''
|
|
|
|
- GROUP BY
|
|
|
|
- data_time, classify_type
|
|
|
|
- ) sub
|
|
|
|
- ON
|
|
|
|
- tpt.data_time = sub.data_time AND tpt.classify_type = sub.classify_type AND tpt.rank = sub.max_rank
|
|
|
|
- WHERE
|
|
|
|
- tpt.classify_name = ? AND tpt.classify_type IN (%s)
|
|
|
|
- )`
|
|
|
|
- sql = fmt.Sprintf(sql, tableName, tableName, contractReplacer, contractReplacer, tableName, tableName, contractReplacer, contractReplacer)
|
|
|
|
- o := orm.NewOrmUsingDB("data")
|
|
|
|
- _, err = o.Raw(sql, classifyName, contracts, classifyName, contracts, classifyName, contracts, classifyName, contracts).QueryRows(&items)
|
|
|
|
- return
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// GetLastTradeZhengzhouDataByClassify 郑商所-获取[合约]末位多空单数据
|
|
|
|
-func GetLastTradeZhengzhouDataByClassify(exchange string, contracts []string) (items []*OriginTradeData, err error) {
|
|
|
|
- if exchange == "" {
|
|
|
|
- err = fmt.Errorf("数据表名称有误")
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- if len(contracts) == 0 {
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- contractReplacer := utils.GetOrmInReplace(len(contracts))
|
|
|
|
-
|
|
|
|
- tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
|
|
|
|
- sql := `SELECT
|
|
|
|
- tpt.rank,
|
|
|
|
- tpt.buy_short_name AS company_name,
|
|
|
|
- tpt.buy_value AS val,
|
|
|
|
- tpt.buy_change AS val_change,
|
|
|
|
- tpt.classify_name AS classify_type,
|
|
|
|
- tpt.data_time,
|
|
|
|
- 1 AS val_type
|
|
|
|
- FROM
|
|
|
|
- %s tpt
|
|
|
|
- JOIN
|
|
|
|
- (
|
|
|
|
- SELECT
|
|
|
|
- data_time, classify_name, MAX(rank) AS max_rank
|
|
|
|
- FROM
|
|
|
|
- %s
|
|
|
|
- WHERE
|
|
|
|
- classify_name IN (%s) AND buy_short_name <> ''
|
|
|
|
- GROUP BY
|
|
|
|
- data_time,
|
|
|
|
- classify_name
|
|
|
|
- ) sub
|
|
|
|
- ON
|
|
|
|
- tpt.data_time = sub.data_time AND tpt.classify_name = sub.classify_name AND tpt.rank = sub.max_rank
|
|
|
|
- WHERE
|
|
|
|
- tpt.classify_name IN (%s)
|
|
|
|
- UNION ALL
|
|
|
|
- (
|
|
|
|
- SELECT
|
|
|
|
- tpt.rank, tpt.sold_short_name, tpt.sold_value, tpt.sold_change, tpt.classify_name AS classify_type, tpt.data_time, 2 AS val_type
|
|
|
|
- FROM
|
|
|
|
- %s tpt
|
|
|
|
- JOIN
|
|
|
|
- (
|
|
|
|
- SELECT
|
|
|
|
- data_time, classify_name, MAX(rank) AS max_rank
|
|
|
|
- FROM
|
|
|
|
- %s
|
|
|
|
- WHERE
|
|
|
|
- classify_name IN (%s) AND sold_short_name <> ''
|
|
|
|
- GROUP BY
|
|
|
|
- data_time, classify_name
|
|
|
|
- ) sub
|
|
|
|
- ON
|
|
|
|
- tpt.data_time = sub.data_time AND tpt.classify_name = sub.classify_name AND tpt.rank = sub.max_rank
|
|
|
|
- WHERE
|
|
|
|
- tpt.classify_name IN (%s)
|
|
|
|
- )`
|
|
|
|
- sql = fmt.Sprintf(sql, tableName, tableName, contractReplacer, contractReplacer, tableName, tableName, contractReplacer, contractReplacer)
|
|
|
|
- o := orm.NewOrmUsingDB("data")
|
|
|
|
- _, err = o.Raw(sql, contracts, contracts, contracts, contracts).QueryRows(&items)
|
|
|
|
- return
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
type BaseFromTradeGuangzhouIndex struct {
|
|
type BaseFromTradeGuangzhouIndex struct {
|
|
BaseFromTradeGuangzhouIndexId int `orm:"column(base_from_trade_guangzhou_index_id);pk"`
|
|
BaseFromTradeGuangzhouIndexId int `orm:"column(base_from_trade_guangzhou_index_id);pk"`
|
|
BaseFromTradeGuangzhouClassifyId int `description:"分类id"`
|
|
BaseFromTradeGuangzhouClassifyId int `description:"分类id"`
|
|
@@ -550,10 +345,27 @@ type BaseFromTradeGuangzhouIndex struct {
|
|
ModifyTime time.Time `description:"修改日期"`
|
|
ModifyTime time.Time `description:"修改日期"`
|
|
}
|
|
}
|
|
|
|
|
|
-func GetBaseFromTradeGuangzhouIndexByClassifyId(classifyId int) (list []*BaseFromTradeGuangzhouIndex, err error) {
|
|
|
|
|
|
+func GetBaseFromTradeGuangzhouIndex(classifyIds []int, contracts []string, indexKeyword string) (list []*BaseFromTradeGuangzhouIndex, err error) {
|
|
o := orm.NewOrmUsingDB("data")
|
|
o := orm.NewOrmUsingDB("data")
|
|
- sql := `SELECT * FROM base_from_trade_guangzhou_index WHERE base_from_trade_guangzhou_classify_id = ?`
|
|
|
|
- _, err = o.Raw(sql, classifyId).QueryRows(&list)
|
|
|
|
|
|
+ cond := ``
|
|
|
|
+ pars := make([]interface{}, 0)
|
|
|
|
+ if len(classifyIds) > 0 {
|
|
|
|
+ cond += fmt.Sprintf(` AND b.base_from_trade_guangzhou_classify_id IN (%s)`, utils.GetOrmInReplace(len(classifyIds)))
|
|
|
|
+ pars = append(pars, classifyIds)
|
|
|
|
+ }
|
|
|
|
+ if len(contracts) > 0 {
|
|
|
|
+ cond += fmt.Sprintf(` AND b.contract IN (%s)`, utils.GetOrmInReplace(len(contracts)))
|
|
|
|
+ pars = append(pars, contracts)
|
|
|
|
+ }
|
|
|
|
+ if indexKeyword != "" {
|
|
|
|
+ cond += fmt.Sprintf(` AND a.index_name LIKE ?`)
|
|
|
|
+ pars = append(pars, indexKeyword)
|
|
|
|
+ }
|
|
|
|
+ sql := `SELECT a.* FROM base_from_trade_guangzhou_index AS a
|
|
|
|
+ JOIN base_from_trade_guangzhou_contract AS b ON a.base_from_trade_guangzhou_contract_id = b.base_from_trade_guangzhou_contract_id
|
|
|
|
+ WHERE 1=1 %s`
|
|
|
|
+ sql = fmt.Sprintf(sql, cond)
|
|
|
|
+ _, err = o.Raw(sql, pars).QueryRows(&list)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
@@ -568,45 +380,120 @@ type BaseFromTradeGuangzhouData struct {
|
|
ModifyTime time.Time `description:"修改日期"`
|
|
ModifyTime time.Time `description:"修改日期"`
|
|
}
|
|
}
|
|
|
|
|
|
-// GetBaseFromTradeGuangzhouDataByIndexIds 获取指标数据
|
|
|
|
-func GetBaseFromTradeGuangzhouDataByIndexIds(indexIds []int) (list []*BaseFromTradeGuangzhouData, err error) {
|
|
|
|
|
|
+// GetBaseFromTradeGuangzhouDataByIndexIds 广期所-获取指标数据
|
|
|
|
+func GetBaseFromTradeGuangzhouDataByIndexIds(indexIds []int, startDate, endDate time.Time) (list []*BaseFromTradeGuangzhouData, err error) {
|
|
if len(indexIds) == 0 {
|
|
if len(indexIds) == 0 {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- o := orm.NewOrmUsingDB("data")
|
|
|
|
- sql := fmt.Sprintf(`SELECT * FROM base_from_trade_guangzhou_data WHERE base_from_trade_guangzhou_index_id IN (%s) ORDER BY base_from_trade_guangzhou_index_id`, utils.GetOrmInReplace(len(indexIds)))
|
|
|
|
- _, err = o.Raw(sql, indexIds).QueryRows(&list)
|
|
|
|
|
|
+ cond := fmt.Sprintf(` AND base_from_trade_guangzhou_index_id IN (%s)`, utils.GetOrmInReplace(len(indexIds)))
|
|
|
|
+ pars := make([]interface{}, 0)
|
|
|
|
+ pars = append(pars, indexIds)
|
|
|
|
+ if !startDate.IsZero() && !endDate.IsZero() {
|
|
|
|
+ if startDate.Equal(endDate) {
|
|
|
|
+ cond += ` AND data_time = ?`
|
|
|
|
+ pars = append(pars, startDate.Format(utils.FormatDate))
|
|
|
|
+ }
|
|
|
|
+ if !startDate.Equal(endDate) {
|
|
|
|
+ cond += ` AND (data_time BETWEEN ? AND ?)`
|
|
|
|
+ pars = append(pars, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ sql := fmt.Sprintf(`SELECT * FROM base_from_trade_guangzhou_data WHERE 1=1 %s ORDER BY base_from_trade_guangzhou_index_id`, cond)
|
|
|
|
+ _, err = orm.NewOrmUsingDB("data").Raw(sql, pars).QueryRows(&list)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-// GetBaseFromTradeGuangzhouMinDataByIndexIds 获取指标中的末位数据
|
|
|
|
-func GetBaseFromTradeGuangzhouMinDataByIndexIds(indexIds []int) (list []*BaseFromTradeGuangzhouData, err error) {
|
|
|
|
- indexLen := len(indexIds)
|
|
|
|
- if indexLen == 0 {
|
|
|
|
|
|
+// ContractTopRankData TOP20合约排名数据
|
|
|
|
+type ContractTopRankData struct {
|
|
|
|
+ Exchange string `description:"交易所"`
|
|
|
|
+ DealValue int `description:"成交量"`
|
|
|
|
+ BuyValue int `description:"多单持仓量"`
|
|
|
|
+ BuyChange int `description:"多单变化"`
|
|
|
|
+ SoldValue int `description:"空单持仓量"`
|
|
|
|
+ SoldChange int `description:"空单变化"`
|
|
|
|
+ PureBuyValue int `description:"净多单持仓量"`
|
|
|
|
+ PureBuyChange int `description:"净多单变化"`
|
|
|
|
+ ClassifyName string `description:"品种名称"`
|
|
|
|
+ ClassifyType string `description:"合约代码"`
|
|
|
|
+ DataTime time.Time `description:"数据日期"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetContractTopRankData 获取合约TOP20根据当日成交量排名
|
|
|
|
+func GetContractTopRankData(exchange string, classifyNames []string, dataDate time.Time) (items []*ContractTopRankData, err error) {
|
|
|
|
+ if exchange == "" {
|
|
|
|
+ err = fmt.Errorf("数据表名称有误")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- o := orm.NewOrmUsingDB("data")
|
|
|
|
- sql := fmt.Sprintf(`SELECT
|
|
|
|
- t1.data_time,
|
|
|
|
- t1.min_value AS value
|
|
|
|
- FROM
|
|
|
|
- (
|
|
|
|
- SELECT
|
|
|
|
- data_time,
|
|
|
|
- MIN(value) AS min_value
|
|
|
|
- FROM
|
|
|
|
- base_from_trade_guangzhou_data
|
|
|
|
- WHERE
|
|
|
|
- base_from_trade_guangzhou_index_id IN (%s)
|
|
|
|
- GROUP BY
|
|
|
|
- data_time
|
|
|
|
- ) t1
|
|
|
|
- JOIN
|
|
|
|
- base_from_trade_guangzhou_data t2
|
|
|
|
- ON
|
|
|
|
- t1.data_time = t2.data_time AND t1.min_value = t2.value AND t2.base_from_trade_guangzhou_index_id IN (%s)
|
|
|
|
- GROUP BY
|
|
|
|
- t1.data_time`, utils.GetOrmInReplace(indexLen), utils.GetOrmInReplace(indexLen))
|
|
|
|
- _, err = o.Raw(sql, indexIds, indexIds).QueryRows(&list)
|
|
|
|
|
|
+ if len(classifyNames) == 0 {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
|
|
|
|
+ // 大商所存在TOP20的rank=0
|
|
|
|
+ queryRank := ` rank = 999`
|
|
|
|
+ if exchange == TradeExchangeDalian {
|
|
|
|
+ queryRank = ` (rank = 999 OR rank = 0)`
|
|
|
|
+ }
|
|
|
|
+ sql := `SELECT * FROM %s WHERE data_time = ? AND classify_name IN (%s) AND %s GROUP BY classify_type ORDER BY deal_value DESC`
|
|
|
|
+ sql = fmt.Sprintf(sql, tableName, utils.GetOrmInReplace(len(classifyNames)), queryRank)
|
|
|
|
+ _, err = orm.NewOrmUsingDB("data").Raw(sql, dataDate.Format(utils.FormatDate), classifyNames).QueryRows(&items)
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetZhengzhouContractTopRankData 郑商所-获取合约根据当日成交量排名
|
|
|
|
+func GetZhengzhouContractTopRankData(classifyNames []string, dataDate time.Time) (items []*ContractTopRankData, err error) {
|
|
|
|
+ if len(classifyNames) == 0 {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ sql := `SELECT * FROM base_from_trade_zhengzhou_index WHERE data_time = ? AND classify_name IN (%s) AND rank = 999 GROUP BY classify_name ORDER BY deal_value DESC`
|
|
|
|
+ sql = fmt.Sprintf(sql, utils.GetOrmInReplace(len(classifyNames)))
|
|
|
|
+ _, err = orm.NewOrmUsingDB("data").Raw(sql, dataDate.Format(utils.FormatDate), classifyNames).QueryRows(&items)
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// ContractCompanyTradeEdb [合约-期货公司]指标
|
|
|
|
+type ContractCompanyTradeEdb struct {
|
|
|
|
+ Exchange string `description:"交易所"`
|
|
|
|
+ ClassifyName string `description:"品种"`
|
|
|
|
+ ClassifyType string `description:"合约代码"`
|
|
|
|
+ CompanyName string `description:"期货公司名称"`
|
|
|
|
+ IsTotal bool `description:"是否为合约加总"`
|
|
|
|
+ ContractPosition int `description:"合约方向"`
|
|
|
|
+ StartDate time.Time `description:"数据开始日期"`
|
|
|
|
+ EndDate time.Time `description:"数据结束日期"`
|
|
|
|
+ DataList []*ContractCompanyTradeEdbData `description:"数据序列"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// ContractCompanyTradeEdbData [合约-期货公司]指标数据
|
|
|
|
+type ContractCompanyTradeEdbData struct {
|
|
|
|
+ DataTime time.Time `description:"数据日期"`
|
|
|
|
+ Val int `description:"数据值"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetClassifyNewestDataTime 获取品种最新数据日期
|
|
|
|
+func GetClassifyNewestDataTime(exchange string, classifyNames []string) (dateTime time.Time, err error) {
|
|
|
|
+ if exchange == "" {
|
|
|
|
+ err = fmt.Errorf("数据表名称有误")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if len(classifyNames) == 0 {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
|
|
|
|
+ sql := `SELECT data_time FROM %s WHERE classify_name IN (%s) ORDER BY data_time DESC LIMIT 1`
|
|
|
|
+ sql = fmt.Sprintf(sql, tableName, utils.GetOrmInReplace(len(classifyNames)))
|
|
|
|
+ err = orm.NewOrmUsingDB("data").Raw(sql, classifyNames).QueryRow(&dateTime)
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetGuangzhouClassifyNewestDataTime 广期所-获取品种最新数据日期
|
|
|
|
+func GetGuangzhouClassifyNewestDataTime(indexIds []int) (dateTime time.Time, err error) {
|
|
|
|
+ if len(indexIds) == 0 {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ cond := fmt.Sprintf(` AND base_from_trade_guangzhou_index_id IN (%s)`, utils.GetOrmInReplace(len(indexIds)))
|
|
|
|
+ pars := make([]interface{}, 0)
|
|
|
|
+ pars = append(pars, indexIds)
|
|
|
|
+ sql := fmt.Sprintf(`SELECT data_time FROM base_from_trade_guangzhou_data WHERE 1=1 %s ORDER BY data_time DESC LIMIT 1`, cond)
|
|
|
|
+ err = orm.NewOrmUsingDB("data").Raw(sql, pars).QueryRow(&dateTime)
|
|
return
|
|
return
|
|
}
|
|
}
|