ProductStorage.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. 
  2. using qdr.app.studiou.orders2printpack.Extensions;
  3. namespace qdr.app.studiou.orders2printpack.ProductStorage
  4. {
  5. public class ProductStorage
  6. {
  7. private IList<ProductDto> _rawCollection = new List<ProductDto>();
  8. private IList<VariantDto> _variants = new List<VariantDto>();
  9. private IList<CategoryDto> _categories = new List<CategoryDto>();
  10. public IEnumerable<ProductDto> Products => _rawCollection.ToArray();
  11. public IEnumerable<VariantDto> Variants => GetVariants().ToArray();
  12. public IEnumerable<CategoryDto> Categories => GetCategories().ToArray();
  13. public void Load(string filePath, string separator)
  14. {
  15. _rawCollection = CsvParser.Parse<ProductDto>(filePath, delimiter: separator).ToList();
  16. CheckProductVariantValidity();
  17. }
  18. public void Save(string filePath, string separator)
  19. {
  20. CsvParser.Save<ProductDto>(_rawCollection.OrderBy(x=>x.IsVariant).ToArray(), filePath, delimiter:separator);
  21. }
  22. public void CheckProductVariantValidity()
  23. {
  24. foreach (var product in _rawCollection.Where(x => x.IsVariant))
  25. {
  26. product.IsParentValid = _rawCollection.Any(x => string.Equals(x.Sku, product.ParentName, StringComparison.InvariantCultureIgnoreCase));
  27. }
  28. }
  29. public ProductDto? GetProductByIdentifier(string? identifier)
  30. {
  31. if (string.IsNullOrWhiteSpace(identifier))
  32. return null;
  33. return _rawCollection.FirstOrDefault(x => string.Equals(x.GetIdentifier(), identifier, StringComparison.InvariantCultureIgnoreCase));
  34. }
  35. public ProductDto? GetProductBySku(string sku)
  36. {
  37. if (string.IsNullOrWhiteSpace(sku))
  38. return null;
  39. return _rawCollection.FirstOrDefault(x => !x.IsVariant && string.Equals(x.Sku, sku, StringComparison.InvariantCultureIgnoreCase));
  40. }
  41. public ProductDto? GetProductByName(string productName)
  42. {
  43. if (string.IsNullOrWhiteSpace(productName))
  44. return null;
  45. return _rawCollection.FirstOrDefault(x => !x.IsVariant && string.Equals(x.ProductName, productName, StringComparison.InvariantCultureIgnoreCase));
  46. }
  47. public VariantDto? GetVariantByIdentifier(string? identifier)
  48. {
  49. if (identifier==null)
  50. return null;
  51. return Variants.FirstOrDefault(x => string.Equals(x.GetIdentifier(), identifier, StringComparison.InvariantCultureIgnoreCase));
  52. }
  53. public CategoryDto? GetCategoryByIdentifier(string? identifier)
  54. {
  55. if (identifier==null)
  56. return null;
  57. return Categories.FirstOrDefault(x => string.Equals(x.GetIdentifier(), identifier, StringComparison.InvariantCultureIgnoreCase));
  58. }
  59. public void AppendVariantsFromString(string variantsString)
  60. {
  61. var variants = variantsString.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  62. foreach (var variant in variants)
  63. {
  64. var variantParts = variant.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
  65. if (variantParts.Length != 2)
  66. continue;
  67. var variantName = variantParts[0];
  68. var variantPrice = decimal.Parse(variantParts[1]);
  69. if (_variants.Any(x => string.Equals(x.Name,variantName, StringComparison.InvariantCultureIgnoreCase)))
  70. continue;
  71. _variants.Add(new VariantDto()
  72. {
  73. Name = variantName,
  74. Price = variantPrice
  75. });
  76. }
  77. }
  78. public void AppendCategoriesFromString(string categoriesString)
  79. {
  80. var categories = categoriesString.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  81. foreach (var category in categories)
  82. {
  83. if (_categories.Any(x=>string.Equals(x.Name ,categoriesString, StringComparison.InvariantCultureIgnoreCase)))
  84. _categories.Add(new CategoryDto() { Name = category });
  85. }
  86. }
  87. public ProductDto AddNewProduct()
  88. {
  89. var item = new ProductDto() { Type = ProductDto.CS_ITEM_TYPE_PRODUCT };
  90. _rawCollection.Add(item);
  91. return item;
  92. }
  93. public ProductDto AddNewProductVariant(string? parentSku)
  94. {
  95. if (!string.IsNullOrWhiteSpace(parentSku))
  96. if (!_rawCollection.Any(x => string.Equals(x.Sku, parentSku, StringComparison.InvariantCultureIgnoreCase)))
  97. throw new Exception($"Parent Product SKU '{parentSku}' not found!");
  98. var item = new ProductDto() {Type = ProductDto.CS_ITEM_TYPE_VARIANT, ParentName = parentSku == null ? string.Empty : parentSku};
  99. _rawCollection.Add(item);
  100. return item;
  101. }
  102. public VariantDto AddNewVariant()
  103. {
  104. var item = new VariantDto() { };
  105. _variants.Add(item);
  106. return item;
  107. }
  108. public CategoryDto AddNewCategory()
  109. {
  110. var item = new CategoryDto() { };
  111. _categories.Add(item);
  112. return item;
  113. }
  114. public void RemoveProduct(string[]? identifiers)
  115. {
  116. if (identifiers == null || identifiers.Length==0)
  117. return;
  118. foreach (var identifier in identifiers)
  119. {
  120. var itemToRemove = _rawCollection.FirstOrDefault(x => string.Equals(x.GetIdentifier(), identifier, StringComparison.InvariantCultureIgnoreCase));
  121. if (itemToRemove != null)
  122. _rawCollection.Remove(itemToRemove);
  123. }
  124. }
  125. public void RemoveVariants(string[]? identifiers)
  126. {
  127. if (identifiers == null || identifiers.Length==0)
  128. return;
  129. foreach (var identifier in identifiers)
  130. {
  131. var itemToRemove = _variants.FirstOrDefault(x => string.Equals(x.GetIdentifier(), identifier, StringComparison.InvariantCultureIgnoreCase));
  132. if (itemToRemove != null)
  133. _variants.Remove(itemToRemove);
  134. }
  135. }
  136. public void RemoveCategories(string[]? identifiers)
  137. {
  138. if (identifiers == null || identifiers.Length==0)
  139. return;
  140. foreach (var identifier in identifiers)
  141. {
  142. var itemToRemove = _categories.FirstOrDefault(x => string.Equals(x.GetIdentifier(), identifier, StringComparison.InvariantCultureIgnoreCase));
  143. if (itemToRemove != null)
  144. _categories.Remove(itemToRemove);
  145. }
  146. }
  147. public string GetVariantsAsString()
  148. {
  149. return string.Join(";", _variants.Select(x => x.Name + "=" + x.Price.ToString()));
  150. }
  151. public string GetCategoriesAsString()
  152. {
  153. return string.Join(";", _categories.Select(x => x.Name));
  154. }
  155. public IEnumerable<ProductDto> GetProductsView(bool includeProduct, bool includeVariants)
  156. {
  157. return _rawCollection.Where(x => x.IsVariant == includeVariants || !x.IsVariant == includeProduct).ToArray();
  158. }
  159. private IEnumerable<VariantDto> GetVariants()
  160. {
  161. var productVariants = _rawCollection.Where(x => x.IsVariant).DistinctBy(x => x.VariantAndPrice).Select(x => new VariantDto() { Name = x.VariantName, Price = x.Price });
  162. foreach (var variant in productVariants)
  163. {
  164. if (!_variants.Any(x => x.Name == variant.Name))
  165. {
  166. _variants.Add(variant);
  167. }
  168. else
  169. {
  170. if (_variants.First(x => x.Name == variant.Name).Price != variant.Price)
  171. _variants.First(x => x.Name == variant.Name).Price = variant.Price;
  172. }
  173. }
  174. return _variants;
  175. }
  176. private IEnumerable<CategoryDto> GetCategories()
  177. {
  178. var productCategories = _rawCollection.Where(x=>!x.IsVariant).DistinctBy(x=>x.Category).Select(x => new CategoryDto() { Name = x.Category });
  179. foreach (var category in productCategories)
  180. {
  181. if (!_categories.Any(x => x.Name == category.Name))
  182. {
  183. _categories.Add(category);
  184. }
  185. }
  186. return _categories;
  187. }
  188. }
  189. }