diff --git a/YLErpDAL/Modules/EodModule/GLMSGreeksHandleService.cs b/YLErpDAL/Modules/EodModule/GLMSGreeksHandleService.cs index 1baf43e8..3b39427e 100644 --- a/YLErpDAL/Modules/EodModule/GLMSGreeksHandleService.cs +++ b/YLErpDAL/Modules/EodModule/GLMSGreeksHandleService.cs @@ -31,6 +31,7 @@ namespace YLErp.Modules.EodModule Dictionary _dDic = new Dictionary(); Dictionary _cDic = new Dictionary(); Dictionary _ytmDic = new Dictionary(); + Dictionary _cfDic = new Dictionary(); //期货对应的债券代码 Dictionary _TFeaturesBondCodeDic = new Dictionary(); @@ -51,24 +52,9 @@ namespace YLErp.Modules.EodModule using var db = DbContextFactory.GetYLDbContext(); var conn = db.Database.GetDbConnection(); var bondsUmCodes = umDatas.Where(p => instrumentBondsTypes.Contains(p.UnderlyingInstrumentType)).Select(p => p.UnderlyingCode).Distinct().ToList(); - if (bondsUmCodes != null && bondsUmCodes.Count > 0) + if (bondsUmCodes == null) { - var datas = db.china_bond_valuation.AsNoTracking().Where(p => p.valuation_date == valueDate && bondsUmCodes.Contains(p.bond_id)).Select(p => new - { - p.bond_id, - p.modi_dura, - p.convexity, - p.yield - }).ToList(); - if (datas != null && datas.Count > 0) - { - foreach (var item in datas) - { - _dDic.Add(item.bond_id, item.modi_dura != null ? (double)item.modi_dura : null); - _cDic.Add(item.bond_id, item.convexity != null ? (double)item.convexity : null); - _ytmDic.Add(item.bond_id, item.yield != null ? (double)item.yield : null); - } - } + bondsUmCodes = new List(); } var bondIndexUmInnerCodes = umDatas.Where(p => ConsGlobal.InstrumentType.BondIndex.Equals(p.UnderlyingInstrumentType)).Select(p => p.InnerCode??0).Distinct().ToList(); @@ -107,6 +93,7 @@ namespace YLErp.Modules.EodModule if (bondUm != null) { _TFeaturesBondCodeDic.Add(item.Key, bondUm.UnderlyingCode); + bondsUmCodes.Add(bondUm.UnderlyingCode); } var um = DataCacheProvider.GetUnderlyingDataSource().GetData(item.Key); if (um.InnerCode != null) @@ -114,9 +101,42 @@ namespace YLErp.Modules.EodModule contractInnerCodeBondInnerCodeDic.Add(um.InnerCode ?? 0, item.Value); } } - if (tFeatureBondInnerCodeDic.Count > 0) + if (contractInnerCodeBondInnerCodeDic.Count > 0) { + var cfDatas = GetCFDatas(contractInnerCodeBondInnerCodeDic, conn); + if (cfDatas != null && cfDatas.Count > 0) + { + foreach (var item in cfDatas) + { + var um = DataCacheProvider.GetUnderlyingDataSource().AsQueryable().FirstOrDefault(p => p.InnerCode == item.Key); + if (um != null) + { + _cfDic.Add(um.UnderlyingCode, item.Value); + } + } + } + } + } + } + + if (bondsUmCodes != null && bondsUmCodes.Count > 0) + { + bondsUmCodes = bondsUmCodes.Distinct().ToList(); + var datas = db.china_bond_valuation.AsNoTracking().Where(p => p.valuation_date == valueDate && bondsUmCodes.Contains(p.bond_id)).Select(p => new + { + p.bond_id, + p.modi_dura, + p.convexity, + p.yield + }).ToList(); + if (datas != null && datas.Count > 0) + { + foreach (var item in datas) + { + _dDic.Add(item.bond_id, item.modi_dura != null ? (double)item.modi_dura : null); + _cDic.Add(item.bond_id, item.convexity != null ? (double)item.convexity : null); + _ytmDic.Add(item.bond_id, item.yield != null ? (double)item.yield : null); } } } @@ -181,22 +201,66 @@ namespace YLErp.Modules.EodModule return res; } - private GLMSGreeksCalcArgs GetCalcArgs(string underlyingCode,string underlyingInstrumentType) + private GLMSGreeksCalcArgs GetCalcArgs(string underlyingCode, string underlyingInstrumentType) { + var res = new GLMSGreeksCalcArgs(); if (ConsGlobal.InstrumentType.RateYield.Equals(underlyingInstrumentType)) { - return null; + return res; } - return new GLMSGreeksCalcArgs + if (ConsGlobal.InstrumentType.TBFutures.Equals(underlyingInstrumentType)) { - - }; + var bondUnderlyingCode = _TFeaturesBondCodeDic.GetValueOrDefault(underlyingCode, null); + if (!string.IsNullOrEmpty(bondUnderlyingCode)) + { + res.P = _pDic.GetValueOrDefault(bondUnderlyingCode, null); + res.D = _dDic.GetValueOrDefault(bondUnderlyingCode, null); + res.C = _cDic.GetValueOrDefault(bondUnderlyingCode, null); + res.Ytm = _ytmDic.GetValueOrDefault(bondUnderlyingCode, null); + } + res.CF = _cfDic.GetValueOrDefault(underlyingCode, null); + } + else + { + res.P = _pDic.GetValueOrDefault(underlyingCode, null); + res.D = _dDic.GetValueOrDefault(underlyingCode, null); + res.C = _cDic.GetValueOrDefault(underlyingCode, null); + res.Ytm = _ytmDic.GetValueOrDefault(underlyingCode, null); + } + // + return res; } - private Dictionary GetCFDatas(Dictionary contractInnerCodeBondInnerCodeDic, DbConnection conn) + private Dictionary GetCFDatas(Dictionary contractInnerCodeBondInnerCodeDic, DbConnection conn) { - var sql = "SELECT infopubldate,contractinnercode,ibmarketinnercode,conversionfactors FROM fut_conversionfactors WHERE (contractinnercode,ibmarketinnercode) IN ((2059453,477500));"; - return null; + + if (contractInnerCodeBondInnerCodeDic == null || contractInnerCodeBondInnerCodeDic.Count == 0) + return new Dictionary(); + + var parameters = new DynamicParameters(); + var tupleList = new List(); + int idx = 0; + foreach (var kvp in contractInnerCodeBondInnerCodeDic) + { + var cp = $"@C{idx}"; + var bp = $"@B{idx}"; + tupleList.Add($"({cp},{bp})"); + parameters.Add(cp, kvp.Key); + parameters.Add(bp, kvp.Value); + idx++; + } + + var sql = $@"SELECT infopubldate, contractinnercode, ibmarketinnercode, conversionfactors + FROM fut_conversionfactors + WHERE (contractinnercode, ibmarketinnercode) IN ({string.Join(",", tupleList)})"; + + var datas = conn.Query(sql, parameters, commandTimeout: 1800).ToList(); + + return datas.GroupBy(p => p.contractinnercode).ToDictionary(d => d.Key, d => + { + var res = d.OrderByDescending(p => p.infopubldate).First().conversionfactors; + return res != null ? (double)res : (double?)null; + }); } private void handle(GreeksCalcDto dto, underlying_manager um) @@ -206,8 +270,6 @@ namespace YLErp.Modules.EodModule return; } - - var calcArgs = GetCalcArgs(um.UnderlyingCode, um.UnderlyingInstrumentType); if (ConsGlobal.InstrumentType.RateYield.Equals(um.UnderlyingInstrumentType)) @@ -310,4 +372,13 @@ namespace YLErp.Modules.EodModule public decimal? spread { get; set; } } + + public class CFQueryDto + { + public DateTime infopubldate { get; set; } + + public long contractinnercode { get; set; } + public long ibmarketinnercode { get; set; } + public decimal? conversionfactors { get; set; } + } }