Exemplo n.º 1
0
        private Task Export()
        {
            return(Task.Run(() =>
            {
                using (AmazonspiderDbContext context = new AmazonspiderDbContext())
                {
                    var list = context.Products.Where(q => q.Status == 1).Take(5000).ToList();
                    int canExportCount = 0;
                    int errorExportCount = 0;

                    if (!System.IO.Directory.Exists(exportImagePath))
                    {
                        Directory.CreateDirectory(exportImagePath);
                    }
                    CSVUtil csvUtin = new CSVUtil(exportFilePath);

                    foreach (var item in list)
                    {
                        var titles = item.Title.Split(' ').ToList();
                        foreach (var title in titles)
                        {
                            var l = Setting.Key.Where(q => q != "").Where(q => title.Trim() == q.Trim()).ToList();
                            if (l.Count > 0)
                            {
                                item.Status = 2;//.Update(model.Id, 2);
                                errorExportCount++;
                                continue;
                            }
                        }

                        var img = context.ProductImages.Where(q => q.Asin == item.Asin).FirstOrDefault();
                        var imgName = "";
                        if (img != null)
                        {
                            item.Status = 2;
                            if (File.Exists(Environment.CurrentDirectory + $"\\Images\\{img.Asin}_{img.Number}.tbi"))
                            {
                                System.IO.File.Copy(Environment.CurrentDirectory + $"\\Images\\{img.Asin}_{img.Number}.tbi", $"{exportImagePath}\\{img.Asin}_{img.Number}.tbi", true);
                                imgName = $"{img.Asin}_{img.Number}";
                            }

                            if (item.Price > 0.01M)
                            {
                                csvUtin.AddPorductInfo(item.Title, item.Price.ToString(), "0", "<SPAN style='COLOR: #ffffff; BACKGROUND-COLOR: #ffffff'>" + item.Asin + "</SPAN> " + descFilter(item.Desc), imgName, "", "", "", "", "");
                                canExportCount++;
                            }
                        }
                        else
                        {
                            errorExportCount++;
                            context.Products.Remove(item);
                        }

                        label2.Invoke(new Action(() =>
                        {
                            label2.Text = $"正在导出导出{list.Count},已导出{canExportCount},失败{errorExportCount}";
                        }));
                    }
                    context.SaveChanges();
                    csvUtin.WriteCSV5();
                    label2.Invoke(new Action(() =>
                    {
                        label2.Text = $"导出成功,一共导出{list.Count},已导出{canExportCount},失败{errorExportCount}";
                        MessageBox.Show("导出成功");
                    }));
                }
            }));
        }