DBDataProvider.cs 5.9 KB

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