2025-03-25 18:36:42 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2024-12-26 13:15:57 +00:00
|
|
|
|
using Avalonia.Controls;
|
2025-03-26 09:39:33 +00:00
|
|
|
|
using Avalonia.Interactivity;
|
2025-04-03 20:00:11 +00:00
|
|
|
|
using Avalonia.Media;
|
2025-03-25 18:36:42 +00:00
|
|
|
|
using Avalonia.Media.Imaging;
|
2025-04-03 20:00:11 +00:00
|
|
|
|
using demofinish.Model;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-12-26 13:15:57 +00:00
|
|
|
|
|
|
|
|
|
namespace demofinish
|
|
|
|
|
{
|
|
|
|
|
public partial class MainWindow : Window
|
|
|
|
|
{
|
2025-03-25 18:36:42 +00:00
|
|
|
|
private ObservableCollection<Agent> agents = new ObservableCollection<Agent>();
|
2025-04-11 08:55:27 +00:00
|
|
|
|
private List<AgentPresenter> allAgents = new List<AgentPresenter>();
|
|
|
|
|
private List<AgentPresenter> filteredAgents = new List<AgentPresenter>();
|
2025-03-26 09:39:33 +00:00
|
|
|
|
private const int pageSize = 10;
|
|
|
|
|
private int currentPage = 1;
|
|
|
|
|
private int pageCount = 0;
|
2025-03-25 18:36:42 +00:00
|
|
|
|
|
2024-12-26 13:15:57 +00:00
|
|
|
|
public MainWindow()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2025-03-25 18:36:42 +00:00
|
|
|
|
LoadAgents();
|
2025-03-31 18:03:22 +00:00
|
|
|
|
ComboboxAgentType();
|
2025-04-11 08:55:27 +00:00
|
|
|
|
InitializeSearchFields();
|
|
|
|
|
|
2025-03-31 18:03:22 +00:00
|
|
|
|
TypeAgentCombobox.SelectionChanged += TypeAgentCombobox_SelectionChanged;
|
|
|
|
|
NameComboBox.SelectionChanged += SortName_SelectedChanged;
|
|
|
|
|
PriorityCombobox.SelectionChanged += PrioritySort_SelectedChanged;
|
2025-04-03 20:00:11 +00:00
|
|
|
|
SaleCombobox.SelectionChanged += SaleSort_SelectedChanged;
|
2025-04-02 11:50:36 +00:00
|
|
|
|
}
|
2025-03-26 09:39:33 +00:00
|
|
|
|
|
|
|
|
|
public class AgentPresenter : Agent
|
2025-03-25 18:36:42 +00:00
|
|
|
|
{
|
2025-04-03 20:00:11 +00:00
|
|
|
|
public string AgentTypeName { get; set; }
|
|
|
|
|
public int DiscountPercent { get; set; }
|
|
|
|
|
public decimal TotalSales { get; set; }
|
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
public Bitmap? LogoImage
|
2025-03-25 18:36:42 +00:00
|
|
|
|
{
|
2025-03-26 09:39:33 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-04-11 08:55:27 +00:00
|
|
|
|
string absolutePath = Path.Combine(AppContext.BaseDirectory, Logo);
|
|
|
|
|
return new Bitmap(absolutePath);
|
2025-03-26 09:39:33 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2025-04-03 20:00:11 +00:00
|
|
|
|
return null;
|
2025-03-26 09:39:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-25 18:36:42 +00:00
|
|
|
|
}
|
2025-03-26 09:39:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-03 20:00:11 +00:00
|
|
|
|
private void LoadAgents()
|
2024-12-26 13:15:57 +00:00
|
|
|
|
{
|
2025-03-25 18:36:42 +00:00
|
|
|
|
using var context = new User1Context();
|
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
allAgents = context.Agents
|
2025-04-03 20:00:11 +00:00
|
|
|
|
.Include(a => a.Agenttype)
|
|
|
|
|
.Include(a => a.Productsales)
|
|
|
|
|
.ThenInclude(ps => ps.Product)
|
|
|
|
|
.AsEnumerable()
|
|
|
|
|
.Select(agent =>
|
|
|
|
|
{
|
|
|
|
|
decimal totalSales = agent.Productsales?.Sum(ps =>
|
|
|
|
|
ps.Productcount * (ps.Product?.Mincostforagent ?? 0)) ?? 0;
|
|
|
|
|
|
|
|
|
|
return new AgentPresenter
|
|
|
|
|
{
|
|
|
|
|
Id = agent.Id,
|
|
|
|
|
Title = agent.Title,
|
|
|
|
|
Agenttypeid = agent.Agenttypeid,
|
|
|
|
|
AgentTypeName = agent.Agenttype?.Title ?? "Неизвестный тип",
|
|
|
|
|
Address = agent.Address,
|
|
|
|
|
Inn = agent.Inn,
|
|
|
|
|
Kpp = agent.Kpp,
|
|
|
|
|
Directorname = agent.Directorname,
|
|
|
|
|
Phone = agent.Phone,
|
|
|
|
|
Email = agent.Email,
|
|
|
|
|
Logo = agent.Logo,
|
|
|
|
|
Priority = agent.Priority,
|
|
|
|
|
TotalSales = totalSales,
|
|
|
|
|
DiscountPercent = CalculateDiscount(totalSales)
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
filteredAgents = allAgents.ToList();
|
|
|
|
|
UpdateDisplay();
|
2025-03-26 09:39:33 +00:00
|
|
|
|
}
|
2025-04-03 20:00:11 +00:00
|
|
|
|
|
|
|
|
|
private int CalculateDiscount(decimal totalSales)
|
|
|
|
|
{
|
|
|
|
|
if (totalSales >= 500000) return 25;
|
|
|
|
|
if (totalSales >= 150000) return 20;
|
|
|
|
|
if (totalSales >= 50000) return 10;
|
|
|
|
|
if (totalSales >= 10000) return 5;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
private void ComboboxAgentType()
|
|
|
|
|
{
|
|
|
|
|
using var context = new User1Context();
|
|
|
|
|
var types = context.Agenttypes.Select(x => x.Title).ToList();
|
|
|
|
|
types.Insert(0, "Все типы");
|
|
|
|
|
TypeAgentCombobox.ItemsSource = types;
|
|
|
|
|
TypeAgentCombobox.SelectedIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitializeSearchFields()
|
|
|
|
|
{
|
|
|
|
|
using var context = new User1Context();
|
|
|
|
|
|
|
|
|
|
var searchItems = context.Agents
|
|
|
|
|
.Select(a => a.Title)
|
|
|
|
|
.Union(context.Agents.Select(a => a.Phone))
|
|
|
|
|
.Union(context.Agents.Select(a => a.Email))
|
|
|
|
|
.Where(x => !string.IsNullOrEmpty(x))
|
|
|
|
|
.ToList();
|
2025-04-01 11:42:23 +00:00
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
SearchBox.ItemsSource = searchItems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SearchBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
2025-04-03 20:00:11 +00:00
|
|
|
|
{
|
2025-04-11 08:55:27 +00:00
|
|
|
|
if (SearchBox.SelectedItem == null || string.IsNullOrEmpty(SearchBox.Text))
|
|
|
|
|
{
|
|
|
|
|
ApplyFilters();
|
2025-04-03 20:00:11 +00:00
|
|
|
|
return;
|
2025-04-11 08:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string searchValue = SearchBox.SelectedItem.ToString();
|
|
|
|
|
filteredAgents = allAgents
|
|
|
|
|
.Where(a => a.Title.Contains(searchValue) ||
|
|
|
|
|
a.Phone.Contains(searchValue) ||
|
|
|
|
|
a.Email.Contains(searchValue))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
ApplyCurrentSort();
|
|
|
|
|
UpdateDisplay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TypeAgentCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ApplyFilters();
|
|
|
|
|
}
|
2025-04-03 20:00:11 +00:00
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
private void ApplyFilters()
|
|
|
|
|
{
|
|
|
|
|
if (TypeAgentCombobox.SelectedItem == null)
|
|
|
|
|
return;
|
2025-04-03 20:00:11 +00:00
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
using var context = new User1Context();
|
|
|
|
|
|
|
|
|
|
if (TypeAgentCombobox.SelectedItem.ToString() == "Все типы")
|
2025-04-03 20:00:11 +00:00
|
|
|
|
{
|
2025-04-11 08:55:27 +00:00
|
|
|
|
filteredAgents = allAgents.ToList();
|
2025-04-03 20:00:11 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-04-11 08:55:27 +00:00
|
|
|
|
string selectedType = TypeAgentCombobox.SelectedItem.ToString();
|
|
|
|
|
var selectedTypeId = context.Agenttypes
|
|
|
|
|
.Where(at => at.Title == selectedType)
|
|
|
|
|
.Select(at => at.Id)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
filteredAgents = allAgents
|
|
|
|
|
.Where(x => x.Agenttypeid == selectedTypeId)
|
|
|
|
|
.ToList();
|
2025-04-03 20:00:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
ApplyCurrentSort();
|
|
|
|
|
currentPage = 1;
|
|
|
|
|
UpdateDisplay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ApplyCurrentSort()
|
|
|
|
|
{
|
|
|
|
|
if (NameComboBox.SelectedItem != null)
|
|
|
|
|
{
|
|
|
|
|
filteredAgents = NameComboBox.SelectedItem.ToString() == "по возрастанию"
|
|
|
|
|
? filteredAgents.OrderBy(it => it.Title).ToList()
|
|
|
|
|
: filteredAgents.OrderByDescending(it => it.Title).ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (PriorityCombobox.SelectedItem != null)
|
|
|
|
|
{
|
|
|
|
|
filteredAgents = PriorityCombobox.SelectedItem.ToString() == "по возрастанию"
|
|
|
|
|
? filteredAgents.OrderBy(it => it.Priority).ToList()
|
|
|
|
|
: filteredAgents.OrderByDescending(it => it.Priority).ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (SaleCombobox.SelectedItem != null)
|
2025-04-03 20:00:11 +00:00
|
|
|
|
{
|
2025-04-11 08:55:27 +00:00
|
|
|
|
filteredAgents = SaleCombobox.SelectedItem.ToString() == "по возрастанию"
|
|
|
|
|
? filteredAgents.OrderBy(it => it.DiscountPercent).ToList()
|
|
|
|
|
: filteredAgents.OrderByDescending(it => it.DiscountPercent).ToList();
|
2025-04-03 20:00:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-25 18:36:42 +00:00
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
private void SortName_SelectedChanged(object sender, SelectionChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (NameComboBox.SelectedItem == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ApplyCurrentSort();
|
|
|
|
|
currentPage = 1;
|
|
|
|
|
UpdateDisplay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PrioritySort_SelectedChanged(object sender, SelectionChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (PriorityCombobox.SelectedItem == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ApplyCurrentSort();
|
|
|
|
|
currentPage = 1;
|
|
|
|
|
UpdateDisplay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaleSort_SelectedChanged(object sender, SelectionChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (SaleCombobox.SelectedItem == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ApplyCurrentSort();
|
|
|
|
|
currentPage = 1;
|
|
|
|
|
UpdateDisplay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateDisplay()
|
2025-03-26 09:39:33 +00:00
|
|
|
|
{
|
2025-03-25 18:36:42 +00:00
|
|
|
|
agents.Clear();
|
2025-04-11 08:55:27 +00:00
|
|
|
|
var pageAgents = filteredAgents
|
2025-03-31 18:03:22 +00:00
|
|
|
|
.Skip((currentPage - 1) * pageSize)
|
|
|
|
|
.Take(pageSize);
|
2025-04-11 08:55:27 +00:00
|
|
|
|
|
|
|
|
|
foreach (var agent in pageAgents)
|
2025-03-25 18:36:42 +00:00
|
|
|
|
{
|
2025-04-11 08:55:27 +00:00
|
|
|
|
agents.Add(agent);
|
2025-03-25 18:36:42 +00:00
|
|
|
|
}
|
2025-04-03 20:00:11 +00:00
|
|
|
|
|
|
|
|
|
AgentListBox.ItemsSource = agents;
|
2025-04-11 08:55:27 +00:00
|
|
|
|
pageCount = (int)Math.Ceiling(filteredAgents.Count / (double)pageSize);
|
2025-03-26 09:39:33 +00:00
|
|
|
|
}
|
2025-03-25 18:36:42 +00:00
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
private void PrevPage_Click(object sender, RoutedEventArgs e)
|
2025-03-26 09:39:33 +00:00
|
|
|
|
{
|
2025-04-11 08:55:27 +00:00
|
|
|
|
if (currentPage > 1)
|
2025-03-26 09:39:33 +00:00
|
|
|
|
{
|
2025-04-11 08:55:27 +00:00
|
|
|
|
currentPage--;
|
|
|
|
|
UpdateDisplay();
|
2025-03-26 09:39:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
private void NextPage_Click(object sender, RoutedEventArgs e)
|
2025-03-26 09:39:33 +00:00
|
|
|
|
{
|
2025-04-11 08:55:27 +00:00
|
|
|
|
if (currentPage < pageCount)
|
2025-03-26 09:39:33 +00:00
|
|
|
|
{
|
2025-04-11 08:55:27 +00:00
|
|
|
|
currentPage++;
|
|
|
|
|
UpdateDisplay();
|
2025-03-26 09:39:33 +00:00
|
|
|
|
}
|
2024-12-26 13:15:57 +00:00
|
|
|
|
}
|
2025-03-26 09:39:33 +00:00
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
private void AddNew_Agent(object sender, RoutedEventArgs e)
|
2025-04-01 11:42:23 +00:00
|
|
|
|
{
|
|
|
|
|
new AddAgent_Window().ShowDialog(this);
|
|
|
|
|
}
|
2025-04-02 10:09:44 +00:00
|
|
|
|
|
2025-04-11 08:55:27 +00:00
|
|
|
|
private void AgentListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
2025-04-02 10:09:44 +00:00
|
|
|
|
{
|
|
|
|
|
if (AgentListBox.SelectedItem is AgentPresenter selectedAgent)
|
|
|
|
|
{
|
|
|
|
|
var editWindow = new EditWindow(selectedAgent);
|
|
|
|
|
editWindow.ShowDialog(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-18 11:41:58 +00:00
|
|
|
|
|
|
|
|
|
private void HistoryRealisation(object? sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
new HistoryWindow().ShowDialog(this);
|
|
|
|
|
}
|
2024-12-26 13:15:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|