public void UpdatePrevPrice(ProductBox lastbox) { if (lastbox == null) { this.PrevPrice = this.CurrentPrice; return; } if (int.Parse(lastbox.UpdateDate.ToString("HH")) < 22 && (int.Parse(this.UpdateDate.ToString("HH")) > 22 || double.Parse(this.UpdateDate.ToString("yyyyMMdd")) > double.Parse(lastbox.UpdateDate.ToString("yyyyMMdd")))) { this.PrevPrice = lastbox.CurrentPrice; }else { this.PrevPrice = lastbox.PrevPrice; } if (this.PrevPrice == 0) {this.PrevPrice = this.CurrentPrice; } }
public void UpdateBoxHangHoa() { try { if ((ConfigurationManager.AppSettings["ProductBoxAllowance"] ?? "") != "TRUE") { return; } var tabVN = new List<string>() { ChiTieuCrawler.TabVietnam.VangTheGioi, ChiTieuCrawler.TabVietnam.VangSJC, ChiTieuCrawler.TabVietnam.USDSIN, ChiTieuCrawler.TabVietnam.USDHKD, ChiTieuCrawler.TabVietnam.CNY, ChiTieuCrawler.TabVietnam.BangAnh, ChiTieuCrawler.TabVietnam.USDVCB, ChiTieuCrawler.TabVietnam.EURVCB }; /*, ChiTieuCrawler.TabVietnam.USDtudo, ChiTieuCrawler.TabVietnam.EURtudo*/ var tabTG = new List<string>() { ChiTieuCrawler.TabTheGioi.USDIndex, ChiTieuCrawler.TabTheGioi.DowJones, ChiTieuCrawler.TabTheGioi.Nasdaq, ChiTieuCrawler.TabTheGioi.SP500, ChiTieuCrawler.TabTheGioi.FTSE100, ChiTieuCrawler.TabTheGioi.DAX, ChiTieuCrawler.TabTheGioi.Nikkei225, ChiTieuCrawler.TabTheGioi.HangSeng, ChiTieuCrawler.TabTheGioi.StraitTimes }; var tabHH = new List<string>() { ChiTieuCrawler.TabHangHoa.CrudeOil, ChiTieuCrawler.TabHangHoa.NaturalGas, ChiTieuCrawler.TabHangHoa.Gold, ChiTieuCrawler.TabHangHoa.Copper, ChiTieuCrawler.TabHangHoa.Silver, ChiTieuCrawler.TabHangHoa.Corn, ChiTieuCrawler.TabHangHoa.Sugar, ChiTieuCrawler.TabHangHoa.Coffee, ChiTieuCrawler.TabHangHoa.Cotton, ChiTieuCrawler.TabHangHoa.RoughRice, ChiTieuCrawler.TabHangHoa.Wheat, ChiTieuCrawler.TabHangHoa.Soybean, ChiTieuCrawler.TabHangHoa.Ethanol }; var tabMobile = new List<string>() { ChiTieuCrawler.TabMobile.VangSJC, ChiTieuCrawler.TabMobile.USDVCB, ChiTieuCrawler.TabMobile.VangTD, ChiTieuCrawler.TabMobile.USDTD }; var bFirstTime = true; while (ServiceStarted) { try { var dt = SqlDb.GetCrawlerData(); var manual = SqlDb.GetManualProductData(); var redis = new RedisClient(ConfigRedis.Host, ConfigRedis.Port); #region Tab Viet Nam //tab vietnam var key = string.Format(RedisKey.KeyProductBox, 1); var ls = redis.ContainsKey(key) ? redis.Get<List<ProductBox>>(key) : new List<ProductBox>(); var data = new List<ProductBox>(); ProductBox box, newbox, otherbox; foreach (var item in tabVN) { if (int.Parse(item) < 0) { newbox = otherbox = null; box = FindBox(item, ls); otherbox = null; if (manual.Rows.Count > 0) { switch (item) { case ChiTieuCrawler.TabVietnam.EURtudo: if (double.Parse(manual.Rows[0]["Price_EURO"].ToString()) == 0) continue; otherbox = new ProductBox() { ProductName = "EUR (tự do)", CurrentPrice = double.Parse(manual.Rows[0]["Price_EURO"].ToString()), OtherPrice = double.Parse(manual.Rows[0]["Price_Euro_Sale"].ToString()), PrevPrice = 0, UpdateDate = DateTime.Now, DbId = "-2" }; break; case ChiTieuCrawler.TabVietnam.USDtudo: if (double.Parse(manual.Rows[0]["Price_USD"].ToString()) == 0) continue; otherbox = new ProductBox() { ProductName = "USD (tự do)", CurrentPrice = double.Parse(manual.Rows[0]["Price_USD"].ToString()), OtherPrice = double.Parse(manual.Rows[0]["Price_USD_Sale"].ToString()), PrevPrice = 0, UpdateDate = DateTime.Now, DbId = "-1" }; break; } } if (otherbox != null) { otherbox.UpdatePrevPrice(bFirstTime ? null : box); data.Add(otherbox); } continue; } var drs = dt.Select("ID=" + item); if (drs.Length == 0) continue; var dr = drs[0]; try { newbox = otherbox = null; box = FindBox(dr["ID"].ToString(), ls); switch (dr["ID"].ToString()) { case ChiTieuCrawler.TabVietnam.VangTheGioi: newbox = new ProductBox() { ProductName = "Vàng TG(USD)", CurrentPrice = double.Parse(dr["MuaVao"].ToString()), OtherPrice = 0, PrevPrice = double.Parse(dr["MuaVao"].ToString()) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabVietnam.VangSJC: newbox = new ProductBox() { ProductName = "Vàng SJC", CurrentPrice = double.Parse(dr["MuaVao"].ToString().Replace(",", "")), OtherPrice = double.Parse(dr["BanRa"].ToString().Replace(",", "")), PrevPrice = 0, UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabVietnam.USDVCB: newbox = new ProductBox() { ProductName = "USD (VCB)", CurrentPrice = double.Parse(dr["MuaVao"].ToString().Replace(",", "")), OtherPrice = double.Parse(dr["BanRa"].ToString().Replace(",", "")), PrevPrice = 0, UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabVietnam.EURVCB: newbox = new ProductBox() { ProductName = "EUR (VCB)", CurrentPrice = double.Parse(dr["MuaVao"].ToString().Replace(",", "")), OtherPrice = double.Parse(dr["BanRa"].ToString().Replace(",", "")), PrevPrice = 0, UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabVietnam.CNY: newbox = new ProductBox() { ProductName = "CNY", CurrentPrice = double.Parse(dr["MuaVao"].ToString().Replace(",", "")), OtherPrice = double.Parse(dr["BanRa"].ToString().Replace(",", "")), PrevPrice = 0, UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabVietnam.USDSIN: newbox = new ProductBox() { ProductName = "SGD", CurrentPrice = double.Parse(dr["MuaVao"].ToString().Replace(",", "")), OtherPrice = double.Parse(dr["BanRa"].ToString().Replace(",", "")), PrevPrice = 0, UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabVietnam.USDHKD: newbox = new ProductBox() { ProductName = "HKD", CurrentPrice = double.Parse(dr["MuaVao"].ToString().Replace(",", "")), OtherPrice = double.Parse(dr["BanRa"].ToString().Replace(",", "")), PrevPrice = 0, UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabVietnam.BangAnh: newbox = new ProductBox() { ProductName = "Bảng Anh", CurrentPrice = double.Parse(dr["MuaVao"].ToString().Replace(",", "")), OtherPrice = double.Parse(dr["BanRa"].ToString().Replace(",", "")), PrevPrice = 0, UpdateDate = DateTime.Now, DbId = box.DbId }; break; default: newbox = null; break; } if (newbox != null) { if (newbox.DbId != ChiTieuCrawler.TabVietnam.VangTheGioi) newbox.UpdatePrevPrice(bFirstTime ? null : box); data.Add(newbox); } } catch (Exception ex) { log.WriteEntry("GetBox : " + dr["ID"] + ":" + ex.ToString(), EventLogEntryType.Error); } } if (data.Count > 0) { if (redis.ContainsKey(key)) redis.Set(key, data); else redis.Add(key, data); } #endregion #region Tab The gioi key = string.Format(RedisKey.KeyProductBox, 2); data = new List<ProductBox>(); double d; foreach (var item in tabTG) { var drs = dt.Select("ID=" + item); if (drs.Length == 0) continue; var dr = drs[0]; try { box = FindBox(dr["ID"].ToString(), ls); switch (dr["ID"].ToString()) { case ChiTieuCrawler.TabTheGioi.USDIndex: newbox = new ProductBox() { ProductName = "US Dollar Index", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString().Replace(",", "")), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabTheGioi.DowJones: newbox = new ProductBox() { ProductName = "DowJones", CurrentPrice = double.Parse(dr["Index"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), UpdateDate = DateTime.Now, DbId = box.DbId }; if (newbox.CurrentPrice == newbox.PrevPrice) { newbox.PrevPrice = double.Parse(dr["ThayDoi"].ToString()); } break; case ChiTieuCrawler.TabTheGioi.Nasdaq: newbox = new ProductBox() { ProductName = "Nasdaq", CurrentPrice = double.Parse(dr["Index"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), UpdateDate = DateTime.Now, DbId = box.DbId }; if (newbox.CurrentPrice == newbox.PrevPrice && double.TryParse(dr["ThayDoi"].ToString(), out d)) { newbox.PrevPrice = double.Parse(dr["ThayDoi"].ToString()); } break; case ChiTieuCrawler.TabTheGioi.SP500: newbox = new ProductBox() { ProductName = "S&P 500", CurrentPrice = double.Parse(dr["Index"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), UpdateDate = DateTime.Now, DbId = box.DbId }; if (newbox.CurrentPrice == newbox.PrevPrice) { newbox.PrevPrice = double.Parse(dr["ThayDoi"].ToString()); } break; case ChiTieuCrawler.TabTheGioi.FTSE100: newbox = new ProductBox() { ProductName = "FTSE 100", CurrentPrice = double.Parse(dr["Index"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), UpdateDate = DateTime.Now, DbId = box.DbId }; if (newbox.CurrentPrice == newbox.PrevPrice) { newbox.PrevPrice = double.Parse(dr["ThayDoi"].ToString()); } break; case ChiTieuCrawler.TabTheGioi.DAX: newbox = new ProductBox() { ProductName = "DAX", CurrentPrice = double.Parse(dr["Index"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), UpdateDate = DateTime.Now, DbId = box.DbId }; if (newbox.CurrentPrice == newbox.PrevPrice) { newbox.PrevPrice = double.Parse(dr["ThayDoi"].ToString()); } break; case ChiTieuCrawler.TabTheGioi.Nikkei225: newbox = new ProductBox() { ProductName = "Nikkei 225", CurrentPrice = double.Parse(dr["Index"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), UpdateDate = DateTime.Now, DbId = box.DbId }; if (newbox.CurrentPrice == newbox.PrevPrice) { newbox.PrevPrice = double.Parse(dr["ThayDoi"].ToString()); } break; case ChiTieuCrawler.TabTheGioi.HangSeng: newbox = new ProductBox() { ProductName = "Hang Seng", CurrentPrice = double.Parse(dr["Index"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), UpdateDate = DateTime.Now, DbId = box.DbId }; if (newbox.CurrentPrice == newbox.PrevPrice) { newbox.PrevPrice = double.Parse(dr["ThayDoi"].ToString()); } break; case ChiTieuCrawler.TabTheGioi.StraitTimes: newbox = new ProductBox() { ProductName = "Strait Times", CurrentPrice = double.Parse(dr["Index"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), UpdateDate = DateTime.Now, DbId = box.DbId }; if (newbox.CurrentPrice == newbox.PrevPrice) { newbox.PrevPrice = double.Parse(dr["ThayDoi"].ToString()); } break; default: newbox = null; break; } if (newbox != null) { data.Add(newbox); } } catch (Exception ex) { log.WriteEntry("GetBox : " + dr["ID"] + ":" + ex.ToString(), EventLogEntryType.Error); } } if (data.Count > 0) { if (redis.ContainsKey(key)) redis.Set(key, data); else redis.Add(key, data); } #endregion #region Tab Hàng hóa key = string.Format(RedisKey.KeyProductBox, 3); data = new List<ProductBox>(); foreach (var item in tabHH) { if (item == "-1") { switch (item) { } continue; } var drs = dt.Select("ID=" + item); if (drs.Length == 0) continue; var dr = drs[0]; try { box = FindBox(dr["ID"].ToString(), ls); switch (dr["ID"].ToString()) { case ChiTieuCrawler.TabHangHoa.CrudeOil: newbox = new ProductBox() { ProductName = "Crude Oil", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabHangHoa.NaturalGas: newbox = new ProductBox() { ProductName = "Natural Gas", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabHangHoa.Gold: newbox = new ProductBox() { ProductName = "Gold", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabHangHoa.Copper: newbox = new ProductBox() { ProductName = "Copper", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabHangHoa.Silver: newbox = new ProductBox() { ProductName = "Silver", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabHangHoa.Corn: newbox = new ProductBox() { ProductName = "Corn", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabHangHoa.Sugar: newbox = new ProductBox() { ProductName = "Sugar", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabHangHoa.Coffee: newbox = new ProductBox() { ProductName = "Coffee", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabHangHoa.Cotton: newbox = new ProductBox() { ProductName = "Cotton", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabHangHoa.RoughRice: newbox = new ProductBox() { ProductName = "Rough rice", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabHangHoa.Wheat: newbox = new ProductBox() { ProductName = "Wheat", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabHangHoa.Soybean: newbox = new ProductBox() { ProductName = "Soybean", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabHangHoa.Ethanol: newbox = new ProductBox() { ProductName = "Ethanol", CurrentPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")), OtherPrice = 0, PrevPrice = double.Parse(dr["Gia"].ToString().Replace(",", "")) - double.Parse(dr["ThayDoi"].ToString()), UpdateDate = DateTime.Now, DbId = box.DbId }; break; default: newbox = null; break; } if (newbox != null) { data.Add(newbox); } } catch (Exception ex) { log.WriteEntry("GetBox : " + dr["ID"] + ":" + ex.ToString(), EventLogEntryType.Error); } } if (data.Count > 0) { if (redis.ContainsKey(key)) redis.Set(key, data); else redis.Add(key, data); } #endregion #region Tab Mobile key = string.Format(RedisKey.KeyProductBox, 4); data = new List<ProductBox>(); foreach (var item in tabMobile) { newbox = null; if (double.Parse(item) < 0) { double price; //manual switch (item) { case ChiTieuCrawler.TabMobile.USDTD: price = manual.Rows.Count > 0 ? double.Parse(manual.Rows[0]["Price_USD"].ToString()) : 0; newbox = new ProductBox() { ProductName = "USDTD", CurrentPrice = price }; break; case ChiTieuCrawler.TabMobile.VangTD: price = manual.Rows.Count > 0 ? double.Parse(manual.Rows[0]["Price_Gold"].ToString()) : 0; newbox = new ProductBox() { ProductName = "VangTD", CurrentPrice = price }; break; } if (newbox != null) { data.Add(newbox); } continue; } var drs = dt.Select("ID=" + item); if (drs.Length == 0) continue; var dr = drs[0]; try { box = FindBox(dr["ID"].ToString(), ls); switch (dr["ID"].ToString()) { case ChiTieuCrawler.TabMobile.VangSJC: newbox = new ProductBox() { ProductName = "Vàng SJC", CurrentPrice = double.Parse(dr["MuaVao"].ToString().Replace(",", "")), OtherPrice = double.Parse(dr["BanRa"].ToString().Replace(",", "")), PrevPrice = 0, UpdateDate = DateTime.Now, DbId = box.DbId }; break; case ChiTieuCrawler.TabMobile.USDVCB: newbox = new ProductBox() { ProductName = "USD (VCB)", CurrentPrice = double.Parse(dr["MuaVao"].ToString().Replace(",", "")), OtherPrice = double.Parse(dr["BanRa"].ToString().Replace(",", "")), PrevPrice = 0, UpdateDate = DateTime.Now, DbId = box.DbId }; break; } if (newbox != null) { data.Add(newbox); } } catch (Exception ex) { log.WriteEntry("GetBox : " + dr["ID"] + ":" + ex.ToString(), EventLogEntryType.Error); } } if (data.Count > 0) { if (redis.ContainsKey(key)) redis.Set(key, data); else redis.Add(key, data); } #endregion bFirstTime = false; } catch (Exception ex) { log.WriteEntry(ex.ToString(), EventLogEntryType.Error); } Thread.Sleep(crawlerInterval); } } catch (Exception ex) { log.WriteEntry(ex.ToString(), EventLogEntryType.Error); } }