private void PushNotify() { string qrystr = "select a.*, b.SymbolName, b.Market, b.TempRefer, b.Refer, b.CouponDate, b.LastTDPrice, b.PriceDiff, " + "b.PriceDiffRatio from UserSubRec a, SubStockInfo b " + "where a.Symbol=b.Symbol and b.StopDate>=@StopDate order by a.Serial"; if (GD.SubAppDbTool == null) { return; } SqlConnection conn = GD.SubAppDbTool.GetDbConnection(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; SqlDataReader dr = null; try { Cursor.Current = Cursors.WaitCursor; conn.Open(); //cmd.Parameters.Clear(); cmd.CommandText = qrystr; //cmd.Parameters.AddWithValue("@StopDate", DateTime.Today.ToString("yyyy/MM/dd")); cmd.Parameters.AddWithValue("@StopDate", "2014/04/04"); dr = cmd.ExecuteReader(); while (dr.Read()) { if (int.Parse(dr["Type"].ToString()) == 2)//漲幅 { if (decimal.Parse(dr["Value"].ToString()) <= decimal.Parse(dr["PriceDiffRatio"].ToString())) { string content = string.Format(ConfigurationManager.AppSettings["MsgPriceDiffRatio"], dr["SymbolName"].ToString(), dr["Symbol"].ToString(), dr["PriceDiffRatio"].ToString(), dr["Value"].ToString().Trim('0')); NotifyMsgReply rep; PushUtils.PushNotify(dr, content, out rep); } } else if (int.Parse(dr["Type"].ToString()) == 0)//差價 { if (decimal.Parse(dr["Value"].ToString()) <= decimal.Parse(dr["PriceDiff"].ToString())) { string content = string.Format(ConfigurationManager.AppSettings["MsgPriceDiff"], dr["SymbolName"].ToString(), dr["Symbol"].ToString(), dr["PriceDiff"].ToString(), dr["Value"].ToString().Trim('0')); NotifyMsgReply rep; PushUtils.PushNotify(dr, content, out rep); } } } } catch (Exception ex) { GD.aplogger.Error(string.Format("Sub Push Exception - {0}", ex.Message)); } finally { if (dr != null && cmd != null) { cmd.Cancel(); dr.Close(); cmd.Dispose(); } if (conn.State == ConnectionState.Open) { conn.Close(); } Cursor.Current = Cursors.Default; } }
private void tsbQuerySub_Click(object sender, EventArgs e) { TcpClient tcpClientQuote = new TcpClient(); string respxml = null; Stopwatch stopWatch = new Stopwatch(); try { Cursor.Current = Cursors.WaitCursor; tcpClientQuote.Connect(ConfigurationManager.AppSettings["QueryHost"], int.Parse(ConfigurationManager.AppSettings["QueryPort"])); if (tcpClientQuote.Connected) { //TaiwanCalendar twC = new TaiwanCalendar(); //string tdate = twC.GetYear(DateTime.Today) + "/" + twC.GetMonth(DateTime.Today) + "/" + twC.GetDayOfMonth(DateTime.Today); string ldate = PushUtils.GetTALastTradeDate(tcpClientQuote, int.Parse(ConfigurationManager.AppSettings["OneTryTime"]), int.Parse(ConfigurationManager.AppSettings["RetryTimes"])); string param = string.Format(ConfigurationManager.AppSettings["QuerySubParam"], ldate); stopWatch.Start(); respxml = PushUtils.QueryTA(tcpClientQuote, ConfigurationManager.AppSettings["QuerySubURL"] + "\x01" + param + "\x02", int.Parse(ConfigurationManager.AppSettings["OneTryTime"]), int.Parse(ConfigurationManager.AppSettings["RetryTimes"])); stopWatch.Stop(); textBox1.Text = stopWatch.ElapsedMilliseconds.ToString(); } tcpClientQuote.Close(); tcpClientQuote = null; if (GD.stocklist == null) { GD.stocklist = new SubStockList(); } if (GD.bsSubStockList == null) { GD.bsSubStockList = new BindingSource(); } stopWatch.Restart(); if (!string.IsNullOrEmpty(respxml)) { GD.stocklist.LoadList(respxml); } stopWatch.Stop(); textBox2.Text = stopWatch.ElapsedMilliseconds.ToString(); stopWatch.Restart(); if (GD.stocklist.Count > 0) { GD.bsSubStockList.DataSource = null; GD.bsSubStockList.DataSource = GD.stocklist.Values; dgvSubStockList.DataSource = GD.bsSubStockList; GD.stocklist.SaveToDB(); } stopWatch.Stop(); textBox3.Text = stopWatch.ElapsedMilliseconds.ToString(); } catch (Exception ex) { GD.aplogger.Error(string.Format("Query Sub Stock exception {0}", ex.Message)); } finally { Cursor.Current = Cursors.Default; } }
public void LoadList(string xml) { string referdumm = ""; this.Clear(); TextReader sr = new StringReader(xml); XElement xe = XElement.Load(sr); sr.Close(); //SubStock item = null; /*var qitems = from qitem in xe.Descendants("NFSLISTS") * select new * { * Symbol = qitem.Attribute("symb") == null ? "" : qitem.Attribute("symb").Value, * SymbolName = qitem.Attribute("name") == null ? "" : qitem.Attribute("name").Value, * Market = qitem.Attribute("mark") == null ? "" : qitem.Attribute("mark").Value, * StartDate = qitem.Attribute("name") == null ? "" : qitem.Attribute("name").Value * qitem.Element("NFSLIDURS").Element() * };*/ var qitems = from qitem in xe.Descendants("NFSLISTS") select qitem; TcpClient tcpClientQuote = new TcpClient(); string ldate = ""; try { tcpClientQuote.Connect(ConfigurationManager.AppSettings["QueryHost"], int.Parse(ConfigurationManager.AppSettings["QueryPort"])); ldate = PushUtils.GetTALastTradeDate(tcpClientQuote, int.Parse(ConfigurationManager.AppSettings["OneTryTime"]), int.Parse(ConfigurationManager.AppSettings["RetryTimes"])); } catch (Exception ex) { GD.aplogger.Error(string.Format("SubStock loadlist TCP connect exception - {0}", ex.Message)); } foreach (var qitem in qitems) { SubStock ss = new SubStock(); var nitems = from nitem in qitem.Descendants("NFSLIDUR") select nitem; referdumm = ""; foreach (var nitem in nitems) { if (nitem.Attribute("duno") == null) { continue; } if (int.Parse(nitem.Attribute("duno").Value) == 0) { ss.StartDate = nitem.Attribute("duii").Value; ss.StopDate = nitem.Attribute("ducl").Value; } else if (int.Parse(nitem.Attribute("duno").Value) == 5) { ss.DebitDate = nitem.Attribute("duii").Value; } else if (int.Parse(nitem.Attribute("duno").Value) == 10) { ss.DrawDate = nitem.Attribute("duii").Value; } else if (int.Parse(nitem.Attribute("duno").Value) == 20) { ss.RefundDate = nitem.Attribute("duii").Value; } else if (int.Parse(nitem.Attribute("duno").Value) == 30) { ss.CouponDate = nitem.Attribute("duii").Value; } else if (int.Parse(nitem.Attribute("duno").Value) == 91) { referdumm = nitem.Attribute("dumm").Value; //if (referdumm == null || int.Parse(referdumm) > 0) //int result= 0; decimal result = 0; //if (!int.TryParse(referdumm, out result) || result == 0) // referdumm = null; if (!decimal.TryParse(referdumm, out result) || result == 0) { referdumm = null; } } } ss.Symbol = qitem.Attribute("symb") == null ? "" : qitem.Attribute("symb").Value; string memo = qitem.Attribute("memo") == null ? "" : qitem.Attribute("memo").Value; ss.SymbolName = qitem.Attribute("name") == null ? "" : qitem.Attribute("name").Value; if (memo.IndexOf('(') >= 0) { ss.Market = memo.Substring(memo.IndexOf('(') + 1, memo.IndexOf(')') - memo.IndexOf('(') - 1); ss.SymbolName = memo.Substring(0, memo.IndexOf('(')); } ss.Share = qitem.Attribute("shar") == null ? "" : qitem.Attribute("shar").Value; ss.TempRefer = string.IsNullOrEmpty(referdumm) ? qitem.Attribute("refe").Value : referdumm; if (DateTime.Today > DateTime.Parse(PushUtils.RepublicToAD(ss.DebitDate))) { ss.Refer = qitem.Attribute("refe") == null ? "" : qitem.Attribute("refe").Value; } ss.Volumn = qitem.Attribute("valu") == null ? "" : qitem.Attribute("valu").Value; ss.Txun = qitem.Attribute("txun") == null ? "" : qitem.Attribute("txun").Value; ss.TAMarket = qitem.Attribute("mark") == null ? "" : qitem.Attribute("mark").Value; /*try{ * tcpClientQuote.Connect(ConfigurationManager.AppSettings["QueryHost"], int.Parse(ConfigurationManager.AppSettings["QueryPort"])); * if (tcpClientQuote.Connected) { * ss.LastDeal = PushUtils.QueryTAStockRefer(tcpClientQuote, ss.Symbol, int.Parse(ConfigurationManager.AppSettings["OneTryTime"]), int.Parse(ConfigurationManager.AppSettings["RetryTimes"])); * } * }catch (Exception ex) { * GD.aplogger.Error(string.Format("SubStock loadlist exception - {0}", ex.Message)); * }finally{ * if (tcpClientQuote != null) * tcpClientQuote.Close(); * }*/ //Add(ss.Symbol, ss); if (DateTime.Today > DateTime.Parse(PushUtils.RepublicToAD(ss.CouponDate))) { ss = null; } else { try { if (tcpClientQuote.Connected) { ss.LastDeal = PushUtils.QueryTAStockRefer(tcpClientQuote, ss.Symbol, ldate, int.Parse(ConfigurationManager.AppSettings["OneTryTime"]), int.Parse(ConfigurationManager.AppSettings["RetryTimes"])); } } catch (Exception ex) { GD.aplogger.Error(string.Format("SubStock loadlist LastDeal exception - {0}", ex.Message)); } Add(ss.Symbol, ss); } }//foreach if (tcpClientQuote != null) { tcpClientQuote.Close(); } }
public void SaveToDB() { SqlCommand cmd = null; if (GD.SubAppDbTool == null) { return; } SqlConnection conn = GD.SubAppDbTool.GetDbConnection(); try { conn.Open(); string delstr = "delete from SubStockInfo where Symbol=@Symbol and StartDate=@StartDate"; string addstr = "insert into SubStockInfo (Symbol, SymbolName, Market, StartDate, StopDate, DebitDate, DrawDate, " + "Share, Txun, TempRefer, Refer, Volumn, CouponDate, RefundDate, UpdateTime, LastTDPrice, PriceDiff, PriceDiffRatio, TAMarket) " + "values (@Symbol, @SymbolName, @Market, @StartDate, @StopDate, @DebitDate, @DrawDate, @Share, @Txun, " + "@TempRefer, @Refer, @Volumn, @CouponDate, @RefundDate, @UpdateTime, @LastTDPrice, @PriceDiff, @PriceDiffRatio, @TAMarket)"; //SqlCommand cmd = new SqlCommand(delstr, conn); cmd = new SqlCommand(); cmd.Connection = conn; foreach (KeyValuePair <string, SubStock> item in this) { SubStock stock = (SubStock)item.Value; cmd.Parameters.Clear(); cmd.CommandText = delstr; cmd.Parameters.AddWithValue("@Symbol", stock.Symbol); cmd.Parameters.AddWithValue("@StartDate", PushUtils.RepublicToAD(stock.StartDate)); cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); cmd.CommandText = addstr; cmd.Parameters.AddWithValue("@Symbol", stock.Symbol); cmd.Parameters.AddWithValue("@SymbolName", stock.SymbolName); cmd.Parameters.AddWithValue("@Market", stock.Market); if (!string.IsNullOrEmpty(stock.StartDate)) { cmd.Parameters.AddWithValue("@StartDate", PushUtils.RepublicToAD(stock.StartDate)); } else { cmd.Parameters.AddWithValue("@StartDate", DBNull.Value); } if (!string.IsNullOrEmpty(stock.StopDate)) { cmd.Parameters.AddWithValue("@StopDate", PushUtils.RepublicToAD(stock.StopDate)); } else { cmd.Parameters.AddWithValue("@StopDate", DBNull.Value); } if (!string.IsNullOrEmpty(stock.DebitDate)) { cmd.Parameters.AddWithValue("@DebitDate", PushUtils.RepublicToAD(stock.DebitDate)); } else { cmd.Parameters.AddWithValue("@DebitDate", DBNull.Value); } if (!string.IsNullOrEmpty(stock.DrawDate)) { cmd.Parameters.AddWithValue("@DrawDate", PushUtils.RepublicToAD(stock.DrawDate)); } else { cmd.Parameters.AddWithValue("@DrawDate", DBNull.Value); } if (!string.IsNullOrEmpty(stock.Share)) { cmd.Parameters.AddWithValue("@Share", stock.Share); } else { cmd.Parameters.AddWithValue("@Share", 0); } if (!string.IsNullOrEmpty(stock.Txun)) { cmd.Parameters.AddWithValue("@Txun", stock.Txun); } else { cmd.Parameters.AddWithValue("@Txun", 0); } if (!string.IsNullOrEmpty(stock.TempRefer)) { cmd.Parameters.AddWithValue("@TempRefer", stock.TempRefer); } else { cmd.Parameters.AddWithValue("@TempRefer", 0); } if (!string.IsNullOrEmpty(stock.Refer)) { cmd.Parameters.AddWithValue("@Refer", stock.Refer); } else { cmd.Parameters.AddWithValue("@Refer", 0); } if (!string.IsNullOrEmpty(stock.Volumn)) { cmd.Parameters.AddWithValue("@Volumn", stock.Volumn); } else { cmd.Parameters.AddWithValue("@Volumn", 0); } if (!string.IsNullOrEmpty(stock.CouponDate)) { cmd.Parameters.AddWithValue("@CouponDate", PushUtils.RepublicToAD(stock.CouponDate)); } else { cmd.Parameters.AddWithValue("@CouponDate", DBNull.Value); } if (!string.IsNullOrEmpty(stock.RefundDate)) { cmd.Parameters.AddWithValue("@RefundDate", PushUtils.RepublicToAD(stock.RefundDate)); } else { cmd.Parameters.AddWithValue("@RefundDate", DBNull.Value); } cmd.Parameters.AddWithValue("@UpdateTime", DateTime.Now); if (!string.IsNullOrEmpty(stock.LastDeal)) { cmd.Parameters.AddWithValue("@LastTDPrice", stock.LastDeal); } else { cmd.Parameters.AddWithValue("@LastTDPrice", DBNull.Value); } if (!string.IsNullOrEmpty(stock.PriceDiff)) { cmd.Parameters.AddWithValue("@PriceDiff", stock.PriceDiff); } else { cmd.Parameters.AddWithValue("@PriceDiff", DBNull.Value); } if (!string.IsNullOrEmpty(stock.PriceDiffRatio)) { cmd.Parameters.AddWithValue("@PriceDiffRatio", stock.PriceDiffRatio.Trim('%')); } else { cmd.Parameters.AddWithValue("@PriceDiffRatio", DBNull.Value); } cmd.Parameters.AddWithValue("@TAMarket", (object)stock.TAMarket ?? DBNull.Value); cmd.ExecuteNonQuery(); } } catch (SqlException ex) { GD.aplogger.Error(ex.Message); } finally { if (cmd != null) { cmd.Dispose(); } conn.Close(); } //using (SqlConnection SqlConn = new SqlConnection(constr)) //{ // /*SqlCommand Cmd = new SqlCommand(strSQL, SqlConn); // SqlDataAdapter da = new SqlDataAdapter(); // try // { // ......... // } // catch (Exception ex) // { // } // finally // { // SqlConnection.ClearPool(SqlConn); // }*/ //} }
static public string QueryTAStockRefer(TcpClient tcpclient, string symbol, int onetrytime, int retrytimes) { string ldate = GetTALastTradeDate(tcpclient, onetrytime, retrytimes); if (string.IsNullOrEmpty(ldate)) { return("0"); } string param = string.Format(ConfigurationManager.AppSettings["QueryStockPriceParam"], PushUtils.GetTaiwanToday(), symbol); string reqstr = ConfigurationManager.AppSettings["QueryStockPriceURL"] + "\x01" + param + "\x02"; string repxml = QueryTA(tcpclient, reqstr, int.Parse(ConfigurationManager.AppSettings["OneTryTime"]), int.Parse(ConfigurationManager.AppSettings["RetryTimes"])); TextReader sr = new StringReader(repxml); XElement xe = XElement.Load(sr); sr.Close(); var qitems = from qitem in xe.Descendants("NFSSNAPS") select new { Refer = qitem.Attribute("refe") == null ? "" : qitem.Attribute("refe").Value }; if (qitems.Count() > 0) { return(qitems.First().Refer); } else { return("0"); } }