public void Show(int fCount) { List<APIStock> listStock = new List<APIStock>(); List<APIMarket> listMarket = new List<APIMarket>(); try { //if(!((DateTime.Now.Hour > 9 && DateTime.Now.Hour < 12) || (DateTime.Now.Hour >= 13 && DateTime.Now.Hour < 16))) //{ // return; //} string sUrl = Convert.ToString(ConfigurationManager.AppSettings["GPURL"]); //stockid=sz002673,sh600030,sz002673,sz002673,sz002673,sz002673,sz002673,sz002673,sz002673,sz002673&list=1 string stockidList = ConfigurationManager.AppSettings["GPPamamList"].Trim(); if (string.IsNullOrEmpty(stockidList)) { MessageBox.Show("请先添加股票"); return; } string sParam = "stockid=" + stockidList + "&list=1"; string sMessage = HttpGet(sUrl, sParam); if (!string.IsNullOrEmpty(sMessage)) { JObject jo = (JObject)JsonConvert.DeserializeObject(sMessage); string sReturnCode = Convert.ToString(jo["errNum"]); string sReturnMsg = Convert.ToString(jo["errMsg"]); if (string.IsNullOrEmpty(sReturnCode)) { log.Error("请求异常,次数:" + fCount); //MessageBox.Show("请求异常"); this.Text = "请求异常"; return; } int mReturnCode = Convert.ToInt32(sReturnCode); if (mReturnCode == (int)ErrorCode.Success) { string sReturnContent = Convert.ToString(jo["retData"]); if (!string.IsNullOrEmpty(sReturnContent)) { JObject jContent = (JObject)JsonConvert.DeserializeObject(sReturnContent); string sGSList = Convert.ToString(jContent["stockinfo"]);//上市公司列表 string sDPList = Convert.ToString(jContent["market"]);//上证,深证 int mGSCount = jContent["stockinfo"].AsQueryable().Count(); //涨跌判断 List<APIStock> listGPYJ = GetGPYJList(); for (int i = 0; i < mGSCount; i++) { APIStock stock = new APIStock(); JObject jStock = (JObject)jContent["stockinfo"][i]; stock.name = Convert.ToString(jStock["name"]).Trim('"'); if (string.IsNullOrEmpty(stock.name)) { continue; } stock.SysNo = Convert.ToString(jStock["code"]).Trim('"'); stock.currentPrice = Convert.ToString(jStock["currentPrice"]); string mIncrease = Convert.ToString(Math.Round(Convert.ToDouble(jStock["increase"].ToString()), 2, MidpointRounding.AwayFromZero));//涨跌幅 if (BIsTX == true) { int txT = 1; if (string.IsNullOrEmpty(BTXTime) || int.TryParse(BTXTime, out txT) == false) { txT = 1; } else { txT = Convert.ToInt32(BTXTime); } //当前股票已提醒的间隔时间 var mGPYTX = (from t in queueTXMsgList where t.Code == stock.SysNo && t.CreateTime >= DateTime.Now.AddMinutes(-txT) select t).ToList(); if (mGPYTX.Count <= 0) { if (listGPYJ != null && listGPYJ.Count > 0) { var mGP = (from t in listGPYJ where t.SysNo == stock.SysNo && t.ISYJ == 1 select t).ToList(); if (mGP.Count > 0) { string returnMsg = PriceYJ(stock.currentPrice, mIncrease, mGP[0]); if (!string.IsNullOrEmpty(returnMsg)) { ErrorMsg eMsg = new ErrorMsg(); eMsg.Code = stock.SysNo; eMsg.TitleType = (int)ErrorTitleType.GJMsg; eMsg.Content = returnMsg; eMsg.CreateTime = DateTime.Now; eMsg.ShowTime = 5000; eMsg.ToolTripType = ToolTipIcon.Warning; queueMsgList.Enqueue(eMsg); queueTXMsgList.Enqueue(eMsg); //this.notifyIconMsg.ShowBalloonTip(5000, "消息提醒", returnMsg, ToolTipIcon.Warning); } } } } } stock.name = Convert.ToString(jStock["name"]).Trim('"'); stock.code = Convert.ToString(jStock["code"]).Trim('"').Substring(2); stock.date = jStock["time"].ToString().Trim('"'); stock.openningPrice = Convert.ToString(jStock["OpenningPrice"]); stock.closingPrice = Convert.ToString(jStock["closingPrice"]); stock.hPrice = Convert.ToString(jStock["hPrice"]); stock.lPrice = Convert.ToString(jStock["lPrice"]); //stock.growth = Convert.ToString(jStock["growth"]); //stock.growthPercent = Convert.ToString(jStock["growthPercent"]); //stock.dealnumber = Convert.ToString(jStock["dealnumber"]); stock.turnover = Convert.ToString(Math.Round(Convert.ToDouble(jStock["turnover"].ToString()) / 100000000, 2, MidpointRounding.AwayFromZero)); //stock.hPrice52 = Convert.ToString(jStock["52hPrice"]); //stock.lPrice52 = Convert.ToString(jStock["52lPrice"]); stock.competitivePrice = Convert.ToString(jStock["competitivePrice"]); stock.auctionPrice = Convert.ToString(jStock["auctionPrice"]); stock.totalNumber = Convert.ToString(Math.Round(Convert.ToDouble(jStock["totalNumber"].ToString()) / 1000000, 2, MidpointRounding.AwayFromZero)); stock.increase = (mIncrease.Substring(0, 1) == "-") ? mIncrease.Substring(1) : mIncrease; listStock.Add(stock); } JObject jMarket = (JObject)jContent["market"]; APIMarket mSH = new APIMarket(); mSH.name = jMarket["shanghai"]["name"].ToString().Trim('"'); mSH.curdot = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shanghai"]["curdot"].ToString()), 2, MidpointRounding.AwayFromZero)); mSH.curprice = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shanghai"]["curprice"].ToString()), 2, MidpointRounding.AwayFromZero)); mSH.rate = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shanghai"]["rate"].ToString()), 2, MidpointRounding.AwayFromZero)); mSH.dealnumber = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shanghai"]["dealnumber"].ToString()) / 100000000, 2, MidpointRounding.AwayFromZero)); mSH.turnover = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shanghai"]["turnover"].ToString()) / 10000, 2, MidpointRounding.AwayFromZero)); APIMarket mSZ = new APIMarket(); mSZ.name = Convert.ToString(jMarket["shenzhen"]["name"]).Trim('"'); mSZ.curdot = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shenzhen"]["curdot"].ToString()), 2, MidpointRounding.AwayFromZero)); mSZ.curprice = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shenzhen"]["curprice"].ToString()), 2, MidpointRounding.AwayFromZero)); mSZ.rate = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shenzhen"]["rate"].ToString()), 2, MidpointRounding.AwayFromZero)); mSZ.dealnumber = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shenzhen"]["dealnumber"].ToString()) / 100000000, 2, MidpointRounding.AwayFromZero)); mSZ.turnover = Convert.ToString(Math.Round(Convert.ToDouble(jMarket["shenzhen"]["turnover"].ToString()) / 10000, 2, MidpointRounding.AwayFromZero)); listMarket.Add(mSH); listMarket.Add(mSZ); } } else if (!Enum.IsDefined(typeof(ErrorCode), mReturnCode)) { log.Error("返回未知异常,次数:" + fCount + ",异常编号:" + mReturnCode + ";异常说明:" + sReturnMsg); //MessageBox.Show("返回未知异常,异常编号:" + mReturnCode + ";异常说明:" + sReturnMsg); this.Text = "返回未知异常,异常编号:" + mReturnCode + ";异常说明:" + sReturnMsg; return; } else { MessageBox.Show(GetDescription(typeof(ErrorCode), mReturnCode)); } } else { log.Error("返回数据空,次数:" + fCount); //MessageBox.Show("返回数据空"); this.Text = "返回数据空"; return; } } catch (Exception ex) { log.Error("获取股票数据1,出现异常:"+ex.Message+";异常详情:" + ex); } try { if (listMarket.Count > 0) { APIMarket apiSHZS = listMarket[0]; double shRate = Convert.ToDouble(apiSHZS.rate); Color shColor = Color.Red; string shZF = "+"; string shJT = "↑"; if (shRate >= 0) { shColor = Color.Red; shZF = "+"; shJT = "↑"; } else { shColor = Color.Green; shZF = ""; shJT = "↓"; } lblSHZS1.Text = apiSHZS.name; lblSHZS1.ForeColor = shColor; lblSHZS2.Text = shJT + apiSHZS.curdot + " " + shZF + apiSHZS.rate + "%"; lblSHZS2.ForeColor = shColor; //lblSHZS3.Text = shZF + apiSHZS.curprice + " " + shZF + apiSHZS.rate + "%"; //lblSHZS3.ForeColor = shColor; APIMarket apiSZZS = listMarket[1]; double szRate = Convert.ToDouble(apiSZZS.rate); Color szColor = Color.Red; string szZF = "+"; string szJT = "↑"; if (szRate >= 0) { szColor = Color.Red; szZF = "+"; szJT = "↑"; } else { szColor = Color.Green; szZF = ""; szJT = "↓"; } lblSZZS1.Text = apiSZZS.name; lblSZZS1.ForeColor = szColor; lblSZZS2.Text = szJT + apiSZZS.curdot + " " + szZF + apiSZZS.rate + "%"; lblSZZS2.ForeColor = szColor; //lblSZZS3.Text = szZF + apiSZZS.curprice + " " + szZF + apiSZZS.rate + "%"; //lblSZZS3.ForeColor = szColor; } } catch (Exception ex) { log.Error("获取股票数据2,出现异常:"+ex.Message+";异常详情:" + ex); } try { lock (locker) { #region 绑定数据到DataGridView dataGridView1.Columns.Clear(); if (listStock.Count > 0) { //dataGridView1.Columns[4].DataPropertyName DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn(); col1.HeaderText = "股票名称"; col1.DataPropertyName = "name"; col1.Name = "name"; col1.Width = 80; col1.ReadOnly = true; col1.Frozen = true; //col1.ContextMenuStrip = contextMenuStripGridView; dataGridView1.Columns.Insert(0, col1); DataGridViewTextBoxColumn col3 = new DataGridViewTextBoxColumn(); col3.HeaderText = "当前价"; col3.DataPropertyName = "currentPrice"; col3.Name = "currentPrice"; col3.Width = 70; col3.ReadOnly = true; //col3.ContextMenuStrip = contextMenuStripGridView; dataGridView1.Columns.Insert(1, col3); DataGridViewTextBoxColumn col18 = new DataGridViewTextBoxColumn(); col18.HeaderText = "涨幅(%)"; col18.DataPropertyName = "increase"; col18.Name = "increase"; col18.Width = 75; col18.ReadOnly = true; //col18.ContextMenuStrip = contextMenuStripGridView; dataGridView1.Columns.Insert(2, col18); DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn(); col2.HeaderText = "股票代码"; col2.DataPropertyName = "code"; col2.Name = "code"; col2.Width = 80; col2.ReadOnly = true; //col2.ContextMenuStrip = contextMenuStripGridView; dataGridView1.Columns.Insert(3, col2); DataGridViewTextBoxColumn col6 = new DataGridViewTextBoxColumn(); col6.HeaderText = "今日最高价"; col6.DataPropertyName = "hPrice"; col6.Name = "hPrice"; col6.Width = 90; col6.ReadOnly = true; dataGridView1.Columns.Insert(4, col6); DataGridViewTextBoxColumn col7 = new DataGridViewTextBoxColumn(); col7.HeaderText = "今日最低价"; col7.DataPropertyName = "lPrice"; col7.Name = "lPrice"; col7.Width = 90; col7.ReadOnly = true; dataGridView1.Columns.Insert(5, col7); DataGridViewTextBoxColumn col8 = new DataGridViewTextBoxColumn(); col8.HeaderText = "开盘价"; col8.DataPropertyName = "openningPrice"; col8.Name = "openningPrice"; col8.Width = 70; col8.ReadOnly = true; dataGridView1.Columns.Insert(6, col8); DataGridViewTextBoxColumn col9 = new DataGridViewTextBoxColumn(); col9.HeaderText = "昨日收盘价"; col9.DataPropertyName = "closingPrice"; col9.Name = "closingPrice"; col9.Width = 90; col9.ReadOnly = true; dataGridView1.Columns.Insert(7, col9); DataGridViewTextBoxColumn col17 = new DataGridViewTextBoxColumn(); col17.HeaderText = "成交量(万)"; col17.DataPropertyName = "totalNumber"; col17.Name = "totalNumber"; col17.Width = 90; col17.ReadOnly = true; dataGridView1.Columns.Insert(8, col17); DataGridViewTextBoxColumn col13 = new DataGridViewTextBoxColumn(); col13.HeaderText = "成交金额(亿)"; col13.DataPropertyName = "turnover"; col13.Name = "turnover"; col13.Width = 100; col13.ReadOnly = true; dataGridView1.Columns.Insert(9, col13); DataGridViewTextBoxColumn col14 = new DataGridViewTextBoxColumn(); col14.HeaderText = "刷新日期"; col14.DataPropertyName = "date"; col14.Name = "date"; col14.Width = 90; col14.ReadOnly = true; dataGridView1.Columns.Insert(10, col14); //DataGridViewTextBoxColumn col4 = new DataGridViewTextBoxColumn(); //col4.HeaderText = "涨幅比例"; //col4.DataPropertyName = "growthPercent"; //col4.Name = "growthPercent"; //col4.Width = 100; //col4.ReadOnly = true; //dataGridView1.Columns.Add(col4); //DataGridViewTextBoxColumn col5 = new DataGridViewTextBoxColumn(); //col5.HeaderText = "价格涨幅"; //col5.DataPropertyName = "growth"; //col5.Name = "growth"; //col5.Width = 100; //col5.ReadOnly = true; //dataGridView1.Columns.Add(col5); //DataGridViewTextBoxColumn col10 = new DataGridViewTextBoxColumn(); //col10.HeaderText = "52周最高价"; //col10.DataPropertyName = "hPrice52"; //col10.Name = "hPrice52"; //col10.Width = 100; //col10.ReadOnly = true; //dataGridView1.Columns.Add(col10); //DataGridViewTextBoxColumn col11 = new DataGridViewTextBoxColumn(); //col11.HeaderText = "52周最低价"; //col11.DataPropertyName = "lPrice52"; //col11.Name = "lPrice52"; //col11.Width = 100; //col11.ReadOnly = true; //dataGridView1.Columns.Add(col11); //DataGridViewTextBoxColumn col12 = new DataGridViewTextBoxColumn(); //col12.HeaderText = "成交量股"; //col12.DataPropertyName = "dealnumber"; //col12.Name = "dealnumber"; //col12.Width = 100; //col12.ReadOnly = true; //dataGridView1.Columns.Add(col12); //DataGridViewTextBoxColumn col15 = new DataGridViewTextBoxColumn(); //col15.HeaderText = "未知competitivePrice"; //col15.DataPropertyName = "competitivePrice"; //col15.Name = "competitivePrice"; //col15.Width = 100; //col15.ReadOnly = true; //dataGridView1.Columns.Add(col15); //DataGridViewTextBoxColumn col16 = new DataGridViewTextBoxColumn(); //col16.HeaderText = "未知auctionPrice"; //col16.DataPropertyName = "auctionPrice"; //col16.Name = "auctionPrice"; //col16.Width = 100; //col16.ReadOnly = true; //dataGridView1.Columns.Add(col16); //dataGridView1.AllowUserToAddRows = false; //dataGridView1.AllowUserToOrderColumns = true; //允许用户调整列的位置 //dataGridView1.Columns[0].Visible = false; //dataGridView1.ColumnHeadersVisible = false; // 列头隐藏 //dataGridView1.RowHeadersVisible = false; // 行头隐藏 dataGridView1.ReadOnly = true; dataGridView1.AutoGenerateColumns = false; dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView1.DataSource = listStock; //http://wsh1798.iteye.com/blog/601592 } #endregion } } catch (Exception ex) { log.Error("获取股票数据3,出现异常:"+ex.Message+";异常详情:" + ex); } try { //ShowHGT(); } catch (Exception ex) { log.Error("获取股票数据4,出现异常:"+ex.Message+";异常详情:" + ex); } }
/// <summary> /// 价格判断 /// </summary> /// <param name="currentPrice">当前价</param> /// <param name="increase">当前涨幅</param> /// <param name="stock">预警对象</param> /// <returns>提醒信息</returns> private string PriceYJ(string currentPrice, string increase, APIStock stock) { string returnMsg = ""; //先价格后涨幅 if (string.IsNullOrEmpty(currentPrice) || string.IsNullOrEmpty(increase) || string.IsNullOrEmpty(stock.SysNo)) { return returnMsg; } double CurrentPrice = Convert.ToDouble(currentPrice);//当前价 double Increase = Convert.ToDouble(increase);//当前涨幅 string sZFB = stock.ZFB;//涨幅百分比 string sDFB = stock.DFB;//跌幅百分比 string sZFY = stock.ZFY;//涨幅元 string sDFY = stock.DFY;//跌幅元 if(!string.IsNullOrEmpty(sZFY)) { double ZFY = 0.0; if (double.TryParse(sZFY, out ZFY) == true) { if (CurrentPrice >= ZFY) { returnMsg = "当前股票:" + stock.name + " 的价格已涨到预警价:" + ZFY + "元;当前价格:" + sZFY + "元"; return returnMsg; } } } if (!string.IsNullOrEmpty(sDFY)) { double DFY = 0.0; if (double.TryParse(sDFY, out DFY) == true) { if (CurrentPrice <= DFY) { returnMsg = "当前股票:" + stock.name + " 的价格已跌到预警价:" + DFY + "元;当前价格:" + sDFY + "元"; return returnMsg; } } } if (!string.IsNullOrEmpty(sZFB)) { double ZFB = 0.0; if (double.TryParse(sZFB, out ZFB) == true) { if (Increase >= ZFB) { returnMsg = "当前股票:" + stock.name + " 的幅度已涨到预警涨幅:" + ZFB + "%;当前涨幅:" + Increase + "%"; return returnMsg; } } } if (!string.IsNullOrEmpty(sDFB)) { double DFB = 0.0; if (double.TryParse(sDFB, out DFB) == true) { if (Increase <= -DFB) { returnMsg = "当前股票:" + stock.name + " 的幅度已跌到预警跌幅:" + DFB + "%;当前跌幅:" + sDFY + "%"; return returnMsg; } } } return returnMsg; }
//预警列表(停用) private void GetGPYJListM() { List<APIStock> listYJ = new List<APIStock>(); XmlDocument GridXml = new XmlDocument(); GridXml.Load(Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_Data/GPYJ.xml"))); XmlNode root = GridXml.SelectSingleNode("Root"); XmlElement rootElement = (XmlElement)root; string YYCode = Convert.ToString(ConfigurationManager.AppSettings["GPPamamList"]).Trim(); string[] YYCodes = YYCode.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach(string mCode in YYCodes) { XmlNode GridNode = GridXml.DocumentElement.SelectSingleNode("//GPDM[@Code='" + mCode + "']"); if (GridNode != null)//存在节点仅修改名称 { XmlElement oneElement = (XmlElement)GridNode; APIStock stock = new APIStock(); stock.SysNo = oneElement.Attributes["Code"].Value; stock.name = oneElement.Attributes["Name"].Value; stock.code = oneElement.Attributes["Code"].Value.Substring(2); stock.ZFB = oneElement.SelectSingleNode("ZFB").InnerText; stock.DFB = oneElement.SelectSingleNode("DFB").InnerText; stock.ZFY = oneElement.SelectSingleNode("ZFY").InnerText; stock.DFY = oneElement.SelectSingleNode("DFY").InnerText; listYJ.Add(stock); } } //XmlNodeList ColumnList = rootElement.SelectNodes("GPDM"); //for (int i = 0; i < ColumnList.Count;i++ ) //{ // XmlElement oneElement = (XmlElement)ColumnList[i]; //} if(listYJ.Count > 0) { Cache c = System.Web.HttpRuntime.Cache; CacheDependency cdd = new CacheDependency(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "GPSmallTools.exe.Config")); c.Insert("MGPYJ", listYJ, cdd, System.DateTime.UtcNow.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration); dataGridView2.AutoGenerateColumns = false; dataGridView2.Columns[0].ReadOnly = true; dataGridView2.Columns[1].ReadOnly = true; dataGridView2.DataSource = listYJ; } }
//预警列表 private List<APIStock> GetGPYJList() { List<APIStock> listYJ = new List<APIStock>(); Cache cache = System.Web.HttpRuntime.Cache; listYJ = (List<APIStock>)cache.Get("MGPYJ"); if (listYJ != null && listYJ.Count > 0) { return listYJ; } #region 预警配置文件API更新 string sUrl = Convert.ToString(ConfigurationManager.AppSettings["GPURL"]); string stockidList = ConfigurationManager.AppSettings["GPPamamList"].Trim(); if (string.IsNullOrEmpty(stockidList)) { MessageBox.Show("请先添加股票"); return null; } string sParam = "stockid=" + stockidList + "&list=1"; string sMessage = HttpGet(sUrl, sParam); if (!string.IsNullOrEmpty(sMessage)) { JObject jo = (JObject)JsonConvert.DeserializeObject(sMessage); string sReturnCode = Convert.ToString(jo["errNum"]); string sReturnMsg = Convert.ToString(jo["errMsg"]); if (string.IsNullOrEmpty(sReturnCode)) { this.Text = "请求异常"; return null; } int mReturnCode = Convert.ToInt32(sReturnCode); if (mReturnCode == (int)ErrorCode.Success) { string sReturnContent = Convert.ToString(jo["retData"]); if (!string.IsNullOrEmpty(sReturnContent)) { JObject jContent = (JObject)JsonConvert.DeserializeObject(sReturnContent); string sGSList = Convert.ToString(jContent["stockinfo"]);//上市公司列表 string sDPList = Convert.ToString(jContent["market"]);//上证,深证 int mGSCount = jContent["stockinfo"].AsQueryable().Count(); XmlDocument GridXmlX = new XmlDocument(); GridXmlX.Load(Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_Data/GPYJ.xml"))); XmlNode rootX = GridXmlX.SelectSingleNode("Root"); for (int i = 0; i < mGSCount; i++) { APIStock stock = new APIStock(); JObject jStock = (JObject)jContent["stockinfo"][i]; string name = Convert.ToString(jStock["name"]).Trim('"'); string code = Convert.ToString(jStock["code"]).Trim('"'); if (string.IsNullOrEmpty(name)) { continue; } XmlNode GridNode = GridXmlX.DocumentElement.SelectSingleNode("//GPDM[@Code='" + code + "']"); if (GridNode != null)//存在节点仅修改名称 { XmlElement GridElement = (XmlElement)GridNode; GridElement.SetAttribute("Name", name); } else { XmlElement oneGPDM = GridXmlX.CreateElement("GPDM"); oneGPDM.SetAttribute("Code", code); oneGPDM.SetAttribute("Name", name); XmlElement twoZFB = GridXmlX.CreateElement("ZFB"); //twoZFB.InnerText = "5"; oneGPDM.AppendChild(twoZFB); XmlElement twoDFB = GridXmlX.CreateElement("DFB"); oneGPDM.AppendChild(twoDFB); XmlElement twoZFY = GridXmlX.CreateElement("ZFY"); oneGPDM.AppendChild(twoZFY); XmlElement twoDFY = GridXmlX.CreateElement("DFY"); oneGPDM.AppendChild(twoDFY); rootX.AppendChild(oneGPDM); } } GridXmlX.Save(Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_Data/GPYJ.xml"))); } } else if (!Enum.IsDefined(typeof(ErrorCode), mReturnCode)) { this.Text = "返回未知异常,异常编号:" + mReturnCode + ";异常说明:" + sReturnMsg; return null; } else { MessageBox.Show(GetDescription(typeof(ErrorCode), mReturnCode)); } } else { this.Text = "返回数据空"; return null; } #endregion #region 根据配置文件获取集合 listYJ = new List<APIStock>(); XmlDocument GridXml = new XmlDocument(); GridXml.Load(Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_Data/GPYJ.xml"))); XmlNode root = GridXml.SelectSingleNode("Root"); XmlElement rootElement = (XmlElement)root; string YYCode = Convert.ToString(ConfigurationManager.AppSettings["GPPamamList"]).Trim(); string[] YYCodes = YYCode.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string mCode in YYCodes) { XmlNode GridNode = GridXml.DocumentElement.SelectSingleNode("//GPDM[@Code='" + mCode + "']"); if (GridNode != null)//存在节点仅修改名称 { XmlElement oneElement = (XmlElement)GridNode; APIStock stock = new APIStock(); stock.SysNo = oneElement.Attributes["Code"].Value; stock.name = oneElement.Attributes["Name"].Value; stock.code = oneElement.Attributes["Code"].Value.Substring(2); stock.ZFB = oneElement.SelectSingleNode("ZFB").InnerText; stock.DFB = oneElement.SelectSingleNode("DFB").InnerText; stock.ZFY = oneElement.SelectSingleNode("ZFY").InnerText; stock.DFY = oneElement.SelectSingleNode("DFY").InnerText; stock.ISYJ = 0; if (!string.IsNullOrEmpty(stock.ZFB) || !string.IsNullOrEmpty(stock.DFB) || !string.IsNullOrEmpty(stock.ZFY) || !string.IsNullOrEmpty(stock.DFY)) { stock.ISYJ = 1;//价格预警 } listYJ.Add(stock); } } //XmlNodeList ColumnList = rootElement.SelectNodes("GPDM"); //for (int i = 0; i < ColumnList.Count;i++ ) //{ // XmlElement oneElement = (XmlElement)ColumnList[i]; //} CacheDependency cdd = new CacheDependency(new string[] { Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "GPSmallTools.exe.Config"), Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_Data/GPYJ.xml") }); cache.Insert("MGPYJ", listYJ, cdd, System.DateTime.UtcNow.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);//级联缓存(指定配置文件发生修改时,触发缓存清空) #endregion return listYJ; }
//预警配置文件API更新(停用) private bool GetAPIDataPushGPYJ() { Cache cache = System.Web.HttpRuntime.Cache; List<APIStock> listYJ = (List<APIStock>)cache.Get("MGPYJ"); if (listYJ != null && listYJ.Count > 0) { dataGridView2.AutoGenerateColumns = false; dataGridView2.Columns[0].ReadOnly = true; dataGridView2.Columns[1].ReadOnly = true; dataGridView2.DataSource = listYJ; return true; } string sUrl = Convert.ToString(ConfigurationManager.AppSettings["GPURL"]); string stockidList = ConfigurationManager.AppSettings["GPPamamList"].Trim(); if (string.IsNullOrEmpty(stockidList)) { MessageBox.Show("请先添加股票"); return false; } string sParam = "stockid=" + stockidList + "&list=1"; string sMessage = HttpGet(sUrl, sParam); if (!string.IsNullOrEmpty(sMessage)) { JObject jo = (JObject)JsonConvert.DeserializeObject(sMessage); string sReturnCode = Convert.ToString(jo["errNum"]); string sReturnMsg = Convert.ToString(jo["errMsg"]); if (string.IsNullOrEmpty(sReturnCode)) { this.Text = "请求异常"; return false; } int mReturnCode = Convert.ToInt32(sReturnCode); if (mReturnCode == (int)ErrorCode.Success) { string sReturnContent = Convert.ToString(jo["retData"]); if (!string.IsNullOrEmpty(sReturnContent)) { JObject jContent = (JObject)JsonConvert.DeserializeObject(sReturnContent); string sGSList = Convert.ToString(jContent["stockinfo"]);//上市公司列表 string sDPList = Convert.ToString(jContent["market"]);//上证,深证 int mGSCount = jContent["stockinfo"].AsQueryable().Count(); XmlDocument GridXmlX = new XmlDocument(); GridXmlX.Load(Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_Data/GPYJ.xml"))); XmlNode rootX = GridXmlX.SelectSingleNode("Root"); for (int i = 0; i < mGSCount; i++) { APIStock stock = new APIStock(); JObject jStock = (JObject)jContent["stockinfo"][i]; string name = Convert.ToString(jStock["name"]).Trim('"'); string code = Convert.ToString(jStock["code"]).Trim('"'); if (string.IsNullOrEmpty(name)) { continue; } XmlNode GridNode = GridXmlX.DocumentElement.SelectSingleNode("//GPDM[@Code='" + code + "']"); if (GridNode != null)//存在节点仅修改名称 { XmlElement GridElement = (XmlElement)GridNode; GridElement.SetAttribute("Name", name); } else { XmlElement oneGPDM = GridXmlX.CreateElement("GPDM"); oneGPDM.SetAttribute("Code", code); oneGPDM.SetAttribute("Name", name); XmlElement twoZFB = GridXmlX.CreateElement("ZFB"); //twoZFB.InnerText = "5"; oneGPDM.AppendChild(twoZFB); XmlElement twoDFB = GridXmlX.CreateElement("DFB"); oneGPDM.AppendChild(twoDFB); XmlElement twoZFY = GridXmlX.CreateElement("ZFY"); oneGPDM.AppendChild(twoZFY); XmlElement twoDFY = GridXmlX.CreateElement("DFY"); oneGPDM.AppendChild(twoDFY); rootX.AppendChild(oneGPDM); } } GridXmlX.Save(Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_Data/GPYJ.xml"))); } } else if (!Enum.IsDefined(typeof(ErrorCode), mReturnCode)) { this.Text = "返回未知异常,异常编号:" + mReturnCode + ";异常说明:" + sReturnMsg; return false; } else { MessageBox.Show(GetDescription(typeof(ErrorCode), mReturnCode)); } } else { this.Text = "返回数据空"; return false; } listYJ = GetGPYJList(); dataGridView2.AutoGenerateColumns = false; dataGridView2.Columns[0].ReadOnly = true; dataGridView2.Columns[1].ReadOnly = true; dataGridView2.DataSource = listYJ; return true; }