78 lines
2.2 KiB
C#
Raw Permalink Normal View History

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