MainWindow.xaml.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System.Text;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Data;
  5. using System.Windows.Documents;
  6. using System.Windows.Input;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Imaging;
  9. using System.Windows.Navigation;
  10. using System.Windows.Shapes;
  11. using MySqlConnector;
  12. using Dapper;
  13. using mysql_connector2.res;
  14. using System.ComponentModel;
  15. using mysql_connector2.Windows;
  16. namespace mysql_connector2
  17. {
  18. public partial class MainWindow : Window, INotifyPropertyChanged
  19. {
  20. public event PropertyChangedEventHandler PropertyChanged;
  21. private void Invalidate()
  22. {
  23. if (PropertyChanged != null)
  24. PropertyChanged(this, new PropertyChangedEventArgs("productList"));
  25. }
  26. public int productsSelectedCount = 0;
  27. public decimal Result;
  28. private void CostChangeButton_Click(object sender, RoutedEventArgs e)
  29. {
  30. decimal sum = 0;
  31. List<int> idList = new List<int>();
  32. if (ProductListBox.SelectedItems.Count == 0)
  33. {
  34. MessageBox.Show("Выберите продукты для изменения цены.");
  35. return;
  36. }
  37. foreach (Product item in ProductListBox.SelectedItems)
  38. {
  39. sum += item.MinCostForAgent;
  40. idList.Add(item.ID);
  41. }
  42. var newWindow = new EnterMinCostForAgentWindow(sum / ProductListBox.SelectedItems.Count);
  43. if ((bool)newWindow.ShowDialog())
  44. {
  45. try
  46. {
  47. Globals.dataProvider.setMinCostForAgent(newWindow.Result, idList.ToArray());
  48. MessageBox.Show("Стоимость успешно изменена.");
  49. }
  50. catch (Exception ex)
  51. {
  52. MessageBox.Show($"Ошибка: {ex.Message}");
  53. }
  54. }
  55. }
  56. private void ProductListBox_OnSelectionChanged(
  57. object? sender,
  58. SelectionChangedEventArgs e)
  59. {
  60. if (ProductListBox != null)
  61. {
  62. productsSelectedCount = ProductListBox.SelectedItems.Count;
  63. }
  64. Invalidate();
  65. }
  66. private int productCount;
  67. public List<String> pageList { get; set; } = new List<String>();
  68. public IEnumerable<Product> productList
  69. {
  70. get
  71. {
  72. Globals.dataProvider.clearFilter();
  73. if (productTypeFilterId > 0)
  74. Globals.dataProvider.addFilter(
  75. "ProductTypeID = @ProductTypeID",
  76. new { ProductTypeID = productTypeFilterId }
  77. );
  78. switch (sortType)
  79. {
  80. case 0:
  81. Globals.dataProvider.setOrder("");
  82. break;
  83. case 1:
  84. Globals.dataProvider.setOrder("Title");
  85. break;
  86. case 2:
  87. Globals.dataProvider.setOrder("Title DESC");
  88. break;
  89. case 3:
  90. Globals.dataProvider.setOrder("ArticleNumber");
  91. break;
  92. case 4:
  93. Globals.dataProvider.setOrder("ArticleNumber DESC");
  94. break;
  95. case 5:
  96. Globals.dataProvider.setOrder("MaterialCost");
  97. break;
  98. case 6:
  99. Globals.dataProvider.setOrder("MaterialCost DESC");
  100. break;
  101. }
  102. if (searchFilter.Length > 0)
  103. {
  104. Globals.dataProvider.addFilter(
  105. "(Title LIKE @search OR Description LIKE @search)",
  106. new { search = $"%{searchFilter}%" }
  107. );
  108. }
  109. var result = Globals.dataProvider.getProduct(currentPage);
  110. productCount = Globals.dataProvider.getProductCount();
  111. pageList.Clear();
  112. pageList.Add("<");
  113. for (int i = 1; i < (productCount / Globals.PAGE_LEN) + 1; i++)
  114. {
  115. pageList.Add(i.ToString());
  116. }
  117. pageList.Add(">");
  118. PageListListBox.ItemsSource = pageList;
  119. return result;
  120. }
  121. set
  122. {
  123. _productList = value;
  124. Invalidate();
  125. }
  126. }
  127. public List<ProductType> productTypeList { get; set; }
  128. public MainWindow()
  129. {
  130. InitializeComponent();
  131. DataContext = this;
  132. Globals.dataProvider = new DBDataProvider();
  133. productList = Globals.dataProvider.getProduct(currentPage);
  134. productTypeList = Globals.dataProvider.getProductTypes().ToList();
  135. productTypeList.Insert(0, new ProductType { Title = "Все типы продукции" });
  136. }
  137. public string[] sortList { get; set; } = {
  138. "Без сортировки",
  139. "Название по убыванию",
  140. "Название по возрастанию",
  141. "Артикул по убыванию",
  142. "Артикул по возрастанию",
  143. "Цена по убыванию",
  144. "Цена по возрастанию" };
  145. private IEnumerable<Product> _productList;
  146. private const int PAGE_LEN = 20;
  147. private int _currentPage = 1;
  148. private string searchFilter = "";
  149. private int currentPage
  150. {
  151. get
  152. {
  153. return _currentPage;
  154. }
  155. set
  156. {
  157. _currentPage = value;
  158. Invalidate();
  159. }
  160. }
  161. private void InputElement_OnPointerPressed(
  162. object? sender, MouseButtonEventArgs e)
  163. {
  164. switch ((sender as TextBlock).Text)
  165. {
  166. case "<":
  167. if (currentPage > 1) currentPage--;
  168. return;
  169. case ">":
  170. if (currentPage < productCount / Globals.PAGE_LEN) currentPage++;
  171. return;
  172. default:
  173. currentPage = Convert.ToInt32(
  174. (sender as TextBlock).Text
  175. );
  176. return;
  177. }
  178. }
  179. private int productTypeFilterId = 0;
  180. private void ProductTypeFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
  181. {
  182. productTypeFilterId = (ProductTypeFilter.SelectedItem as ProductType).ID;
  183. Invalidate();
  184. }
  185. private void ProductListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  186. {
  187. if (ProductListBox != null)
  188. {
  189. productsSelectedCount = ProductListBox.SelectedItems.Count;
  190. CostChangeButton.Visibility = productsSelectedCount > 0 ? Visibility.Visible : Visibility.Collapsed;
  191. }
  192. }
  193. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  194. {
  195. searchFilter = searchFilterTextBox.Text;
  196. Invalidate();
  197. }
  198. private int sortType = 0;
  199. private bool sortAsc = true;
  200. private void TextBlock_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  201. {
  202. switch ((sender as TextBlock).Text)
  203. {
  204. case "<":
  205. if (currentPage > 1) currentPage--;
  206. return;
  207. case ">":
  208. if (currentPage < productCount / Globals.PAGE_LEN) currentPage++;
  209. return;
  210. default:
  211. currentPage = Convert.ToInt32(
  212. (sender as TextBlock).Text
  213. );
  214. return;
  215. }
  216. }
  217. private void SortTypeComboBox_SelectionChanged(
  218. object? sender,
  219. SelectionChangedEventArgs e)
  220. {
  221. if (SortTypeComboBox != null)
  222. {
  223. sortType = SortTypeComboBox.SelectedIndex;
  224. Invalidate();
  225. }
  226. }
  227. private void SearchFilterTextBox_OnKeyUp(object? sender, KeyEventArgs e)
  228. {
  229. if (searchFilterTextBox.Text != null && searchFilterTextBox.Text != "")
  230. {
  231. searchFilter = searchFilterTextBox.Text;
  232. Invalidate();
  233. }
  234. }
  235. private void ExitButton_Click(object sender, RoutedEventArgs e)
  236. {
  237. Application.Current.Shutdown();
  238. }
  239. public string costChangeButtonVisible
  240. {
  241. get
  242. {
  243. return productsSelectedCount > 1 ? "Visible" : "Hidden";
  244. }
  245. }
  246. private void ProductListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  247. {
  248. var product = (sender as ListBox).SelectedItem as Product;
  249. var costWindow = new CostWindow(product);
  250. costWindow.ShowDialog();
  251. }
  252. }
  253. }