trade_analysis.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. if len(condCompanies) > 0 {
  180. condBuy += fmt.Sprintf(` AND (rank = 999 OR buy_short_name IN (%s))`, utils.GetOrmInReplace(len(condCompanies)))
  181. condSold += fmt.Sprintf(` AND (rank = 999 OR sold_short_name IN (%s))`, utils.GetOrmInReplace(len(condCompanies)))
  182. parsBuy = append(parsBuy, condCompanies)
  183. parsSold = append(parsSold, condCompanies)
  184. } else {
  185. condBuy += ` AND rank = 999`
  186. condSold += ` AND rank = 999`
  187. }
  188. }
  189. tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
  190. sql := `SELECT
  191. rank,
  192. buy_short_name AS company_name,
  193. buy_value AS val,
  194. buy_change AS val_change,
  195. classify_name,
  196. classify_type,
  197. data_time,
  198. 1 AS val_type
  199. FROM
  200. %s
  201. WHERE
  202. %s
  203. UNION ALL
  204. (
  205. SELECT
  206. rank,
  207. sold_short_name,
  208. sold_value,
  209. sold_change,
  210. classify_name,
  211. classify_type,
  212. data_time,
  213. 2 AS val_type
  214. FROM
  215. %s
  216. WHERE
  217. %s
  218. )`
  219. sql = fmt.Sprintf(sql, tableName, condBuy, tableName, condSold)
  220. o := orm.NewOrmUsingDB("data")
  221. _, err = o.Raw(sql, parsBuy, parsSold).QueryRows(&items)
  222. return
  223. }
  224. // GetTradeZhengzhouDataByClassifyAndCompany 郑商所-根据品种和公司名称获取持仓数据
  225. func GetTradeZhengzhouDataByClassifyAndCompany(exchange string, contracts, companies []string) (items []*OriginTradeData, err error) {
  226. if exchange == "" {
  227. err = fmt.Errorf("数据表名称有误")
  228. return
  229. }
  230. if len(contracts) == 0 || len(companies) == 0 {
  231. return
  232. }
  233. condBuy := fmt.Sprintf(`classify_name IN (%s)`, utils.GetOrmInReplace(len(contracts)))
  234. parsBuy := make([]interface{}, 0)
  235. parsBuy = append(parsBuy, contracts)
  236. condSold := fmt.Sprintf(`classify_name IN (%s)`, utils.GetOrmInReplace(len(contracts)))
  237. parsSold := make([]interface{}, 0)
  238. parsSold = append(parsSold, contracts)
  239. // 是否含有TOP20
  240. var hasTop bool
  241. var condCompanies []string
  242. for _, v := range companies {
  243. if v == TradeFuturesCompanyTop20 {
  244. hasTop = true
  245. continue
  246. }
  247. condCompanies = append(condCompanies, v)
  248. }
  249. if !hasTop {
  250. if len(condCompanies) == 0 {
  251. err = fmt.Errorf("查询条件-期货公司异常")
  252. return
  253. }
  254. condBuy += fmt.Sprintf(` AND buy_short_name IN (%s)`, utils.GetOrmInReplace(len(condCompanies)))
  255. parsBuy = append(parsBuy, condCompanies)
  256. condSold += fmt.Sprintf(` AND sold_short_name IN (%s)`, utils.GetOrmInReplace(len(condCompanies)))
  257. parsSold = append(parsSold, condCompanies)
  258. } else {
  259. if len(condCompanies) > 0 {
  260. condBuy += fmt.Sprintf(` AND (rank = 999 OR buy_short_name IN (%s))`, utils.GetOrmInReplace(len(condCompanies)))
  261. condSold += fmt.Sprintf(` AND (rank = 999 OR sold_short_name IN (%s))`, utils.GetOrmInReplace(len(condCompanies)))
  262. parsBuy = append(parsBuy, condCompanies)
  263. parsSold = append(parsSold, condCompanies)
  264. } else {
  265. condBuy += ` AND rank = 999`
  266. condSold += ` AND rank = 999`
  267. }
  268. }
  269. tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
  270. sql := `SELECT
  271. rank,
  272. buy_short_name AS company_name,
  273. buy_value AS val,
  274. buy_change AS val_change,
  275. classify_name AS classify_type,
  276. data_time,
  277. 1 AS val_type
  278. FROM
  279. %s
  280. WHERE
  281. %s
  282. UNION ALL
  283. (
  284. SELECT
  285. rank,
  286. sold_short_name,
  287. sold_value,
  288. sold_change,
  289. classify_name AS classify_type,
  290. data_time,
  291. 2 AS val_type
  292. FROM
  293. %s
  294. WHERE
  295. %s
  296. )`
  297. sql = fmt.Sprintf(sql, tableName, condBuy, tableName, condSold)
  298. o := orm.NewOrmUsingDB("data")
  299. _, err = o.Raw(sql, parsBuy, parsSold).QueryRows(&items)
  300. return
  301. }
  302. // ContractCompanyTradeData [合约-期货公司]持仓数据
  303. type ContractCompanyTradeData struct {
  304. CompanyName string `description:"期货公司名称"`
  305. ClassifyType string `description:"合约代码"`
  306. StartDate time.Time `description:"数据开始日期"`
  307. EndDate time.Time `description:"数据结束日期"`
  308. DataList []*ContractCompanyTradeDataList `description:"数据序列"`
  309. }
  310. const (
  311. TradeDataTypeNull = 0 // 无值
  312. TradeDataTypeOrigin = 1 // 原始值
  313. TradeDataTypeCalculate = 2 // 推算值
  314. WarehouseBuyChartType = 1 // 多单图
  315. WarehouseSoldChartType = 2 // 空单图
  316. WarehousePureBuyChartType = 3 // 净多单图
  317. WarehouseDefaultUnit = "手"
  318. WarehouseDefaultFrequency = "日度"
  319. GuangZhouTopCompanyAliasName = "日成交持仓排名" // 广期所TOP20对应的公司名称
  320. GuangZhouSeatNameBuy = "持买单量" // 广期所指标名称中的多单名称
  321. GuangZhouSeatNameSold = "持卖单量" // 广期所指标名称中的空单名称
  322. GuangZhouTopSeatNameBuy = "持买单量总计" // 广期所指标名称中的TOP20多单名称
  323. GuangZhouTopSeatNameSold = "持卖单量总计" // 广期所指标名称中的TOP20空单名称
  324. )
  325. const (
  326. TradeExchangeZhengzhou = "zhengzhou"
  327. TradeExchangeGuangzhou = "guangzhou"
  328. )
  329. var WarehouseTypeSuffixNames = map[int]string{
  330. WarehouseBuyChartType: "席位多单",
  331. WarehouseSoldChartType: "席位空单",
  332. WarehousePureBuyChartType: "席位净多单",
  333. }
  334. // GuangzhouSeatNameValType 广期所数据名称对应的席位方向
  335. var GuangzhouSeatNameValType = map[string]int{
  336. GuangZhouSeatNameBuy: 1,
  337. GuangZhouSeatNameSold: 2,
  338. GuangZhouTopSeatNameBuy: 1,
  339. GuangZhouTopSeatNameSold: 2,
  340. }
  341. // ContractCompanyTradeDataList [合约-期货公司]持仓数据详情
  342. type ContractCompanyTradeDataList struct {
  343. Date time.Time `description:"数据日期"`
  344. BuyVal int `description:"多单持仓量"`
  345. BuyValType int `description:"多单数据类型: 0-无值; 1-原始值; 2-推算值"`
  346. BuyChange int `description:"多单持仓增减"`
  347. BuyChangeType int `description:"多单持仓增减类型: 0-无值; 1-原始值; 2-推算值"`
  348. SoldVal int `description:"空单持仓量"`
  349. SoldValType int `description:"空单数据类型: 0-无值; 1-原始值; 2-推算值"`
  350. SoldChange int `description:"空单持仓增减"`
  351. SoldChangeType int `description:"空单持仓增减类型: 0-无值; 1-原始值; 2-推算值"`
  352. PureBuyVal int `description:"净多单持仓量"`
  353. PureBuyValType int `description:"净多单数据类型: 0-无值; 1-原始值; 2-推算值"`
  354. PureBuyChange int `description:"净多单持仓增减"`
  355. PureBuyChangeType int `description:"净多单持仓增减类型: 0-无值; 1-原始值; 2-推算值"`
  356. }
  357. // GetLastTradeDataByClassify 获取[合约]末位多空单数据
  358. func GetLastTradeDataByClassify(exchange, classifyName string, contracts []string) (items []*OriginTradeData, err error) {
  359. if exchange == "" {
  360. err = fmt.Errorf("数据表名称有误")
  361. return
  362. }
  363. if len(contracts) == 0 {
  364. return
  365. }
  366. contractReplacer := utils.GetOrmInReplace(len(contracts))
  367. tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
  368. sql := `SELECT
  369. tpt.rank,
  370. tpt.buy_short_name AS company_name,
  371. tpt.buy_value AS val,
  372. tpt.buy_change AS val_change,
  373. tpt.classify_name,
  374. tpt.classify_type,
  375. tpt.data_time,
  376. 1 AS val_type
  377. FROM
  378. %s tpt
  379. JOIN
  380. (
  381. SELECT
  382. data_time, classify_type, MAX(rank) AS max_rank
  383. FROM
  384. %s
  385. WHERE
  386. classify_name = ? AND classify_type IN (%s) AND buy_short_name <> ''
  387. GROUP BY
  388. data_time,
  389. classify_type
  390. ) sub
  391. ON
  392. tpt.data_time = sub.data_time AND tpt.classify_type = sub.classify_type AND tpt.rank = sub.max_rank
  393. WHERE
  394. tpt.classify_name = ? AND tpt.classify_type IN (%s)
  395. UNION ALL
  396. (
  397. SELECT
  398. 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
  399. FROM
  400. %s tpt
  401. JOIN
  402. (
  403. SELECT
  404. data_time, classify_type, MAX(rank) AS max_rank
  405. FROM
  406. %s
  407. WHERE
  408. classify_name = ? AND classify_type IN (%s) AND sold_short_name <> ''
  409. GROUP BY
  410. data_time, classify_type
  411. ) sub
  412. ON
  413. tpt.data_time = sub.data_time AND tpt.classify_type = sub.classify_type AND tpt.rank = sub.max_rank
  414. WHERE
  415. tpt.classify_name = ? AND tpt.classify_type IN (%s)
  416. )`
  417. sql = fmt.Sprintf(sql, tableName, tableName, contractReplacer, contractReplacer, tableName, tableName, contractReplacer, contractReplacer)
  418. o := orm.NewOrmUsingDB("data")
  419. _, err = o.Raw(sql, classifyName, contracts, classifyName, contracts, classifyName, contracts, classifyName, contracts).QueryRows(&items)
  420. return
  421. }
  422. // GetLastTradeZhengzhouDataByClassify 郑商所-获取[合约]末位多空单数据
  423. func GetLastTradeZhengzhouDataByClassify(exchange string, contracts []string) (items []*OriginTradeData, err error) {
  424. if exchange == "" {
  425. err = fmt.Errorf("数据表名称有误")
  426. return
  427. }
  428. if len(contracts) == 0 {
  429. return
  430. }
  431. contractReplacer := utils.GetOrmInReplace(len(contracts))
  432. tableName := fmt.Sprintf("base_from_trade_%s_index", exchange)
  433. sql := `SELECT
  434. tpt.rank,
  435. tpt.buy_short_name AS company_name,
  436. tpt.buy_value AS val,
  437. tpt.buy_change AS val_change,
  438. tpt.classify_name AS classify_type,
  439. tpt.data_time,
  440. 1 AS val_type
  441. FROM
  442. %s tpt
  443. JOIN
  444. (
  445. SELECT
  446. data_time, classify_name, MAX(rank) AS max_rank
  447. FROM
  448. %s
  449. WHERE
  450. classify_name IN (%s) AND buy_short_name <> ''
  451. GROUP BY
  452. data_time,
  453. classify_name
  454. ) sub
  455. ON
  456. tpt.data_time = sub.data_time AND tpt.classify_name = sub.classify_name AND tpt.rank = sub.max_rank
  457. WHERE
  458. tpt.classify_name IN (%s)
  459. UNION ALL
  460. (
  461. SELECT
  462. 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
  463. FROM
  464. %s tpt
  465. JOIN
  466. (
  467. SELECT
  468. data_time, classify_name, MAX(rank) AS max_rank
  469. FROM
  470. %s
  471. WHERE
  472. classify_name IN (%s) AND sold_short_name <> ''
  473. GROUP BY
  474. data_time, classify_name
  475. ) sub
  476. ON
  477. tpt.data_time = sub.data_time AND tpt.classify_name = sub.classify_name AND tpt.rank = sub.max_rank
  478. WHERE
  479. tpt.classify_name IN (%s)
  480. )`
  481. sql = fmt.Sprintf(sql, tableName, tableName, contractReplacer, contractReplacer, tableName, tableName, contractReplacer, contractReplacer)
  482. o := orm.NewOrmUsingDB("data")
  483. _, err = o.Raw(sql, contracts, contracts, contracts, contracts).QueryRows(&items)
  484. return
  485. }
  486. type BaseFromTradeGuangzhouIndex struct {
  487. BaseFromTradeGuangzhouIndexId int `orm:"column(base_from_trade_guangzhou_index_id);pk"`
  488. BaseFromTradeGuangzhouClassifyId int `description:"分类id"`
  489. IndexCode string `description:"指标编码"`
  490. IndexName string `description:"指标名称"`
  491. Frequency string `description:"频率"`
  492. Unit string `description:"单位"`
  493. StartDate string `description:"开始日期"`
  494. EndDate string `description:"结束日期"`
  495. CreateTime time.Time `description:"创建日期"`
  496. ModifyTime time.Time `description:"修改日期"`
  497. }
  498. func GetBaseFromTradeGuangzhouIndexByClassifyId(classifyId int) (list []*BaseFromTradeGuangzhouIndex, err error) {
  499. o := orm.NewOrmUsingDB("data")
  500. sql := `SELECT * FROM base_from_trade_guangzhou_index WHERE base_from_trade_guangzhou_classify_id = ?`
  501. _, err = o.Raw(sql, classifyId).QueryRows(&list)
  502. return
  503. }
  504. type BaseFromTradeGuangzhouData struct {
  505. BaseFromTradeGuangzhouDataId int `orm:"column(base_from_trade_guangzhou_data_id);pk"`
  506. BaseFromTradeGuangzhouIndexId int `description:"指标id"`
  507. IndexCode string `description:"指标编码"`
  508. DataTime time.Time `description:"数据日期"`
  509. Value float64 `description:"数据值"`
  510. QtySub float64 `description:"增减"`
  511. CreateTime time.Time `description:"创建日期"`
  512. ModifyTime time.Time `description:"修改日期"`
  513. }
  514. // GetBaseFromTradeGuangzhouDataByIndexIds 获取指标数据
  515. func GetBaseFromTradeGuangzhouDataByIndexIds(indexIds []int) (list []*BaseFromTradeGuangzhouData, err error) {
  516. if len(indexIds) == 0 {
  517. return
  518. }
  519. o := orm.NewOrmUsingDB("data")
  520. 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)))
  521. _, err = o.Raw(sql, indexIds).QueryRows(&list)
  522. return
  523. }
  524. // GetBaseFromTradeGuangzhouMinDataByIndexIds 获取指标中的末位数据
  525. func GetBaseFromTradeGuangzhouMinDataByIndexIds(indexIds []int) (list []*BaseFromTradeGuangzhouData, err error) {
  526. indexLen := len(indexIds)
  527. if indexLen == 0 {
  528. return
  529. }
  530. o := orm.NewOrmUsingDB("data")
  531. sql := fmt.Sprintf(`SELECT
  532. t1.data_time,
  533. t1.min_value AS value
  534. FROM
  535. (
  536. SELECT
  537. data_time,
  538. MIN(value) AS min_value
  539. FROM
  540. base_from_trade_guangzhou_data
  541. WHERE
  542. base_from_trade_guangzhou_index_id IN (%s)
  543. GROUP BY
  544. data_time
  545. ) t1
  546. JOIN
  547. base_from_trade_guangzhou_data t2
  548. ON
  549. t1.data_time = t2.data_time AND t1.min_value = t2.value AND t2.base_from_trade_guangzhou_index_id IN (%s)
  550. GROUP BY
  551. t1.data_time`, utils.GetOrmInReplace(indexLen), utils.GetOrmInReplace(indexLen))
  552. _, err = o.Raw(sql, indexIds, indexIds).QueryRows(&list)
  553. return
  554. }