Exemplo n.º 1
0
        //Added by Evgeni
        public static void SaveProductsToCsvAutospace(string path, Encodings.EncodingsEnum encodeType, Separators.SeparatorsEnum delimetr)
        {
            ImportStatistic.WriteLog("Start working on SaveProductsToCsvAutospace. DateTime: " + DateTime.Now);

            using (var writer = new CsvHelper.CsvWriter(new StreamWriter(path, false, Encodings.GetEncoding(encodeType))))
            {
                ImportStatistic.WriteLog("Start working on SaveProductsToCsvAutospace 2.Encoding: " + Encodings.GetEncoding(encodeType) + " DateTime: " + DateTime.Now);

                writer.Configuration.Delimiter = Separators.GetCharSeparator(delimetr);

                //var items = new List<Product>();

                //using (var db = new SQLDataAccess())
                //{
                //    db.cnOpen();
                //    items = (List<Product>)ProductService.GetProductsWhere(db, "ArtNo like '2.607.001.733'");
                //    db.cnClose();
                //}
                var items = ProductService.GetProducts();// .Where(t => t.ArtNo.Contains("2.607.001.733")).ToList();

                List <string> loadedProducts = new List <string>();

                ImportStatistic.WriteLog("Start working on SaveProductsToCsvAutospace 3. DateTime: " + DateTime.Now + "Number of products to proceed: " + items.Count);
                //select only relevants offers
                //  items = items.Where(t => t.Brand.Name.ToLower() == "bosch" || t.Brand.Name.ToLower() == "skil").Where(t => t.Offers.FirstOrDefault().Amount > 0 && t.Offers.FirstOrDefault().Price > 0).ToList();

                if (items == null)
                {
                    return;
                }
                var    regex = new Regex("^[0-9]+$");
                string pcs   = "шт.";
                for (int j = 0; j < items.Count; j++)
                {
                    try
                    {
                        if (j % 1000 == 0)
                        {
                            ImportStatistic.WriteLog("Start working on SaveProductsToCsvAutospace 4. DateTime: " + DateTime.Now
                                                     + " File No " + j);
                        }

                        //check that product was not inserted yet
                        if (loadedProducts.Where(t => t.Contains(items[j].ArtNo)).ToList().Count > 0)
                        {
                            continue;
                        }
                        else
                        {
                            loadedProducts.Add(items[j].ArtNo);
                        }

                        var product = items[j];
                        var offer   = product.Offers.Where(t => t.OfferListId == 15).FirstOrDefault() ?? new Offer {
                            Amount = 0, Price = 0, SupplyPrice = 0, Unit = "", ShippingPrice = 0
                        };                                                                                                                                                                //get only spec price
                        if (offer.OfferListId != 15 || offer.Amount <= 0 || offer.Price <= 0)
                        {
                            continue;
                        }

                        if (product.Brand == null)
                        {
                            if (product.ArtNo.ToLower().StartsWith("f"))
                            {
                                writer.WriteField("Skil");
                            }
                            else
                            {
                                writer.WriteField("Bosch");
                            }
                        }
                        else
                        {
                            writer.WriteField(product.Brand.Name);
                        }

                        writer.WriteField(product.ArtNo);
                        writer.WriteField(product.BriefDescription.Replace(";", "").Replace("&nbsp;", " ").Replace(",", "").Replace("<p>", "").Replace("<p/>", ""));

                        if (product.ProductCategories.Count == 0)
                        {
                            writer.WriteField("Прочее");
                        }
                        else
                        {
                            writer.WriteField(product.ProductCategories.FirstOrDefault().Name.Replace(";", "").Replace("&nbsp;", "").Replace(",", ""));
                        }

                        writer.WriteField(offer.Amount);
                        writer.WriteField(offer.Price.ToString("F2"));
                        writer.WriteField(pcs);
                        writer.WriteField("1");

                        writer.NextRecord();
                    }
                    catch (Exception ex)
                    {
                        ImportStatistic.WriteLog("Error while adding product:" + ex.Message);
                    }
                }

                ImportStatistic.WriteLog("End working on SaveProductsToCsvAutospace 5. DateTime: " + DateTime.Now);
            }
            ImportStatistic.WriteLog("End working on SaveProductsToCsvAutospace 6. DateTime: " + DateTime.Now);
        }
 private static void LogInvalidData(string message)
 {
     ImportStatistic.WriteLog(message);
     ImportStatistic.TotalErrorRow++;
     ImportStatistic.RowPosition++;
 }
Exemplo n.º 3
0
 private static void Log(string message)
 {
     ImportStatistic.WriteLog(message);
 }