DBDataprovider.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MySqlConnector;
  7. using mysql_connector2.res;
  8. using System.Text.RegularExpressions;
  9. using System.Windows.Controls.Primitives;
  10. using System.Windows.Media.Media3D;
  11. using Dapper;
  12. namespace mysql_connector2.res
  13. {
  14. public class DBDataProvider : IDataProvider
  15. {
  16. private Dictionary<string, object> filters = new Dictionary<string, object>();
  17. private string orderCondition = "";
  18. static string connectionString = "Server=kolei.ru; User ID=akapralov; Password=300806; Database=akapralov";
  19. public virtual IEnumerable<Product> getProduct(int pageNum)
  20. {
  21. using (MySqlConnection db = new MySqlConnection(connectionString))
  22. {
  23. var builder = new SqlBuilder();
  24. if (orderCondition.Length > 0)
  25. builder.OrderBy(orderCondition);
  26. if (filters.Count > 0)
  27. {
  28. foreach (var item in filters)
  29. builder.Where(item.Key, item.Value);
  30. }
  31. var template = builder.AddTemplate(
  32. "SELECT * FROM ProductView /**where**/ /**orderby**/ LIMIT @pageLen OFFSET @offset",
  33. new { pageLen = Globals.PAGE_LEN, offset = (pageNum - 1) * Globals.PAGE_LEN }
  34. );
  35. return db.Query<Product>(
  36. template.RawSql,
  37. template.Parameters).ToList();
  38. }
  39. }
  40. public int getProductCount()
  41. {
  42. using (MySqlConnection db = new MySqlConnection(connectionString))
  43. {
  44. var builder = new SqlBuilder();
  45. if (filters.Count > 0)
  46. {
  47. foreach (var item in filters)
  48. {
  49. builder.Where(item.Key, item.Value);
  50. }
  51. }
  52. var template = builder.AddTemplate(
  53. "SELECT count(*) FROM ProductView /**where**/");
  54. return db.QuerySingle<int>(
  55. template.RawSql,
  56. template.Parameters);
  57. }
  58. }
  59. public virtual void setOrder(string condition)
  60. {
  61. orderCondition = condition;
  62. }
  63. public void addFilter(string name, object value)
  64. {
  65. filters.Add(name, value);
  66. }
  67. public void clearFilter()
  68. {
  69. filters.Clear();
  70. }
  71. public virtual IEnumerable<ProductType> getProductTypes()
  72. {
  73. using (MySqlConnection db = new MySqlConnection(connectionString))
  74. {
  75. return db.Query<ProductType>("SELECT * FROM ProductType");
  76. }
  77. }
  78. public virtual void setMinCostForAgent(decimal minCostForAgent, int[] ids)
  79. {
  80. using (MySqlConnection db = new MySqlConnection(connectionString))
  81. {
  82. db.Execute("UPDATE Product SET MinCostForAgent=@newCost WHERE ID in @idList",
  83. new
  84. {
  85. newCost = minCostForAgent,
  86. idList = ids
  87. });
  88. }
  89. }
  90. public virtual void saveProduct(Product product)
  91. {
  92. using (MySqlConnection db = new MySqlConnection(connectionString))
  93. {
  94. if (product.ID == 0)
  95. {
  96. db.Execute("INSERT INTO Product (ProductTypeID, ArticleNumber, Title, MinCostForAgent, Description, Image) VALUES (@ProductTypeID, @ArticleNumber, @Title, @MinCostForAgent, @Description, @Image)", product);
  97. }
  98. else
  99. {
  100. db.Execute("UPDATE Product SET ProductTypeID = @ProductTypeID, ArticleNumber = @ArticleNumber, Title = @Title, MinCostForAgent = @MinCostForAgent, Description = @Description, Image = @Image WHERE ID = @ID", product);
  101. }
  102. }
  103. }
  104. public Product GetProductByArticle(string article)
  105. {
  106. using (MySqlConnection db = new MySqlConnection(connectionString))
  107. {
  108. return db.QueryFirstOrDefault<Product>("SELECT * FROM Product WHERE ArticleNumber = @ArticleNumber", new { ArticleNumber = article });
  109. }
  110. }
  111. public virtual void removePriceHistory(int productId)
  112. {
  113. using (MySqlConnection db = new MySqlConnection(connectionString))
  114. {
  115. try
  116. {
  117. db.Open();
  118. MySqlCommand command = new MySqlCommand("DELETE FROM ProductCostHistory WHERE ProductID = @ProductID", db);
  119. command.Parameters.AddWithValue("@ProductID", productId);
  120. int rowsAffected = command.ExecuteNonQuery();
  121. }
  122. catch (Exception ex)
  123. {
  124. Console.WriteLine("Ошибка удаления истории цен: " + ex.Message);
  125. }
  126. finally
  127. {
  128. db.Close();
  129. }
  130. }
  131. }
  132. public void removeProduct(int productId)
  133. {
  134. using (MySqlConnection db = new MySqlConnection(connectionString))
  135. {
  136. db.Execute("DELETE FROM Product WHERE ID = @ProductID", new { ProductID = productId });
  137. }
  138. }
  139. public void removeProductMaterial(int productId)
  140. {
  141. using (MySqlConnection db = new MySqlConnection(connectionString))
  142. {
  143. db.Execute("DELETE FROM ProductMaterial WHERE ProductID = @ProductID", new { ProductID = productId });
  144. }
  145. }
  146. public virtual int saleCount(int productId)
  147. {
  148. using (MySqlConnection db = new MySqlConnection(connectionString))
  149. {
  150. return db.QuerySingle<int>("SELECT COUNT(*) FROM ProductSale WHERE ProductID = @ProductID", new { ProductID = productId });
  151. }
  152. }
  153. private int _currentProductId;
  154. public virtual IEnumerable<ProductMaterial> getProductMaterials(int productId)
  155. {
  156. _currentProductId = productId;
  157. using (MySqlConnection db = new MySqlConnection(connectionString))
  158. {
  159. string sql = @"
  160. SELECT
  161. pm.ProductID,
  162. pm.MaterialID,
  163. pm.Count,
  164. m.Title AS MaterialTitle
  165. FROM
  166. ProductMaterial pm
  167. JOIN
  168. Material m ON pm.MaterialID = m.ID
  169. WHERE
  170. pm.ProductID = @productId";
  171. var productMaterials = db.Query<ProductMaterial>(sql, new { productId = _currentProductId });
  172. return productMaterials;
  173. }
  174. }
  175. public virtual void deleteProductMaterial(ProductMaterial productMaterial)
  176. {
  177. using (MySqlConnection db = new MySqlConnection(connectionString))
  178. {
  179. string sql = "DELETE FROM ProductMaterial WHERE MaterialID = @MaterialID AND ProductID = @ProductID";
  180. db.Execute(sql, productMaterial);
  181. }
  182. }
  183. public virtual void addProductMaterial(ProductMaterial productMaterial)
  184. {
  185. using (MySqlConnection db = new MySqlConnection(connectionString))
  186. {
  187. db.Execute("INSERT INTO ProductMaterial (ProductID, MaterialID, Count) VALUES (@ProductID, @MaterialID, @Count)", productMaterial);
  188. }
  189. }
  190. public virtual IEnumerable<Material> getAvailableMaterials()
  191. {
  192. using (MySqlConnection db = new MySqlConnection(connectionString))
  193. {
  194. return db.Query<Material>("SELECT * FROM Material").ToList();
  195. }
  196. }
  197. }
  198. }