using System.Collections.Generic;
using System.Linq;
namespace OpenNest.IO.Bom;
///
/// The rows of one material and thickness; each group becomes one nest.
///
public sealed class BomImportGroup
{
public BomImportGroup(string material, double thickness, IReadOnlyList parts)
{
Material = material;
Thickness = thickness;
Parts = parts;
}
/// The material as the group's first row spells it.
public string Material { get; }
public double Thickness { get; }
public IReadOnlyList Parts { get; }
/// Sum of the rows' quantities.
public int TotalQty => Parts.Sum(p => p.Qty ?? 0);
public string Key => BomImportGroups.Key(Material, Thickness);
}
///
/// Groups importable rows by material and thickness. The Groups tab and
/// Create Nests both use this, so they always agree.
///
public static class BomImportGroups
{
///
/// Returns the groups of rows. Material
/// is compared case-insensitively. Groups are ordered by material, then
/// thickness.
///
public static List Build(IEnumerable rows)
{
return rows.Where(p => p.Status == BomRowStatus.Ready)
.GroupBy(p => new
{
Material = p.Material.ToUpperInvariant(),
Thickness = p.Thickness.Value,
})
.OrderBy(g => g.First().Material)
.ThenBy(g => g.Key.Thickness)
.Select(g => new BomImportGroup(g.First().Material, g.Key.Thickness, g.ToList()))
.ToList();
}
///
/// The key that identifies a group's plate settings across regrouping.
///
public static string Key(string material, double thickness) =>
$"{material?.ToUpperInvariant()}|{thickness}";
}