RhSolutions-AddIn/RhSolutions.AddIn/Services/SleevesCalculator.cs
2023-06-21 07:11:31 +03:00

64 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Text.RegularExpressions;
namespace RhSolutions.Services;
public class SleevesCalculator : ISleevesCalculator
{
private const string doublePattern =
@"((?i)равнопроходная|угольник 90|угольник 45|Т-образная|Комплект трубок(?i))(.+?(?<Sleeve>\b16\b|\b20\b|\b25\b|\b32\b|\b40\b|\b50\b|\b63\b))+";
private const string singlePattern =
@"((?i)муфта|тройник|переходник|угольник|штуцер|Г-образная|заглушка(?i))(.+?(?<Sleeve>\b16\b|\b20\b|\b25\b|\b32\b|\b40\b|\b50\b|\b63\b))+";
public Dictionary<Product, double> CalculateSleeves(Dictionary<Product, double> products)
{
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"));
foreach (var kvp in rautitanProducts)
{
var doubleCollection = Regex.Matches(kvp.Key.Name, doublePattern);
if (doubleCollection.Count != 0)
{
CaptureCollection collection = doubleCollection[0].Groups["Sleeve"].Captures;
foreach (Capture sleeve in collection)
{
result[sleeve.Value] += kvp.Value * 2;
}
continue;
}
var singleCollection = Regex.Matches(kvp.Key.Name, singlePattern);
if (singleCollection.Count != 0)
{
CaptureCollection collection = singleCollection[0].Groups["Sleeve"].Captures;
foreach (Capture sleeve in collection)
{
result[sleeve.Value] += kvp.Value;
}
}
}
return result
.ToDictionary(kvp =>
kvp.Key switch
{
"16" => new Product("11600011001"),
"20" => new Product("11600021001"),
"25" => new Product("11600031001"),
"32" => new Product("11600041001"),
"40" => new Product("11600051001"),
"50" => new Product("11397711002"),
"63" => new Product("11397811002"),
_ => throw new Exception($"Неизвестный диаметр {kvp.Key}")
}, kvp => kvp.Value);
}
}