demo_trade/ViewModels/OrderManagerViewModel.cs

82 lines
3.0 KiB
C#
Raw Normal View History

2024-10-04 12:41:04 +00:00
using demo_trade.Domain;
using demo_trade.Models;
using DynamicData;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Text;
using System.Threading.Tasks;
namespace demo_trade.ViewModels
{
public class OrderManagerViewModel : ViewModelBase, IRoutableViewModel
{
OrderManagerUseCase _orderManagerUseCase;
public string? UrlPathSegment { get; } = Guid.NewGuid().ToString();
private ObservableCollection<OrderManager> _orders = new();
private Dictionary<int, OrderChanged> _changedOrdes = new();
private List<OrderManager> _dateSource = new();
public ObservableCollection<OrderManager> Orders { get => _orders; set => this.RaiseAndSetIfChanged(ref _orders, value); }
private int _selectedSortNumber = 0;
public int SelectedSortNumber { get => _selectedSortNumber; set => this.RaiseAndSetIfChanged(ref _selectedSortNumber, value); }
public IScreen HostScreen { get; }
public ReactiveCommand<Unit, Unit> ConfirmChangeCommand { get; }
public OrderManagerViewModel(IScreen screen, OrderManagerUseCase orderManagerUseCase)
{
_orderManagerUseCase = orderManagerUseCase;
_dateSource = _orderManagerUseCase.GetOrders();
this.WhenAnyValue(sort => sort.SelectedSortNumber).Subscribe(_ => DisplayList());
ConfirmChangeCommand = ReactiveCommand.Create(() => {
if (_changedOrdes.Values.Count == 0) return;
_orderManagerUseCase.ChangeOrders(_changedOrdes.Values.ToList());
});
HostScreen = screen;
}
private void DisplayList() {
var temp = _dateSource;
if (SelectedSortNumber == 1)
{
temp = temp.OrderByDescending(it => it.OrderSumCost).ToList();
}
else if (SelectedSortNumber == 2) temp = temp.OrderBy(it => it.OrderSumCost).ToList();
_orders.Clear();
foreach (var order in temp)
{
_orders.Add(order);
}
}
public void OrderDateChanged(int id, DateTimeOffset dateDelivery) {
if (dateDelivery.DateTime <= DateTime.Now) return;
if (_changedOrdes.ContainsKey(id)) {
_changedOrdes[id].OrderDeliveryDate = DateOnly.FromDateTime(dateDelivery.DateTime);
return;
}
_changedOrdes.Add(id, new OrderChanged { OrderDeliveryDate = DateOnly.FromDateTime(dateDelivery.DateTime), OrderId= id });
}
public void OrderStatusChanged(int id, int status)
{
if (status != 1) return;
if (_changedOrdes.ContainsKey(id))
{
_changedOrdes[id].OrderStatus = "Завершен";
return;
}
_changedOrdes.Add(id, new OrderChanged { OrderStatus = "Завершен", OrderId = id });
}
}
}