sample_demo/ServiceApp/ServiceWindowAdmin.axaml.cs

135 lines
5.1 KiB
C#
Raw Normal View History

using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using System.Linq;
using ServiceApp.EnityModels;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using SkiaSharp;
using Microsoft.EntityFrameworkCore.Storage.Json;
using System;
using Npgsql.TypeMapping;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
namespace ServiceApp;
public partial class ServiceAdminWindow : Window
{
public List<ServicePresenter> sourceList { get; set; }
public ObservableCollection<ServicePresenter> displayList { get; }
private string _searchWord = string.Empty;
private string[] _sortingSource = new string[3] { "<22><><EFBFBD><EFBFBD><EFBFBD>.", "<22><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.", "<22><> <20><><EFBFBD><EFBFBD>." };
private Dictionary<string, (int, int)> _dicountFilter = new Dictionary<string, (int, int)> {
{"<22><><EFBFBD>", (0, 0) },
{ "0-5", (0,5)},
{ "15-30", (5,15)},
{ "30-70", (30,70)},
{ "70-100", (70,100)},
};
public ServiceAdminWindow()
{
InitializeComponent();
using (var dbContext = new IsajkinContext())
{
sourceList = dbContext.Services.Select(service =>
new ServicePresenter
{
Title = service.Title,
Discount = service.Discount,
Durationinminutes = service.Durationinminutes,
Id = service.Id,
Cost = service.Cost,
Mainimagepath = service.Mainimagepath,
}
).ToList();
}
displayList = new ObservableCollection<ServicePresenter>(sourceList);
SortingComboBox.ItemsSource = _sortingSource;
ServiceListBox.ItemsSource = displayList;
FilteringComboBox.ItemsSource = _dicountFilter.Keys;
StatisticTextBlock.Text = String.Format("{0} <20><> {1}", displayList.Count, sourceList.Count);
}
private void DisplayService() {
var displayServiceList = sourceList;
if (FilteringComboBox.SelectedIndex > 0) displayServiceList = displayServiceList.Where(service =>
FilterByDiscount(service, FilteringComboBox.SelectionBoxItem.ToString())).ToList();
if(!String.IsNullOrEmpty(_searchWord)) displayServiceList = displayServiceList.Where(service => SearchByWord(service, _searchWord)).ToList();
if(SortingComboBox.SelectedIndex > 0) displayServiceList = SortingComboBox.SelectedIndex == 1?
displayServiceList.OrderBy(service => service.Cost).ToList() :
displayServiceList.OrderByDescending(service => service.Cost).ToList();
displayList.Clear();
foreach (var service in displayServiceList) { displayList.Add(service); }
StatisticTextBlock.Text = String.Format("{0} <20><> {1}", displayServiceList.Count, sourceList.Count);
}
private bool SearchByWord(ServicePresenter service, string word) {
if (service.Title.Contains(word, StringComparison.CurrentCultureIgnoreCase)) return true;
if (String.IsNullOrEmpty(service.Description)) return false;
if (service.Description.Contains(word, StringComparison.CurrentCultureIgnoreCase)) return true;
return false;
}
private bool FilterByDiscount(ServicePresenter servicePresenter, string key) {
(int left, int right) = _dicountFilter[key];
double discount = servicePresenter.Discount != null? Convert.ToDouble(servicePresenter.Discount) * 100 : 0;
return left <= discount && discount < right;
}
private void SearchTextBox_TextChanged(object? sender, Avalonia.Controls.TextChangedEventArgs e)
{
_searchWord = SearchTextBox.Text.ToString();
DisplayService();
}
private void FilteringComboBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
{
DisplayService();
}
private void SortingComboBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
{
DisplayService();
}
private void EditButton_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
}
private void RemoveButton_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
var serviceID = Convert.ToInt32((sender as Button).Tag);
using (var dbContext = new IsajkinContext()) {
var service = dbContext.Services.First(service => service.Id == serviceID);
var serviceLocal = displayList.First(service => service.Id == serviceID);
if (service == null) return;
if (dbContext.Clientservices.Where(item => item.Serviceid == serviceID).Count() > 0) return;
using var transaction = dbContext.Database.BeginTransaction();
try
{
if (dbContext.Servicephotos.Where(item => item.Serviceid == serviceID).Count() > 0)
{
dbContext.RemoveRange(dbContext.Servicephotos.Where(item => item.Serviceid == serviceID));
}
dbContext.Services.Remove(service);
displayList.Remove(serviceLocal);
dbContext.SaveChanges();
transaction.Commit();
}
catch (Exception ex) {
}
} ;
}
}