using YLErp.BLL; namespace YLErp.Modules.ToolsModule { public class EodCommodityFuturePriceService : YLBaseService { public EodCommodityFuturePriceService(OptUserInfo userInfo) : base(userInfo) { } /// /// 获取收盘价或结算价差值 /// /// 标的 /// 日期被减数 /// 日期减数 /// /// 价格模式 /// true:结算价 /// false:收盘价 /// 默认为根据系统设置选择收盘价或结算价 /// /// /// Key:code /// Value:差值 /// public Dictionary GetPriceDiff(List codes, DateTime date1, DateTime date2, bool? isSettlePrice = null) { string date1Str = date1.ToString("yyyyMMdd"); string date2Str = date2.ToString("yyyyMMdd"); Dictionary result = new Dictionary(); if (codes == null || codes.Count == 0) { return result; } if (isSettlePrice == null) { isSettlePrice = valuedateBLL.SystemDate.EodSettlePriceMode == "结算价"; } using (YLContext context = new YLContext()) { Dictionary> dict = (from left in (from db in context.eod_commodity_future_price where db.ValueDate == date1 && codes.Contains(db.UnderlyingCode) select db) join right in (from db in context.eod_commodity_future_price where db.ValueDate >= date2 && db.ValueDate < date1 && codes.Contains(db.UnderlyingCode) orderby db.ValueDate descending select db).Take(1) on left.UnderlyingCode equals right.UnderlyingCode into temp from right in temp.DefaultIfEmpty() select new { left, right }) .ToDictionary(O => O.left.UnderlyingCode, O => new List() { O.left, O.right }); for (int i = 0; i < codes.Count; i++) { if (!dict.ContainsKey(codes[i])) { result[codes[i]] = 1; continue; } if (isSettlePrice.Value) { result[codes[i]] = dict[codes[i]][0].SettlePrice - ((dict[codes[i]][1]?.SettlePrice) ?? 0); } else { result[codes[i]] = dict[codes[i]][0].ClosePrice - ((dict[codes[i]][1]?.ClosePrice) ?? 0); } } } return result; } } }