diff options
author | Serghei Cebotari <serghei@cebotari.ru> | 2023-12-08 23:14:56 +0300 |
---|---|---|
committer | Serghei Cebotari <serghei@cebotari.ru> | 2023-12-08 23:14:56 +0300 |
commit | f448cccbebaf9c6cc92f5576cb89e8918b1beec9 (patch) | |
tree | e319a8e8e0e4f763d852ae66479f71f67311a9cd /RhSolutions.ProductSku | |
parent | e6546254baf8130638aa0dee12f247769da4e308 (diff) |
Add RhSolutions.ProductSku project
Diffstat (limited to 'RhSolutions.ProductSku')
-rw-r--r-- | RhSolutions.ProductSku/Measure.cs | 3 | ||||
-rw-r--r-- | RhSolutions.ProductSku/Product.cs | 91 | ||||
-rw-r--r-- | RhSolutions.ProductSku/ProductSku.cs | 194 | ||||
-rw-r--r-- | RhSolutions.ProductSku/RhSolutions.ProductSku.csproj | 20 |
4 files changed, 308 insertions, 0 deletions
diff --git a/RhSolutions.ProductSku/Measure.cs b/RhSolutions.ProductSku/Measure.cs new file mode 100644 index 0000000..d7911ab --- /dev/null +++ b/RhSolutions.ProductSku/Measure.cs @@ -0,0 +1,3 @@ +namespace RhSolutions.Models; + +public enum Measure { Kg, M, M2, P } diff --git a/RhSolutions.ProductSku/Product.cs b/RhSolutions.ProductSku/Product.cs new file mode 100644 index 0000000..5a6f5fa --- /dev/null +++ b/RhSolutions.ProductSku/Product.cs @@ -0,0 +1,91 @@ +using Newtonsoft.Json; + +namespace RhSolutions.Models; +public class Product : IDisposable +{ + [JsonIgnore] + public string Id + { + get => ProductSku.Id; + set + { + ProductSku = new(value); + } + } + public string Name { get; set; } = string.Empty; + public ProductSku ProductSku { get; set; } + public List<ProductSku> DeprecatedSkus { get; set; } = new(); + public List<string> ProductLines { get; set; } = new(); + public bool IsOnWarehouse { get; set; } = false; + public Measure ProductMeasure { get; set; } + public double? DeliveryMakeUp { get; set; } + public decimal Price { get; set; } + + [JsonConstructor] + public Product(string id, + string name, + ProductSku productSku, + ProductSku[] deprecatedSkus, + string[] productLines, + bool isOnWarehouse, + int productMeasure, + int deliveryMakeUp, + decimal price) + { + Id = id; + Name = name; + ProductSku = productSku; + DeprecatedSkus = deprecatedSkus.ToList(); + ProductLines = productLines.ToList(); + IsOnWarehouse = isOnWarehouse; + ProductMeasure = (Measure)productMeasure; + DeliveryMakeUp = deliveryMakeUp; + Price = price; + } + public Product(string sku) + { + ProductSku = new(sku); + } + + public Product(ProductSku productSku) + { + ProductSku = productSku; + } + + public override bool Equals(object? obj) + { + return obj is Product product && + Name == product.Name && + EqualityComparer<ProductSku>.Default.Equals(ProductSku, product.ProductSku) && + DeprecatedSkus.SequenceEqual(product.DeprecatedSkus) && + ProductLines.SequenceEqual(product.ProductLines) && + IsOnWarehouse == product.IsOnWarehouse && + ProductMeasure == product.ProductMeasure && + DeliveryMakeUp == product.DeliveryMakeUp && + Price == product.Price; + } + + public override int GetHashCode() + { + HashCode hash = new HashCode(); + hash.Add(Name); + hash.Add(ProductSku); + DeprecatedSkus.ForEach(x => hash.Add(x)); + ProductLines.ForEach(x => hash.Add(x)); + hash.Add(IsOnWarehouse); + hash.Add(ProductMeasure); + hash.Add(DeliveryMakeUp); + hash.Add(Price); + return hash.ToHashCode(); + } + + public override string ToString() + { + return $"({ProductSku}) {Name}"; + } + + public void Dispose() + { + + } +} diff --git a/RhSolutions.ProductSku/ProductSku.cs b/RhSolutions.ProductSku/ProductSku.cs new file mode 100644 index 0000000..7e3e763 --- /dev/null +++ b/RhSolutions.ProductSku/ProductSku.cs @@ -0,0 +1,194 @@ +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(); + } +} diff --git a/RhSolutions.ProductSku/RhSolutions.ProductSku.csproj b/RhSolutions.ProductSku/RhSolutions.ProductSku.csproj new file mode 100644 index 0000000..ecf5130 --- /dev/null +++ b/RhSolutions.ProductSku/RhSolutions.ProductSku.csproj @@ -0,0 +1,20 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFrameworks>net472;net6.0;net7.0;net8.0</TargetFrameworks> + <LangVersion>10.0</LangVersion> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + <Version>1.0.3</Version> + <Authors>Serghei Cebotari</Authors> + <Product>RhSolutions Sku</Product> + <Description>Библиотека с классами моделей артикулов для плагинов и приложений RhSolutions</Description> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" /> + <PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> + <PackageReference Include="System.Net.Http" Version="4.3.4" /> + </ItemGroup> + +</Project> |