using BaseOUDAL; using DocumentFormat.OpenXml.Bibliography; using NPOI.POIFS.NIO; using YLErp.Helpers; namespace YLErp.Modules.EodModule { /// /// 日终价格服务 /// public class EodPriceService : YLBaseService { public EodPriceService(OptUserInfo userInfo) : base(userInfo) { } public SearchListResult SearchUnderlyingList(EodCommodityFuturePriceReq req) { var valueDtStart = req.ValueDateStart.Year > 2000 ? req.ValueDateStart : DateTime.Today.AddYears(-1); var valueDtEnd = req.ValueDateEnd.Year > 2000 ? req.ValueDateEnd.AddDays(1) : DateTime.Today.AddYears(1); var predicatUn = PredicateBuilder.Create(d => d.LaunchState == "1"); var predicatEoc = PredicateBuilder.Create(source => source.ValueDate >= valueDtStart && source.ValueDate < valueDtEnd); var predicatEot = PredicateBuilder.Create(source => source.ValueDate >= valueDtStart && source.ValueDate < valueDtEnd); var predicatEob = PredicateBuilder.Create(source => source.valuation_date >= valueDtStart && source.valuation_date < valueDtEnd); if (!string.IsNullOrEmpty(req.DataSource)) { predicatEoc = predicatEoc.And(d => d.DataSource.Contains(req.DataSource)); predicatEot = predicatEot.And(d => d.DataSource.Contains(req.DataSource)); if (req.DataSource=="系统") { predicatEob= predicatEob.And(d => d.JSID!=null); } else { predicatEob = predicatEob.And(d => d.JSID==null); } } if (!string.IsNullOrEmpty(req.MarketName)) { predicatUn = predicatUn.And(d => d.MarketName == req.MarketName); } if (!string.IsNullOrEmpty(req.UnderlyingCode)) { predicatUn = predicatUn.And(d => d.UnderlyingCode.Contains(req.UnderlyingCode)); } var queryUn = DbContext.underlying_manager.Where(predicatUn).Select(n => new { n.id, n.LaunchState, n.MarketName, n.UnderlyingState, n.UnderlyingType, n.UnderlyingCode, n.UnderlyingName,n.UnderlyingInstrumentType }); var query1 = from un in queryUn join source in DbContext.eod_commodity_future_price.Where(predicatEoc) on un.id equals source.UnderlyingId select new EodUnderlyingPriceDto { IsBond=false, id = source.id, DataSource = source.DataSource, LaunchState = un.LaunchState, MarketName = un.MarketName, UnderlyingId = un.id, UnderlyingCode = un.UnderlyingCode, UnderlyingName = un.UnderlyingName, UnderlyingState = un.UnderlyingState, UnderlyingType = un.UnderlyingType, UnderlyingInstrumentType = "CommodityFutures", ValueDate = source.ValueDate, SettlePrice = source.SettlePrice, ClosePrice = source.ClosePrice, UpdateTime = source.OptDate, ReferencePrice = source.ReferencePrice, SourceTime = source.SourceTime, DeciClosePrice=0, DeciSettlePrice = 0, DeciReferencePrice=0, JSID = null }; var query2 = from un in queryUn join stockClose in DbContext.eod_stock_price.Where(predicatEot) on un.UnderlyingCode equals stockClose.UnderlyingCode select new EodUnderlyingPriceDto { IsBond = false, id = stockClose.id, DataSource = stockClose.DataSource, LaunchState = un.LaunchState, MarketName = un.MarketName, UnderlyingId = un.id, UnderlyingCode = un.UnderlyingCode, UnderlyingName = un.UnderlyingName, UnderlyingState = un.UnderlyingState, UnderlyingType = un.UnderlyingType, UnderlyingInstrumentType = "Stock", ValueDate = stockClose.ValueDate, SettlePrice = stockClose.ClosePrice, ClosePrice = stockClose.ClosePrice, UpdateTime = stockClose.OptDate, ReferencePrice = stockClose.ReferencePrice, SourceTime = stockClose.SourceTime, DeciClosePrice = 0, DeciSettlePrice = 0, DeciReferencePrice = 0, JSID=null }; var query3 = from un in queryUn join bondClose in DbContext.china_bond_valuation.Where(predicatEob) on un.UnderlyingCode equals bondClose.bond_id select new EodUnderlyingPriceDto { IsBond = true, id = bondClose.id, DataSource="人工", LaunchState = un.LaunchState, MarketName = un.MarketName, UnderlyingId = un.id, UnderlyingCode = un.UnderlyingCode, UnderlyingName = un.UnderlyingName, UnderlyingState = un.UnderlyingState, UnderlyingType = un.UnderlyingType, UnderlyingInstrumentType = un.UnderlyingInstrumentType, ValueDate = bondClose.valuation_date, SettlePrice=0, DeciSettlePrice =bondClose.net_price, ClosePrice=0, DeciClosePrice = bondClose.dirty_price_close, UpdateTime = bondClose.update_time, ReferencePrice=0, DeciReferencePrice = bondClose.yield, SourceTime="", JSID=bondClose.JSID }; var unionQuery = query1.Concat(query2); var finalQuery = unionQuery.Concat(query3); if (string.IsNullOrEmpty(req.sidx)) { req.sidx = "ValueDate"; req.sord = "desc"; } var result= finalQuery.ToSearchList(req); foreach (var item in result.rows) { if (item.IsBond) { item.SourceTime = item.UpdateTime.HasValue? item.UpdateTime.Value.ToString("yyyy-MM-dd HH:mm:ss"):""; item.SettlePrice=Convert.ToDouble(item.DeciSettlePrice); item.ClosePrice = Convert.ToDouble(item.DeciClosePrice); item.ReferencePrice = Convert.ToDouble(item.DeciReferencePrice); if (item.JSID.HasValue) { item.DataSource = "系统"; } } } return result; } /// /// 保存日终期货价格 /// public eod_commodity_future_price SaveEodFuturePrice(eod_commodity_future_price req) { if (req is null) { throw new ArgumentNullException(nameof(req)); } if (DbContext.eod_commodity_future_price.Any(n => n.id != req.id && n.UnderlyingCode == req.UnderlyingCode && n.ValueDate == req.ValueDate)) { throw new ServiceException("已存在相同估值日期,相同合约的数据"); } eod_commodity_future_price dbmodel; if (req.id == 0) { DbContext.eod_commodity_future_price.Add(dbmodel = req); } else { dbmodel = DbContext.eod_commodity_future_price.Find(req.id); if (dbmodel == null) { throw new ServiceException("数据不存在"); } UpdateChanges(dbmodel, req); } SetDBModelOpt(dbmodel); dbmodel.DataSource = EodPriceBase.人工; DbContext.SaveChanges(); return dbmodel; } public ChinaBondValuation SaveBondPrice(ChinaBondValuation req) { if (req is null) { throw new ArgumentNullException(nameof(req)); } ChinaBondValuation dbmodel; if (req.id == 0) { DbContext.china_bond_valuation.Add(dbmodel = req); } else { dbmodel = DbContext.china_bond_valuation.Find(req.id); if (dbmodel == null) { throw new ServiceException("数据不存在"); } UpdateChanges(dbmodel, req); } dbmodel.update_time = DateTime.Now; DbContext.SaveChanges(); return dbmodel; } /// /// 保存日终股票价格 /// public eod_stock_price SaveEodStockPrice(eod_stock_price req) { if (req is null) { throw new ArgumentNullException(nameof(req)); } if (DbContext.eod_stock_price.Any(n => n.id != req.id && n.UnderlyingCode == req.UnderlyingCode && n.ValueDate == req.ValueDate)) { throw new ServiceException("已存在相同估值日期,相同合约的数据"); } eod_stock_price dbmodel; if (req.id == 0) { DbContext.eod_stock_price.Add(dbmodel = req); } else { dbmodel = DbContext.eod_stock_price.Find(req.id); if (dbmodel == null) { throw new ServiceException("数据不存在"); } UpdateChanges(dbmodel, req); } SetDBModelOpt(dbmodel); dbmodel.DataSource = EodPriceBase.人工; DbContext.SaveChanges(); return dbmodel; } } /// /// /// public class EodCommodityFuturePriceReq : BaseSearchReq { /// /// 数据来源 /// public string DataSource { get; set; } public string MarketName { get; set; } public string LaunchState { get; set; } /// /// 标的代码 /// public string UnderlyingCode { get; set; } public DateTime ValueDateStart { get; set; } public DateTime ValueDateEnd { get; set; } } public class EodUnderlyingPriceDto { public string EncryptId { get { return DataProtectHelper.Encrypt(id.ToString()); } } public long id { get; set; } public DateTime ValueDate { get; set; } public double SettlePrice { get; set; } public double ClosePrice { get; set; } public double? ReferencePrice { get; set; } public string DataSource { get; set; } public string UnderlyingType { get; set; } public string UnderlyingInstrumentType { get; set; } public string UnderlyingInstrumentTypeCn => ConsGlobal.InstrumentType.GetDesc(UnderlyingInstrumentType); public string UnderlyingState { get; set; } public string MarketName { get; set; } public string LaunchState { get; set; } public int UnderlyingId { get; set; } public string UnderlyingCode { get; set; } public string UnderlyingName { get; set; } public DateTime? UpdateTime { get; set; } public string SourceTime { get; set; } public decimal? DeciSettlePrice { get; set; } public decimal? DeciClosePrice { get; set; } public decimal? DeciReferencePrice { get; set; } public bool IsBond { get; set; } public long? JSID { get; set; } } }