0
0

DBDataProvider.cs 7.6 KB

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