/// <summary> /// 保存财务指标 /// </summary> /// <returns></returns> public static object SaveFinaceIndex(object rep, object reqObj) { try { ReqFinance req = reqObj as ReqFinance; ulong securityID = Convert.ToUInt64(req.securityIDs[0]); if (rep is Exception) { string errMsg = string.Format("SecurityID:{0}.{1}", securityID, (rep as Exception).Message); LogHelper.Track("财务指标", errMsg); LstRetryReqFinance.Add(req); return(null); } List <FinanceIndexInfo> lstInfo = DSPHelper.TransferFinance(rep, securityID); int count = lstInfo.Count; if (count > 0) { string delFilter = "{SecurityID:" + securityID + ", EndDate:{$gte:ISODate(\"" + req.dateBegin + "T00:00:00.000+0800\")}}"; MongoDBHelper.DeleteMany <FinanceIndexInfo>(delFilter); MongoDBHelper.InsertMany <FinanceIndexInfo>(lstInfo); } var tttttt = lstInfo.Where(m => m.IndexCode == "BEPS").ToList(); LogHelper.Track("财务指标", string.Format("SecurityID:{0}. Count:{1}", securityID, count)); } catch (Exception ex) { string errMsg = string.Format("SecurityID:{0}.{1}", ((ReqFinance)reqObj).securityIDs[0], ex.Message); LogHelper.Track("财务指标", errMsg); LstRetryReqFinance.Add(reqObj as ReqFinance); } return(null); }
/// <summary> /// 根据代码取财务数据的请求参数 /// </summary> /// <returns></returns> public static ReqFinance GetReqFinance(ulong securityID, List <string> lstIndexCode, DateTime lastDate) { ReqFinance req = new ReqFinance(); req.securityIDs.Add(securityID); req.fields.Add("Symbol"); req.fields.Add("EndDate"); req.fields.AddRange(lstIndexCode); //List<DateTime> listDate = TransferHelper.GetFinanceDate(); //DateTime maxDate = listDate.Max(); //DateTime startDate = maxDate; //foreach(DateTime date in listDate) //{ // if(date >= lastDate) // { // startDate = date; // break; // } //} List <DateTime> listDate = TransferHelper.GetFinanceDate(); DateTime maxDate = listDate.Max(); if (lastDate >= maxDate) { return(null); } DateTime startDate = listDate[0]; foreach (DateTime date in listDate) { if (date > lastDate) { startDate = date; break; } } req.dateBegin = TransferHelper.DateTimeToString(startDate); req.dateEnd = TransferHelper.DateTimeToString(maxDate); // 财报统计的截止日期 req.dateType = ERptDateType.ERptDateClose; // 合并本期 req.reportType = EReportType.MergeCur; // TTM,12个月滚动累计 或者是 季度累计 req.trailType = ETrailType.TrailAddup; req.page = new DataPage() { begin = 0, end = 0 }; return(req); }
/// <summary> /// 同步财务指标信息 /// </summary> public static void SyncFinaceIndex() { // 记日志 LogHelper.Info("总运行", "开始:同步财务指标数据"); DspRequest dspRequest = new DspRequest(); dspRequest.DealData = SaveFinaceIndex; LstRetryReqFinance = new List <ReqFinance>(); try { //取得所有a股安全码 List <ulong> lstSecurityID = MongoDBHelper.AsQueryable <SymbolInfo>().Select(m => m.SecurityID).ToList(); //取得所有财务指标编码 List <string> lstIndexCode = GetAllFinaceIndexCode(); ////删除已过期的数据 //DeleteOverdueFinaceIndex(lstSecurityID[0]); // 根据代码同步所有财务指标数据 foreach (var securityID in lstSecurityID) { try { FinanceIndexInfo info = MongoDBHelper.AsQueryable <FinanceIndexInfo>() .Where(m => m.SecurityID == securityID) .OrderByDescending(m => m.EndDate) .FirstOrDefault(); DateTime lastDate = new DateTime(1990, 1, 1); if (info != null) { lastDate = info.EndDate; } ReqFinance req = DSPHelper.GetReqFinance(securityID, lstIndexCode, lastDate); if (req != null) { dspRequest.AsyncSendData(req, DSPHelper.WaitTime, req); } } catch (Exception ex) { string errMsg = "同步财务指标异常.SecurityID:" + securityID; LogHelper.Error("异常", errMsg, ex); } } //等待DSP任务完成 dspRequest.WaitFinished(); if (LstRetryReqFinance.Count > 0) { //重试 RetrySyncFinaceIndex(dspRequest, 1); //等待DSP任务完成 dspRequest.WaitFinished(); } ////给10秒钟mongodb。 //Thread.Sleep(10000); } catch (Exception ex) { LogHelper.Error("异常", "同步财务指标发生异常.", ex); } finally { dspRequest.Dispose(); LogHelper.Info("总运行", "结束:同步财务指标数据"); } }