using Models;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Web;
using System.Text.RegularExpressions;
namespace DAL
{
///
/// 汇率接口缓存 数据处理
///
public class CurrencyCache
{
///
/// 雷怡 2021-08-17
/// 将汇率接口数据 存入Cache缓存
///
///
///
public static void GetRateCache()
{
DataTable dt = (DataTable)HttpRuntime.Cache["CurrencyCache"];
if (dt == null)
{
//处理汇率
ExchangeRateAPI rateAPI = new ExchangeRateAPI();
string jsonText = rateAPI.ForeignCurrency();
JArray jar = JArray.Parse(jsonText);
List rateAPIs = new List();
for (int i = 0; i < jar.Count; i++)
{
JObject j = JObject.Parse(jar[i].ToString());
for (int x = 1; x < j.Count + 1; x++)
{
string ss = "data" + x;
string s = j[ss].ToString();
JObject s1 = (JObject)JsonConvert.DeserializeObject(s);
RateAPI rateModel = new RateAPI();
rateModel.name = s1["name"].ToString();
rateModel.fBuyPri = double.Parse(s1["fBuyPri"].ToString() == "" ? "0" : s1["fBuyPri"].ToString());
rateModel.mBuyPri = double.Parse(s1["mBuyPri"].ToString() == "" ? "0" : s1["mBuyPri"].ToString());
rateModel.fSellPri = double.Parse(s1["fSellPri"].ToString() == "" ? "0" : s1["fSellPri"].ToString());
rateModel.mSellPri = double.Parse(s1["mSellPri"].ToString() == "" ? "0" : s1["mSellPri"].ToString());
rateModel.bankConversionPri = double.Parse(s1["bankConversionPri"].ToString() == "" ? "0" : s1["bankConversionPri"].ToString());
rateModel.date = s1["date"].ToString();//年月日
rateModel.time = s1["time"].ToString();//时刻
rateAPIs.Add(rateModel);
}
}
DataTable dt1 = DTList.ToDataTable(new List(), rateAPIs);
HttpRuntime.Cache.Insert("CurrencyCache", dt1,null, DateTime.Now.AddHours(2),TimeSpan.Zero);
}
}
///
/// 查询 Cache 缓存 接口数据
///
/// 币种三字码
///
public static ErrorCode GetRate(string RateThreeCode)
{
string Rate = "";
ErrorCode ec = new ErrorCode();
try
{
DataTable dt = (DataTable)HttpRuntime.Cache["CurrencyCache"];
if (dt == null)
{
GetRateCache(); //添加缓存
dt = (DataTable)HttpRuntime.Cache["CurrencyCache"];
}
//处理币种;
string CurrencyCH_EN = "";
switch (RateThreeCode)
{
case "USD":
CurrencyCH_EN = "美元";
break;
case "EUR":
CurrencyCH_EN = "欧元";
break;
case "HKD":
CurrencyCH_EN = "港币";
break;
case "JPY":
CurrencyCH_EN = "日元";
break;
case "GBP":
CurrencyCH_EN = "英镑";
break;
case "AUD":
CurrencyCH_EN = "美元";
break;
case "CAD":
CurrencyCH_EN = "澳大利亚元";
break;
case "THB":
CurrencyCH_EN = "泰国铢";
break;
case "SGD":
CurrencyCH_EN = "新加坡元";
break;
case "CHF":
CurrencyCH_EN = "瑞士法郎";
break;
case "DKK":
CurrencyCH_EN = "丹麦克朗";
break;
case "MOP":
CurrencyCH_EN = "澳门元";
break;
case "MYR":
CurrencyCH_EN = "林吉特";
break;
case "NOK":
CurrencyCH_EN = "挪威克朗";
break;
case "NZD":
CurrencyCH_EN = "新西兰元";
break;
case "RUB":
CurrencyCH_EN = "卢布";
break;
case "SEK":
CurrencyCH_EN = "瑞典克朗";
break;
case "PHP":
CurrencyCH_EN = "菲律宾比索";
break;
case "TWD":
CurrencyCH_EN = "新台币";
break;
case "BRL":
CurrencyCH_EN = "巴西雷亚尔";
break;
case "KRW":
CurrencyCH_EN = "韩国元";
break;
case "ZAR":
CurrencyCH_EN = "南非兰特";
break;
case "CNY":
ec.errorCode = "0";
ec.msg = "1";
return ec;
default:
break;
}
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["name"].ToString() == CurrencyCH_EN)
{
ec.errorCode = "0";
Rate = dt.Rows[i]["fSellPri"].ToString();
ec.msg = Rate;
}
}
Regex reg = new Regex("^[0]+(.[0-9]{1,3})?$");
if (reg.Match(Rate).Success)
{
ec.errorCode = "1";
ec.msg = "该汇率接口没有 “" + CurrencyCH_EN + "”的汇率!";
}
}
catch (Exception)
{
ec.errorCode = "2";
ec.msg = "该汇率接口访问次数达到上限!";
}
return ec;
}
}
}