CurrencyCache.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using Models;
  2. using Newtonsoft.Json.Linq;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Data;
  10. using System.Web;
  11. using System.Text.RegularExpressions;
  12. namespace DAL
  13. {
  14. /// <summary>
  15. /// 汇率接口缓存 数据处理
  16. /// </summary>
  17. public class CurrencyCache
  18. {
  19. /// <summary>
  20. /// 雷怡 2021-08-17
  21. /// 将汇率接口数据 存入Cache缓存
  22. /// </summary>
  23. /// <param name="Country"></param>
  24. /// <returns></returns>
  25. public static void GetRateCache()
  26. {
  27. DataTable dt = (DataTable)HttpRuntime.Cache["CurrencyCache"];
  28. if (dt == null)
  29. {
  30. //处理汇率
  31. ExchangeRateAPI rateAPI = new ExchangeRateAPI();
  32. string jsonText = rateAPI.ForeignCurrency();
  33. JArray jar = JArray.Parse(jsonText);
  34. List<RateAPI> rateAPIs = new List<RateAPI>();
  35. for (int i = 0; i < jar.Count; i++)
  36. {
  37. JObject j = JObject.Parse(jar[i].ToString());
  38. for (int x = 1; x < j.Count + 1; x++)
  39. {
  40. string ss = "data" + x;
  41. string s = j[ss].ToString();
  42. JObject s1 = (JObject)JsonConvert.DeserializeObject(s);
  43. RateAPI rateModel = new RateAPI();
  44. rateModel.name = s1["name"].ToString();
  45. rateModel.fBuyPri = double.Parse(s1["fBuyPri"].ToString() == "" ? "0" : s1["fBuyPri"].ToString());
  46. rateModel.mBuyPri = double.Parse(s1["mBuyPri"].ToString() == "" ? "0" : s1["mBuyPri"].ToString());
  47. rateModel.fSellPri = double.Parse(s1["fSellPri"].ToString() == "" ? "0" : s1["fSellPri"].ToString());
  48. rateModel.mSellPri = double.Parse(s1["mSellPri"].ToString() == "" ? "0" : s1["mSellPri"].ToString());
  49. rateModel.bankConversionPri = double.Parse(s1["bankConversionPri"].ToString() == "" ? "0" : s1["bankConversionPri"].ToString());
  50. rateModel.date = s1["date"].ToString();//年月日
  51. rateModel.time = s1["time"].ToString();//时刻
  52. rateAPIs.Add(rateModel);
  53. }
  54. }
  55. DataTable dt1 = DTList.ToDataTable<RateAPI>(new List<RateAPI>(), rateAPIs);
  56. HttpRuntime.Cache.Insert("CurrencyCache", dt1,null, DateTime.Now.AddHours(2),TimeSpan.Zero);
  57. }
  58. }
  59. /// <summary>
  60. /// 查询 Cache 缓存 接口数据
  61. /// </summary>
  62. /// <param name="RateThreeCode">币种三字码</param>
  63. /// <returns></returns>
  64. public static ErrorCode GetRate(string RateThreeCode)
  65. {
  66. string Rate = "";
  67. ErrorCode ec = new ErrorCode();
  68. try
  69. {
  70. DataTable dt = (DataTable)HttpRuntime.Cache["CurrencyCache"];
  71. if (dt == null)
  72. {
  73. GetRateCache(); //添加缓存
  74. dt = (DataTable)HttpRuntime.Cache["CurrencyCache"];
  75. }
  76. //处理币种;
  77. string CurrencyCH_EN = "";
  78. switch (RateThreeCode)
  79. {
  80. case "USD":
  81. CurrencyCH_EN = "美元";
  82. break;
  83. case "EUR":
  84. CurrencyCH_EN = "欧元";
  85. break;
  86. case "HKD":
  87. CurrencyCH_EN = "港币";
  88. break;
  89. case "JPY":
  90. CurrencyCH_EN = "日元";
  91. break;
  92. case "GBP":
  93. CurrencyCH_EN = "英镑";
  94. break;
  95. case "AUD":
  96. CurrencyCH_EN = "美元";
  97. break;
  98. case "CAD":
  99. CurrencyCH_EN = "澳大利亚元";
  100. break;
  101. case "THB":
  102. CurrencyCH_EN = "泰国铢";
  103. break;
  104. case "SGD":
  105. CurrencyCH_EN = "新加坡元";
  106. break;
  107. case "CHF":
  108. CurrencyCH_EN = "瑞士法郎";
  109. break;
  110. case "DKK":
  111. CurrencyCH_EN = "丹麦克朗";
  112. break;
  113. case "MOP":
  114. CurrencyCH_EN = "澳门元";
  115. break;
  116. case "MYR":
  117. CurrencyCH_EN = "林吉特";
  118. break;
  119. case "NOK":
  120. CurrencyCH_EN = "挪威克朗";
  121. break;
  122. case "NZD":
  123. CurrencyCH_EN = "新西兰元";
  124. break;
  125. case "RUB":
  126. CurrencyCH_EN = "卢布";
  127. break;
  128. case "SEK":
  129. CurrencyCH_EN = "瑞典克朗";
  130. break;
  131. case "PHP":
  132. CurrencyCH_EN = "菲律宾比索";
  133. break;
  134. case "TWD":
  135. CurrencyCH_EN = "新台币";
  136. break;
  137. case "BRL":
  138. CurrencyCH_EN = "巴西雷亚尔";
  139. break;
  140. case "KRW":
  141. CurrencyCH_EN = "韩国元";
  142. break;
  143. case "ZAR":
  144. CurrencyCH_EN = "南非兰特";
  145. break;
  146. case "CNY":
  147. ec.errorCode = "0";
  148. ec.msg = "1";
  149. return ec;
  150. default:
  151. break;
  152. }
  153. for (int i = 0; i < dt.Rows.Count; i++)
  154. {
  155. if (dt.Rows[i]["name"].ToString() == CurrencyCH_EN)
  156. {
  157. ec.errorCode = "0";
  158. Rate = dt.Rows[i]["fSellPri"].ToString();
  159. ec.msg = Rate;
  160. }
  161. }
  162. Regex reg = new Regex("^[0]+(.[0-9]{1,3})?$");
  163. if (reg.Match(Rate).Success)
  164. {
  165. ec.errorCode = "1";
  166. ec.msg = "该汇率接口没有 “" + CurrencyCH_EN + "”的汇率!";
  167. }
  168. }
  169. catch (Exception)
  170. {
  171. ec.errorCode = "2";
  172. ec.msg = "该汇率接口访问次数达到上限!";
  173. }
  174. return ec;
  175. }
  176. }
  177. }