RhSolutions-AddIn/RhSolutions.AddIn/Services/CouplingsCalculator.cs

68 lines
2.1 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 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)
{
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("11080111001"),
"20" => new Product("11080121001"),
"25" => new Product("11080131001"),
"32" => new Product("11080141001"),
"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;
}
}
}