MainWindow.xaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using sql_pagining.models;
  2. using System.ComponentModel;
  3. using System.Text;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Data;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. using System.Windows.Navigation;
  12. using System.Windows.Shapes;
  13. using Dapper;
  14. using MySqlConnector;
  15. namespace sql_pagining
  16. {
  17. public partial class MainWindow : Window, INotifyPropertyChanged
  18. {
  19. public event PropertyChangedEventHandler PropertyChanged;
  20. private void Invalidate()
  21. {
  22. if (PropertyChanged != null)
  23. PropertyChanged(this, new PropertyChangedEventArgs("productList"));
  24. }
  25. private int productCount;
  26. public List<String> pageList { get; set; } = new List<String>();
  27. public IEnumerable<Product> productList
  28. {
  29. get
  30. {
  31. Globals.dataProvider.clearFilter();
  32. if (productTypeFilterId > 0)
  33. Globals.dataProvider.addFilter(
  34. "ProductTypeID = @ProductTypeID",
  35. new { ProductTypeID = productTypeFilterId }
  36. );
  37. switch (sortType)
  38. {
  39. case 0:
  40. Globals.dataProvider.setOrder("");
  41. break;
  42. case 1:
  43. Globals.dataProvider.setOrder("Title");
  44. break;
  45. case 2:
  46. Globals.dataProvider.setOrder("Title DESC");
  47. break;
  48. case 3:
  49. Globals.dataProvider.setOrder("ArticleNumber");
  50. break;
  51. case 4:
  52. Globals.dataProvider.setOrder("ArticleNumber DESC");
  53. break;
  54. case 5:
  55. Globals.dataProvider.setOrder("MaterialCost");
  56. break;
  57. case 6:
  58. Globals.dataProvider.setOrder("MaterialCost DESC");
  59. break;
  60. }
  61. if (searchFilter.Length > 0)
  62. {
  63. Globals.dataProvider.addFilter(
  64. "(Title LIKE @search OR Description LIKE @search)",
  65. new { search = $"%{searchFilter}%" }
  66. );
  67. }
  68. var result = Globals.dataProvider.getProduct(currentPage);
  69. productCount = Globals.dataProvider.getProductCount();
  70. pageList.Clear();
  71. pageList.Add("<");
  72. for (int i = 1; i < (productCount / Globals.PAGE_LEN) + 1; i++)
  73. {
  74. pageList.Add(i.ToString());
  75. }
  76. pageList.Add(">");
  77. PageListListBox.ItemsSource = pageList;
  78. return result;
  79. }
  80. set
  81. {
  82. _productList = value;
  83. Invalidate();
  84. }
  85. }
  86. public List<ProductType> productTypeList { get; set; }
  87. public MainWindow()
  88. {
  89. InitializeComponent();
  90. DataContext = this;
  91. Globals.dataProvider = new DBDataProvider();
  92. productList = Globals.dataProvider.getProduct(currentPage);
  93. productTypeList = Globals.dataProvider.getProductTypes().ToList();
  94. productTypeList.Insert(0, new ProductType { Title = "Все типы продукции" });
  95. }
  96. public string[] sortList { get; set; } = {
  97. "Без сортировки",
  98. "название по убыванию",
  99. "название по возрастанию",
  100. "артикул по убыванию",
  101. "артикул по возрастанию",
  102. "цена по убыванию",
  103. "цена по возрастанию" };
  104. private IEnumerable<Product> _productList;
  105. private const int PAGE_LEN = 20;
  106. private int _currentPage = 1;
  107. private string searchFilter = "";
  108. private int currentPage
  109. {
  110. get
  111. {
  112. return _currentPage;
  113. }
  114. set
  115. {
  116. _currentPage = value;
  117. Invalidate();
  118. }
  119. }
  120. private void InputElement_OnPointerPressed(
  121. object? sender, MouseButtonEventArgs e)
  122. {
  123. switch ((sender as TextBlock).Text)
  124. {
  125. case "<":
  126. if (currentPage > 1) currentPage--;
  127. return;
  128. case ">":
  129. if (currentPage < productCount / Globals.PAGE_LEN) currentPage++;
  130. return;
  131. default:
  132. currentPage = Convert.ToInt32(
  133. (sender as TextBlock).Text
  134. );
  135. return;
  136. }
  137. }
  138. private int productTypeFilterId = 0;
  139. private void ProductTypeFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
  140. {
  141. productTypeFilterId = (ProductTypeFilter.SelectedItem as ProductType).ID;
  142. Invalidate();
  143. }
  144. private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  145. {
  146. }
  147. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  148. {
  149. searchFilter = searchFilterTextBox.Text;
  150. Invalidate();
  151. }
  152. private int sortType = 0;
  153. private bool sortAsc = true;
  154. private void TextBlock_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  155. {
  156. switch ((sender as TextBlock).Text)
  157. {
  158. case "<":
  159. if (currentPage > 1) currentPage--;
  160. return;
  161. case ">":
  162. if (currentPage < productCount / Globals.PAGE_LEN) currentPage++;
  163. return;
  164. default:
  165. currentPage = Convert.ToInt32(
  166. (sender as TextBlock).Text
  167. );
  168. return;
  169. }
  170. }
  171. private void SortTypeComboBox_SelectionChanged(
  172. object? sender,
  173. SelectionChangedEventArgs e)
  174. {
  175. if (SortTypeComboBox != null)
  176. {
  177. sortType = SortTypeComboBox.SelectedIndex;
  178. Invalidate();
  179. }
  180. }
  181. private void SearchFilterTextBox_OnKeyUp(object? sender, KeyEventArgs e)
  182. {
  183. if (searchFilterTextBox.Text != null && searchFilterTextBox.Text != "")
  184. {
  185. searchFilter = searchFilterTextBox.Text;
  186. Invalidate();
  187. }
  188. }
  189. private void ExitButton_Click(object sender, RoutedEventArgs e)
  190. {
  191. Application.Current.Shutdown();
  192. }
  193. }
  194. }