DBDataProvider.cs 7.5 KB

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