diff options
author | Sergey Chebotar <s.chebotar@gmail.com> | 2023-03-22 08:36:13 +0300 |
---|---|---|
committer | Sergey Chebotar <s.chebotar@gmail.com> | 2023-03-22 08:36:13 +0300 |
commit | 4f448f203471a19a1444555a895f503515ad2fda (patch) | |
tree | 301b7df2561d650e4d300d24341342bd77ac7f3e /src/Models | |
parent | 6484cac4f025bd84f7ffa2b2030d729dafe09d3d (diff) |
Add basic test
Diffstat (limited to 'src/Models')
-rw-r--r-- | src/Models/Dialog.cs | 40 | ||||
-rw-r--r-- | src/Models/PriceListBase.cs | 15 | ||||
-rw-r--r-- | src/Models/PriceListHeaders.cs | 11 | ||||
-rw-r--r-- | src/Models/Product.cs | 35 | ||||
-rw-r--r-- | src/Models/ProgressBar.cs | 22 | ||||
-rw-r--r-- | src/Models/ResultBar.cs | 45 | ||||
-rw-r--r-- | src/Models/SourcePriceList.cs | 116 | ||||
-rw-r--r-- | src/Models/StatusbarBase.cs | 18 | ||||
-rw-r--r-- | src/Models/TargetPriceList.cs | 41 | ||||
-rw-r--r-- | src/Models/WorksheetExtensions.cs | 41 |
10 files changed, 0 insertions, 384 deletions
diff --git a/src/Models/Dialog.cs b/src/Models/Dialog.cs deleted file mode 100644 index abc89b8..0000000 --- a/src/Models/Dialog.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Microsoft.Office.Interop.Excel; -using System.Collections.Generic; -using System.Windows.Forms; - -namespace RhSolutions.Models -{ - static class Dialog - { - public static string GetFilePath() - { - using (OpenFileDialog dialog = new OpenFileDialog()) - { - dialog.Filter = "Файлы Excel (*.xls;*.xlsx;*.xlsm)|*.xls;*.xlsx;*.xlsm"; - - if (dialog.ShowDialog() == DialogResult.OK) - { - return dialog.FileName; - } - - else return string.Empty; - } - } - - public static string[] GetMultiplyFiles() - { - using (OpenFileDialog dialog = new OpenFileDialog()) - { - dialog.Filter = "Файлы Excel (*.xls;*.xlsx;*.xlsm)|*.xls;*.xlsx;*.xlsm"; - dialog.Multiselect = true; - - if (dialog.ShowDialog() == DialogResult.OK) - { - return dialog.FileNames; - } - - else return null; - } - } - } -} diff --git a/src/Models/PriceListBase.cs b/src/Models/PriceListBase.cs deleted file mode 100644 index 95f385d..0000000 --- a/src/Models/PriceListBase.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Microsoft.Office.Interop.Excel; - -namespace RhSolutions.Models -{ - internal abstract class PriceListBase - { - public Range AmountCell { get; protected set; } - public Range SkuCell { get; protected set; } - public Range GroupCell { get; protected set; } - public Range NameCell { get; protected set; } - - public Worksheet Sheet { get; protected set; } - public string Name { get; protected set; } - } -}
\ No newline at end of file diff --git a/src/Models/PriceListHeaders.cs b/src/Models/PriceListHeaders.cs deleted file mode 100644 index c9a8370..0000000 --- a/src/Models/PriceListHeaders.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace RhSolutions.Models -{ - internal static class PriceListHeaders - { - public static readonly string Amount = "Кол-во"; - public static readonly string OldSku = "Прежний материал"; - public static readonly string Sku = "Актуальный материал"; - public static readonly string Group = "Программа"; - public static readonly string Name = "Наименование"; - } -}
\ No newline at end of file diff --git a/src/Models/Product.cs b/src/Models/Product.cs deleted file mode 100644 index 2f092d1..0000000 --- a/src/Models/Product.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Linq; - -namespace RhSolutions.Models -{ - public class Product - { - public string ProductLine { get; set; } - public string ProductSku { get; set; } - public string Name { get; set; } - - public override bool Equals(object obj) - { - if (obj as Product == null) - return false; - - Product other = obj as Product; - - return ProductLine == other.ProductLine && - ProductSku == other.ProductSku && - Name == other.Name; - } - - public override int GetHashCode() - { - string[] properties = new[] - { - ProductLine, - ProductSku, - Name - }; - - return string.Concat(properties.Where(p => p != null)).GetHashCode(); - } - } -}
\ No newline at end of file diff --git a/src/Models/ProgressBar.cs b/src/Models/ProgressBar.cs deleted file mode 100644 index 82012e5..0000000 --- a/src/Models/ProgressBar.cs +++ /dev/null @@ -1,22 +0,0 @@ -namespace RhSolutions.Models -{ - internal class ProgressBar : StatusbarBase - { - private double CurrentProgress { get; set; } - private readonly double TaskWeight; - private readonly string Message; - - public ProgressBar(string message, int weight) - { - Message = message; - TaskWeight = weight; - CurrentProgress = 0; - } - - public override void Update() - { - double percent = ++CurrentProgress / TaskWeight * 100; - Excel.StatusBar = $"{Message} Выполнено {percent:#.#} %"; - } - } -} diff --git a/src/Models/ResultBar.cs b/src/Models/ResultBar.cs deleted file mode 100644 index 655540c..0000000 --- a/src/Models/ResultBar.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Text; - -namespace RhSolutions.Models -{ - internal class ResultBar : StatusbarBase - { - private int Success { get; set; } - private int Replaced { get; set; } - private int NotFound { get; set; } - - public ResultBar() - { - Success = 0; - Replaced = 0; - NotFound = 0; - } - - public void IncrementSuccess() => Success++; - public void IncrementReplaced() => Replaced++; - public void IncrementNotFound() => NotFound++; - - public override void Update() - { - StringBuilder sb = new StringBuilder(); - - if (Success > 0) - { - sb.Append($"Успешно экспортировано {Success} артикулов. "); - } - - if (Replaced > 0) - { - sb.Append($"Заменено {Replaced} артикулов. "); - } - - if (NotFound > 0) - { - sb.Append($"Не найдено {NotFound} артикулов."); - } - - Excel.StatusBar = sb.ToString(); - } - } -} diff --git a/src/Models/SourcePriceList.cs b/src/Models/SourcePriceList.cs deleted file mode 100644 index dc950eb..0000000 --- a/src/Models/SourcePriceList.cs +++ /dev/null @@ -1,116 +0,0 @@ -using ExcelDna.Integration; -using Microsoft.Office.Interop.Excel; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using Range = Microsoft.Office.Interop.Excel.Range; - -namespace RhSolutions.Models -{ - internal class SourcePriceList : PriceListBase - { - public Dictionary<Product, double> PositionAmount { get; private set; } - - public SourcePriceList(Workbook workbook) - { - if (workbook == null) - { - throw new ArgumentException($"Нет рабочего файла"); - } - - Sheet = workbook.ActiveSheet; - Name = Path.GetFileNameWithoutExtension(workbook.FullName); - - Range[] cells = new[] - { - AmountCell = Sheet.Cells.Find(PriceListHeaders.Amount), - SkuCell = Sheet.Cells.Find(PriceListHeaders.Sku), - GroupCell = Sheet.Cells.Find(PriceListHeaders.Group), - NameCell = Sheet.Cells.Find(PriceListHeaders.Name) - }; - - if (cells.Any(x => x == null)) - { - throw new ArgumentException($"Файл {Name} не распознан"); - } - - CreatePositionsDict(); - } - - public static List<SourcePriceList> GetSourceLists(string[] files) - { - var ExcelApp = (Application)ExcelDnaUtil.Application; - ProgressBar bar = new ProgressBar("Открываю исходные файлы...", files.Length); - - List<SourcePriceList> sourceFiles = new List<SourcePriceList>(); - - foreach (string file in files) - { - ExcelApp.ScreenUpdating = false; - Workbook wb = ExcelApp.Workbooks.Open(file); - try - { - SourcePriceList priceList = new SourcePriceList(wb); - sourceFiles.Add(priceList); - wb.Close(); - bar.Update(); - } - catch (Exception ex) - { - System.Windows.Forms.MessageBox.Show - (ex.Message, - "Ошибка открытия исходного прайс-листа", - System.Windows.Forms.MessageBoxButtons.OK, - System.Windows.Forms.MessageBoxIcon.Information); - wb.Close(); - bar.Update(); - } - ExcelApp.ScreenUpdating = true; - } - - return sourceFiles; - } - - private void CreatePositionsDict() - { - PositionAmount = new Dictionary<Product, double>(); - - for (int row = AmountCell.Row + 1; row <= Sheet.Cells[Sheet.Rows.Count, AmountCell.Column].End[XlDirection.xlUp].Row; row++) - { - double? amount = Sheet.Cells[row, AmountCell.Column].Value2 as double?; - - if (amount != null && amount.Value != 0) - { - object group = Sheet.Cells[row, GroupCell.Column].Value2; - object name = Sheet.Cells[row, NameCell.Column].Value2; - object sku = Sheet.Cells[row, SkuCell.Column].Value2; - - if (group == null || name == null || sku == null) - continue; - - if (!Sku.TryParse(sku.ToString(), out _)) - continue; - - Product p = new Product - { - ProductSku = sku.ToString(), - ProductLine = group.ToString(), - Name = name.ToString() - }; - - if (PositionAmount.ContainsKey(p)) - { - PositionAmount[p] += amount.Value; - } - - else - { - PositionAmount.Add(p, amount.Value); - } - } - } - } - } -} - diff --git a/src/Models/StatusbarBase.cs b/src/Models/StatusbarBase.cs deleted file mode 100644 index fbec70e..0000000 --- a/src/Models/StatusbarBase.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Microsoft.Office.Interop.Excel; -using RhSolutions.AddIn; -using System; - -namespace RhSolutions.Models -{ - internal abstract class StatusbarBase : IDisposable - { - protected Application Excel = RhSolutionsAddIn.Excel; - - public abstract void Update(); - - public void Dispose() - { - Excel.OnTime(DateTime.Now + new TimeSpan(0, 0, 5), "_ResetStatusBar"); - } - } -} diff --git a/src/Models/TargetPriceList.cs b/src/Models/TargetPriceList.cs deleted file mode 100644 index 2dcd48e..0000000 --- a/src/Models/TargetPriceList.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Microsoft.Office.Interop.Excel; -using System; -using System.IO; -using System.Linq; -using Range = Microsoft.Office.Interop.Excel.Range; - -namespace RhSolutions.Models -{ - internal class TargetPriceList : PriceListBase - { - public Range OldSkuCell { get; private set; } - - public TargetPriceList(Workbook workbook) - { - if (workbook == null) - { - throw new ArgumentException("Невозможно открыть книгу шаблонного файла. " + - "Возможно открыт файл с именем, совпадающим с именем шаблонного файла."); - } - - Sheet = workbook.ActiveSheet; - Name = Path.GetFileNameWithoutExtension(workbook.FullName); - - Range[] cells = new[] - { - AmountCell = Sheet.Cells.Find(PriceListHeaders.Amount), - SkuCell = Sheet.Cells.Find(PriceListHeaders.Sku), - GroupCell = Sheet.Cells.Find(PriceListHeaders.Group), - NameCell = Sheet.Cells.Find(PriceListHeaders.Name) - }; - - OldSkuCell = Sheet.Cells.Find(PriceListHeaders.OldSku); - - if (cells.Any(x => x == null)) - { - throw new ArgumentException($"Шаблон {Name} не является прайс-листом"); - } - } - } -} - diff --git a/src/Models/WorksheetExtensions.cs b/src/Models/WorksheetExtensions.cs deleted file mode 100644 index 6b5fc3e..0000000 --- a/src/Models/WorksheetExtensions.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Microsoft.Office.Interop.Excel; -using RhSolutions.Models; -using System.Linq; - -namespace RhSolutions.Services -{ - public static class WorksheetExtensions - { - public static bool IsRehauSource(this Worksheet worksheet) - { - Range amountCell; - Range skuCell; - Range groupCell; - Range nameCell; - - Range[] cells = new[] - { - amountCell = worksheet.Cells.Find(PriceListHeaders.Amount), - skuCell = worksheet.Cells.Find(PriceListHeaders.Sku), - groupCell = worksheet.Cells.Find(PriceListHeaders.Group), - nameCell = worksheet.Cells.Find(PriceListHeaders.Name) - }; - - return cells.All(x => x != null); - } - - public static void AddValue(this Range range, double value) - { - if (range.Value2 == null) - { - range.Value2 = value; - } - - else - { - range.Value2 += value; - } - } - } -} - |