using System.Reflection;
using System.Text.Json;
namespace XFEEDToolBox.Core.Data;
public static class CommodityCatalog
{
private sealed record Entry(long Id, string Symbol, string Name);
private static readonly Lazy<(Dictionary<long, string> Ids, Dictionary<string, string> Names)> Entries = new(() =>
{
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("XFEEDToolBox.Core.Resources.commodities.json");
var rows = stream == null ? [] : JsonSerializer.Deserialize<Entry[]>(stream, new JsonSerializerOptions(JsonSerializerDefaults.Web)) ?? [];
return (rows.ToDictionary(x => x.Id, x => JsonFields.CommodityKey(x.Symbol)), rows.DistinctBy(x => JsonFields.CommodityKey(x.Name)).ToDictionary(x => JsonFields.CommodityKey(x.Name), x => JsonFields.CommodityKey(x.Symbol)));
});
public static string Key(string name, long id = 0, string symbol = "")
{
if (symbol.Length > 0) return JsonFields.CommodityKey(symbol);
if (id > 0 && Entries.Value.Ids.TryGetValue(id, out var byId)) return byId;
var normalized = JsonFields.CommodityKey(name);
return Entries.Value.Names.GetValueOrDefault(normalized, normalized);
}
public static string SelectionKey(string name)
{
var key=Key(name);
if(!Localization.ChineseNames.IsChinese(key))return key;
var matches=Entries.Value.Names.Where(p=>Localization.ChineseNames.Translate(p.Key).Equals(name.Trim(),StringComparison.OrdinalIgnoreCase))
.Select(p=>p.Value).Distinct(StringComparer.Ordinal).ToArray();
return matches.Length==1?matches[0]:key;
}
}
using System.Reflection;
using System.Text.Json;
namespace XFEEDToolBox.Core.Data;
public static class CommodityCatalog
{
private sealed record Entry(long Id, string Symbol, string Name);
private static readonly Lazy<(Dictionary<long, string> Ids, Dictionary<string, string> Names)> Entries = new(() =>
{
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("XFEEDToolBox.Core.Resources.commodities.json");
var rows = stream == null ? [] : JsonSerializer.Deserialize<Entry[]>(stream, new JsonSerializerOptions(JsonSerializerDefaults.Web)) ?? [];
return (rows.ToDictionary(x => x.Id, x => JsonFields.CommodityKey(x.Symbol)), rows.DistinctBy(x => JsonFields.CommodityKey(x.Name)).ToDictionary(x => JsonFields.CommodityKey(x.Name), x => JsonFields.CommodityKey(x.Symbol)));
});
public static string Key(string name, long id = 0, string symbol = "")
{
if (symbol.Length > 0) return JsonFields.CommodityKey(symbol);
if (id > 0 && Entries.Value.Ids.TryGetValue(id, out var byId)) return byId;
var normalized = JsonFields.CommodityKey(name);
return Entries.Value.Names.GetValueOrDefault(normalized, normalized);
}
public static string SelectionKey(string name)
{
var key=Key(name);
if(!Localization.ChineseNames.IsChinese(key))return key;
var matches=Entries.Value.Names.Where(p=>Localization.ChineseNames.Translate(p.Key).Equals(name.Trim(),StringComparison.OrdinalIgnoreCase))
.Select(p=>p.Value).Distinct(StringComparer.Ordinal).ToArray();
return matches.Length==1?matches[0]:key;
}
}