MainWindow.xaml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using mysql.Classes;
  19. using mysql.Model;
  20. using mysql.Windows;
  21. using MySql.Data.MySqlClient;
  22. namespace mysql
  23. {
  24. /// <summary>
  25. /// Логика взаимодействия для MainWindow.xaml
  26. /// </summary>
  27. public partial class MainWindow : Window, INotifyPropertyChanged
  28. {
  29. private IEnumerable<Product> _ProductList;
  30. private int _CurrentPage = 0;
  31. private int ProductTypeFilterId = 0;
  32. public List<ProductType> ProductTypeList { get; set; }
  33. public string[] SortList { get; set; } = {
  34. "Без сортировки",
  35. "название по убыванию",
  36. "название по возрастанию",
  37. "номер цеха по убыванию",
  38. "номер цеха по возрастанию",
  39. "цена по убыванию",
  40. "цена по возрастанию" };
  41. public event PropertyChangedEventHandler PropertyChanged;
  42. private int CurrentPage {
  43. get {
  44. return _CurrentPage;
  45. }
  46. set {
  47. _CurrentPage = value;
  48. Invalidate();
  49. }
  50. }
  51. private int SortType = 0;
  52. public IEnumerable<Product> ProductList {
  53. get {
  54. var Result = _ProductList;
  55. if (ProductTypeFilterId > 0)
  56. Result = Result.Where(i => i.CurrentProductType.ID == ProductTypeFilterId);
  57. switch (SortType)
  58. {
  59. // сортировка по названию продукции
  60. case 1:
  61. Result = Result.OrderBy(p => p.Title);
  62. break;
  63. case 2:
  64. Result = Result.OrderByDescending(p => p.Title);
  65. break;
  66. // остальные сортировки реализуйте сами
  67. }
  68. // ищем вхождение строки фильтра в названии и описании объекта без учета регистра
  69. if (SearchFilter != "")
  70. Result = Result.Where(
  71. p => p.Title.IndexOf(SearchFilter, StringComparison.OrdinalIgnoreCase) >= 0 ||
  72. p.Description.IndexOf(SearchFilter, StringComparison.OrdinalIgnoreCase) >= 0
  73. );
  74. Paginator.Children.Clear();
  75. Paginator.Children.Add(new TextBlock { Text = " < " });
  76. for (int i = 1; i <= (Result.Count() / 20)+1; i++)
  77. Paginator.Children.Add(new TextBlock { Text = " " + i.ToString() + " " });
  78. Paginator.Children.Add(new TextBlock { Text = " > " });
  79. foreach (TextBlock tb in Paginator.Children)
  80. tb.PreviewMouseDown += PrevPage_PreviewMouseDown;
  81. if (CurrentPage > Result.Count() / 20)
  82. CurrentPage = Result.Count() / 20;
  83. return Result.Skip(20 * CurrentPage).Take(20);
  84. }
  85. set {
  86. _ProductList = value;
  87. Invalidate();
  88. }
  89. }
  90. private void Invalidate(string ComponentName = "ProductList") {
  91. if (PropertyChanged != null)
  92. PropertyChanged(this, new PropertyChangedEventArgs(ComponentName));
  93. }
  94. public MainWindow()
  95. {
  96. InitializeComponent();
  97. DataContext = this;
  98. Globals.DataProvider = new MySQLDataProvider();
  99. Globals.ProductTypeList = Globals.DataProvider.GetProductTypes();
  100. ProductList = Globals.DataProvider.GetProducts();
  101. ProductTypeList = Globals.DataProvider.GetProductTypes().ToList();
  102. ProductTypeList.Insert(0, new ProductType { Title = "Все типы" });
  103. }
  104. private void PrevPage_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  105. {
  106. switch ((sender as TextBlock).Text)
  107. {
  108. case " < ":
  109. if (CurrentPage > 0) CurrentPage--;
  110. return;
  111. case " > ":
  112. if (CurrentPage < _ProductList.Count() / 20) CurrentPage++;
  113. return;
  114. default:
  115. CurrentPage = Convert.ToInt32((sender as TextBlock).Text.Trim())-1;
  116. return;
  117. }
  118. }
  119. private void SortTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  120. {
  121. SortType = SortTypeComboBox.SelectedIndex;
  122. Invalidate();
  123. }
  124. private void ProductTypeFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
  125. {
  126. ProductTypeFilterId = (ProductTypeFilter.SelectedItem as ProductType).ID;
  127. Invalidate();
  128. }
  129. private string SearchFilter="";
  130. private void SearchFilterTextBox_KeyUp(object sender, KeyEventArgs e)
  131. {
  132. SearchFilter = SearchFilterTextBox.Text;
  133. Invalidate();
  134. }
  135. public string CostChangeButtonVisible {
  136. get {
  137. if (ProductsSelectedCount > 1) return "Visible";
  138. return "Collapsed";
  139. }
  140. }
  141. public int ProductsSelectedCount = 0;
  142. private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  143. {
  144. ProductsSelectedCount = ProductListView.SelectedItems.Count;
  145. Invalidate("CostChangeButtonVisible");
  146. }
  147. private void CostChangeButton_Click(object sender, RoutedEventArgs e)
  148. {
  149. decimal AvgSum = 0;
  150. List<int> idList = new List<int>();
  151. foreach (Product item in ProductListView.SelectedItems)
  152. {
  153. AvgSum += item.MinCostForAgent;
  154. idList.Add(item.ID);
  155. }
  156. var ww = new EnterMinCostForAgentWindow(AvgSum / ProductListView.SelectedItems.Count);
  157. if((bool)ww.ShowDialog())
  158. {
  159. Globals.DataProvider.SetAverageCostForAgent(idList, ww.Result);
  160. ProductList = Globals.DataProvider.GetProducts();
  161. }
  162. }
  163. private void ProductListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  164. {
  165. var NewEditWindow = new EditWindow(ProductListView.SelectedItem as Product);
  166. if ((bool)NewEditWindow.ShowDialog())
  167. {
  168. ProductList = Globals.DataProvider.GetProducts();
  169. }
  170. }
  171. }
  172. }