RhSolutions-AddIn/RhSolutions.ProductSku/ProductSku.cs

195 lines
3.7 KiB
C#
Raw Normal View History

2023-12-08 23:14:56 +03:00
using System.Text.RegularExpressions;
using Newtonsoft.Json;
namespace RhSolutions.Models;
public class ProductSku
{
[JsonIgnore]
public string Id
{
get
{
return $"1{Article}{Delimiter}{Variant}";
}
set
{
var matches = GetMatches(value);
if (matches.Count == 0)
{
throw new ArgumentException($"Wrong Id: {value}");
}
else
{
var p = GetProducts(matches).First();
_article = p.Article;
_delimiter = p.Delimiter;
_variant = p.Variant;
}
}
}
private const string matchPattern = @"(?<Lead>[1\s]|^|\b)(?<Article>\d{6})(?<Delimiter>[\s13-])(?<Variant>\d{3})(\b|$)";
private string _article;
private string _variant;
private char _delimiter = '1';
[JsonConstructor]
public ProductSku(string article, string delimiter, string variant)
{
_article = article;
_delimiter = delimiter[0];
_variant = variant;
}
public ProductSku(string article, string variant)
{
_article = IsCorrectArticle(article) ? article
: throw new ArgumentException($"Wrong Article: {article}");
_variant = IsCorrectVariant(variant) ? variant
: throw new ArgumentException($"Wrong Variant: {variant}");
}
public ProductSku(string line)
{
var matches = GetMatches(line);
if (matches.Count == 0)
{
throw new ArgumentException($"Wrong new Sku input {line}");
}
else
{
var p = GetProducts(matches).First();
_article = p.Article;
_delimiter = p.Delimiter;
_variant = p.Variant;
}
}
private ProductSku(Match match)
{
_article = match.Groups["Article"].Value;
_delimiter = match.Groups["Delimiter"].Value switch
{
"3" => '3',
_ => '1'
};
_variant = match.Groups["Variant"].Value;
}
public string Article
{
get => _article;
set
{
_article = IsCorrectArticle(value) ? value
: throw new ArgumentException($"Wrong Article: {value}");
}
}
public string Variant
{
get => _variant;
set
{
_variant = IsCorrectVariant(value) ? value
: throw new ArgumentException($"Wrong Variant: {value}");
}
}
public char Delimiter
{
get => _delimiter;
set
{
if (value != '1' || value != '3')
{
throw new ArgumentException($"Wrong Delimiter: {value}");
}
else
{
_delimiter = value;
}
}
}
public static IEnumerable<ProductSku> Parse(string line)
{
MatchCollection matches = GetMatches(line);
if (matches.Count == 0)
{
return Enumerable.Empty<ProductSku>();
}
else
{
return GetProducts(matches);
}
}
public static bool TryParse(string line, out IEnumerable<ProductSku> skus)
{
MatchCollection matches = GetMatches(line);
if (matches.Count == 0)
{
skus = Enumerable.Empty<ProductSku>();
return false;
}
else
{
skus = GetProducts(matches);
return true;
}
}
private static MatchCollection GetMatches(string line)
{
return Regex.Matches(line, matchPattern);
}
private static IEnumerable<ProductSku> GetProducts(MatchCollection matches)
{
foreach (Match match in matches)
{
yield return new ProductSku(match);
}
}
private static bool IsCorrectArticle(string line)
{
return line != null
&& line.Length == 6
&& line.All(c => char.IsDigit(c));
}
private static bool IsCorrectVariant(string line)
{
return line != null
&& line.Length == 3
&& line.All(c => char.IsDigit(c));
}
public override string ToString()
{
return Id;
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
ProductSku other = (ProductSku)obj;
return this.Article == other.Article &&
this.Delimiter == other.Delimiter &&
this.Variant == other.Variant;
}
public override int GetHashCode()
{
HashCode hash = new();
hash.Add(_article);
hash.Add(_variant);
hash.Add(_delimiter);
return hash.ToHashCode();
}
}