trade_position_analysis_guangzhou.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package data_manage
  2. import (
  3. "eta/eta_task/global"
  4. "eta/eta_task/utils"
  5. "time"
  6. )
  7. func MultiInsertTradeGuangzhouDataToTop(exchange string, startDate, endDate string) (err error) {
  8. o := global.DbMap[utils.DbNameIndex]
  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.Exec(sql1, now, now, startDate, endDate).Error
  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.Exec(sql2, now, now, startDate, endDate).Error
  41. if err != nil {
  42. return
  43. }
  44. //更新买单、卖单榜单排名字段
  45. var sql3 string
  46. var updatePars []interface{}
  47. if utils.DbDriverName == utils.DbDriverByDm {
  48. sql3 = `WITH ranked_data AS (
  49. SELECT
  50. classify_type,
  51. deal_short_name,
  52. data_time,
  53. deal_type,
  54. ROW_NUMBER() OVER (
  55. PARTITION BY data_time, deal_type, classify_type
  56. ORDER BY deal_value DESC
  57. ) AS row_number
  58. FROM
  59. trade_position_guangzhou_top
  60. WHERE
  61. data_time BETWEEN ? AND ?
  62. )
  63. UPDATE trade_position_guangzhou_top s
  64. SET s.rank = (
  65. SELECT t.row_number
  66. FROM ranked_data t
  67. WHERE s.classify_type = t.classify_type
  68. AND s.deal_short_name = t.deal_short_name
  69. AND s.data_time = t.data_time
  70. AND s.deal_type = t.deal_type
  71. )
  72. WHERE s.data_time BETWEEN ? AND ?`
  73. updatePars = append(updatePars, startDate, endDate, startDate, endDate)
  74. } else {
  75. sql3 = `update trade_position_guangzhou_top s
  76. JOIN (
  77. SELECT
  78. classify_type,
  79. deal_short_name,
  80. data_time,
  81. deal_type,
  82. (@row_number := IF(@prev_year = CONCAT_WS( "_", data_time, deal_type, classify_type), @row_number + 1, 1) ) AS row_number,
  83. @prev_year := CONCAT_WS( "_", data_time, deal_type, classify_type)
  84. FROM
  85. trade_position_guangzhou_top,(SELECT @row_number := 0, @prev_year := NULL) r
  86. where data_time between ? and ?
  87. ORDER BY
  88. data_time asc,
  89. deal_type asc,
  90. classify_type asc,
  91. deal_value DESC
  92. ) t
  93. ON s.classify_type = t.classify_type AND s.deal_short_name = t.deal_short_name
  94. AND s.data_time = t.data_time AND s.deal_type = t.deal_type
  95. SET s.rank = t.row_number where s.data_time between ? and ?;`
  96. updatePars = append(updatePars, startDate, endDate, startDate, endDate)
  97. }
  98. err = o.Exec(sql3, updatePars...).Error
  99. return
  100. }
  101. // GetTradePositionTopOriginGuangzhouDataTimes 获取榜单原始数据日期-正序
  102. func GetTradePositionTopOriginGuangzhouDataTimes(exchange string) (dates []string, err error) {
  103. o := global.DbMap[utils.DbNameIndex]
  104. sql := `SELECT
  105. DISTINCT a.data_time
  106. FROM
  107. base_from_trade_guangzhou_data a
  108. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  109. WHERE
  110. c.base_from_trade_guangzhou_classify_id IN ( 7, 8 )
  111. AND ( c.index_name LIKE '%持买单量%' OR c.index_name LIKE '%持卖单量%' )
  112. AND c.index_name NOT LIKE '%日成交持仓排名%'
  113. ORDER BY
  114. a.data_time asc`
  115. err = o.Raw(sql).Scan(&dates).Error
  116. return
  117. }
  118. func GetTradePositionOriginGuangzhouClassifyCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  119. o := global.DbMap[utils.DbNameIndex]
  120. var sql string
  121. var pars []interface{}
  122. if utils.DbDriverName == utils.DbDriverByDm {
  123. sql = `SELECT COUNT(*)
  124. FROM (
  125. SELECT DISTINCT n.classify_code, n.contract
  126. FROM base_from_trade_guangzhou_data a
  127. LEFT JOIN base_from_trade_guangzhou_index c
  128. ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  129. LEFT JOIN base_from_trade_guangzhou_contract n
  130. ON c.base_from_trade_guangzhou_contract_id = n.base_from_trade_guangzhou_contract_id
  131. WHERE a.data_time BETWEEN ? AND ?
  132. AND c.base_from_trade_guangzhou_classify_id IN (7, 8)
  133. AND (c.index_name LIKE '%持买单量%' OR c.index_name LIKE '%持卖单量%')
  134. AND c.index_name NOT LIKE '%日成交持仓排名%'
  135. ) AS distinct_records`
  136. pars = append(pars, startDate, endDate)
  137. } else {
  138. sql = `SELECT
  139. count(DISTINCT n.classify_code, n.contract )
  140. FROM
  141. base_from_trade_guangzhou_data a
  142. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  143. LEFT JOIN base_from_trade_guangzhou_contract n ON c.base_from_trade_guangzhou_contract_id = n.base_from_trade_guangzhou_contract_id
  144. WHERE
  145. a.data_time between ? and ?
  146. AND c.base_from_trade_guangzhou_classify_id IN ( 7, 8 )
  147. AND ( c.index_name LIKE '%持买单量%' OR c.index_name LIKE '%持卖单量%' )
  148. AND c.index_name NOT LIKE '%日成交持仓排名%'
  149. ORDER BY
  150. a.value DESC`
  151. pars = append(pars, startDate, endDate)
  152. }
  153. err = o.Raw(sql, pars...).Scan(&count).Error
  154. return
  155. }
  156. func GetFirstBaseFromTradeGuangzhouIndexByDate(exchange string) (item *GetFirstBaseFromTradeIndeDate, err error) {
  157. o := global.DbMap[utils.DbNameIndex]
  158. sql := `SELECT
  159. a.data_time
  160. FROM
  161. base_from_trade_guangzhou_data a
  162. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  163. WHERE
  164. c.base_from_trade_guangzhou_classify_id IN ( 7, 8 )
  165. AND ( c.index_name LIKE '%持买单量%' OR c.index_name LIKE '%持卖单量%' )
  166. AND c.index_name NOT LIKE '%日成交持仓排名%'
  167. ORDER BY
  168. a.data_time asc`
  169. err = o.Raw(sql).First(&item).Error
  170. return
  171. }
  172. func GetTradePositionOriginClassifyGuangzhouByExchangeDataTime(exchange, startDate, endDate string) (list []TradePositionClassifyInfo, err error) {
  173. o := global.DbMap[utils.DbNameIndex]
  174. sql := `SELECT DISTINCT classify_code as classify_name, contract as classify_type FROM base_from_trade_guangzhou_contract where trade_date >= ? and trade_date <= ?`
  175. err = o.Raw(sql, startDate, endDate).Scan(&list).Error
  176. return
  177. }
  178. func MultiInsertTradeBaseDataToTopGuangzhouByClassify(exchange string, startDate, endDate string, classifyNames, classifyTypes []string) (err error) {
  179. o := global.DbMap[utils.DbNameIndex]
  180. now := time.Now().Format(utils.FormatDateTime)
  181. //新增买单榜单
  182. 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)
  183. SELECT
  184. 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,?,?
  185. FROM
  186. base_from_trade_guangzhou_data a
  187. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  188. LEFT JOIN base_from_trade_guangzhou_contract n on c.base_from_trade_guangzhou_contract_id = n.base_from_trade_guangzhou_contract_id
  189. WHERE
  190. a.data_time between ? and ?
  191. and c.base_from_trade_guangzhou_classify_id in (7,8)
  192. and n.classify_code in (` + utils.GetOrmInReplace(len(classifyNames)) + `) and n.contract in (` + utils.GetOrmInReplace(len(classifyTypes)) + `)
  193. and c.index_name like '%持买单量%'
  194. and c.index_name not like '%日成交持仓排名%'`
  195. err = o.Exec(sql1, now, now, startDate, endDate, classifyNames, classifyTypes).Error
  196. if err != nil {
  197. return
  198. }
  199. //新增卖单榜单
  200. 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)
  201. SELECT
  202. 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,?,?
  203. FROM
  204. base_from_trade_guangzhou_data a
  205. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  206. LEFT JOIN base_from_trade_guangzhou_contract n on c.base_from_trade_guangzhou_contract_id = n.base_from_trade_guangzhou_contract_id
  207. WHERE
  208. a.data_time between ? and ?
  209. and c.base_from_trade_guangzhou_classify_id in (7,8)
  210. and n.classify_code in (` + utils.GetOrmInReplace(len(classifyNames)) + `) and n.contract in (` + utils.GetOrmInReplace(len(classifyTypes)) + `)
  211. and c.index_name like '%持卖单量%'
  212. and c.index_name not like '%日成交持仓排名%'`
  213. err = o.Exec(sql2, now, now, startDate, endDate, classifyNames, classifyTypes).Error
  214. if err != nil {
  215. return
  216. }
  217. //更新买单、卖单榜单排名字段
  218. var sql3 string
  219. var updatePars []interface{}
  220. if utils.DbDriverName == utils.DbDriverByDm {
  221. // TODO:
  222. sql3 = `WITH ranked_data AS (
  223. SELECT
  224. classify_type,
  225. deal_short_name,
  226. data_time,
  227. deal_type,
  228. ROW_NUMBER() OVER (
  229. PARTITION BY data_time, deal_type, classify_type
  230. ORDER BY deal_value DESC
  231. ) AS row_number
  232. FROM
  233. trade_position_guangzhou_top
  234. WHERE
  235. data_time BETWEEN ? AND ?
  236. AND classify_type IN (?)
  237. )
  238. UPDATE trade_position_guangzhou_top s
  239. SET s.rank = (
  240. SELECT t.row_number
  241. FROM ranked_data t
  242. WHERE s.classify_type = t.classify_type
  243. AND s.deal_short_name = t.deal_short_name
  244. AND s.data_time = t.data_time
  245. AND s.deal_type = t.deal_type
  246. )
  247. WHERE s.data_time BETWEEN ? AND ?
  248. AND s.classify_type IN (?)`
  249. updatePars = append(updatePars, startDate, endDate, classifyTypes, startDate, endDate, classifyTypes)
  250. } else {
  251. sql3 = `update trade_position_guangzhou_top s
  252. JOIN (
  253. SELECT
  254. classify_type,
  255. deal_short_name,
  256. data_time,
  257. deal_type,
  258. (@row_number := IF(@prev_year = CONCAT_WS( "_", data_time, deal_type, classify_type), @row_number + 1, 1) ) AS row_number,
  259. @prev_year := CONCAT_WS( "_", data_time, deal_type, classify_type)
  260. FROM
  261. trade_position_guangzhou_top,(SELECT @row_number := 0, @prev_year := NULL) r
  262. where data_time between ? and ? and classify_type in (` + utils.GetOrmInReplace(len(classifyTypes)) + `)
  263. ORDER BY
  264. data_time asc,
  265. deal_type asc,
  266. classify_type asc,
  267. deal_value DESC
  268. ) t
  269. ON s.classify_type = t.classify_type AND s.deal_short_name = t.deal_short_name
  270. AND s.data_time = t.data_time AND s.deal_type = t.deal_type
  271. SET s.rank = t.row_number where s.data_time between ? and ? and s.classify_type in (` + utils.GetOrmInReplace(len(classifyTypes)) + `);`
  272. updatePars = append(updatePars, startDate, endDate, classifyTypes, startDate, endDate, classifyTypes)
  273. }
  274. // sql3 := `UPDATE trade_position_guangzhou_top s
  275. // SET s.rank = t.row_number
  276. // FROM (
  277. // SELECT
  278. // classify_type,
  279. // deal_short_name,
  280. // data_time,
  281. // deal_type,
  282. // ROW_NUMBER() OVER (
  283. // PARTITION BY data_time, deal_type, classify_type
  284. // ORDER BY deal_value DESC
  285. // ) as row_number
  286. // FROM trade_position_guangzhou_top
  287. // WHERE data_time BETWEEN ? AND ?
  288. // AND classify_type IN (` + utils.GetOrmInReplace(len(classifyTypes)) + `)
  289. // ) t
  290. // WHERE s.classify_type = t.classify_type
  291. // AND s.deal_short_name = t.deal_short_name
  292. // AND s.data_time = t.data_time
  293. // AND s.deal_type = t.deal_type
  294. // AND s.data_time BETWEEN ? AND ?
  295. // AND s.classify_type IN (` + utils.GetOrmInReplace(len(classifyTypes)) + `)`
  296. err = o.Exec(sql3, updatePars...).Error
  297. return
  298. }