demo_trade/ViewModels/ClientProductViewModel.cs

180 lines
6.7 KiB
C#
Raw Permalink Normal View History

2024-10-04 12:41:04 +00:00
using Avalonia.Markup.Xaml.Templates;
using demo_trade.Data.RemoteData.Entity;
using demo_trade.Data.Repository;
using demo_trade.Domain;
using demo_trade.Models;
using demo_trade.UI.Presenters;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using Tmds.DBus.Protocol;
namespace demo_trade.ViewModels
{
public class ClientProductViewModel : ViewModelBase, IRoutableViewModel
{
private GuestProductUseCase _guestProductUseCase;
public string? UrlPathSegment => Guid.NewGuid().ToString();
private User _user = new();
private bool _isManager = false;
public bool IsManager { get => _isManager; set => this.RaiseAndSetIfChanged(ref _isManager, value); }
private Dictionary<string, (ProductPresenter, int)> _productOrder = new();
public Dictionary<string, (ProductPresenter, int)> ProductOrder
{
get => _productOrder;
set => this.RaiseAndSetIfChanged(ref _productOrder, value);
}
public User AutorizedUser {
get => _user;
set => this.RaiseAndSetIfChanged(ref _user, value);
}
private List<ProductPresenter> _productOrderList = new();
public List<ProductPresenter> ProductOrderList
{
get => _productOrderList;
set => this.RaiseAndSetIfChanged(ref _productOrderList, value);
}
private bool _thereItemsInOrder = false;
public bool ThereItemsInOrder
{
get => _thereItemsInOrder;
set => this.RaiseAndSetIfChanged(ref _thereItemsInOrder, value);
}
private Dictionary<int, (int, int?)> _filterValues = new Dictionary<int, (int, int?)>() {
{0, (0, null)},
{1, (0, 10)},
{2, (10, 15)},
{3, (15, null)}
};
public ReactiveCommand<ProductPresenter, Unit> AddToOrderCommand { get; }
public ReactiveCommand<Unit, Unit> AttachToOrderCommand { get; }
public ReactiveCommand<Unit, Unit> AttachToOrderManagerCommand { get; }
private int _selectedFilterValue = 0;
private int _selectedSortValue = 0;
public int SelectedFilterValue
{
get => _selectedFilterValue;
set => this.RaiseAndSetIfChanged(ref _selectedFilterValue, value);
}
public int SelectedSortValue
{
get => _selectedSortValue;
set => this.RaiseAndSetIfChanged(ref _selectedSortValue, value);
}
private string _searchWord = string.Empty;
public string SearchWord
{
get => _searchWord;
set => this.RaiseAndSetIfChanged(ref _searchWord, value);
}
private string _statisticText = string.Empty;
public string StatisticText
{
get => _statisticText;
set => this.RaiseAndSetIfChanged(ref _statisticText, value);
}
private List<ProductPresenter> _dataSource = new();
private List<ProductPresenter> _products = new();
public List<ProductPresenter> Products
{
get => _products;
set => this.RaiseAndSetIfChanged(ref _products, value);
}
public IScreen HostScreen { get; }
public ClientProductViewModel(IScreen home, IProductRepository productRepository, User user)
{
AutorizedUser = user;
HostScreen = home;
if (AutorizedUser.Userrole == 1 || AutorizedUser.Userrole == 3) IsManager = true;
_guestProductUseCase = new GuestProductUseCase(productRepository);
_dataSource = _guestProductUseCase.GetProducts();
this.WhenAnyValue(search => search.SearchWord).Subscribe(_ => { DisplayList(); });
this.WhenAnyValue(selectedFilter => selectedFilter.SelectedFilterValue).Subscribe(_ => { DisplayList(); });
this.WhenAnyValue(selectedSort => selectedSort.SelectedSortValue).Subscribe(_ => { DisplayList(); });
DisplayList();
AddToOrderCommand = ReactiveCommand.Create<ProductPresenter>(product => {
if (ProductOrder.ContainsKey(product.ArticleNumber))
{
var values = ProductOrder[product.ArticleNumber];
ProductOrder[product.ArticleNumber] = (product, values.Item2 + 1);
}
else
{
ProductOrder.Add(product.ArticleNumber, (product, 1));
}
ThereItemsInOrder = ProductOrder.Keys.Count > 0;
});
AttachToOrderManagerCommand = ReactiveCommand.Create(
() =>
{
var orderManagerUseCase = new OrderManagerUseCase(new SQLOrderManagerRepository());
HostScreen.Router.Navigate.Execute(new OrderManagerViewModel(HostScreen, orderManagerUseCase));
}
);
ShowDialog = new Interaction<OrderShowDialogViewModel, bool>();
AttachToOrderCommand = ReactiveCommand.CreateFromTask(async () =>
{
var userOrderCase = new OrderClientUseCase(new SQLOrderClientRepository(), new SQLPickupRepository());
var order = new OrderShowDialogViewModel(ProductOrder, userOrderCase, user);
var result = await ShowDialog.Handle(order);
}
);
}
public Interaction<OrderShowDialogViewModel, bool> ShowDialog { get; }
private bool FilterByDiscount(short? discount)
{
if (discount == null) return false;
(int leftBound, int? rightBound) = _filterValues[SelectedFilterValue];
if (rightBound == null) return leftBound <= discount;
return leftBound <= discount && discount < rightBound;
}
private void DisplayList()
{
var temp = _dataSource;
if (SelectedFilterValue > 0)
{
temp = temp.Where(it => FilterByDiscount(it.DiscountAmount)).ToList();
}
if (!String.IsNullOrEmpty(SearchWord))
{
temp = temp.Where(it => it.Name.Contains(SearchWord, StringComparison.CurrentCultureIgnoreCase)).ToList();
}
if (SelectedSortValue == 1)
{
temp = temp.OrderByDescending(it => it.Cost).ToList();
}
else if (SelectedSortValue == 2)
{
temp = temp.OrderBy(it => it.Cost).ToList();
}
this.Products = temp;
StatisticText = String.Format("{0} из {1}", temp.Count, _dataSource.Count);
}
}
}