protected void btnAction_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(lblError.Text)) { return; } divAction.Visible = false; choseDiv.Visible = false; if (File.Exists(_fullPath)) { try { if (!ImportStatistic.IsRun) { _hasHeadrs = Request["hasheadrs"] == "true"; ImportStatistic.Init(); ImportStatistic.IsRun = true; linkCancel.Visible = true; MsgErr(true); lblRes.Text = string.Empty; var tr = new Thread(ProcessCsv); ImportStatistic.ThreadImport = tr; tr.Start(); OutDiv.Visible = true; } } catch (Exception ex) { Debug.LogError(ex); } } }
//public static void SubParseAndCreateCategory(string strCategory, string artNo) //{ // // // // strCategory "[Техника >> Игровые приставки >> PlayStation 3];[....]" // // // foreach (string strT in strCategory.Split(new[] { ';' })) // { // var st = strT; // st = st.Replace("[", ""); // st = st.Replace("]", ""); // int parentId = 0; // string[] temp = st.Split(new[] { ">>" }, StringSplitOptions.RemoveEmptyEntries); // for (int i = 0; i <= temp.Length - 1; i++) // { // string str = temp[i].Trim(); // if (!string.IsNullOrEmpty(str)) // { // var cat = CategoryService.GetChildCategoryIDByName(parentId, str); // if (cat != 0) // parentId = cat; // else // { // parentId = CategoryService.AddCategory(new Category // { // Name = str, // ParentCategoryId = parentId, // Picture = string.Empty, // SortOrder = 0, // Enabled = true // }, false); // } // } // if (i == temp.Length - 1) // { // var tempProductId = ProductService.GetProductIdByArtNo(artNo); // if (tempProductId != -1) // ProductService.AddProductLink(tempProductId, parentId); // } // } // } //} protected void linkCancel_Click(object sender, EventArgs e) { if (ImportStatistic.ThreadImport.IsAlive) { //ImportStatistic.ThreadImport.Abort(); ImportStatistic.IsRun = false; ImportStatistic.Init(); hlDownloadImportLog.Attributes.CssStyle["display"] = "inline"; } }
protected void btnLoad_Click(object sender, EventArgs e) { try { if (!FileUpload1.HasFile) { MsgErr(Resource.Admin_ImportXLS_ChooseFile); return; } if (!ImportStatistic.IsRun) { ImportStatistic.Init(); ImportStatistic.IsRun = true; linkCancel.Visible = true; MsgErr(true); lblRes.Text = string.Empty; bool boolSuccess = true; // Files try { FileHelpers.CreateDirectory(_filePath); FileHelpers.DeleteFile(_fullPath); } catch (Exception ex) { MsgErr(ex.Message + " at Files"); boolSuccess = false; } if (!boolSuccess) { return; } pUploadExcel.Visible = false; // Save file FileUpload1.SaveAs(_fullPath); var tr = new Thread(ProcessExcel); ImportStatistic.ThreadImport = tr; tr.Start(); pUploadExcel.Visible = false; OutDiv.Visible = true; } } catch (Exception ex) { Debug.LogError(ex); } }
private void ProcessCsv(string fullPath) { ImportStatistic.Init(); ImportStatistic.IsRun = true; var fieldMapping = new Dictionary <string, int>(); using (var csv = new CsvHelper.CsvReader(new StreamReader(fullPath, Encoding.UTF8))) { csv.Configuration.Delimiter = ';'; csv.Configuration.HasHeaderRecord = false; csv.Read(); for (int i = 0; i < csv.CurrentRecord.Length; i++) { if (csv.CurrentRecord[i] == ProductFields.GetStringNameByEnum(ProductFields.Fields.None)) { continue; } if (!fieldMapping.ContainsKey(csv.CurrentRecord[i])) { fieldMapping.Add(csv.CurrentRecord[i], i); } } } using (var csv = new CsvHelper.CsvReader(new StreamReader(fullPath, Encoding.UTF8))) { csv.Configuration.Delimiter = ';'; csv.Configuration.HasHeaderRecord = true; while (csv.Read()) { if (!ImportStatistic.IsRun) { csv.Dispose(); FileHelpers.DeleteFile(fullPath); return; } ImportStatistic.RowPosition++; try { // Step by rows var productInStrings = new Dictionary <ProductFields.Fields, string>(); string nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Sku); if (fieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Sku, Convert.ToString(csv[fieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Name).Trim('*'); if (fieldMapping.ContainsKey(nameField)) { var name = Convert.ToString(csv[fieldMapping[nameField]]); if (!string.IsNullOrEmpty(name)) { productInStrings.Add(ProductFields.Fields.Name, name); } else { Log(string.Format(Resource.Admin_ImportCsv_CanNotEmpty, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Name), ImportStatistic.RowPosition + 2), "InvalidData"); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Enabled).Trim('*'); if (fieldMapping.ContainsKey(nameField)) { string enabled = Convert.ToString(csv[fieldMapping[nameField]]); productInStrings.Add(ProductFields.Fields.Enabled, enabled); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Discount); if (fieldMapping.ContainsKey(nameField)) { string discount = Convert.ToString(csv[fieldMapping[nameField]]); if (string.IsNullOrEmpty(discount)) { discount = "0"; } decimal tmp; if (decimal.TryParse(discount, out tmp)) { productInStrings.Add(ProductFields.Fields.Discount, tmp.ToString()); } else if (decimal.TryParse(discount, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.Discount, tmp.ToString()); } else { Log(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Discount), ImportStatistic.RowPosition + 2), "InvalidData"); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Weight); if (fieldMapping.ContainsKey(nameField)) { string weight = Convert.ToString(csv[fieldMapping[nameField]]); if (string.IsNullOrEmpty(weight)) { weight = "0"; } decimal tmp; if (decimal.TryParse(weight, out tmp)) { productInStrings.Add(ProductFields.Fields.Weight, tmp.ToString()); } else if (decimal.TryParse(weight, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.Weight, tmp.ToString()); } else { Log(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Weight), ImportStatistic.RowPosition + 2), "InvalidData"); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Size); if (fieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Size, Convert.ToString(csv[fieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.BriefDescription); if (fieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.BriefDescription, Convert.ToString(csv[fieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Description); if (fieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Description, Convert.ToString(csv[fieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Price).Trim('*'); if (fieldMapping.ContainsKey(nameField)) { string price = Convert.ToString(csv[fieldMapping[nameField]]); if (string.IsNullOrEmpty(price)) { price = "0"; } decimal tmp; if (decimal.TryParse(price, out tmp)) { productInStrings.Add(ProductFields.Fields.Price, tmp.ToString()); } else if (decimal.TryParse(price, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.Price, tmp.ToString()); } else { Log(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Price), ImportStatistic.RowPosition + 2), "InvalidData"); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.PurchasePrice); if (fieldMapping.ContainsKey(nameField)) { string sypplyprice = Convert.ToString(csv[fieldMapping[nameField]]); if (string.IsNullOrEmpty(sypplyprice)) { sypplyprice = "0"; } decimal tmp; if (decimal.TryParse(sypplyprice, out tmp)) { productInStrings.Add(ProductFields.Fields.PurchasePrice, tmp.ToString()); } else if (decimal.TryParse(sypplyprice, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.PurchasePrice, tmp.ToString()); } else { Log(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.PurchasePrice), ImportStatistic.RowPosition + 2), "InvalidData"); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.ShippingPrice); if (fieldMapping.ContainsKey(nameField)) { string shippingPrice = Convert.ToString(csv[fieldMapping[nameField]]); if (string.IsNullOrEmpty(shippingPrice)) { shippingPrice = "0"; } decimal tmp; if (decimal.TryParse(shippingPrice, out tmp)) { productInStrings.Add(ProductFields.Fields.ShippingPrice, tmp.ToString()); } else if (decimal.TryParse(shippingPrice, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.ShippingPrice, tmp.ToString()); } else { Log(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.ShippingPrice), ImportStatistic.RowPosition + 2), "InvalidData"); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Amount); if (fieldMapping.ContainsKey(nameField)) { string amount = Convert.ToString(csv[fieldMapping[nameField]]); if (string.IsNullOrEmpty(amount)) { amount = "0"; } int tmp; if (int.TryParse(amount, out tmp)) { productInStrings.Add(ProductFields.Fields.Amount, amount); } else { Log(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Amount), ImportStatistic.RowPosition + 2), "InvalidData"); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Unit); if (fieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Unit, Convert.ToString(csv[fieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.ParamSynonym); if (fieldMapping.ContainsKey(nameField)) { string rewurl = Convert.ToString(csv[fieldMapping[nameField]]); productInStrings.Add(ProductFields.Fields.ParamSynonym, rewurl); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Title); if (fieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Title, Convert.ToString(csv[fieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.MetaKeywords); if (fieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.MetaKeywords, Convert.ToString(csv[fieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.MetaDescription); if (fieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.MetaDescription, Convert.ToString(csv[fieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Photos); if (fieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Photos, Convert.ToString(csv[fieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Markers); if (fieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Markers, Convert.ToString(csv[fieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Properties); if (fieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Properties, Convert.ToString(csv[fieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Producer); if (fieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Producer, Convert.ToString(csv[fieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Category).Trim('*'); if (fieldMapping.ContainsKey(nameField)) { var parentCategory = Convert.ToString(csv[fieldMapping[nameField]]); if (!string.IsNullOrEmpty(parentCategory)) { productInStrings.Add(ProductFields.Fields.Category, parentCategory); } } ImportProduct.UpdateInsertProduct(productInStrings); } catch (Exception ex) { Log(ex.Message, "InvalidData"); } } CategoryService.RecalculateProductsCountManual(); } ImportStatistic.IsRun = false; FileHelpers.DeleteFile(fullPath); }