0
0

DBDataProvider.cs 6.2 KB

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