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

68 lines
2.1 KiB
C#
Raw Normal View History

2023-11-09 23:34:52 +03:00
using System.Text.RegularExpressions;
namespace RhSolutions.Services;
2023-11-02 23:07:06 +03:00
public class CouplingsCalculator : IFittingsCalculator
{
2023-11-09 23:34:52 +03:00
private static readonly string pattern =
@"(^|\W)труба.*(?'Diameter'16|20|25|32|40|50|63).*(отрезки|бухт[ае])\D*(?'Length'\d{1,3})(\D|$)";
2023-11-02 23:07:06 +03:00
public Dictionary<Product, double> Calculate(Dictionary<Product, double> products)
{
2023-11-09 23:34:52 +03:00
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;
}
2023-11-02 23:07:06 +03:00
}
}