Agents/MainWindow.axaml.cs
2025-04-05 20:05:02 +03:00

168 lines
6.7 KiB
C#

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<Agent> AgentsitemSource = new ObservableCollection<Agent>(db.Agents.OrderBy(it=>it.Title)); // îñíîâíîé ëèñò äëÿ Àãåíòîâ
ObservableCollection<Agent> 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<Agent>(AgentsitemSource.OrderByDescending(it => it.Title).ToList()); break; // ïî óáûâàíèþ
case 2: AgentsitemSource = new ObservableCollection<Agent>(AgentsitemSource.OrderBy(it => it.Title).ToList()); break; // ïî âîçðàñòàíèþ
default: AgentsitemSource = ListAgents2; break; // èçíà÷àëüíûé ïîðÿäîê áåç ñîðòèðîâêè
}
ListAgents.ItemsSource = new ObservableCollection<Agent>(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<ProductDTO>(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<Agent>(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<Agent>(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();
}
}
}