using Avalonia.Controls; using System; using Agents.Models; using System.Collections.ObjectModel; using Avalonia.Interactivity; using System.Linq; using System.Runtime.ExceptionServices; namespace Agents { public partial class MainWindow : Window { static User11Context db = new User11Context(); // подключение к БД ObservableCollection AgentsitemSource = new ObservableCollection(db.Agents.OrderBy(it=>it.Title)); // основной лист для Агентов ObservableCollection ListAgents2; // второй лист для сортировки Агентов по умолчанию public MainWindow() { InitializeComponent(); ListAgents.ItemsSource = AgentsitemSource; ListAgents2 = AgentsitemSource; } private void SortAg(object? sender, Avalonia.Controls.SelectionChangedEventArgs e) // Выбор сортировки Агентов { SortAgent(); } private void ListAgents_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e) // Событие выбора Агента и вывод спика продуктов { UpdateInfoListProduct(); } private void SortListProductAgents_method(object? sender, Avalonia.Controls.SelectionChangedEventArgs e) // выбор сортировки списка товаров { UpdateInfoListProduct(); } void SortAgent() // сортировка Агентов { switch (SortListAgents.SelectedIndex) { case 1: AgentsitemSource = new ObservableCollection(AgentsitemSource.OrderByDescending(it => it.Title).ToList()); break; // по убыванию case 2: AgentsitemSource = new ObservableCollection(AgentsitemSource.OrderBy(it => it.Title).ToList()); break; // по возрастанию default: AgentsitemSource = ListAgents2; break; // изначальный порядок без сортировки } ListAgents.ItemsSource = new ObservableCollection(AgentsitemSource); } private void UpdateInfoListProduct() // список товаров и информация о них { Agent? agent = db.Agents.Where(it => it == ListAgents.SelectedItem).FirstOrDefault(); // находим выделенного Агента if (agent != null) { Productsale? PS = db.Productsales.Where(it => it.Agentid == agent.Id).FirstOrDefault(); var prods = db.Productsales .Where(PWS => PWS.Agentid == agent.Id) .Select(PWS => new ProductDTO { Title = PWS.Product.Title, Image = PWS.Product.Image, Producttypeid = PWS.Product.Producttypeid, Productionpersoncount = PWS.Product.Productionpersoncount, Mincostforagent = PWS.Product.Mincostforagent, Description = PWS.Product.Description, Articlenumber = PWS.Product.Articlenumber, SalesCount = db.Productsales.Where(it => it.Productid == PWS.Productid).Count(), ProductDiscount = productDiscount(db.Productsales.Where(it => it.Productid == PWS.Productid).Count()) }).ToList(); switch (SortListProductAgents.SelectedIndex) { case 1: prods = prods.OrderBy(p => p.Title).ToList(); break; case 2: prods = prods.OrderByDescending(p => p.Title).ToList(); break; default: break; } ListProducts.ItemsSource = new ObservableCollection(prods); } } static int productDiscount(int SalesCount) // расчёт скидок { int Discount = 0; if (10_000 <= SalesCount && SalesCount < 50_000) { Discount = 5; } else if (50_000 <= SalesCount && SalesCount < 150_000) { Discount = 10; } else if (150_000 <= SalesCount && SalesCount < 500_000) { Discount = 20; } else if (SalesCount>500_000) { Discount = 25; } return Discount; } void Button_search(object sender, RoutedEventArgs e) // поисковик агентов { string? Search = search.Text; if (search.Text != "") { AgentsitemSource = new ObservableCollection(db.Agents.Where(it => it.Title == search.Text || it.Address == search.Text || it.Inn == search.Text || it.Kpp == search.Text || it.Directorname == search.Text || it.Phone == search.Text || it.Email == search.Text )); if (AgentsitemSource != null) { ListAgents.ItemsSource = AgentsitemSource; ListAgents2 = AgentsitemSource; SortAgent(); } else { ListAgents.ItemsSource = null; } } else { AgentsitemSource = new ObservableCollection(db.Agents.OrderBy(it => it.Title)); ListAgents2 = AgentsitemSource; SortAgent(); } } private void ButtonUpdate(object sender, RoutedEventArgs e) // Обновление списка продуктов для выделенного Агента { UpdateInfoListProduct(); } private void ButtonAgentEidtor(object sender, RoutedEventArgs e) // переход в окно редактирования Агентов { new AgentEidtor().Show(); Close(); } private void ButtonProductEidtor(object sender, RoutedEventArgs e) // переход в окно редактирования товаров { new ProductEditor().Show(); Close(); } private void ButtonAddProduct(object sender, RoutedEventArgs e) // переход в окно добавления товаров { new AddProduct().Show(); Close(); } } }