public CProduct[] getDiscountList()
        {
            //SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["SQLSERVER_CONNECTION_STRING"]);

            if (isChanged())
                parseSite();

            using (var ctx = new DiscountDBContext())
            {
                var p = ctx.getProducts();

                List<CProduct> products = (from prod_i in p.AsParallel()
                                          select new CProduct
                                          {
                                                productID = prod_i.ProductID,
                                                storeID = prod_i.StoreID,
                                                storeName = prod_i.StoreName,
                                                productName = prod_i.ProductName,
                                                productsTypeID = prod_i.ProductsTypeID,
                                                productsType = prod_i.ProductsType,
                                                discount = prod_i.Discount,
                                                oldPrice = prod_i.OldPrice,
                                                newPrice = prod_i.NewPrice,
                                                startDate = prod_i.StartDate,
                                                endDate = prod_i.EndDate,
                                                imageURL = prod_i.ImageURL
                                           }).ToList();

                //foreach (var prod_i in p)
                //{
                //    CProduct product = new CProduct();

                //    product.productID = prod_i.ProductID;
                //    product.storeID = prod_i.StoreID;
                //    product.storeName = prod_i.StoreName;
                //    product.productName = prod_i.ProductName;
                //    product.productsTypeID = prod_i.ProductsTypeID;
                //    product.productsType = prod_i.ProductsType;
                //    product.discount = prod_i.Discount;
                //    product.oldPrice = prod_i.OldPrice;
                //    product.newPrice = prod_i.NewPrice;
                //    product.startDate = prod_i.StartDate;
                //    product.endDate = prod_i.EndDate;
                //    product.imageURL = prod_i.ImageURL;
                //    products.Add(product);
                //}

                return products.ToArray();
            }
            /*
            string query = @"SELECT DISTINCT * FROM Products;";

            if (cn.State == ConnectionState.Closed) cn.Open();
            SqlCommand cm = cn.CreateCommand();
            cm.CommandText = query;

            SqlDataReader reader = cm.ExecuteReader();
            while (reader.Read())
            {
                CProduct product = new CProduct();
                product.productID = reader["ProductID"].ToString();
                product.productName = reader["ProductName"].ToString();
                product.storeID = reader["StoreID"];
                product.discount = reader["Discount"];
                product.oldPrice = reader["OldPrice"].ToString();
                product.newPrice = reader["NewPrice"].ToString();
                product.startDate = reader["StartDate"].ToString();
                product.endDate = reader["EndDate"].ToString();
                product.imageURL = reader["ImageURL"].ToString();
                products.Add(product);
            }
            reader.Close();
            cn.Close();
             */
        }
 private void updateHash()
 {
     using (var ctx = new DiscountDBContext())
     {
         string contentForHash = calcStringForHash();
         ctx.HashTs.DeleteObject(ctx.HashTs.First());
         HashT hash = new HashT { Hash = calcMD5(contentForHash) };
         ctx.HashTs.AddObject(hash);
         Version vers = new Version { VersionOfCatalog = ctx.Versions.First().VersionOfCatalog + 0.01 };
         ctx.Versions.DeleteObject(ctx.Versions.First());
         ctx.Versions.AddObject(vers);
         ctx.SaveChanges();
     }
 }
 private bool isChanged()
 {
     var content = calcStringForHash();
     string hashNow = calcMD5(content);
     string hash = null;
     using (var ctx = new DiscountDBContext())
     {
         hash = ctx.HashTs.First().Hash;
     }
     if (hash == hashNow)
         return false;
     else
     {
         return true;
     }
 }
        private void parseSite()
        {
            HttpWebRequest req;
            HttpWebResponse resp;
            StreamReader sr;
            string allContent;
            req = (HttpWebRequest)WebRequest.Create("http://tomall.ru/allmarket/okey/");
            resp = (HttpWebResponse)req.GetResponse();
            sr = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("windows-1251"));
            allContent = sr.ReadToEnd();
            sr.Close();
            cleanTableProducts();

            using (var ctx = new DiscountDBContext())
            {
                for (int i = 10; i < 30; i++)
                {
                    int startInd = allContent.IndexOf("all" + i.ToString()) + 7;
                    int endInd = allContent.IndexOf("];", startInd);
                    string content = allContent.Remove(endInd).Remove(0, startInd);
                    //textBox1.Text = content;
                    List<string> productsList = new List<string>();
                    for (int index = 0; index < content.Length - 1; )
                    {

                        int startInd_ = content.IndexOf("[", index) + 1;
                        int endInd_ = content.IndexOf("]", startInd_);
                        //textBox1.Text += startInd_.ToString() + " " + endInd_.ToString() + " ";
                        index = endInd_;
                        string oneLine = content.Remove(endInd_).Remove(0, startInd_);
                        productsList.Add(oneLine);
                    }
                    foreach (string line in productsList)
                    {
                        int index = 0;
                        int startInd_ = 0;
                        int endInd_ = line.IndexOf(",", index);
                        string id = line.Remove(endInd_);
                        /*
                        var r = from c in ctx.Products
                                where c.ProductID == id
                                select c;
                        if (r.Count() > 0)
                            continue;
                        */
                        index = endInd_;
                        startInd_ = line.IndexOf("'", index) + 1;
                        endInd_ = line.IndexOf("'", startInd_);
                        string productName = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_;
                        startInd_ = line.IndexOf(",", index) + 2;
                        endInd_ = line.IndexOf(",", startInd_);
                        string oldPrice = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_;
                        startInd_ = line.IndexOf("'", index) + 1;
                        endInd_ = line.IndexOf("'", startInd_);
                        string newPrice = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_;
                        startInd_ = line.IndexOf(",", index) + 2;
                        endInd_ = line.IndexOf(",", startInd_);
                        string discount = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_;
                        startInd_ = line.IndexOf("'", index) + 1;
                        endInd_ = line.IndexOf("'", startInd_);
                        string storeName = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_;
                        startInd_ = line.IndexOf(",", index) + 2;
                        endInd_ = line.IndexOf(",", startInd_);
                        string storeID = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_;
                        startInd_ = line.IndexOf("'", index) + 1;
                        endInd_ = line.IndexOf("'", startInd_);
                        string startDate = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_ + 1;
                        startInd_ = line.IndexOf("'", index) + 1;
                        endInd_ = line.IndexOf("'", startInd_);
                        string endDate = line.Remove(endInd_).Remove(0, startInd_);

                        string imageURLFolder = Convert.ToInt32(id.Remove(3)).ToString();
                        string imageURLFile = Convert.ToInt32(id.Remove(0, 3)).ToString();
                        string imageURL = @"http://tomall.ru/allmarket/foto/" + imageURLFolder + @"/" + imageURLFile + ".jpg";

                        Product p = new Product
                        {
                            ProductID = id,
                            StoreID = Convert.ToInt32(storeID),
                            ProductName = productName,
                            ProductsTypeID = i,
                            Discount = Convert.ToInt32(discount),
                            OldPrice = oldPrice,
                            NewPrice = newPrice,
                            StartDate = startDate,
                            EndDate = endDate,
                            ImageURL = imageURL
                        };

                        ctx.Products.AddObject(p);
                        ctx.SaveChanges();
                    }
                }
                updateHash();
            }
        }
 private void cleanTableProducts()
 {
     using (var ctx = new DiscountDBContext())
     {
         ctx.delProducts();
     }
 }
 public double getVersionOfCatalog()
 {
     using (var ctx = new DiscountDBContext())
     {
         double ver = ctx.Versions.First().VersionOfCatalog;
         if(!isChanged())
             return ctx.Versions.First().VersionOfCatalog;
         return ver + 0.01;
     }
 }
        public CStore[] getStoreList()
        {
            using (var ctx = new DiscountDBContext())
            {
                var stores = from c in ctx.Stores
                             select new CStore { storeID = c.StoreID, storeName = c.StoreName };

                return stores.ToArray();
            }
        }
 public string getHash()
 {
     using (var ctx = new DiscountDBContext())
     {
         return ctx.HashTs.First().Hash;
     }
 }
 public CProduct[] getDiscountListByID(int storeID)
 {
     using (var ctx = new DiscountDBContext())
     {
         var products = (from c in ctx.getProductsByID(storeID)
                        select new CProduct
                        {
                            discount = c.Discount,
                            endDate = c.EndDate,
                            imageURL = c.ImageURL,
                            newPrice = c.NewPrice,
                            oldPrice = c.OldPrice,
                            productID = c.ProductID,
                            productName = c.ProductName,
                            productsType = c.ProductsType,
                            productsTypeID = c.ProductsTypeID,
                            startDate = c.StartDate,
                            storeID = c.StoreID,
                            storeName = c.StoreName
                        }).ToArray();
         return products;
     }
 }