123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using MySqlConnector;
- using Dapper;
- using mysql_connector2.res;
- using System.ComponentModel;
- using mysql_connector2.Windows;
- namespace mysql_connector2
- {
- public partial class MainWindow : Window, INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- private void Invalidate()
- {
- if (PropertyChanged != null)
- PropertyChanged(this, new PropertyChangedEventArgs("productList"));
- }
- public int productsSelectedCount = 0;
- public decimal Result;
- private void CostChangeButton_Click(object sender, RoutedEventArgs e)
- {
- decimal sum = 0;
- List<int> idList = new List<int>();
- if (ProductListBox.SelectedItems.Count == 0)
- {
- MessageBox.Show("Выберите продукты для изменения цены.");
- return;
- }
- foreach (Product item in ProductListBox.SelectedItems)
- {
- sum += item.MinCostForAgent;
- idList.Add(item.ID);
- }
- var newWindow = new EnterMinCostForAgentWindow(sum / ProductListBox.SelectedItems.Count);
- if ((bool)newWindow.ShowDialog())
- {
- try
- {
- Globals.dataProvider.setMinCostForAgent(newWindow.Result, idList.ToArray());
- MessageBox.Show("Стоимость успешно изменена.");
- }
- catch (Exception ex)
- {
- MessageBox.Show($"Ошибка: {ex.Message}");
- }
- }
- }
- private void ProductListBox_OnSelectionChanged(
- object? sender,
- SelectionChangedEventArgs e)
- {
- if (ProductListBox != null)
- {
- productsSelectedCount = ProductListBox.SelectedItems.Count;
- }
- Invalidate();
- }
- private int productCount;
- public List<String> pageList { get; set; } = new List<String>();
- public IEnumerable<Product> productList
- {
- get
- {
- Globals.dataProvider.clearFilter();
- if (productTypeFilterId > 0)
- Globals.dataProvider.addFilter(
- "ProductTypeID = @ProductTypeID",
- new { ProductTypeID = productTypeFilterId }
- );
- switch (sortType)
- {
- case 0:
- Globals.dataProvider.setOrder("");
- break;
- case 1:
- Globals.dataProvider.setOrder("Title");
- break;
- case 2:
- Globals.dataProvider.setOrder("Title DESC");
- break;
- case 3:
- Globals.dataProvider.setOrder("ArticleNumber");
- break;
- case 4:
- Globals.dataProvider.setOrder("ArticleNumber DESC");
- break;
- case 5:
- Globals.dataProvider.setOrder("MaterialCost");
- break;
- case 6:
- Globals.dataProvider.setOrder("MaterialCost DESC");
- break;
- }
- if (searchFilter.Length > 0)
- {
- Globals.dataProvider.addFilter(
- "(Title LIKE @search OR Description LIKE @search)",
- new { search = $"%{searchFilter}%" }
- );
- }
- var result = Globals.dataProvider.getProduct(currentPage);
- productCount = Globals.dataProvider.getProductCount();
- pageList.Clear();
- pageList.Add("<");
- for (int i = 1; i < (productCount / Globals.PAGE_LEN) + 1; i++)
- {
- pageList.Add(i.ToString());
- }
- pageList.Add(">");
- PageListListBox.ItemsSource = pageList;
- return result;
- }
- set
- {
- _productList = value;
- Invalidate();
- }
- }
- public List<ProductType> productTypeList { get; set; }
- public MainWindow()
- {
- InitializeComponent();
- DataContext = this;
- Globals.dataProvider = new DBDataProvider();
- productList = Globals.dataProvider.getProduct(currentPage);
- productTypeList = Globals.dataProvider.getProductTypes().ToList();
- productTypeList.Insert(0, new ProductType { Title = "Все типы продукции" });
- }
- public string[] sortList { get; set; } = {
- "Без сортировки",
- "Название по убыванию",
- "Название по возрастанию",
- "Артикул по убыванию",
- "Артикул по возрастанию",
- "Цена по убыванию",
- "Цена по возрастанию" };
- private IEnumerable<Product> _productList;
- private const int PAGE_LEN = 20;
- private int _currentPage = 1;
- private string searchFilter = "";
- private int currentPage
- {
- get
- {
- return _currentPage;
- }
- set
- {
- _currentPage = value;
- Invalidate();
- }
- }
- private void InputElement_OnPointerPressed(
- object? sender, MouseButtonEventArgs e)
- {
- switch ((sender as TextBlock).Text)
- {
- case "<":
- if (currentPage > 1) currentPage--;
- return;
- case ">":
- if (currentPage < productCount / Globals.PAGE_LEN) currentPage++;
- return;
- default:
- currentPage = Convert.ToInt32(
- (sender as TextBlock).Text
- );
- return;
- }
- }
- private int productTypeFilterId = 0;
- private void ProductTypeFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- productTypeFilterId = (ProductTypeFilter.SelectedItem as ProductType).ID;
- Invalidate();
- }
- private void ProductListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (ProductListBox != null)
- {
- productsSelectedCount = ProductListBox.SelectedItems.Count;
- CostChangeButton.Visibility = productsSelectedCount > 0 ? Visibility.Visible : Visibility.Collapsed;
- }
- }
- private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
- {
- searchFilter = searchFilterTextBox.Text;
- Invalidate();
- }
- private int sortType = 0;
- private bool sortAsc = true;
- private void TextBlock_PreviewMouseDown(object sender, MouseButtonEventArgs e)
- {
- switch ((sender as TextBlock).Text)
- {
- case "<":
- if (currentPage > 1) currentPage--;
- return;
- case ">":
- if (currentPage < productCount / Globals.PAGE_LEN) currentPage++;
- return;
- default:
- currentPage = Convert.ToInt32(
- (sender as TextBlock).Text
- );
- return;
- }
- }
- private void SortTypeComboBox_SelectionChanged(
- object? sender,
- SelectionChangedEventArgs e)
- {
- if (SortTypeComboBox != null)
- {
- sortType = SortTypeComboBox.SelectedIndex;
- Invalidate();
- }
- }
- private void SearchFilterTextBox_OnKeyUp(object? sender, KeyEventArgs e)
- {
- if (searchFilterTextBox.Text != null && searchFilterTextBox.Text != "")
- {
- searchFilter = searchFilterTextBox.Text;
- Invalidate();
- }
- }
- private void ExitButton_Click(object sender, RoutedEventArgs e)
- {
- Application.Current.Shutdown();
- }
- public string costChangeButtonVisible
- {
- get
- {
- return productsSelectedCount > 1 ? "Visible" : "Hidden";
- }
- }
- }
- }
|