trade_analysis.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. package trade_analysis
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. // 上期能源持仓榜单表
  9. type TradePositionTop struct {
  10. Id uint64 `gorm:"primaryKey;column:id" json:"id"`
  11. ClassifyName string `gorm:"column:classify_name" json:"classify_name"` //分类名称
  12. ClassifyType string `gorm:"column:classify_type" json:"classify_type"` //分类名称下的类型
  13. DealShortName string `gorm:"column:deal_short_name" json:"deal_short_name"` //成交量公司简称
  14. DealValue int `gorm:"column:deal_value" json:"deal_value"` //成交量
  15. DealChange int `gorm:"column:deal_change" json:"deal_change"` //成交变化量
  16. DataTime time.Time `gorm:"column:data_time" json:"data_time"` //数据日期
  17. CreateTime time.Time `gorm:"column:create_time" json:"create_time"` //插入时间
  18. ModifyTime time.Time `gorm:"column:modify_time" json:"modify_time"` //修改时间
  19. DealType int `gorm:"column:deal_type" json:"deal_type"` //交易类型:1多单,2空单,3净多单,4净空单
  20. SourceType int `gorm:"column:source_type" json:"source_type"` //数据来源,0是原始数据的值,1是由T+1日推算出的值,2是由T日的榜单数据推算出的值
  21. Rank int `gorm:"column:rank" json:"rank"` //排名
  22. }
  23. type TradeClassifyNameList struct {
  24. Exchange string `description:"交易所"`
  25. ExchangeEn string `description:"交易所英文"`
  26. Sort int `description:"排序字段" `
  27. Num int `description:"品种数量"`
  28. DataTime string `description:"最新更新时间"`
  29. CurrDate string `description:"当前日期"`
  30. Items []TradeClassifyNameListItem `description:"子类"`
  31. }
  32. type TradeClassifyNameListSort []TradeClassifyNameList
  33. func (v TradeClassifyNameListSort) Len() int {
  34. return len(v)
  35. }
  36. func (v TradeClassifyNameListSort) Swap(i, j int) {
  37. v[i], v[j] = v[j], v[i]
  38. }
  39. func (v TradeClassifyNameListSort) Less(i, j int) bool {
  40. return v[i].Sort < v[j].Sort
  41. }
  42. type TradeClassifyNameListItemSort []TradeClassifyNameListItem
  43. func (v TradeClassifyNameListItemSort) Len() int {
  44. return len(v)
  45. }
  46. func (v TradeClassifyNameListItemSort) Swap(i, j int) {
  47. v[i], v[j] = v[j], v[i]
  48. }
  49. func (v TradeClassifyNameListItemSort) Less(i, j int) bool {
  50. return v[i].ClassifyName < v[j].ClassifyName
  51. }
  52. type TradeClassifyNameListItem struct {
  53. ClassifyName string `description:"交易分类"`
  54. Items []TradeClassifyNameListItemItem `description:"合约代码"`
  55. }
  56. type TradeClassifyNameListItemItemSort []TradeClassifyNameListItemItem
  57. func (v TradeClassifyNameListItemItemSort) Len() int {
  58. return len(v)
  59. }
  60. func (v TradeClassifyNameListItemItemSort) Swap(i, j int) {
  61. v[i], v[j] = v[j], v[i]
  62. }
  63. func (v TradeClassifyNameListItemItemSort) Less(i, j int) bool {
  64. return v[i].ClassifyType < v[j].ClassifyType
  65. }
  66. type TradeClassifyNameListItemItem struct {
  67. ClassifyType string `description:"分类名称下的类型"`
  68. }
  69. type TradeClassifyName struct {
  70. ClassifyName string //分类名称
  71. ClassifyType string //分类名称下的类型
  72. LatestDate string //分类下最晚日期
  73. }
  74. // GetExchangeClassify 获取交易所分类列表
  75. func GetExchangeClassify(exchange string) (list []TradeClassifyName, err error) {
  76. tableName := "base_from_trade_" + exchange + "_index"
  77. orderStr := "classify_name DESC, classify_type asc"
  78. if exchange == "zhengzhou" {
  79. orderStr = "classify_name asc"
  80. }
  81. sql := "SELECT classify_name, classify_type FROM " + tableName + " WHERE `rank` <=20 and `rank` > 0 GROUP BY classify_name, classify_type "
  82. sql += ` ORDER BY ` + orderStr
  83. o := orm.NewOrmUsingDB("data")
  84. _, err = o.Raw(sql).QueryRows(&list)
  85. return
  86. }
  87. type LastTimeItem struct {
  88. CreateTime time.Time
  89. }
  90. // GetExchangeLastTime 获取交易所数据最晚的时间
  91. func GetExchangeLastTime(exchange string) (item LastTimeItem, err error) {
  92. tableName := "base_from_trade_" + exchange + "_index"
  93. sql := `SELECT create_time FROM ` + tableName + ` ORDER BY create_time desc`
  94. o := orm.NewOrmUsingDB("data")
  95. err = o.Raw(sql).QueryRow(&item)
  96. return
  97. }
  98. type GetPositionTopReq struct {
  99. Exchange string `json:"exchange" form:"exchange"` //交易所
  100. ClassifyName string `json:"classify_name" form:"classify_name"` //分类名称
  101. ClassifyType string `json:"classify_type" form:"classify_type"` //具体合约名称
  102. DataTime string `json:"data_time" form:"data_time"` //请求日期,如果为空,则返回最新的榜单日期
  103. }
  104. type GetPositionTopResp struct {
  105. BuyList GetPositionTopList `description:"多单列表"`
  106. SoldList GetPositionTopList `description:"空单列表"`
  107. CleanBuyList GetPositionTopList `description:"净多单列表"`
  108. CleanSoldList GetPositionTopList `description:"净空单列表"`
  109. DataTime string `description:"最新日期或者请求日期"`
  110. LastDataTime string `description:"最新日期"`
  111. }
  112. type GetPositionTopList struct {
  113. TotalDealValue int `description:"总计成交量"`
  114. TotalDealChange int `description:"校昨日变化"`
  115. List []GetPositionTopListItem `description:"榜单详情列表"`
  116. }
  117. type GetPositionTopListItem struct {
  118. DealShortName string `description:"成交量公司简称"`
  119. DealValue int `description:"成交量"`
  120. DealChange int `description:"成交变化量"`
  121. Rank int `description:"当前名次"`
  122. Rate string `description:"当前占比"`
  123. BeforeAllRate string `description:"排在前面的成交量总计占比(包含)"`
  124. BeforeAllValue int `description:"排在前面的成交量总计"`
  125. BeforeAllChange int `description:"排在前面的成交量增减总计"`
  126. }
  127. func GetTradePositionTop(exchange string, classifyName, classifyType, dataTime string) (list []TradePositionTop, err error) {
  128. tableName := "trade_position_" + exchange + "_top"
  129. sql := `SELECT * FROM ` + tableName + " WHERE classify_name=? and classify_type=? and data_time=? and `rank` <=20 and `rank` > 0 ORDER BY deal_value desc"
  130. o := orm.NewOrmUsingDB("data")
  131. _, err = o.Raw(sql, classifyName, classifyType, dataTime).QueryRows(&list)
  132. return
  133. }
  134. type OriginTradeData struct {
  135. Rank int `description:"排名"`
  136. CompanyName string `description:"期货公司名称"`
  137. Val int `description:"持仓量"`
  138. ValChange int `description:"持仓增减"`
  139. DataTime time.Time `description:"数据日期"`
  140. ClassifyName string `description:"品种名称"`
  141. ClassifyType string `description:"合约代码"`
  142. ValType int `description:"数据类型: 1-多单; 2-空单"`
  143. }
  144. // GetTradeDataByClassifyAndCompany 根据品种和公司名称获取持仓数据
  145. func GetTradeDataByClassifyAndCompany(exchange, classifyName string, contracts, companies []string) (items []*OriginTradeData, err error) {
  146. if exchange == "" {
  147. err = fmt.Errorf("数据表名称有误")
  148. return
  149. }
  150. if len(contracts) == 0 || len(companies) == 0 {
  151. return
  152. }
  153. condBuy := fmt.Sprintf(`classify_name = ? AND classify_type IN (%s)`, utils.GetOrmInReplace(len(contracts)))
  154. parsBuy := make([]interface{}, 0)
  155. parsBuy = append(parsBuy, classifyName, contracts)
  156. condSold := fmt.Sprintf(`classify_name = ? AND classify_type IN (%s)`, utils.GetOrmInReplace(len(contracts)))
  157. parsSold := make([]interface{}, 0)
  158. parsSold = append(parsSold, classifyName, contracts)
  159. // 是否含有TOP20
  160. var hasTop bool
  161. var condCompanies []string
  162. for _, v := range companies {
  163. if v == TradeFuturesCompanyTop20 {
  164. hasTop = true
  165. continue
  166. }
  167. condCompanies = append(condCompanies, v)
  168. }
  169. if !hasTop {
  170. if len(condCompanies) == 0 {
  171. err = fmt.Errorf("查询条件-期货公司异常")
  172. return
  173. }
  174. condBuy += fmt.Sprintf(` AND buy_short_name IN (%s)`, utils.GetOrmInReplace(len(condCompanies)))
  175. parsBuy = append(parsBuy, condCompanies)
  176. condSold += fmt.Sprintf(` AND sold_short_name IN (%s)`, utils.GetOrmInReplace(len(condCompanies)))
  177. parsSold = append(parsSold, condCompanies)
  178. } else {
  179. // 这里rank=0或者999是因为大商所的数据并不只有999
  180. if len(condCompanies) > 0 {
  181. condBuy += fmt.Sprintf(` AND (rank = 999 OR rank = 0 OR buy_short_name IN (%s))`, utils.GetOrmInReplace(len(condCompanies)))
  182. condSold += fmt.Sprintf(` AND (rank = 999 OR rank = 0 OR sold_short_name IN (%s))`, utils.GetOrmInReplace(len(condCompanies)))
  183. parsBuy = append(parsBuy, condCompanies)
  184. parsSold = append(parsSold, condCompanies)
  185. } else {
  186. condBuy += ` AND (rank = 999 OR rank = 0)`
  187. condSold += ` AND (rank = 999 OR rank = 0)`
  188. }
  189. }
  190. tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
  191. sql := `SELECT
  192. rank,
  193. buy_short_name AS company_name,
  194. buy_value AS val,
  195. buy_change AS val_change,
  196. classify_name,
  197. classify_type,
  198. data_time,
  199. 1 AS val_type
  200. FROM
  201. %s
  202. WHERE
  203. %s
  204. UNION ALL
  205. (
  206. SELECT
  207. rank,
  208. sold_short_name,
  209. sold_value,
  210. sold_change,
  211. classify_name,
  212. classify_type,
  213. data_time,
  214. 2 AS val_type
  215. FROM
  216. %s
  217. WHERE
  218. %s
  219. )`
  220. sql = fmt.Sprintf(sql, tableName, condBuy, tableName, condSold)
  221. o := orm.NewOrmUsingDB("data")
  222. _, err = o.Raw(sql, parsBuy, parsSold).QueryRows(&items)
  223. return
  224. }
  225. // GetTradeZhengzhouDataByClassifyAndCompany 郑商所-根据品种和公司名称获取持仓数据
  226. func GetTradeZhengzhouDataByClassifyAndCompany(exchange string, contracts, companies []string) (items []*OriginTradeData, err error) {
  227. if exchange == "" {
  228. err = fmt.Errorf("数据表名称有误")
  229. return
  230. }
  231. if len(contracts) == 0 || len(companies) == 0 {
  232. return
  233. }
  234. condBuy := fmt.Sprintf(`classify_name IN (%s)`, utils.GetOrmInReplace(len(contracts)))
  235. parsBuy := make([]interface{}, 0)
  236. parsBuy = append(parsBuy, contracts)
  237. condSold := fmt.Sprintf(`classify_name IN (%s)`, utils.GetOrmInReplace(len(contracts)))
  238. parsSold := make([]interface{}, 0)
  239. parsSold = append(parsSold, contracts)
  240. // 是否含有TOP20
  241. var hasTop bool
  242. var condCompanies []string
  243. for _, v := range companies {
  244. if v == TradeFuturesCompanyTop20 {
  245. hasTop = true
  246. continue
  247. }
  248. condCompanies = append(condCompanies, v)
  249. }
  250. if !hasTop {
  251. if len(condCompanies) == 0 {
  252. err = fmt.Errorf("查询条件-期货公司异常")
  253. return
  254. }
  255. condBuy += fmt.Sprintf(` AND buy_short_name IN (%s)`, utils.GetOrmInReplace(len(condCompanies)))
  256. parsBuy = append(parsBuy, condCompanies)
  257. condSold += fmt.Sprintf(` AND sold_short_name IN (%s)`, utils.GetOrmInReplace(len(condCompanies)))
  258. parsSold = append(parsSold, condCompanies)
  259. } else {
  260. if len(condCompanies) > 0 {
  261. condBuy += fmt.Sprintf(` AND (rank = 999 OR buy_short_name IN (%s))`, utils.GetOrmInReplace(len(condCompanies)))
  262. condSold += fmt.Sprintf(` AND (rank = 999 OR sold_short_name IN (%s))`, utils.GetOrmInReplace(len(condCompanies)))
  263. parsBuy = append(parsBuy, condCompanies)
  264. parsSold = append(parsSold, condCompanies)
  265. } else {
  266. condBuy += ` AND rank = 999`
  267. condSold += ` AND rank = 999`
  268. }
  269. }
  270. tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
  271. sql := `SELECT
  272. rank,
  273. buy_short_name AS company_name,
  274. buy_value AS val,
  275. buy_change AS val_change,
  276. classify_name AS classify_type,
  277. data_time,
  278. 1 AS val_type
  279. FROM
  280. %s
  281. WHERE
  282. %s
  283. UNION ALL
  284. (
  285. SELECT
  286. rank,
  287. sold_short_name,
  288. sold_value,
  289. sold_change,
  290. classify_name AS classify_type,
  291. data_time,
  292. 2 AS val_type
  293. FROM
  294. %s
  295. WHERE
  296. %s
  297. )`
  298. sql = fmt.Sprintf(sql, tableName, condBuy, tableName, condSold)
  299. o := orm.NewOrmUsingDB("data")
  300. _, err = o.Raw(sql, parsBuy, parsSold).QueryRows(&items)
  301. return
  302. }
  303. // ContractCompanyTradeData [合约-期货公司]持仓数据
  304. type ContractCompanyTradeData struct {
  305. CompanyName string `description:"期货公司名称"`
  306. ClassifyType string `description:"合约代码"`
  307. StartDate time.Time `description:"数据开始日期"`
  308. EndDate time.Time `description:"数据结束日期"`
  309. DataList []*ContractCompanyTradeDataList `description:"数据序列"`
  310. }
  311. const (
  312. TradeDataTypeNull = 0 // 无值
  313. TradeDataTypeOrigin = 1 // 原始值
  314. TradeDataTypeCalculate = 2 // 推算值
  315. WarehouseBuyChartType = 1 // 多单图
  316. WarehouseSoldChartType = 2 // 空单图
  317. WarehousePureBuyChartType = 3 // 净多单图
  318. WarehouseDefaultUnit = "手"
  319. WarehouseDefaultFrequency = "日度"
  320. GuangZhouTopCompanyAliasName = "日成交持仓排名" // 广期所TOP20对应的公司名称
  321. GuangZhouSeatNameBuy = "持买单量" // 广期所指标名称中的多单名称
  322. GuangZhouSeatNameSold = "持卖单量" // 广期所指标名称中的空单名称
  323. GuangZhouTopSeatNameBuy = "持买单量总计" // 广期所指标名称中的TOP20多单名称
  324. GuangZhouTopSeatNameSold = "持卖单量总计" // 广期所指标名称中的TOP20空单名称
  325. )
  326. const (
  327. TradeExchangeZhengzhou = "zhengzhou"
  328. TradeExchangeGuangzhou = "guangzhou"
  329. )
  330. var WarehouseTypeSuffixNames = map[int]string{
  331. WarehouseBuyChartType: "席位多单",
  332. WarehouseSoldChartType: "席位空单",
  333. WarehousePureBuyChartType: "席位净多单",
  334. }
  335. // GuangzhouSeatNameValType 广期所数据名称对应的席位方向
  336. var GuangzhouSeatNameValType = map[string]int{
  337. GuangZhouSeatNameBuy: 1,
  338. GuangZhouSeatNameSold: 2,
  339. GuangZhouTopSeatNameBuy: 1,
  340. GuangZhouTopSeatNameSold: 2,
  341. }
  342. // ContractCompanyTradeDataList [合约-期货公司]持仓数据详情
  343. type ContractCompanyTradeDataList struct {
  344. Date time.Time `description:"数据日期"`
  345. BuyVal int `description:"多单持仓量"`
  346. BuyValType int `description:"多单数据类型: 0-无值; 1-原始值; 2-推算值"`
  347. BuyChange int `description:"多单持仓增减"`
  348. BuyChangeType int `description:"多单持仓增减类型: 0-无值; 1-原始值; 2-推算值"`
  349. SoldVal int `description:"空单持仓量"`
  350. SoldValType int `description:"空单数据类型: 0-无值; 1-原始值; 2-推算值"`
  351. SoldChange int `description:"空单持仓增减"`
  352. SoldChangeType int `description:"空单持仓增减类型: 0-无值; 1-原始值; 2-推算值"`
  353. PureBuyVal int `description:"净多单持仓量"`
  354. PureBuyValType int `description:"净多单数据类型: 0-无值; 1-原始值; 2-推算值"`
  355. PureBuyChange int `description:"净多单持仓增减"`
  356. PureBuyChangeType int `description:"净多单持仓增减类型: 0-无值; 1-原始值; 2-推算值"`
  357. }
  358. // GetLastTradeDataByClassify 获取[合约]末位多空单数据
  359. func GetLastTradeDataByClassify(exchange, classifyName string, contracts []string) (items []*OriginTradeData, err error) {
  360. if exchange == "" {
  361. err = fmt.Errorf("数据表名称有误")
  362. return
  363. }
  364. if len(contracts) == 0 {
  365. return
  366. }
  367. contractReplacer := utils.GetOrmInReplace(len(contracts))
  368. tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
  369. sql := `SELECT
  370. tpt.rank,
  371. tpt.buy_short_name AS company_name,
  372. tpt.buy_value AS val,
  373. tpt.buy_change AS val_change,
  374. tpt.classify_name,
  375. tpt.classify_type,
  376. tpt.data_time,
  377. 1 AS val_type
  378. FROM
  379. %s tpt
  380. JOIN
  381. (
  382. SELECT
  383. data_time, classify_type, MAX(rank) AS max_rank
  384. FROM
  385. %s
  386. WHERE
  387. classify_name = ? AND classify_type IN (%s) AND buy_short_name <> ''
  388. GROUP BY
  389. data_time,
  390. classify_type
  391. ) sub
  392. ON
  393. tpt.data_time = sub.data_time AND tpt.classify_type = sub.classify_type AND tpt.rank = sub.max_rank
  394. WHERE
  395. tpt.classify_name = ? AND tpt.classify_type IN (%s)
  396. UNION ALL
  397. (
  398. SELECT
  399. 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
  400. FROM
  401. %s tpt
  402. JOIN
  403. (
  404. SELECT
  405. data_time, classify_type, MAX(rank) AS max_rank
  406. FROM
  407. %s
  408. WHERE
  409. classify_name = ? AND classify_type IN (%s) AND sold_short_name <> ''
  410. GROUP BY
  411. data_time, classify_type
  412. ) sub
  413. ON
  414. tpt.data_time = sub.data_time AND tpt.classify_type = sub.classify_type AND tpt.rank = sub.max_rank
  415. WHERE
  416. tpt.classify_name = ? AND tpt.classify_type IN (%s)
  417. )`
  418. sql = fmt.Sprintf(sql, tableName, tableName, contractReplacer, contractReplacer, tableName, tableName, contractReplacer, contractReplacer)
  419. o := orm.NewOrmUsingDB("data")
  420. _, err = o.Raw(sql, classifyName, contracts, classifyName, contracts, classifyName, contracts, classifyName, contracts).QueryRows(&items)
  421. return
  422. }
  423. // GetLastTradeZhengzhouDataByClassify 郑商所-获取[合约]末位多空单数据
  424. func GetLastTradeZhengzhouDataByClassify(exchange string, contracts []string) (items []*OriginTradeData, err error) {
  425. if exchange == "" {
  426. err = fmt.Errorf("数据表名称有误")
  427. return
  428. }
  429. if len(contracts) == 0 {
  430. return
  431. }
  432. contractReplacer := utils.GetOrmInReplace(len(contracts))
  433. tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
  434. sql := `SELECT
  435. tpt.rank,
  436. tpt.buy_short_name AS company_name,
  437. tpt.buy_value AS val,
  438. tpt.buy_change AS val_change,
  439. tpt.classify_name AS classify_type,
  440. tpt.data_time,
  441. 1 AS val_type
  442. FROM
  443. %s tpt
  444. JOIN
  445. (
  446. SELECT
  447. data_time, classify_name, MAX(rank) AS max_rank
  448. FROM
  449. %s
  450. WHERE
  451. classify_name IN (%s) AND buy_short_name <> ''
  452. GROUP BY
  453. data_time,
  454. classify_name
  455. ) sub
  456. ON
  457. tpt.data_time = sub.data_time AND tpt.classify_name = sub.classify_name AND tpt.rank = sub.max_rank
  458. WHERE
  459. tpt.classify_name IN (%s)
  460. UNION ALL
  461. (
  462. SELECT
  463. 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
  464. FROM
  465. %s tpt
  466. JOIN
  467. (
  468. SELECT
  469. data_time, classify_name, MAX(rank) AS max_rank
  470. FROM
  471. %s
  472. WHERE
  473. classify_name IN (%s) AND sold_short_name <> ''
  474. GROUP BY
  475. data_time, classify_name
  476. ) sub
  477. ON
  478. tpt.data_time = sub.data_time AND tpt.classify_name = sub.classify_name AND tpt.rank = sub.max_rank
  479. WHERE
  480. tpt.classify_name IN (%s)
  481. )`
  482. sql = fmt.Sprintf(sql, tableName, tableName, contractReplacer, contractReplacer, tableName, tableName, contractReplacer, contractReplacer)
  483. o := orm.NewOrmUsingDB("data")
  484. _, err = o.Raw(sql, contracts, contracts, contracts, contracts).QueryRows(&items)
  485. return
  486. }
  487. type BaseFromTradeGuangzhouIndex struct {
  488. BaseFromTradeGuangzhouIndexId int `orm:"column(base_from_trade_guangzhou_index_id);pk"`
  489. BaseFromTradeGuangzhouClassifyId int `description:"分类id"`
  490. IndexCode string `description:"指标编码"`
  491. IndexName string `description:"指标名称"`
  492. Frequency string `description:"频率"`
  493. Unit string `description:"单位"`
  494. StartDate string `description:"开始日期"`
  495. EndDate string `description:"结束日期"`
  496. CreateTime time.Time `description:"创建日期"`
  497. ModifyTime time.Time `description:"修改日期"`
  498. }
  499. func GetBaseFromTradeGuangzhouIndexByClassifyId(classifyId int) (list []*BaseFromTradeGuangzhouIndex, err error) {
  500. o := orm.NewOrmUsingDB("data")
  501. sql := `SELECT * FROM base_from_trade_guangzhou_index WHERE base_from_trade_guangzhou_classify_id = ?`
  502. _, err = o.Raw(sql, classifyId).QueryRows(&list)
  503. return
  504. }
  505. type BaseFromTradeGuangzhouData struct {
  506. BaseFromTradeGuangzhouDataId int `orm:"column(base_from_trade_guangzhou_data_id);pk"`
  507. BaseFromTradeGuangzhouIndexId int `description:"指标id"`
  508. IndexCode string `description:"指标编码"`
  509. DataTime time.Time `description:"数据日期"`
  510. Value float64 `description:"数据值"`
  511. QtySub float64 `description:"增减"`
  512. CreateTime time.Time `description:"创建日期"`
  513. ModifyTime time.Time `description:"修改日期"`
  514. }
  515. // GetBaseFromTradeGuangzhouDataByIndexIds 获取指标数据
  516. func GetBaseFromTradeGuangzhouDataByIndexIds(indexIds []int) (list []*BaseFromTradeGuangzhouData, err error) {
  517. if len(indexIds) == 0 {
  518. return
  519. }
  520. o := orm.NewOrmUsingDB("data")
  521. 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)))
  522. _, err = o.Raw(sql, indexIds).QueryRows(&list)
  523. return
  524. }
  525. // GetBaseFromTradeGuangzhouMinDataByIndexIds 获取指标中的末位数据
  526. func GetBaseFromTradeGuangzhouMinDataByIndexIds(indexIds []int) (list []*BaseFromTradeGuangzhouData, err error) {
  527. indexLen := len(indexIds)
  528. if indexLen == 0 {
  529. return
  530. }
  531. o := orm.NewOrmUsingDB("data")
  532. sql := fmt.Sprintf(`SELECT
  533. t1.data_time,
  534. t1.min_value AS value
  535. FROM
  536. (
  537. SELECT
  538. data_time,
  539. MIN(value) AS min_value
  540. FROM
  541. base_from_trade_guangzhou_data
  542. WHERE
  543. base_from_trade_guangzhou_index_id IN (%s)
  544. GROUP BY
  545. data_time
  546. ) t1
  547. JOIN
  548. base_from_trade_guangzhou_data t2
  549. ON
  550. t1.data_time = t2.data_time AND t1.min_value = t2.value AND t2.base_from_trade_guangzhou_index_id IN (%s)
  551. GROUP BY
  552. t1.data_time`, utils.GetOrmInReplace(indexLen), utils.GetOrmInReplace(indexLen))
  553. _, err = o.Raw(sql, indexIds, indexIds).QueryRows(&list)
  554. return
  555. }