2025-03-18 20:42:05 +08:00

78 lines
2.2 KiB
C#

using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Seyounth.Core.Extensions;
using Seyounth.Hyosung.Data.Entities;
using Seyounth.Hyosung.Data.Models;
using Seyounth.Hyosung.Data.Services;
namespace Seyounth.Hyosung.ViewModels;
public partial class VarietyViewModel : ObservableObject
{
[ObservableProperty] private ObservableCollection<Variety> _varieties;
[ObservableProperty] private ObservableCollection<Pallet> _pallets;
public IEnumerable<NeedTypeEnumItem> NeedTypeItems { get; }
private readonly IVarietyService _varietyService;
private readonly HomeViewModel _homeViewModel;
public VarietyViewModel(PalletManagerViewModel palletManagerViewModel, IVarietyService varietyService,
HomeViewModel homeViewModel)
{
Pallets = new ObservableCollection<Pallet>(palletManagerViewModel.Pallets);
_homeViewModel = homeViewModel;
_varietyService = varietyService;
_varieties = new ObservableCollection<Variety>(varietyService.GetAll());
var needTypes = EnumHelper.GetValues<NeedType>();
NeedTypeItems = needTypes.Select(nt => new NeedTypeEnumItem
{
Value = nt,
Description = nt.GetDescription()
});
}
public class SaveVarietyCompletedMessage
{
}
[RelayCommand]
private void OnDeleteVariety(object obj)
{
if (obj is Variety variety)
{
Varieties.Remove(variety);
_varietyService.DeleteVarietyAsync(variety);
}
else
{
Console.Write("Object is not a Pallet");
}
}
[RelayCommand]
private void OnSaveVariety(object obj)
{
if (obj is Variety variety)
{
if (variety.Id == 0)
{
_varietyService.AddVarietyAsync(variety);
}
else
_varietyService.UpdateVarietyAsync(variety);
}
//同步更新主页的品种列表
_homeViewModel.Varieties = Varieties;
WeakReferenceMessenger.Default.Send(new SaveVarietyCompletedMessage());
}
}