DBDataprovider.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. }
  92. }