MainWindow.xaml.cs 7.6 KB

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