trade_position_analysis_guangzhou.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. var originDates []string
  116. err = o.Raw(sql).Scan(&originDates).Error
  117. if err != nil {
  118. return
  119. }
  120. for _, v := range originDates {
  121. v = utils.GormDateStrToDateStr(v)
  122. dates = append(dates, v)
  123. }
  124. return
  125. }
  126. func GetTradePositionOriginGuangzhouClassifyCountByExchangeDataTime(exchange string, startDate, endDate string) (count int64, err error) {
  127. o := global.DbMap[utils.DbNameIndex]
  128. var sql string
  129. var pars []interface{}
  130. if utils.DbDriverName == utils.DbDriverByDm {
  131. sql = `SELECT COUNT(*)
  132. FROM (
  133. SELECT DISTINCT n.classify_code, n.contract
  134. FROM base_from_trade_guangzhou_data a
  135. LEFT JOIN base_from_trade_guangzhou_index c
  136. ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  137. LEFT JOIN base_from_trade_guangzhou_contract n
  138. ON c.base_from_trade_guangzhou_contract_id = n.base_from_trade_guangzhou_contract_id
  139. WHERE a.data_time BETWEEN ? AND ?
  140. AND c.base_from_trade_guangzhou_classify_id IN (7, 8)
  141. AND (c.index_name LIKE '%持买单量%' OR c.index_name LIKE '%持卖单量%')
  142. AND c.index_name NOT LIKE '%日成交持仓排名%'
  143. ) AS distinct_records`
  144. pars = append(pars, startDate, endDate)
  145. } else {
  146. sql = `SELECT
  147. count(DISTINCT n.classify_code, n.contract )
  148. FROM
  149. base_from_trade_guangzhou_data a
  150. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  151. LEFT JOIN base_from_trade_guangzhou_contract n ON c.base_from_trade_guangzhou_contract_id = n.base_from_trade_guangzhou_contract_id
  152. WHERE
  153. a.data_time between ? and ?
  154. AND c.base_from_trade_guangzhou_classify_id IN ( 7, 8 )
  155. AND ( c.index_name LIKE '%持买单量%' OR c.index_name LIKE '%持卖单量%' )
  156. AND c.index_name NOT LIKE '%日成交持仓排名%'
  157. ORDER BY
  158. a.value DESC`
  159. pars = append(pars, startDate, endDate)
  160. }
  161. err = o.Raw(sql, pars...).Scan(&count).Error
  162. return
  163. }
  164. func GetFirstBaseFromTradeGuangzhouIndexByDate(exchange string) (item *GetFirstBaseFromTradeIndeDate, err error) {
  165. o := global.DbMap[utils.DbNameIndex]
  166. sql := `SELECT
  167. a.data_time
  168. FROM
  169. base_from_trade_guangzhou_data a
  170. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  171. WHERE
  172. c.base_from_trade_guangzhou_classify_id IN ( 7, 8 )
  173. AND ( c.index_name LIKE '%持买单量%' OR c.index_name LIKE '%持卖单量%' )
  174. AND c.index_name NOT LIKE '%日成交持仓排名%'
  175. ORDER BY
  176. a.data_time asc`
  177. err = o.Raw(sql).First(&item).Error
  178. if err != nil {
  179. return
  180. }
  181. item.DataTime = utils.GormDateStrToDateStr(item.DataTime)
  182. return
  183. }
  184. func GetTradePositionOriginClassifyGuangzhouByExchangeDataTime(exchange, startDate, endDate string) (list []TradePositionClassifyInfo, err error) {
  185. o := global.DbMap[utils.DbNameIndex]
  186. sql := `SELECT DISTINCT classify_code as classify_name, contract as classify_type FROM base_from_trade_guangzhou_contract where trade_date >= ? and trade_date <= ?`
  187. err = o.Raw(sql, startDate, endDate).Scan(&list).Error
  188. return
  189. }
  190. func MultiInsertTradeBaseDataToTopGuangzhouByClassify(exchange string, startDate, endDate string, classifyNames, classifyTypes []string) (err error) {
  191. o := global.DbMap[utils.DbNameIndex]
  192. now := time.Now().Format(utils.FormatDateTime)
  193. //新增买单榜单
  194. 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)
  195. SELECT
  196. 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,?,?
  197. FROM
  198. base_from_trade_guangzhou_data a
  199. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  200. LEFT JOIN base_from_trade_guangzhou_contract n on c.base_from_trade_guangzhou_contract_id = n.base_from_trade_guangzhou_contract_id
  201. WHERE
  202. a.data_time between ? and ?
  203. and c.base_from_trade_guangzhou_classify_id in (7,8)
  204. and n.classify_code in (` + utils.GetOrmInReplace(len(classifyNames)) + `) and n.contract in (` + utils.GetOrmInReplace(len(classifyTypes)) + `)
  205. and c.index_name like '%持买单量%'
  206. and c.index_name not like '%日成交持仓排名%'`
  207. err = o.Exec(sql1, now, now, startDate, endDate, classifyNames, classifyTypes).Error
  208. if err != nil {
  209. return
  210. }
  211. //新增卖单榜单
  212. 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)
  213. SELECT
  214. 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,?,?
  215. FROM
  216. base_from_trade_guangzhou_data a
  217. LEFT JOIN base_from_trade_guangzhou_index c ON a.base_from_trade_guangzhou_index_id = c.base_from_trade_guangzhou_index_id
  218. LEFT JOIN base_from_trade_guangzhou_contract n on c.base_from_trade_guangzhou_contract_id = n.base_from_trade_guangzhou_contract_id
  219. WHERE
  220. a.data_time between ? and ?
  221. and c.base_from_trade_guangzhou_classify_id in (7,8)
  222. and n.classify_code in (` + utils.GetOrmInReplace(len(classifyNames)) + `) and n.contract in (` + utils.GetOrmInReplace(len(classifyTypes)) + `)
  223. and c.index_name like '%持卖单量%'
  224. and c.index_name not like '%日成交持仓排名%'`
  225. err = o.Exec(sql2, now, now, startDate, endDate, classifyNames, classifyTypes).Error
  226. if err != nil {
  227. return
  228. }
  229. //更新买单、卖单榜单排名字段
  230. var sql3 string
  231. var updatePars []interface{}
  232. if utils.DbDriverName == utils.DbDriverByDm {
  233. // TODO:
  234. sql3 = `WITH ranked_data AS (
  235. SELECT
  236. classify_type,
  237. deal_short_name,
  238. data_time,
  239. deal_type,
  240. ROW_NUMBER() OVER (
  241. PARTITION BY data_time, deal_type, classify_type
  242. ORDER BY deal_value DESC
  243. ) AS row_number
  244. FROM
  245. trade_position_guangzhou_top
  246. WHERE
  247. data_time BETWEEN ? AND ?
  248. AND classify_type IN (?)
  249. )
  250. UPDATE trade_position_guangzhou_top s
  251. SET s.rank = (
  252. SELECT t.row_number
  253. FROM ranked_data t
  254. WHERE s.classify_type = t.classify_type
  255. AND s.deal_short_name = t.deal_short_name
  256. AND s.data_time = t.data_time
  257. AND s.deal_type = t.deal_type
  258. )
  259. WHERE s.data_time BETWEEN ? AND ?
  260. AND s.classify_type IN (?)`
  261. updatePars = append(updatePars, startDate, endDate, classifyTypes, startDate, endDate, classifyTypes)
  262. } else {
  263. sql3 = `update trade_position_guangzhou_top s
  264. JOIN (
  265. SELECT
  266. classify_type,
  267. deal_short_name,
  268. data_time,
  269. deal_type,
  270. (@row_number := IF(@prev_year = CONCAT_WS( "_", data_time, deal_type, classify_type), @row_number + 1, 1) ) AS row_number,
  271. @prev_year := CONCAT_WS( "_", data_time, deal_type, classify_type)
  272. FROM
  273. trade_position_guangzhou_top,(SELECT @row_number := 0, @prev_year := NULL) r
  274. where data_time between ? and ? and classify_type in (` + utils.GetOrmInReplace(len(classifyTypes)) + `)
  275. ORDER BY
  276. data_time asc,
  277. deal_type asc,
  278. classify_type asc,
  279. deal_value DESC
  280. ) t
  281. ON s.classify_type = t.classify_type AND s.deal_short_name = t.deal_short_name
  282. AND s.data_time = t.data_time AND s.deal_type = t.deal_type
  283. SET s.rank = t.row_number where s.data_time between ? and ? and s.classify_type in (` + utils.GetOrmInReplace(len(classifyTypes)) + `);`
  284. updatePars = append(updatePars, startDate, endDate, classifyTypes, startDate, endDate, classifyTypes)
  285. }
  286. // sql3 := `UPDATE trade_position_guangzhou_top s
  287. // SET s.rank = t.row_number
  288. // FROM (
  289. // SELECT
  290. // classify_type,
  291. // deal_short_name,
  292. // data_time,
  293. // deal_type,
  294. // ROW_NUMBER() OVER (
  295. // PARTITION BY data_time, deal_type, classify_type
  296. // ORDER BY deal_value DESC
  297. // ) as row_number
  298. // FROM trade_position_guangzhou_top
  299. // WHERE data_time BETWEEN ? AND ?
  300. // AND classify_type IN (` + utils.GetOrmInReplace(len(classifyTypes)) + `)
  301. // ) t
  302. // WHERE s.classify_type = t.classify_type
  303. // AND s.deal_short_name = t.deal_short_name
  304. // AND s.data_time = t.data_time
  305. // AND s.deal_type = t.deal_type
  306. // AND s.data_time BETWEEN ? AND ?
  307. // AND s.classify_type IN (` + utils.GetOrmInReplace(len(classifyTypes)) + `)`
  308. err = o.Exec(sql3, updatePars...).Error
  309. return
  310. }