RepositoryExtension.SyncStock.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Elasticsearch.Net;
  2. using JiaZhiQuan.Common.ElasticSearch;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using Senparc.Weixin.MP.Containers;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.Diagnostics;
  10. using System.Dynamic;
  11. using System.Linq;
  12. using System.Net.Http;
  13. using System.Reflection;
  14. using System.Security.Cryptography;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using Wicture.DbRESTFul;
  18. using Wicture.DbRESTFul.Cache;
  19. using Wicture.DbRESTFul.Infrastructure.Repository;
  20. namespace JiaZhiQuan.Common
  21. {
  22. public static partial class RepositoryExtension
  23. {
  24. public async static Task<int> SyncStockBKMapping(this DbRESTFulRepository repository, IHttpClientFactory httpClientFactory)
  25. {
  26. // 获取所有版块列表
  27. var bkList = (await repository.QueryAsync<string>("select codeDisplay from n_resource where type=1")).ToList();
  28. var count = 0;
  29. if (bkList.Count > 0)
  30. {
  31. var pageSize = 50;
  32. var httpClient = httpClientFactory.CreateClient();
  33. var urlFormatter = "https://push2.eastmoney.com/api/qt/clist/get?pz=" + pageSize + "&pn={0:G}&fs=b:{1:G}&fields=f12";
  34. // 获取每个板块对应的股票信息
  35. foreach (var bk in bkList)
  36. {
  37. var curIdx = 1;
  38. List<string> stocks = new List<string>();
  39. while (true)
  40. {
  41. var url = string.Format(urlFormatter, curIdx, bk);
  42. var rst = await httpClient.GetStringAsync(url);
  43. var loadComplete = false;
  44. if (!string.IsNullOrEmpty(rst))
  45. {
  46. var data = JsonConvert.DeserializeObject<JObject>(rst)?.Value<JObject>("data");
  47. if (data == null) break;
  48. for (var i = 0; i < pageSize; i++)
  49. {
  50. var code = data.Value<JObject>("diff")?.Value<JObject>(i.ToString())?.Value<string>("f12");
  51. if (string.IsNullOrEmpty(code))
  52. {
  53. loadComplete = true;
  54. break;
  55. }
  56. stocks.Add(code);
  57. }
  58. }
  59. if (loadComplete) break;
  60. curIdx++;
  61. }
  62. // 更新关系到数据库中
  63. var bkCode = "1_" + bk;
  64. var stockCodes = stocks.Select(e => "1_" + e);
  65. if (stockCodes.Count() > 0)
  66. {
  67. await repository.QueryAsync<dynamic>($"insert ignore into n_resource_stock_bk_map(stockCode, bkCode) values(@stockCode, '{bkCode}')", stockCodes.Select(e => new { stockCode = e }), null, null, false);
  68. await repository.QueryAsync<dynamic>("delete from n_resource_stock_bk_map where bkCode=@bkCode and stockCode not in @stockCodes", new { bkCode, stockCodes }, null, null, false);
  69. }
  70. else
  71. {
  72. await repository.QueryAsync<dynamic>("delete from n_resource_stock_bk_map where bkCode=@bkCode", new { bkCode }, null, null, false);
  73. }
  74. count += stocks.Count;
  75. }
  76. }
  77. return count;
  78. }
  79. public async static Task<int> SyncStock(this DbRESTFulRepository repository, int marketType, IHttpClientFactory httpClientFactory)
  80. {
  81. var count = 0;
  82. var pageSize = 2000;
  83. var pageIndex = 1;
  84. var httpClient = httpClientFactory.CreateClient();
  85. var fsList = new List<KeyValuePair<int, string>>();
  86. if (marketType == 0 || marketType == 1) fsList.Add(new KeyValuePair<int, string>(1, "m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048"));
  87. if (marketType == 0 || marketType == 2) fsList.Add(new KeyValuePair<int, string>(2, "m:128+t:3,m:128+t:4,m:128+t:1,m:128+t:2"));
  88. if (marketType == 0 || marketType == 4) fsList.Add(new KeyValuePair<int, string>(4, "m:105,m:106,m:107"));
  89. foreach (var task in fsList)
  90. {
  91. var fs = task.Value;
  92. marketType = task.Key;
  93. var urlFormatter = "http://22.push2.eastmoney.com/api/qt/clist/get?pn={0:G}&pz=" + pageSize + "&fid=f12&fs=" + fs + "&fields=f12,f14";
  94. var loadComplete = false;
  95. while (true)
  96. {
  97. var stocks = new List<JObject>();
  98. var url = string.Format(urlFormatter, pageIndex);
  99. var rst = await httpClient.GetStringAsync(url);
  100. if (!string.IsNullOrEmpty(rst))
  101. {
  102. var data = JsonConvert.DeserializeObject<JObject>(rst)?.Value<JObject>("data");
  103. if (data == null) break;
  104. for (var i = 0; i < pageSize; i++)
  105. {
  106. var item = data.Value<JObject>("diff")?.Value<JObject>(i.ToString());
  107. if (item == null)
  108. {
  109. loadComplete = true;
  110. break;
  111. }
  112. stocks.Add(item);
  113. }
  114. }
  115. if (stocks.Count > 0)
  116. {
  117. await repository.QueryAsync<dynamic>("insert into n_resource(`code`, codeDisplay, name, type, marketType) values(@code, @codeDisplay, @name, 0, " + marketType + ") ON DUPLICATE KEY update name=@name;", stocks.Select(e => new { code = marketType + "_" + e.Value<string>("f12"), codeDisplay = e.Value<string>("f12"), name = e.Value<string>("f14") }), null, null, false);
  118. }
  119. count += stocks.Count;
  120. if (loadComplete) break;
  121. pageIndex++;
  122. }
  123. }
  124. return count;
  125. }
  126. }
  127. }