Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

155 wiersze
5.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CIG
  4. {
  5. public class StoreProduct : IComparable<StoreProduct>
  6. {
  7. public StoreProduct(Dictionary<string, object> props)
  8. {
  9. this.Update(props);
  10. }
  11. public StoreProduct(StoreProduct storeProduct) : this(storeProduct.Serialize())
  12. {
  13. }
  14. public virtual void Update(Dictionary<string, object> props)
  15. {
  16. this.Identifier = (string)props["ProductIdentifier"];
  17. this.Title = this.GetValue<string>(props, "ProductTitle", string.Empty);
  18. this.Description = this.GetValue<string>(props, "ProductDescription", string.Empty);
  19. this.CurrencyCode = this.GetValue<string>(props, "ProductCurrencyCode", string.Empty);
  20. this.FormattedPrice = this.GetValue<string>(props, "ProductFormattedPrice", string.Empty);
  21. this.Price = this.GetValue<decimal>(props, "ProductPrice", 0m);
  22. this.DollarPrice = this.GetValue<decimal>(props, "ProductDollarPrice", 0m);
  23. this.EuroPrice = this.GetValue<decimal>(props, "ProductEuroPrice", 0m);
  24. this.Available = this.GetValue<bool>(props, "Available", false);
  25. if (this.Price == 0m)
  26. {
  27. if (this.DollarPrice != 0m)
  28. {
  29. this.Price = this.DollarPrice;
  30. }
  31. else if (this.EuroPrice != 0m)
  32. {
  33. this.Price = this.EuroPrice;
  34. }
  35. }
  36. object obj;
  37. if (props.TryGetValue("CustomProperties", out obj))
  38. {
  39. Dictionary<string, object> dictionary = obj as Dictionary<string, object>;
  40. if (dictionary != null)
  41. {
  42. this.CustomProperties = new Dictionary<string, string>();
  43. foreach (KeyValuePair<string, object> keyValuePair in dictionary)
  44. {
  45. this.CustomProperties.Add(keyValuePair.Key, (string)keyValuePair.Value);
  46. }
  47. }
  48. else
  49. {
  50. this.CustomProperties = (Dictionary<string, string>)obj;
  51. }
  52. }
  53. else
  54. {
  55. this.CustomProperties = new Dictionary<string, string>();
  56. }
  57. }
  58. public int CompareTo(StoreProduct other)
  59. {
  60. int num = this.Price.CompareTo(other.Price);
  61. if (num == 0)
  62. {
  63. num = this.EuroPrice.CompareTo(other.EuroPrice);
  64. if (num == 0)
  65. {
  66. num = this.DollarPrice.CompareTo(other.DollarPrice);
  67. }
  68. }
  69. return num;
  70. }
  71. public override string ToString()
  72. {
  73. return string.Format("[StoreProduct: {0}; Available: {1}]", this.Identifier, this.Available);
  74. }
  75. public bool Available { get; set; }
  76. public string Identifier { get; private set; }
  77. public int StorePosition { get; set; }
  78. public string Title { get; private set; }
  79. public string Description { get; private set; }
  80. public decimal Price { get; private set; }
  81. public string CurrencyCode { get; private set; }
  82. public string FormattedPrice { get; private set; }
  83. public decimal DollarPrice { get; private set; }
  84. public decimal EuroPrice { get; private set; }
  85. public Dictionary<string, string> CustomProperties { get; private set; }
  86. private T GetValue<T>(Dictionary<string, object> dict, string key, T defaultValue)
  87. {
  88. object obj;
  89. if (dict.TryGetValue(key, out obj) && obj != null)
  90. {
  91. return (T)((object)obj);
  92. }
  93. return defaultValue;
  94. }
  95. public Dictionary<string, object> Serialize()
  96. {
  97. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  98. dictionary["ProductIdentifier"] = this.Identifier;
  99. dictionary["ProductTitle"] = this.Title;
  100. dictionary["ProductDescription"] = this.Description;
  101. dictionary["ProductPrice"] = this.Price;
  102. dictionary["ProductCurrencyCode"] = this.CurrencyCode;
  103. dictionary["ProductFormattedPrice"] = this.FormattedPrice;
  104. dictionary["ProductEuroPrice"] = this.EuroPrice;
  105. dictionary["ProductDollarPrice"] = this.DollarPrice;
  106. Dictionary<string, object> dictionary2 = new Dictionary<string, object>();
  107. foreach (KeyValuePair<string, string> keyValuePair in this.CustomProperties)
  108. {
  109. dictionary2.Add(keyValuePair.Key, keyValuePair.Value);
  110. }
  111. dictionary["CustomProperties"] = dictionary2;
  112. dictionary["Available"] = this.Available;
  113. return dictionary;
  114. }
  115. public const string IdentifierKey = "ProductIdentifier";
  116. public const string TitleKey = "ProductTitle";
  117. public const string DescriptionKey = "ProductDescription";
  118. public const string PriceKey = "ProductPrice";
  119. public const string CurrencyCodeKey = "ProductCurrencyCode";
  120. public const string FormattedPriceKey = "ProductFormattedPrice";
  121. public const string EuroPriceKey = "ProductEuroPrice";
  122. public const string DollarPriceKey = "ProductDollarPrice";
  123. public const string CustomPropertiesKey = "CustomProperties";
  124. public const string AvailableKey = "Available";
  125. }
  126. }