DBDataprovider.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. internal class DBDataProvider : IDataProvider
  15. {
  16. private string searchFilter = "";
  17. private Dictionary<string, object> filters = new Dictionary<string, object>();
  18. private string orderCondition = "";
  19. static string connectionString = "Server=kolei.ru; User ID=akapralov; Password=300806; Database=akapralov";
  20. public IEnumerable<Product> getProduct(int pageNum)
  21. {
  22. using (MySqlConnection db = new MySqlConnection(connectionString))
  23. {
  24. var builder = new SqlBuilder();
  25. if (orderCondition.Length > 0)
  26. builder.OrderBy(orderCondition);
  27. if (filters.Count > 0)
  28. {
  29. foreach (var item in filters)
  30. builder.Where(item.Key, item.Value);
  31. }
  32. var template = builder.AddTemplate(
  33. "SELECT * FROM ProductView /**where**/ /**orderby**/ LIMIT @pageLen OFFSET @offset",
  34. new { pageLen = Globals.PAGE_LEN, offset = (pageNum - 1) * Globals.PAGE_LEN }
  35. );
  36. return db.Query<Product>(
  37. template.RawSql,
  38. template.Parameters).ToList();
  39. }
  40. }
  41. public int getProductCount()
  42. {
  43. using (MySqlConnection db = new MySqlConnection(connectionString))
  44. {
  45. var builder = new SqlBuilder();
  46. if (filters.Count > 0)
  47. {
  48. foreach (var item in filters)
  49. {
  50. builder.Where(item.Key, item.Value);
  51. }
  52. }
  53. var template = builder.AddTemplate(
  54. "SELECT count(*) FROM ProductView /**where**/");
  55. return db.QuerySingle<int>(
  56. template.RawSql,
  57. template.Parameters);
  58. }
  59. }
  60. public void setOrder(string condition)
  61. {
  62. orderCondition = condition;
  63. }
  64. public void addFilter(string name, object value)
  65. {
  66. filters.Add(name, value);
  67. }
  68. public void clearFilter()
  69. {
  70. filters.Clear();
  71. }
  72. public IEnumerable<ProductType> getProductTypes()
  73. {
  74. using (MySqlConnection db = new MySqlConnection(connectionString))
  75. {
  76. return db.Query<ProductType>("SELECT * FROM ProductType");
  77. }
  78. }
  79. public void setMinCostForAgent(decimal minCostForAgent, int[] ids)
  80. {
  81. using (MySqlConnection db = new MySqlConnection(connectionString))
  82. {
  83. db.Execute("UPDATE Product SET MinCostForAgent=@newCost WHERE ID in @idList",
  84. new
  85. {
  86. newCost = minCostForAgent,
  87. idList = ids
  88. });
  89. }
  90. }
  91. public void saveProduct(Product product)
  92. {
  93. using (MySqlConnection db = new MySqlConnection(connectionString))
  94. {
  95. if (product.ID == 0)
  96. {
  97. db.Execute("INSERT INTO Product (ProductTypeID, ArticleNumber, Title, MinCostForAgent, Description, Image) VALUES (@ProductTypeID, @ArticleNumber, @Title, @MinCostForAgent, @Description, @Image)", product);
  98. }
  99. else
  100. {
  101. db.Execute("UPDATE Product SET ProductTypeID = @ProductTypeID, ArticleNumber = @ArticleNumber, Title = @Title, MinCostForAgent = @MinCostForAgent, Description = @Description, Image = @Image WHERE ID = @ID", product);
  102. }
  103. }
  104. }
  105. public Product GetProductByArticle(string article)
  106. {
  107. using (MySqlConnection db = new MySqlConnection(connectionString))
  108. {
  109. return db.QueryFirstOrDefault<Product>("SELECT * FROM Product WHERE ArticleNumber = @ArticleNumber", new { ArticleNumber = article });
  110. }
  111. }
  112. public void removePriceHistory(int productId)
  113. {
  114. using (MySqlConnection db = new MySqlConnection(connectionString))
  115. {
  116. try
  117. {
  118. db.Open();
  119. MySqlCommand command = new MySqlCommand("DELETE FROM ProductCostHistory WHERE ProductID = @ProductID", db);
  120. command.Parameters.AddWithValue("@ProductID", productId);
  121. int rowsAffected = command.ExecuteNonQuery();
  122. }
  123. catch (Exception ex)
  124. {
  125. Console.WriteLine("Ошибка удаления истории цен: " + ex.Message);
  126. }
  127. finally
  128. {
  129. db.Close();
  130. }
  131. }
  132. }
  133. public void removeProduct(int productId)
  134. {
  135. using (MySqlConnection db = new MySqlConnection(connectionString))
  136. {
  137. db.Execute("DELETE FROM Product WHERE ID = @ProductID", new { ProductID = productId });
  138. }
  139. }
  140. public void removeProductMaterial(int productId)
  141. {
  142. using (MySqlConnection db = new MySqlConnection(connectionString))
  143. {
  144. db.Execute("DELETE FROM ProductMaterial WHERE ProductID = @ProductID", new { ProductID = productId });
  145. }
  146. }
  147. public int saleCount(int productId)
  148. {
  149. using (MySqlConnection db = new MySqlConnection(connectionString))
  150. {
  151. return db.QuerySingle<int>("SELECT COUNT(*) FROM ProductSale WHERE ProductID = @ProductID", new { ProductID = productId });
  152. }
  153. }
  154. }
  155. }