diff options
author | Serghei Cebotari <serghei@cebotari.ru> | 2023-11-09 23:34:52 +0300 |
---|---|---|
committer | Serghei Cebotari <serghei@cebotari.ru> | 2023-11-09 23:34:52 +0300 |
commit | 9fd1fd8266a304a2e5c5adf522ae0320a6960c19 (patch) | |
tree | 9cbfc63102971f1ce4a7f7d61ad9932c1053947b | |
parent | 1b72c00b1e2c29117616dcd3da83e1fea68b892d (diff) |
Implement Couplings Calculator
-rw-r--r-- | RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs | 7 | ||||
-rw-r--r-- | RhSolutions.AddIn/Controllers/RibbonController.cs | 5 | ||||
-rw-r--r-- | RhSolutions.AddIn/Services/CouplingsCalculator.cs | 62 | ||||
-rw-r--r-- | RhSolutions.AddIn/Services/FittingsCalculatorFactory.cs | 21 | ||||
-rw-r--r-- | RhSolutions.AddIn/Tools/EventsUtil.cs | 1 | ||||
-rw-r--r-- | RhSolutions.AddIn/Tools/FittingsTool.cs | 25 | ||||
-rw-r--r-- | RhSolutions.AddIn/Tools/SleevesTool.cs | 22 | ||||
-rw-r--r-- | RhSolutions.AddIn/Tools/ToolFactory.cs | 5 |
8 files changed, 119 insertions, 29 deletions
diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs index a7f6796..38cea15 100644 --- a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs +++ b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs @@ -25,7 +25,6 @@ public sealed class RhSolutionsAddIn : IExcelAddIn .AddSingleton<IAddInConfiguration, AddInConfiguration>() .AddSingleton<IDatabaseClient, DatabaseClient>() .AddSingleton<ICurrencyClient, CurrencyClient>() - .AddSingleton<IFittingsCalculator, SleevesCalculator>() .AddTransient<IFileDialog, FileDialog>(); Services.AddSingleton<WriterFactory>(); @@ -42,6 +41,12 @@ public sealed class RhSolutionsAddIn : IExcelAddIn Services.AddTransient<GuessReader>() .AddTransient<IReader, GuessReader>(s => s.GetService<GuessReader>()); + Services.AddSingleton<FittingsCalculatorFactory>(); + Services.AddTransient<CouplingsCalculator>() + .AddTransient<IFittingsCalculator, CouplingsCalculator>(s => s.GetService<CouplingsCalculator>()); + Services.AddTransient<SleevesCalculator>() + .AddTransient<IFittingsCalculator, SleevesCalculator>(s => s.GetService<SleevesCalculator>()); + Services.AddSingleton<ToolFactory>(); ServiceProvider = Services.BuildServiceProvider(); diff --git a/RhSolutions.AddIn/Controllers/RibbonController.cs b/RhSolutions.AddIn/Controllers/RibbonController.cs index 6d234e8..deb56b0 100644 --- a/RhSolutions.AddIn/Controllers/RibbonController.cs +++ b/RhSolutions.AddIn/Controllers/RibbonController.cs @@ -29,7 +29,8 @@ public class RibbonController : ExcelRibbon <button id='convert' getEnabled='GetConvertEnabled' label='Актуализировать' size='normal' imageMso='FileUpdate' onAction='OnToolPressed'/> <button id='merge' label='Объединить' size='normal' imageMso='Copy' onAction='OnToolPressed'/> <button id='guess' getEnabled='GetGuessEnabled' label='Найти и экспортировать' size='normal' imageMso='ControlWizards' onAction='OnToolPressed'/> - <button id='fillsleeves' getEnabled='GetSleevesEnabled' label='Подобрать гильзы' size='normal' imageMso='CreateQueryFromWizard' onAction='OnToolPressed'/> + <button id='fillsleeves' getEnabled='GetFittingsCalcEnabled' label='Подобрать гильзы' size='normal' imageMso='CreateQueryFromWizard' onAction='OnToolPressed'/> + <button id='fillcouplings' getEnabled='GetFittingsCalcEnabled' label='Подобрать муфты' size='normal' imageMso='CreateQueryFromWizard' onAction='OnToolPressed'/> <button id='dxfexport' getEnabled='GetDxfEnabled' label='Экспортировать в DXF' size='normal' imageMso='ExportExcel' onAction='OnToolPressed'/> </group> <group id='rausettings' getLabel='GetVersionLabel'> @@ -85,7 +86,7 @@ public class RibbonController : ExcelRibbon public bool GetConvertEnabled(IRibbonControl control) => _workbookIsValid; public bool GetDxfEnabled(IRibbonControl control) => _workbookIsValid; - public bool GetSleevesEnabled(IRibbonControl control) => _workbookIsValid; + public bool GetFittingsCalcEnabled(IRibbonControl control) => _workbookIsValid; public bool GetGuessEnabled(IRibbonControl control) => RhSolutionsAddIn.Excel.ActiveWorkbook != null && !_workbookIsValid; public bool GetExportEnabled(IRibbonControl control) diff --git a/RhSolutions.AddIn/Services/CouplingsCalculator.cs b/RhSolutions.AddIn/Services/CouplingsCalculator.cs index 9ff4a98..d32e026 100644 --- a/RhSolutions.AddIn/Services/CouplingsCalculator.cs +++ b/RhSolutions.AddIn/Services/CouplingsCalculator.cs @@ -1,9 +1,67 @@ -namespace RhSolutions.Services; +using System.Text.RegularExpressions; + +namespace RhSolutions.Services; public class CouplingsCalculator : IFittingsCalculator { + private static readonly string pattern = + @"(^|\W)труба.*(?'Diameter'16|20|25|32|40|50|63).*(отрезки|бухт[ае])\D*(?'Length'\d{1,3})(\D|$)"; + public Dictionary<Product, double> Calculate(Dictionary<Product, double> products) { - throw new NotImplementedException(); + Dictionary<string, double> result = new() + { + ["16"] = 0, + ["20"] = 0, + ["25"] = 0, + ["32"] = 0, + ["40"] = 0, + ["50"] = 0, + ["63"] = 0, + }; + + var rautitanProducts = products.Where(kvp => kvp.Key.ProductLines.Contains("RAUTITAN")); + Regex regex = new(pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase); + + foreach (var kvp in rautitanProducts) + { + var match = regex.Match(kvp.Key.Name); + if (match.Success) + { + string diameter = match.Groups["Diameter"].Value; + int packingLength = int.Parse(match.Groups["Length"].Value); + result[diameter] += GetCouplesCount(kvp.Value, packingLength); + } + } + + return result + .ToDictionary(kvp => + kvp.Key switch + { + "16" => new Product("11600111001"), + "20" => new Product("11600121001"), + "25" => new Product("11600131001"), + "32" => new Product("11600141001"), + "40" => new Product("11600151001"), + "50" => new Product("14563021001"), + "63" => new Product("14563031001"), + _ => throw new Exception($"Неизвестный диаметр {kvp.Key}") + }, kvp => kvp.Value); + } + + private int GetCouplesCount(double amount, int packingLength) + { + if (amount < packingLength) + { + return 0; + } + else if (amount % packingLength == 0) + { + return (int)amount / packingLength - 1; + } + else + { + return (int)amount / packingLength; + } } } diff --git a/RhSolutions.AddIn/Services/FittingsCalculatorFactory.cs b/RhSolutions.AddIn/Services/FittingsCalculatorFactory.cs new file mode 100644 index 0000000..c8a8dbc --- /dev/null +++ b/RhSolutions.AddIn/Services/FittingsCalculatorFactory.cs @@ -0,0 +1,21 @@ +namespace RhSolutions.Services; + +public class FittingsCalculatorFactory +{ + private readonly IServiceProvider _serviceProvider; + + public FittingsCalculatorFactory(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public IFittingsCalculator GetFittingsCalculator(string calculatorName) + { + return calculatorName switch + { + "Sleeves" => (IFittingsCalculator)_serviceProvider.GetService(typeof(SleevesCalculator)), + "Couplings" => (IFittingsCalculator)_serviceProvider.GetService(typeof(CouplingsCalculator)), + _ => throw new ArgumentException($"Незвестный интерфейс {nameof(IFittingsCalculator)}: {calculatorName}") + }; + } +}
\ No newline at end of file diff --git a/RhSolutions.AddIn/Tools/EventsUtil.cs b/RhSolutions.AddIn/Tools/EventsUtil.cs index b32a849..e62de23 100644 --- a/RhSolutions.AddIn/Tools/EventsUtil.cs +++ b/RhSolutions.AddIn/Tools/EventsUtil.cs @@ -36,6 +36,7 @@ namespace RhSolutions.Tools RibbonController.RefreshControl("dxfexport"); RibbonController.RefreshControl("guess"); RibbonController.RefreshControl("fillsleeves"); + RibbonController.RefreshControl("fillcouplings"); } private static void RefreshExportButton(object sh, Range target) diff --git a/RhSolutions.AddIn/Tools/FittingsTool.cs b/RhSolutions.AddIn/Tools/FittingsTool.cs new file mode 100644 index 0000000..806b346 --- /dev/null +++ b/RhSolutions.AddIn/Tools/FittingsTool.cs @@ -0,0 +1,25 @@ +namespace RhSolutions.Tools; + +internal class FittingsTool : Tool +{ + private readonly FittingsCalculatorFactory _factory; + private string _calculatorName; + + public FittingsTool(ReaderFactory readerFactory, WriterFactory writerFactory, FittingsCalculatorFactory calculatorFactory, string calculatorName) : base(readerFactory, writerFactory) + { + _factory = calculatorFactory; + _calculatorName = calculatorName; + } + + public override void Execute() + { + Application app = RhSolutionsAddIn.Excel.Application; + Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; + _reader = _readerFactory.GetReader("Excel"); + var products = _reader.ReadProducts(new[] { worksheet }); + var calculator = _factory.GetFittingsCalculator(_calculatorName); + var fittings = calculator.Calculate(products.Select(p => p.Item2).First()); + _writer = _writerFactory.GetWriter("CurrentPrice"); + _writer.WriteProducts(fittings); + } +} diff --git a/RhSolutions.AddIn/Tools/SleevesTool.cs b/RhSolutions.AddIn/Tools/SleevesTool.cs deleted file mode 100644 index 26fa0ee..0000000 --- a/RhSolutions.AddIn/Tools/SleevesTool.cs +++ /dev/null @@ -1,22 +0,0 @@ -namespace RhSolutions.Tools; - -internal class SleevesTool : Tool -{ - private readonly IFittingsCalculator _sleevesCaluculator; - - public SleevesTool(ReaderFactory readerFactory, WriterFactory writerFactory, IFittingsCalculator sleevesCaluculator) : base(readerFactory, writerFactory) - { - _sleevesCaluculator = sleevesCaluculator; - } - - public override void Execute() - { - Application app = RhSolutionsAddIn.Excel.Application; - Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; - _reader = _readerFactory.GetReader("Excel"); - var products = _reader.ReadProducts(new[] { worksheet }); - var sleeves = _sleevesCaluculator.Calculate(products.Select(p => p.Item2).First()); - _writer = _writerFactory.GetWriter("CurrentPrice"); - _writer.WriteProducts(sleeves); - } -} diff --git a/RhSolutions.AddIn/Tools/ToolFactory.cs b/RhSolutions.AddIn/Tools/ToolFactory.cs index bdc9bf9..646fc15 100644 --- a/RhSolutions.AddIn/Tools/ToolFactory.cs +++ b/RhSolutions.AddIn/Tools/ToolFactory.cs @@ -4,7 +4,7 @@ internal class ToolFactory { static ReaderFactory readerFactory = RhSolutionsAddIn.ServiceProvider.GetService<ReaderFactory>(); static WriterFactory writerFactory = RhSolutionsAddIn.ServiceProvider.GetService<WriterFactory>(); - static IFittingsCalculator sleevesCaluculator = RhSolutionsAddIn.ServiceProvider.GetService<IFittingsCalculator>(); + static FittingsCalculatorFactory fittingsCalculatorFactory = RhSolutionsAddIn.ServiceProvider.GetService<FittingsCalculatorFactory>(); public Tool GetTool(string toolName) { @@ -15,7 +15,8 @@ internal class ToolFactory "merge" => new MergeTool(readerFactory, writerFactory), "dxfexport" => new DxfTool(readerFactory, writerFactory), "guess" => new GuessTool(readerFactory, writerFactory), - "fillsleeves" => new SleevesTool(readerFactory, writerFactory, sleevesCaluculator), + "fillsleeves" => new FittingsTool(readerFactory, writerFactory, fittingsCalculatorFactory, "Sleeves"), + "fillcouplings" => new FittingsTool(readerFactory, writerFactory, fittingsCalculatorFactory, "Couplings"), _ => throw new Exception($"Неизвестный инструмент {toolName}"), }; return tool; |