DBDataprovider.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. private int _currentProductId;
  155. public IEnumerable<ProductMaterial> getProductMaterials(int productId)
  156. {
  157. _currentProductId = productId;
  158. using (MySqlConnection db = new MySqlConnection(connectionString))
  159. {
  160. string sql = @"
  161. SELECT
  162. pm.ProductID,
  163. pm.MaterialID,
  164. pm.Count,
  165. m.Title AS MaterialTitle
  166. FROM
  167. ProductMaterial pm
  168. JOIN
  169. Material m ON pm.MaterialID = m.ID
  170. WHERE
  171. pm.ProductID = @productId";
  172. var productMaterials = db.Query<ProductMaterial>(sql, new { productId = _currentProductId });
  173. return productMaterials;
  174. }
  175. }
  176. public void deleteProductMaterial(ProductMaterial productMaterial)
  177. {
  178. using (MySqlConnection db = new MySqlConnection(connectionString))
  179. {
  180. string sql = "DELETE FROM ProductMaterial WHERE MaterialID = @MaterialID AND ProductID = @ProductID";
  181. db.Execute(sql, productMaterial);
  182. }
  183. }
  184. public void addProductMaterial(ProductMaterial productMaterial)
  185. {
  186. using (MySqlConnection db = new MySqlConnection(connectionString))
  187. {
  188. db.Execute("INSERT INTO ProductMaterial (ProductID, MaterialID, Count) VALUES (@ProductID, @MaterialID, @Count)", productMaterial);
  189. }
  190. }
  191. public IEnumerable<Material> getAvailableMaterials()
  192. {
  193. using (MySqlConnection db = new MySqlConnection(connectionString))
  194. {
  195. return db.Query<Material>("SELECT * FROM Material").ToList();
  196. }
  197. }
  198. }
  199. }