Example #1
0
        public void Create()
        {
            TransactionHelper.BeginTransaction();
            Specification sp = new Specification();

            sp.Name       = this.Name;
            sp.remark     = this.remark;
            sp.Type       = this.Type;
            sp.showType   = this.showType;
            sp.submitTime = DateTime.Now;
            sp.selectType = selectType;
            specificationService.Insert(sp);

            //获取插入的自增ID
            var s   = specificationService.QueryIdentity().ToString();
            var arr = this.value.Split(',');

            for (int i = 0; i < arr.Length; i++)
            {
                SpecificationDetail spdetail = new SpecificationDetail();
                spdetail.specificationId = Convert.ToInt32(s);
                spdetail.describe        = arr[i].Split('/')[1];
                spdetail.value           = arr[i].Split('/')[0];

                spdetail.submitTime = DateTime.Now;
                specificationDetailService.Insert(spdetail);
            }
            TransactionHelper.Commit();
        }
        public void PopulateMobileSpecifications(bool evenId)
        {
            Settings.AttachToBrowserTimeOut = 240;
            Settings.WaitUntilExistsTimeOut = 240;
            Settings.WaitForCompleteTimeOut = 240;
            Database.SetInitializer(new CreateDatabaseIfNotExists <MobilesDbContext>());

            using (var dbContext = new MobilesDbContext())
            {
                try
                {
                    List <Product> dbProducts = dbContext.Products.Where(p => p.IsRead == false && ((p.Id % 2) == 0) == evenId).ToList();
                    foreach (var dbProduct in dbProducts)
                    {
                        // KillIeProcesses();
                        string url = dbProduct.Url;

                        using (var browser = new IE(url, true))
                        {
                            //browser.ShowWindow(NativeMethods.WindowShowStyle.ForceMinimized);
                            //browser.WaitForComplete();

                            Div specificationsDiv = browser.Div(Find.ById("specs-list"));
                            var productDetails    = new ProductDetails();

                            foreach (Table table in specificationsDiv.Tables)
                            {
                                var     specification         = new Specification();
                                Element specificationTypeCell =
                                    table.TableRows.First().Children().Where(c => c.TagName.ToUpper() == "TH").First();
                                if (specificationTypeCell != null && specificationTypeCell.Exists)
                                {
                                    specification.Type = specificationTypeCell.Text;
                                }

                                foreach (TableRow tableRow in table.TableRows)
                                {
                                    if (tableRow.TableCells.Count > 1)
                                    {
                                        TableCell keyCell   = tableRow.TableCells.FirstOrDefault();
                                        TableCell valueCell = tableRow.TableCells.LastOrDefault();

                                        if (keyCell != null && keyCell.Exists && valueCell != null && valueCell.Exists)
                                        {
                                            if (keyCell.Links.Count > 0)
                                            {
                                                var specDetail = new SpecificationDetail();
                                                specDetail.Key   = keyCell.Links.First().Text;
                                                specDetail.Value = valueCell.Text;
                                                specification.Details.Add(specDetail);
                                            }
                                            else if (specification.Details.Count > 0)
                                            {
                                                specification.Details.Last().Value = string.Format("{0}{1}",
                                                                                                   specification.Details
                                                                                                   .Last().
                                                                                                   Value,
                                                                                                   valueCell.Text);
                                            }
                                        }
                                    }
                                }

                                productDetails.Specifications.Add(specification);
                                dbProduct.IsRead = true;
                            }

                            GenerateProductDetails(dbProduct, productDetails, dbContext);
                            dbContext.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    Console.ReadLine();
                    //throw ex;
                }
            }
        }