public void SaveDishes(StoreInfoEntity storeInfoEntity, StoreInfo siteStoreInfo)
 {
     var dishTypeBll = new Maticsoft.BLL.DishesTyep();
     var dishesEntityBll = new Maticsoft.BLL.DishesBll();
     var storePictureBll = new Maticsoft.BLL.StorePicture();
     DoProgress(30);
     dishesEntityBll.Remove(string.Format("BusinessID = '{0}'", storeInfoEntity.BizID));
     dishTypeBll.Remove(string.Format("BusinessID = '{0}'", storeInfoEntity.BizID));
     foreach (var dishType in siteStoreInfo.DishTypeList)
     {
         dishType.BusinessID = storeInfoEntity.BizID;
         dishTypeBll.Add(dishType);
         foreach (var dishesEntity in dishType.DishesList)
         {
             dishesEntity.BusinessID = storeInfoEntity.BizID;
             dishesEntityBll.Add(dishesEntity);
             if (!string.IsNullOrEmpty(dishesEntity.PictureHref) && !string.IsNullOrWhiteSpace(dishesEntity.PictureHref))
             {
                 var storePicture = new Maticsoft.Model.StorePicture();
                 storePicture.PID = Guid.NewGuid().ToString();
                 storePicture.PictureName = string.Format("{0}.jpg", storePicture.PID);
                 storePicture.PicType = "Food";
                 storePicture.PicturePath = dishesEntity.PictureHref;
                 storePicture.StoreId = storeInfoEntity.BizID;
                 storePictureBll.Add(storePicture);
             }
         }
     }
     DoProgress();
 }
        private void SaveBtn_Click(object sender, EventArgs e)
        {
            if (catalogueListBox.SelectedIndex < 0)
            {
                return;
            }
            var catalogueInfo = catalogueListBox.SelectedItem as Catalogue;
            if (catalogueInfo == null)
            {
                return;
            }
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            var siteStoreInfo = _storeInfoList.Find(x => x.storeId == catalogueInfo.StoreId);
            var oldStoreInfo = textBox4.Tag as StoreInfoEntity;
            var storeInfoEntity = GetStoreInfoEntity(catalogueInfo, oldStoreInfo, siteStoreInfo);
            var storeBll = new StoreInfoBll();
            var storeInfoBll = new Maticsoft.BLL.StoreInfo();
            var storePictureBll = new Maticsoft.BLL.StorePicture();
            var storePicture = new Maticsoft.Model.StorePicture();
            storePicture.PID = Guid.NewGuid().ToString();
            storePicture.PictureName = string.Format("{0}.jpg", storeInfoEntity.BizID);
            storePicture.PicType = "Shop";
            storePicture.PicturePath = siteStoreInfo.StorePictureHref;
            storePicture.StoreId = storeInfoEntity.BizID;
            if (storeBll.Exists(storeInfoEntity.BizID))
            {
                storeBll.Update(storeInfoEntity);
                storeInfoBll.Update(siteStoreInfo);
            }
            else
            {
                storeInfoEntity.ShortID = storeBll.GetMaxShortID();
                storeInfoBll.Add(siteStoreInfo);
                storeBll.Add(storeInfoEntity);
            }
            if (!string.IsNullOrEmpty(storePicture.PicturePath))
            {
                storePictureBll.Remove(string.Format("PicType = 'Shop' and StoreId = '{0}'", storeInfoEntity.BizID));
                storePictureBll.Add(storePicture);
            }
            var saveStoreEntity = new SaveStoreEntity();
            saveStoreEntity.CataloEventHandler += UpdateIncrement;
            saveStoreEntity.InitProgress();
            var specialTagList = chlBoxSpecialTag.CheckedItems.Cast<SpecialTag>().ToList();
            Action<StoreInfoEntity, List<SpecialTag>> storeSpecialTagDelegate = saveStoreEntity.SaveStoreSpecialTag;
            storeSpecialTagDelegate.BeginInvoke(storeInfoEntity, specialTagList, null, null);

            var cookingStylesList = chbCookingStyles.CheckedItems.Cast<CookingStyles>().ToList();
            Action<StoreInfoEntity, IEnumerable<CookingStyles>> cookingStylesDelegate = saveStoreEntity.SaveCookingStyles;
            cookingStylesDelegate.BeginInvoke(storeInfoEntity, cookingStylesList, null, null);

            var cityLocalTagEntityList = clbStoreTag.CheckedItems.Cast<CityLocalTagEntity>().ToList();
            Action<StoreInfoEntity, IEnumerable<CityLocalTagEntity>> cityLocalTagEntityDelegate = saveStoreEntity.SaveCityLocalTagEntity;
            cityLocalTagEntityDelegate.BeginInvoke(storeInfoEntity, cityLocalTagEntityList, null, null);

            if (chbDish.Checked)
            {
                Action<StoreInfoEntity, StoreInfo> dishesDelegate = saveStoreEntity.SaveDishes;
                dishesDelegate.BeginInvoke(storeInfoEntity, siteStoreInfo, null, null);
            }
            if (chbPic.Checked)
            {
                Action<StoreInfoEntity, StoreInfo> storePicturesDelegate = saveStoreEntity.SaveStorePictures;
                storePicturesDelegate.BeginInvoke(storeInfoEntity, siteStoreInfo, null, null);
            }

            saveStoreEntity.DoProgress();
            foreach (var selectedIndex in clbStoreTag.CheckedIndices)
            {
                clbStoreTag.SetItemCheckState((int)selectedIndex, CheckState.Unchecked);
            }
            saveStoreEntity.DoProgress();
            foreach (var selectedIndex in chlBoxSpecialTag.CheckedIndices)
            {
                chlBoxSpecialTag.SetItemCheckState((int)selectedIndex, CheckState.Unchecked);
            }
            saveStoreEntity.DoProgress();
            foreach (var selectedIndex in chbCookingStyles.CheckedIndices)
            {
                chbCookingStyles.SetItemCheckState((int)selectedIndex, CheckState.Unchecked);
            }
            saveStoreEntity.DoProgress();
            stopwatch.Stop();
            Invoke(_showMessageBox, string.Format(@"{0}保存成功,耗时{1}毫秒", storeInfoEntity.StoreName, stopwatch.ElapsedMilliseconds));
            catalogueListBox.SetItemCheckState(catalogueListBox.SelectedIndex, CheckState.Checked);
            if (catalogueListBox.SelectedIndex < catalogueListBox.Items.Count - 1)
            {
                catalogueListBox.SelectedIndex += 1;
            }
            SetCatalogueListBox();
        }
        /// <summary>
        /// 获取图片详细
        /// </summary>
        public void GetPicture(Maticsoft.Model.StoreInfo storeInfo)
        {
            var picturePathName = "PhotoAlbum";
            var picPullList = new List<PicPull>() { PicPull.Environmental, PicPull.Cake };
            //var picThumbnail = string.Format(newThumbnail, picturePathName, storeInfo.StoreName);
            //var newPathThumb = string.Format(newPathThu, picturePathName);
            //try
            //{
            //    string CreatePath = string.Format(newPath, storeInfo.StoreName, picturePathName, string.Empty);
            //    if (!Directory.Exists(CreatePath))
            //    {
            //        Directory.CreateDirectory(CreatePath);
            //    }
            //    if (!Directory.Exists(picThumbnail))
            //    {
            //        Directory.CreateDirectory(picThumbnail);
            //    }
            //    if (!Directory.Exists(newPathThumb))
            //    {
            //        Directory.CreateDirectory(newPathThumb);
            //    }

            //}
            //catch (Exception e)
            //{
            //}

            var storePictureBll = new StorePicture();

            var storePicturelist = storePictureBll.GetModelList(string.Format("StoreID = '{0}' and picType ='{1}'", storeInfo.storeId, picturePathName));
            foreach (var storePicture in storePicturelist)
            {
                storePictureBll.Delete(storePicture.PID);
            }
            var storePicturesTableBll = new StorePicturesTable();
            var storePicturesTableList = storePicturesTableBll.GetModelList(string.Format("businessID = '{0}'", storeInfo.storeId));
            foreach (var storePicturesTable in storePicturesTableList)
            {
                storePicturesTableBll.Delete(storePicturesTable.StorePicturesID);
            }
            foreach (var picPull in picPullList)
            {
                var busphotoAlbumTable = new Maticsoft.Model.BusPhotoAlbumTable();
                busphotoAlbumTable.BusinessID = storeInfo.storeId;
                if (picPull == PicPull.Cake)
                {
                    busphotoAlbumTable.AlbumName = @"菜品图片";
                }
                else
                {
                    busphotoAlbumTable.AlbumName = @"餐厅环境";
                }
                busphotoAlbumTable.BusPhotoAlbumID = Guid.NewGuid().ToString();
                busphotoAlbumTable.IsDefault = true;
                var busPhotoAlbumTableBll = new Maticsoft.BLL.BusPhotoAlbumTable();
                busPhotoAlbumTableBll.Add(busphotoAlbumTable);

                var pageIndex = 1;
                while (true)
                {
                    //t=-1 2 3时有值
                    var picturePath = @"{0}shop/new/ajax/picpull.aspx?resid={1}&t={2}&time={3}";

                    picturePath = string.Format(picturePath, _pageUrl, storeInfo.Fid, (int)picPull, pageIndex);
                    //picturePath = @"http://www.xiaomishu.com/shop/new/ajax/picpull.aspx?resid=C21C09K22533&time=6";
                    pageIndex++;
                    var htmlWeb = new HtmlWeb();

                    var htmlDoc = htmlWeb.Load(picturePath);
                    var pictureNodeList = htmlDoc.DocumentNode.SelectNodes(".//div[@class='bdc bgf0 p10']/a/img");
                    if (pictureNodeList == null)
                    {
                        break;
                    }
                    foreach (var pictureNode in pictureNodeList)
                    {
                        var dishesPicturePath = pictureNode.Attributes["src"].Value;
                        if (string.IsNullOrEmpty(dishesPicturePath) || dishesPicturePath.EndsWith("food_nopic.png"))
                        {
                            return;
                        }
                        var storePicture = new Maticsoft.Model.StorePicture();
                        storePicture.PID = Guid.NewGuid().ToString();
                        var pictureName = string.Format("{0}.jpg", storePicture.PID);
                        storePicture.PictureName = pictureName;
                        storePicture.PicType = picturePathName;
                        storePicture.PicturePath = dishesPicturePath;
                        storePicture.StoreId = storeInfo.storeId;
                        //dishesPicturePath = @"http://f2.95171.cn/pic/D22I15N56303/3cfeefbe-cadd-49af-904b-a33a4b1d6c70.jpg";
                        //try
                        //{
                        //    db.DownFile(dishesPicturePath.Replace("/320_0/", "/").Replace(@"/300_200/", "/"), string.Format(newPath, storeInfo.StoreName, picturePathName, pictureName));
                        //}
                        //catch (Exception)
                        //{
                        //}
                        //try
                        //{
                        //    db.DownFile(dishesPicturePath, newPathThumb + pictureName);
                        //}
                        //catch (Exception)
                        //{
                        //    throw;
                        //}
                        //var storePath = string.Format(newPath, storeInfo.StoreName, picturePathName, pictureName);

                        //db.ZoomAuto(newPathThumb + pictureName, picThumbnail + pictureName, 320, 240);//生成缩略图
                        //db.ZoomAuto(newPathThumb + pictureName, picThumbnail + "640_480_" + pictureName, 640, 480);//生成缩略图
                        //db.ZoomAuto(newPathThumb + pictureName, picThumbnail + "320_240_" + pictureName, 320, 240);//生成缩略图
                        //db.ZoomAuto(newPathThumb + pictureName, picThumbnail
                        //     + "160_120_" + pictureName, 160, 120);//生成缩略图
                        storePictureBll.Add(storePicture);

                        var storePicturesTable = new Maticsoft.Model.StorePicturesTable();
                        storePicturesTable.StorePicturesID = Guid.NewGuid().ToString();
                        storePicturesTable.BusPhotoAlbumID = busphotoAlbumTable.BusPhotoAlbumID;
                        storePicturesTable.BusinessID = busphotoAlbumTable.BusinessID;
                        storePicturesTable.PictureAddress = pictureName;
                        storePicturesTable.PicState = 2;
                        //storePicturesTable.PicName = dishesPicturePath;
                        storePicturesTable.UploadTime = DateTime.Now;
                        storePicturesTableBll.Add(storePicturesTable);
                    }
                }
            }
        }
        /// <summary>
        /// 商家列表页获取
        /// </summary>
        /// <param name="pageUrl"></param>
        /// <param name="poIndex"></param>
        public List<Model.Catalogue> InsretPage(string pageUrl, int poIndex)
        {
            var htmlWeb = new HtmlWeb();
            var htmlDoc = htmlWeb.Load(pageUrl);
            var list =
                htmlDoc.DocumentNode.SelectNodes(
                    ".//div[@class='constr']/div/div[@class='res_hm_c']/div[@class='res_sch_res schResList']/div[@class='l']/a");

            if (list == null)
            {
                return null;
            }
            var catalogueList = new List<Model.Catalogue>();
            foreach (var htmlNode in list)
            //Parallel.ForEach(list, htmlNode =>
            {
                var title = htmlNode.SelectSingleNode(".//img").Attributes["title"].Value;
                var href = htmlNode.Attributes["href"].Value;
                var keyList = href.Trim('/').Split('/');
                var keyID = string.Empty;
                var catalogue = new Maticsoft.Model.Catalogue();
                if (keyList.Count() == 2)
                {
                    keyID = keyList[1];
                    catalogue.FId = keyID;
                    catalogue.title = title;
                    catalogue.href = href;
                    catalogue.LocalTagID = poIndex;
                    catalogue.StoreId = Guid.NewGuid().ToString();
                    var storeInfoBll = new Maticsoft.BLL.StoreInfo();
                    var temStoreInfoList = storeInfoBll.GetModelList(string.Format("Fid = '{0}'", keyID));
                    var storePictureBll = new StorePicture();
                    if (temStoreInfoList != null && temStoreInfoList.Count > 0)
                    {
                        catalogue.IsRead = true;
                        var temStoreInfo = temStoreInfoList.FirstOrDefault();
                        if (temStoreInfo != null)
                        {
                            catalogue.StoreId = temStoreInfo.storeId;
                        }
                        var oldStorePicture =
                            storePictureBll.GetModelList(string.Format("PicType ='Shop' and StoreId = '{0}'",
                                catalogue.StoreId));
                        foreach (var storePicture in oldStorePicture)
                        {
                            storePictureBll.Delete(storePicture.PID);
                        }
                        //Parallel.ForEach(oldStorePicture, storePicture => storePictureBll.Delete(storePicture.PID));
                    }
                    else
                    {
                        catalogue.IsRead = false;
                    }
                    var imgNode = htmlNode.SelectSingleNode(".//img[@class='jsLazyImage']");
                    if (imgNode != null)
                    {
                        var shopPicturePath = imgNode.Attributes["data-url"].Value;
                        if (!string.IsNullOrEmpty(shopPicturePath) && !shopPicturePath.EndsWith("food_nopic.png"))
                        {
                            var storePicture = new Maticsoft.Model.StorePicture();
                            storePicture.PID = Guid.NewGuid().ToString();
                            var pictureName = string.Format("{0}.jpg", storePicture.PID);
                            storePicture.PictureName = pictureName;
                            storePicture.PicType = "Shop";
                            storePicture.PicturePath = shopPicturePath;
                            storePicture.StoreId = catalogue.StoreId;
                            storePictureBll.Add(storePicture);
                            catalogue.picName = storePicture.PictureName;
                        }

                    }
                    catalogueList.Add(catalogue);
                }
                //catalogueBll.Add(catalogue);
            }

            return catalogueList.OrderBy(x => x != null && x.IsRead).ThenBy(x => x != null ? x.FId : string.Empty).ToList();
        }
        public void GetFood(Maticsoft.Model.StoreInfo storeInfo)
        {
            var dishesPath = "Food";
            try
            {
                //string CreatePath = string.Format(newPath, storeInfo.StoreName, dishesPath, string.Empty);
                //if (!Directory.Exists(CreatePath))
                //{
                //    Directory.CreateDirectory(CreatePath);
                //}

                //var foodThumbnail = string.Format(newThumbnail, dishesPath, storeInfo.StoreName);
                //if (!Directory.Exists(foodThumbnail))
                //{
                //    Directory.CreateDirectory(foodThumbnail);
                //}

                //var newPathThumb = string.Format(newPathThu, dishesPath);
                //if (!Directory.Exists(newPathThumb))
                //{
                //    Directory.CreateDirectory(newPathThumb);
                //}

                var dishesBll = new Maticsoft.BLL.Dishes();
                var disheslist = dishesBll.GetModelList(string.Format("StoreId = '{0}'", storeInfo.storeId));
                foreach (var dishese in disheslist)
                {
                    dishesBll.Delete(dishese.DishesID);
                }
                var dishTypeBll = new Maticsoft.BLL.DishesTyepTable();
                var dishTypeList = dishTypeBll.GetModelList(string.Format("BusinessID = '{0}'", storeInfo.storeId));
                foreach (var dishesTyepTable in dishTypeList)
                {
                    dishTypeBll.Delete(dishesTyepTable.DishesTypeID);
                }
                var dishTypePath = string.Format("{0}shop/{1}/dish/", _pageUrl, storeInfo.Fid);

                var storePictureBll = new StorePicture();

                var storePicturelist = storePictureBll.GetModelList(string.Format("StoreID = '{0}' and picType ='{1}'", storeInfo.storeId, dishesPath));
                foreach (var storePicture in storePicturelist)
                {
                    storePictureBll.Delete(storePicture.PID);
                }

                //dishPath = @"http://www.xiaomishu.com/shop/D22I15N56303/dish/";
                var htmlWeb = new HtmlWeb();

                var htmlDoc = htmlWeb.Load(dishTypePath);
                var dishTypeNodeList = htmlDoc.DocumentNode.SelectNodes(".//div[@class='constr']/div[@class='constr_in']/div[@class='l float_four']/div[@class='bdc bgf0 pr10 pb10']/a[@class='res_food_ins_gra']");
                if (dishTypeNodeList == null)
                {
                    return;
                }
                foreach (var dishTypeNode in dishTypeNodeList)
                {
                    var dishType = new Maticsoft.Model.DishesTyepTable();
                    dishType.BusinessID = storeInfo.storeId;
                    dishType.CreateDate = DateTime.Now;
                    dishType.DishesTypeID = Guid.NewGuid().ToString();
                    var dishName = dishTypeNode.InnerText;
                    var dishlength = dishName.IndexOf('(');
                    dishType.DishesTypeName = dishTypeNode.InnerText.Substring(0, dishlength);
                    dishTypeBll.Add(dishType);

                    var pageIndex = 1;
                    while (true)
                    {
                        var dishUrl = dishTypeNode.Attributes["href"].Value;

                        var dishPath = string.Format("{0}{1}p{2}/", _pageUrl, dishUrl, pageIndex);

                        pageIndex++;
                        htmlDoc = htmlWeb.Load(dishPath);
                        var dishesNodeList =
                        htmlDoc.DocumentNode.SelectNodes(
                            ".//div[@class='constr']/div[@class='constr_in']/div[@class='cell pl10']/ul[@id='foodListUl']/li[@class='res_food_list']");
                        if (dishesNodeList == null || dishesNodeList.Count <= 1)
                        {
                            break;
                        }
                        foreach (var dishNode in dishesNodeList)
                        {
                            var dishes = new Maticsoft.Model.Dishes();
                            var foodList = dishNode.SelectSingleNode(".//div[@class='fix']");
                            if (foodList == null)
                            {
                                continue;
                            }
                            var foodChildList = foodList.ChildNodes;
                            var foodName = foodList.ChildNodes.Count > 1 ? foodChildList[1].InnerText ?? string.Empty : string.Empty;
                            var foodPrice = foodList.ChildNodes.Count > 3 ? foodChildList[3].InnerText ?? string.Empty : string.Empty;
                            var popularity = foodList.ChildNodes.Count > 5 ? foodChildList[5].InnerText ?? string.Empty : string.Empty;
                            dishes.DishesID = Guid.NewGuid().ToString();
                            dishes.DishesName = foodName.Trim();
                            foodPrice = foodPrice.Trim().Replace("¥", string.Empty).Replace("¥", string.Empty);
                            dishes.dishTypeID = dishType.DishesTypeID;
                            if (foodPrice.Trim() == "时价")
                            {
                                dishes.DishesMoney = "0";
                                dishes.IsCurrentPrice = true;
                            }
                            else if (foodPrice.Trim() == "不详")
                            {
                                dishes.DishesMoney = "0";
                            }
                            else
                            {
                                dishes.DishesMoney = foodPrice;
                            }

                            dishes.popularity = popularity.Trim();
                            dishes.StoreId = storeInfo.storeId;
                            var dishesPictureNode = dishNode.SelectSingleNode(
                                ".//div[@class='abs_out pt10']/div[@class='fix rel']/div[@class='pct50 l']/a[@class='g3']/img");
                            if (dishesPictureNode != null)
                            {
                                var dishesPicturePath = dishesPictureNode.Attributes["src"].Value;
                                if (!string.IsNullOrEmpty(dishesPicturePath) && !dishesPicturePath.EndsWith("food_nopic.png"))
                                {
                                    var storePicture = new Maticsoft.Model.StorePicture();
                                    storePicture.PID = Guid.NewGuid().ToString();
                                    var pictureName = string.Format("{0}.jpg", storePicture.PID);
                                    storePicture.PictureName = pictureName;
                                    storePicture.PicType = dishesPath;
                                    storePicture.PicturePath = dishesPicturePath;
                                    storePicture.StoreId = storeInfo.storeId;
                                    storePictureBll.Add(storePicture);

                                    dishes.PictureName = pictureName;
                                    //try
                                    //{
                                    //    db.DownFile(dishesPicturePath.Replace(@"/300_200/", "/").Replace("/320_0/", "/"), string.Format(newPath, storeInfo.StoreName, dishesPath, dishes.PictureName));
                                    //}
                                    //catch (Exception)
                                    //{
                                    //}
                                    //try
                                    //{
                                    //    db.DownFile(dishesPicturePath, newPathThumb + dishes.PictureName);
                                    //}
                                    //catch (Exception)
                                    //{

                                    //    throw;
                                    //}
                                    //var storeInfopath = string.Format(newPath, storeInfo.StoreName, dishesPath, dishes.PictureName);
                                    //db.ZoomAuto(newPathThumb + dishes.PictureName, foodThumbnail + dishes.PictureName, 320, 240);//生成缩略图
                                    //db.ZoomAuto(newPathThumb + dishes.PictureName, foodThumbnail + "640_480_" + dishes.PictureName, 640, 480);//生成缩略图
                                    //db.ZoomAuto(newPathThumb + dishes.PictureName, foodThumbnail + "320_240_" + dishes.PictureName, 320, 240);//生成缩略图
                                    //db.ZoomAuto(newPathThumb + dishes.PictureName, foodThumbnail + "160_120_" + dishes.PictureName, 160, 120);//生成缩略图

                                }
                            }
                            dishesBll.Add(dishes);
                        }
                    }
                }

            }
            catch (Exception e)
            {
            }
        }