using System; using System.Collections.Generic; using System.Linq; using YLErp.BLL; namespace YLErp.Modules.MarginModule { public class eod_commodity_future_priceBLLBak { private readonly YLContext db = new YLContext(); public static List DataSources = new List { "系统", "人工" }; /// /// 获取指定日期的收盘价或结算价 /// /// 标的代码 /// 日期 /// /// 价格类型: /// 0:最高价; /// 1:最低价; /// 2:收盘价; /// 3:结算价; /// /// public Dictionary GetEodPrice(IEnumerable codes, DateTime date, int priceType) { if (priceType < 0 || priceType > 3) { throw new ArgumentOutOfRangeException("priceType"); } var result = new Dictionary(); #region 商品期货 var futurePirce = (from priceDb in db.eod_commodity_future_price where priceDb.ValueDate == date && codes.Contains(priceDb.UnderlyingCode) select new { priceDb.UnderlyingCode, priceDb.HighPrice, priceDb.LowPrice, priceDb.ClosePrice, priceDb.SettlePrice }) .ToDictionary( K => K.UnderlyingCode, V => { switch (priceType) { case 0: return V.HighPrice ?? 0; case 1: return V.LowPrice ?? 0; case 2: return V.ClosePrice; case 3: return V.SettlePrice; default: return 0; } } , StringComparer.OrdinalIgnoreCase); #endregion #region 股票 var stockPirce = (from priceDb in db.eod_stock_price where priceDb.ValueDate == date && codes.Contains(priceDb.UnderlyingCode) select new { priceDb.UnderlyingCode, priceDb.HighPrice, priceDb.LowPrice, priceDb.ClosePrice }) .ToDictionary( K => K.UnderlyingCode, V => { switch (priceType) { case 0: return V.HighPrice ?? 0; case 1: return V.LowPrice ?? 0; case 2: return V.ClosePrice; case 3: return V.ClosePrice; default: return 0; } } , StringComparer.OrdinalIgnoreCase); #endregion #region 场内期权 var exchangePirce = (from priceDb in db.eod_exchange_option_price where priceDb.ValueDate == date && codes.Contains(priceDb.UnderlyingCode) select new { priceDb.UnderlyingCode, priceDb.HighPrice, priceDb.LowPrice, priceDb.ClosePrice, priceDb.SettlePrice }) .ToDictionary( K => K.UnderlyingCode, V => { switch (priceType) { case 0: return V.HighPrice ?? 0; case 1: return V.LowPrice ?? 0; case 2: return V.ClosePrice; case 3: return V.SettlePrice; default: return 0; } } , StringComparer.OrdinalIgnoreCase); #endregion var errorCode = new List(); foreach (var item in codes) { if (!futurePirce.ContainsKey(item) && !stockPirce.ContainsKey(item) && !exchangePirce.ContainsKey(item)) { errorCode.Add(item); } if (futurePirce.ContainsKey(item)) { result[item] = futurePirce[item]; } else if (stockPirce.ContainsKey(item)) { result[item] = stockPirce[item]; } else if (exchangePirce.ContainsKey(item)) { result[item] = exchangePirce[item]; } else { result[item] = 0; } } if (errorCode.Count > 0) { throw new Exception($"{string.Join(",", errorCode)} 价格缺失"); } return result; } /// /// 获取指定日期的收盘价或结算价 /// /// 标的代码 /// 日期 /// /// 价格类型: /// 0:最高价; /// 1:最低价; /// 2:收盘价; /// 3:结算价; /// /// public Dictionary GetEodPriceToId(IEnumerable codes, DateTime date, int priceType) { var result = new Dictionary(); var dict = GetEodPrice(codes, date, priceType); foreach (var item in dict) { var um = DataCacheProvider.GetUnderlyingDataSource().GetData(item.Key); if (um == null) { continue; } result[um.id] = item.Value; } return result; } } }