MainWindow.xaml.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System.ComponentModel;
  2. using System.Security.Cryptography.X509Certificates;
  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. using wpf_connection3.model;
  16. using wpf_connection3.Windows;
  17. namespace wpf_connection3
  18. {
  19. public partial class MainWindow : Window,
  20. INotifyPropertyChanged
  21. {
  22. private int _currentPage = 1;
  23. public int currentPage
  24. { get
  25. {
  26. return _currentPage;
  27. }
  28. set
  29. {
  30. _currentPage = value;
  31. Invalidate("productList");
  32. }
  33. }
  34. public List<string> pageList { get; set; } = new List<string>();
  35. private int productCount;
  36. public IEnumerable<Product> productList
  37. {
  38. get
  39. {
  40. switch (sortType)
  41. {
  42. case 0:
  43. Globals.dataProvider.setOrder("");
  44. break;
  45. case 1:
  46. Globals.dataProvider.setOrder("Title");
  47. break;
  48. case 2:
  49. Globals.dataProvider.setOrder("Title DESC");
  50. break;
  51. case 3:
  52. Globals.dataProvider.setOrder("ProductionWorkshopNumber");
  53. break;
  54. case 4:
  55. Globals.dataProvider.setOrder("ProductionWorkshopNumber DESC");
  56. break;
  57. case 5:
  58. Globals.dataProvider.setOrder("MaterialCost");
  59. break;
  60. case 6:
  61. Globals.dataProvider.setOrder("MaterialCost DESC");
  62. break;
  63. }
  64. Globals.dataProvider.clearFilter();
  65. if (productTypeFilterId > 0)
  66. Globals.dataProvider.addFilter(
  67. "ProductTypeID = @ProductTypeID",
  68. new { ProductTypeID = productTypeFilterId }
  69. );
  70. if (searchFilter.Length > 0)
  71. {
  72. Globals.dataProvider.addFilter(
  73. //"(Title LIKE @search OR Description LIKE @search)",
  74. "(Title LIKE @search)",
  75. new { search = $"%{searchFilter}%" }
  76. );
  77. }
  78. var result = Globals.dataProvider.getProduct(currentPage);
  79. productCount = Globals.dataProvider.getProductCount();
  80. pageList.Clear();
  81. pageList.Add("<");
  82. for (int i = 1; i < (productCount / Globals.PAGE_LEN) + 1; i++)
  83. {
  84. pageList.Add(i.ToString());
  85. }
  86. pageList.Add(">");
  87. // PageListListBox.ItemsSource = pageList;
  88. Invalidate("pageList");
  89. return result;
  90. }
  91. }
  92. private void ExitButton_Click(object sender, RoutedEventArgs e)
  93. {
  94. Application.Current.Shutdown();
  95. }
  96. public MainWindow()
  97. {
  98. InitializeComponent();
  99. DataContext = this;
  100. Globals.dataProvider = new ApiDataProvider();
  101. productTypeList = Globals.dataProvider.getProductTypes().ToList();
  102. productTypeList.Insert(0, new ProductType { Title = "Все типы продукции" });
  103. }
  104. public event PropertyChangedEventHandler? PropertyChanged;
  105. private void Invalidate(string element)
  106. {
  107. if (PropertyChanged != null)
  108. PropertyChanged(this, new PropertyChangedEventArgs(element));
  109. }
  110. private void TextBlock_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  111. {
  112. switch ((sender as TextBlock).Text)
  113. {
  114. case "<":
  115. if (currentPage > 1) currentPage--;
  116. return;
  117. case ">":
  118. if (currentPage < productCount / Globals.PAGE_LEN) currentPage++;
  119. return;
  120. default:
  121. currentPage = Convert.ToInt32(
  122. (sender as TextBlock).Text);
  123. return;
  124. }
  125. }
  126. public string[] sortList { get; set; } =
  127. {
  128. "Без сортировки",
  129. "название по убыванию",
  130. "название по возрастанию",
  131. "номер цеха по убыванию",
  132. "номер цеха по возрастанию",
  133. "цена по убыванию",
  134. "цена по возрастанию"
  135. };
  136. private int sortType = 0;
  137. private void SortTypeComboBox_SelectionChanged(
  138. object? sender,
  139. SelectionChangedEventArgs e)
  140. {
  141. if (SortTypeComboBox != null)
  142. {
  143. sortType = SortTypeComboBox.SelectedIndex;
  144. Invalidate("productList");
  145. }
  146. }
  147. public List<ProductType> productTypeList { get; set; }
  148. private int productTypeFilterId = 0;
  149. private void ProductTypeFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
  150. {
  151. // запоминаем ID выбранного типа
  152. productTypeFilterId = (ProductTypeFilter.SelectedItem as ProductType).ID;
  153. Invalidate("productList");
  154. }
  155. private string searchFilter = "";
  156. private void searchFilterTextBox_KeyUp(object sender, KeyEventArgs e)
  157. {
  158. if (searchFilterTextBox.Text != null && searchFilterTextBox.Text != "")
  159. {
  160. searchFilter = searchFilterTextBox.Text;
  161. Invalidate("productList");
  162. }
  163. }
  164. public int productsSelectedCount = 0;
  165. private void ProductListBox_OnSelectionChanged(
  166. object? sender,
  167. SelectionChangedEventArgs e)
  168. {
  169. if (ProductListBox != null)
  170. {
  171. productsSelectedCount = ProductListBox.SelectedItems.Count;
  172. Invalidate("costChangeButtonVisible");
  173. }
  174. }
  175. public string costChangeButtonVisible
  176. {
  177. get
  178. {
  179. return productsSelectedCount > 1 ? "Visible" : "Hidden";
  180. }
  181. }
  182. private void CostChangeButton_Click(object sender, RoutedEventArgs e)
  183. {
  184. decimal sum = 0;
  185. List<int> idList = new List<int>();
  186. foreach (Product item in ProductListBox.SelectedItems)
  187. {
  188. sum += item.MinCostForAgent;
  189. idList.Add(item.ID);
  190. }
  191. // создаём окно, передавая ему среднюю цену
  192. var newWindow = new EnterMinCostForAgentWindow(
  193. sum / ProductListBox.SelectedItems.Count);
  194. try
  195. {
  196. if ((bool)newWindow.ShowDialog())
  197. {
  198. Globals.dataProvider.setMinCostForAgent(
  199. newWindow.Result,
  200. idList.ToArray());
  201. Invalidate("productList");
  202. }
  203. }
  204. catch { MessageBox.Show("Чото не то"); }
  205. }
  206. private void ProductListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  207. {
  208. var clickedElement = e.OriginalSource as FrameworkElement;
  209. var product = clickedElement.DataContext as Product;
  210. var newEditWindow = new EditProductWindow(product);
  211. if ((bool)newEditWindow.ShowDialog())
  212. {
  213. Invalidate("ProductList");
  214. }
  215. }
  216. private void AddProductButton_Click(object sender, RoutedEventArgs e)
  217. {
  218. var newEditWindow = new EditProductWindow(new Product());
  219. if ((bool)newEditWindow.ShowDialog())
  220. {
  221. Invalidate("ProductList");
  222. }
  223. }
  224. }
  225. }