trade_position_analysis_guangzhou.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package data_manage
  2. import (
  3. "eta/eta_task/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. func MultiInsertTradeGuangzhouDataToTop(exchange string, startDate, endDate string) (err error) {
  8. o := orm.NewOrm()
  9. now := time.Now().Format(utils.FormatDateTime)
  10. //新增买单榜单
  11. sql1 := `INSERT INTO trade_position_guangzhou_top(classify_name,classify_type,deal_short_name,deal_value,deal_change,data_time,deal_type,source_type,rank,create_time,modify_time)
  12. SELECT
  13. n.classify_code,n.contract,SUBSTRING_INDEX(c.index_name, '_', 1) AS index_name,a.value,a.qty_sub,a.data_time,1,0,0,?,?
  14. FROM
  15. base_from_trade_guangzhou_data a
  16. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  17. LEFT JOIN base_from_trade_guangzhou_contract n on c.base_from_trade_guangzhou_contract_id = n.base_from_trade_guangzhou_contract_id
  18. WHERE
  19. a.data_time between ? and ?
  20. and c.base_from_trade_guangzhou_classify_id in (7,8)
  21. and c.index_name like "%持买单量%"
  22. and c.index_name not like "%日成交持仓排名%"`
  23. _, err = o.Raw(sql1, now, now, startDate, endDate).Exec()
  24. if err != nil {
  25. return
  26. }
  27. //新增卖单榜单
  28. sql2 := `INSERT INTO trade_position_guangzhou_top(classify_name,classify_type,deal_short_name,deal_value,deal_change,data_time,deal_type,source_type,rank,create_time,modify_time)
  29. SELECT
  30. n.classify_code,n.contract,SUBSTRING_INDEX(c.index_name, '_', 1) AS index_name,a.value,a.qty_sub,a.data_time,2,0,0,?,?
  31. FROM
  32. base_from_trade_guangzhou_data a
  33. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  34. LEFT JOIN base_from_trade_guangzhou_contract n on c.base_from_trade_guangzhou_contract_id = n.base_from_trade_guangzhou_contract_id
  35. WHERE
  36. a.data_time between ? and ?
  37. and c.base_from_trade_guangzhou_classify_id in (7,8)
  38. and c.index_name like "%持卖单量%"
  39. and c.index_name not like "%日成交持仓排名%"`
  40. _, err = o.Raw(sql2, now, now, startDate, endDate).Exec()
  41. if err != nil {
  42. return
  43. }
  44. //更新买单、卖单榜单排名字段
  45. sql3 := `update trade_position_guangzhou_top s
  46. JOIN (
  47. SELECT
  48. classify_type,
  49. deal_short_name,
  50. data_time,
  51. deal_type,
  52. (@row_number := IF(@prev_year = CONCAT_WS( "_", data_time, deal_type, classify_type), @row_number + 1, 1) ) AS row_number,
  53. @prev_year := CONCAT_WS( "_", data_time, deal_type, classify_type)
  54. FROM
  55. trade_position_guangzhou_top,(SELECT @row_number := 0, @prev_year := NULL) r
  56. where data_time between ? and ?
  57. ORDER BY
  58. data_time asc,
  59. deal_type asc,
  60. classify_type asc,
  61. deal_value DESC
  62. ) t
  63. ON s.classify_type = t.classify_type AND s.deal_short_name = t.deal_short_name
  64. AND s.data_time = t.data_time AND s.deal_type = t.deal_type
  65. SET s.rank = t.row_number where s.data_time between ? and ?;`
  66. _, err = o.Raw(sql3, startDate, endDate, startDate, endDate).Exec()
  67. return
  68. }
  69. // GetTradePositionTopOriginGuangzhouDataTimes 获取榜单原始数据日期-正序
  70. func GetTradePositionTopOriginGuangzhouDataTimes(exchange string) (dates []string, err error) {
  71. o := orm.NewOrm()
  72. sql := `SELECT
  73. DISTINCT a.data_time
  74. FROM
  75. base_from_trade_guangzhou_data a
  76. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  77. WHERE
  78. c.base_from_trade_guangzhou_classify_id IN ( 7, 8 )
  79. AND ( c.index_name LIKE "%持买单量%" OR c.index_name LIKE "%持卖单量%" )
  80. AND c.index_name NOT LIKE "%日成交持仓排名%"
  81. ORDER BY
  82. a.data_time asc`
  83. _, err = o.Raw(sql).QueryRows(&dates)
  84. return
  85. }
  86. func GetTradePositionOriginGuangzhouClassifyCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  87. o := orm.NewOrm()
  88. sql := `SELECT
  89. count(DISTINCT n.classify_code, n.contract )
  90. FROM
  91. base_from_trade_guangzhou_data a
  92. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  93. LEFT JOIN base_from_trade_guangzhou_contract n ON c.base_from_trade_guangzhou_contract_id = n.base_from_trade_guangzhou_contract_id
  94. WHERE
  95. a.data_time between ? and ?
  96. AND c.base_from_trade_guangzhou_classify_id IN ( 7, 8 )
  97. AND ( c.index_name LIKE "%持买单量%" OR c.index_name LIKE "%持卖单量%" )
  98. AND c.index_name NOT LIKE "%日成交持仓排名%"
  99. ORDER BY
  100. a.value DESC`
  101. err = o.Raw(sql, startDate, endDate).QueryRow(&count)
  102. return
  103. }
  104. func GetFirstBaseFromTradeGuangzhouIndexByDate(exchange string) (item *GetFirstBaseFromTradeIndeDate, err error) {
  105. o := orm.NewOrm()
  106. sql := `SELECT
  107. a.data_time
  108. FROM
  109. base_from_trade_guangzhou_data a
  110. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  111. WHERE
  112. c.base_from_trade_guangzhou_classify_id IN ( 7, 8 )
  113. AND ( c.index_name LIKE "%持买单量%" OR c.index_name LIKE "%持卖单量%" )
  114. AND c.index_name NOT LIKE "%日成交持仓排名%"
  115. ORDER BY
  116. a.data_time asc`
  117. err = o.Raw(sql).QueryRow(&item)
  118. return
  119. }
  120. func GetTradePositionOriginClassifyGuangzhouByExchangeDataTime(exchange, startDate, endDate string) (list []TradePositionClassifyInfo, err error) {
  121. o := orm.NewOrm()
  122. sql := `SELECT DISTINCT classify_code as classify_name, contract as classify_type FROM base_from_trade_guangzhou_contract where trade_date >= ? and trade_date <= ?`
  123. _, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
  124. return
  125. }
  126. func MultiInsertTradeBaseDataToTopGuangzhouByClassify(exchange string, startDate, endDate string, classifyNames, classifyTypes []string) (err error) {
  127. o := orm.NewOrm()
  128. now := time.Now().Format(utils.FormatDateTime)
  129. //新增买单榜单
  130. sql1 := `INSERT INTO trade_position_guangzhou_top(classify_name,classify_type,deal_short_name,deal_value,deal_change,data_time,deal_type,source_type,rank,create_time,modify_time)
  131. SELECT
  132. n.classify_code,n.contract,SUBSTRING_INDEX(c.index_name, '_', 1) AS index_name,a.value,a.qty_sub,a.data_time,1,0,0,?,?
  133. FROM
  134. base_from_trade_guangzhou_data a
  135. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  136. LEFT JOIN base_from_trade_guangzhou_contract n on c.base_from_trade_guangzhou_contract_id = n.base_from_trade_guangzhou_contract_id
  137. WHERE
  138. a.data_time between ? and ?
  139. and c.base_from_trade_guangzhou_classify_id in (7,8)
  140. and n.classify_code in (` + utils.GetOrmInReplace(len(classifyNames)) + `) and n.contract in (` + utils.GetOrmInReplace(len(classifyTypes)) + `)
  141. and c.index_name like "%持买单量%"
  142. and c.index_name not like "%日成交持仓排名%"`
  143. _, err = o.Raw(sql1, now, now, startDate, endDate, classifyNames, classifyTypes).Exec()
  144. if err != nil {
  145. return
  146. }
  147. //新增卖单榜单
  148. sql2 := `INSERT INTO trade_position_guangzhou_top(classify_name,classify_type,deal_short_name,deal_value,deal_change,data_time,deal_type,source_type,rank,create_time,modify_time)
  149. SELECT
  150. n.classify_code,n.contract,SUBSTRING_INDEX(c.index_name, '_', 1) AS index_name,a.value,a.qty_sub,a.data_time,2,0,0,?,?
  151. FROM
  152. base_from_trade_guangzhou_data a
  153. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  154. LEFT JOIN base_from_trade_guangzhou_contract n on c.base_from_trade_guangzhou_contract_id = n.base_from_trade_guangzhou_contract_id
  155. WHERE
  156. a.data_time between ? and ?
  157. and c.base_from_trade_guangzhou_classify_id in (7,8)
  158. and n.classify_code in (` + utils.GetOrmInReplace(len(classifyNames)) + `) and n.contract in (` + utils.GetOrmInReplace(len(classifyTypes)) + `)
  159. and c.index_name like "%持卖单量%"
  160. and c.index_name not like "%日成交持仓排名%"`
  161. _, err = o.Raw(sql2, now, now, startDate, endDate, classifyNames, classifyTypes).Exec()
  162. if err != nil {
  163. return
  164. }
  165. //更新买单、卖单榜单排名字段
  166. sql3 := `update trade_position_guangzhou_top s
  167. JOIN (
  168. SELECT
  169. classify_type,
  170. deal_short_name,
  171. data_time,
  172. deal_type,
  173. (@row_number := IF(@prev_year = CONCAT_WS( "_", data_time, deal_type, classify_type), @row_number + 1, 1) ) AS row_number,
  174. @prev_year := CONCAT_WS( "_", data_time, deal_type, classify_type)
  175. FROM
  176. trade_position_guangzhou_top,(SELECT @row_number := 0, @prev_year := NULL) r
  177. where data_time between ? and ? and classify_type in (` + utils.GetOrmInReplace(len(classifyTypes)) + `)
  178. ORDER BY
  179. data_time asc,
  180. deal_type asc,
  181. classify_type asc,
  182. deal_value DESC
  183. ) t
  184. ON s.classify_type = t.classify_type AND s.deal_short_name = t.deal_short_name
  185. AND s.data_time = t.data_time AND s.deal_type = t.deal_type
  186. SET s.rank = t.row_number where s.data_time between ? and ? and s.classify_type in (` + utils.GetOrmInReplace(len(classifyTypes)) + `);`
  187. _, err = o.Raw(sql3, startDate, endDate, classifyTypes, startDate, endDate, classifyTypes).Exec()
  188. return
  189. }