DBDataProvider.cs 7.7 KB

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