ProductStorage.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 variantValues = variantParts[1].Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  69. var variantPrice = decimal.Parse(variantValues[0]);
  70. var variantMinimal = 0;
  71. var variantMaximal = 0;
  72. if (variantValues.Length >= 3)
  73. {
  74. variantMinimal = int.Parse(variantValues[1]);
  75. variantMaximal = int.Parse(variantValues[2]);
  76. }
  77. if (_variants.Any(x => string.Equals(x.Name, variantName, StringComparison.InvariantCultureIgnoreCase)))
  78. continue;
  79. _variants.Add(new VariantDto()
  80. {
  81. Name = variantName,
  82. Price = variantPrice,
  83. QuantityMinimum = variantMinimal,
  84. QuantityMaximum = variantMaximal
  85. });
  86. }
  87. }
  88. public void AppendCategoriesFromString(string categoriesString)
  89. {
  90. var categories = categoriesString.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  91. foreach (var category in categories)
  92. {
  93. if (_categories.Any(x => string.Equals(x.Name, categoriesString, StringComparison.InvariantCultureIgnoreCase)))
  94. _categories.Add(new CategoryDto() { Name = category });
  95. }
  96. }
  97. public ProductDto AddNewProduct()
  98. {
  99. var item = new ProductDto() { Type = ProductDto.CS_ITEM_TYPE_PRODUCT };
  100. _rawCollection.Add(item);
  101. return item;
  102. }
  103. public ProductDto AddNewProductVariant(string? parentSku)
  104. {
  105. if (!string.IsNullOrWhiteSpace(parentSku))
  106. if (!_rawCollection.Any(x => string.Equals(x.Sku, parentSku, StringComparison.InvariantCultureIgnoreCase)))
  107. throw new Exception($"Parent Product SKU '{parentSku}' not found!");
  108. var item = new ProductDto() { Type = ProductDto.CS_ITEM_TYPE_VARIANT, ParentName = parentSku == null ? string.Empty : parentSku };
  109. _rawCollection.Add(item);
  110. return item;
  111. }
  112. public VariantDto AddNewVariant()
  113. {
  114. var item = new VariantDto() { };
  115. _variants.Add(item);
  116. return item;
  117. }
  118. public CategoryDto AddNewCategory()
  119. {
  120. var item = new CategoryDto() { };
  121. _categories.Add(item);
  122. return item;
  123. }
  124. public void RemoveProduct(string[]? identifiers)
  125. {
  126. if (identifiers == null || identifiers.Length == 0)
  127. return;
  128. foreach (var identifier in identifiers)
  129. {
  130. var itemToRemove = _rawCollection.FirstOrDefault(x => string.Equals(x.GetIdentifier(), identifier, StringComparison.InvariantCultureIgnoreCase));
  131. if (itemToRemove != null)
  132. _rawCollection.Remove(itemToRemove);
  133. }
  134. }
  135. public void RemoveVariants(string[]? identifiers)
  136. {
  137. if (identifiers == null || identifiers.Length == 0)
  138. return;
  139. foreach (var identifier in identifiers)
  140. {
  141. var itemToRemove = _variants.FirstOrDefault(x => string.Equals(x.GetIdentifier(), identifier, StringComparison.InvariantCultureIgnoreCase));
  142. if (itemToRemove != null)
  143. _variants.Remove(itemToRemove);
  144. }
  145. }
  146. public void RemoveCategories(string[]? identifiers)
  147. {
  148. if (identifiers == null || identifiers.Length == 0)
  149. return;
  150. foreach (var identifier in identifiers)
  151. {
  152. var itemToRemove = _categories.FirstOrDefault(x => string.Equals(x.GetIdentifier(), identifier, StringComparison.InvariantCultureIgnoreCase));
  153. if (itemToRemove != null)
  154. _categories.Remove(itemToRemove);
  155. }
  156. }
  157. public string GetVariantsAsString()
  158. {
  159. return string.Join(";", _variants.Select(x => x.Name + "=" + x.Price.ToString() + "|" + x.QuantityMinimum.ToString() + "|" + x.QuantityMaximum.ToString()));
  160. }
  161. public string GetCategoriesAsString()
  162. {
  163. return string.Join(";", _categories.Select(x => x.Name));
  164. }
  165. public IEnumerable<ProductDto> GetProductsView(bool includeProduct, bool includeVariants)
  166. {
  167. return _rawCollection.Where(x => x.IsVariant == includeVariants || !x.IsVariant == includeProduct).ToArray();
  168. }
  169. private IEnumerable<VariantDto> GetVariants()
  170. {
  171. var productVariants = _rawCollection.Where(x => x.IsVariant).DistinctBy(x => x.VariantAndPrice).Select(x => new VariantDto() { Name = x.VariantName, Price = x.Price });
  172. foreach (var variant in productVariants)
  173. {
  174. if (!_variants.Any(x => x.Name == variant.Name))
  175. {
  176. _variants.Add(variant);
  177. }
  178. else
  179. {
  180. // Keep the existing (e.g. predefined) price when the product variant has
  181. // no price set (0). Freshly imported variants default to price 0 and must
  182. // not overwrite a price the user already configured. Only sync a real,
  183. // non-zero price coming from the products.
  184. var existing = _variants.First(x => x.Name == variant.Name);
  185. if (variant.Price != 0 && existing.Price != variant.Price)
  186. existing.Price = variant.Price;
  187. }
  188. }
  189. return _variants;
  190. }
  191. private IEnumerable<CategoryDto> GetCategories()
  192. {
  193. var productCategories = _rawCollection.Where(x => !x.IsVariant).DistinctBy(x => x.Category).Select(x => new CategoryDto() { Name = x.Category });
  194. foreach (var category in productCategories)
  195. {
  196. if (!_categories.Any(x => x.Name == category.Name))
  197. {
  198. _categories.Add(category);
  199. }
  200. }
  201. return _categories;
  202. }
  203. }
  204. }