From a82b05bd60b4d89c6c7ce1603e5d525e0577f97b Mon Sep 17 00:00:00 2001
From: anerx <512464164@qq.com>
Date: Mon, 17 Mar 2025 22:44:15 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=9B=B8=E5=BA=94=E5=8A=9F?=
=?UTF-8?q?=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Services/IVarietyService.cs | 4 +
.../Services/VarietyService.cs | 43 +++--
.../ViewModels/VarietyViewModel.cs | 46 ++++-
Seyounth.Hyosung/Views/Pages/VarietyPage.xaml | 168 ++++++++++++++----
.../Views/Pages/VarietyPage.xaml.cs | 18 +-
5 files changed, 235 insertions(+), 44 deletions(-)
diff --git a/Seyounth.Hyosung.Data/Services/IVarietyService.cs b/Seyounth.Hyosung.Data/Services/IVarietyService.cs
index 1af60a6..dbdd33c 100644
--- a/Seyounth.Hyosung.Data/Services/IVarietyService.cs
+++ b/Seyounth.Hyosung.Data/Services/IVarietyService.cs
@@ -23,6 +23,10 @@ public interface IVarietyService
///
Task AddVarietyAsync(Variety variety);
+ Task UpdateVarietyAsync(Variety variety);
+
+ Task DeleteVarietyAsync(Variety variety);
+
///
/// 获取所有托盘信息
///
diff --git a/Seyounth.Hyosung.Data/Services/VarietyService.cs b/Seyounth.Hyosung.Data/Services/VarietyService.cs
index b141269..e2fd47d 100644
--- a/Seyounth.Hyosung.Data/Services/VarietyService.cs
+++ b/Seyounth.Hyosung.Data/Services/VarietyService.cs
@@ -10,22 +10,23 @@ public class VarietyService : IVarietyService
{
private readonly IRepository _varietyRepository;
private readonly IRepository _palletRepository;
- private readonly ConcurrentBag _varietiesCache;
- private readonly ConcurrentBag _palletsCache;
+ private readonly List _varietiesCache;
+ private readonly List _palletsCache;
public VarietyService(IServiceProvider provider)
{
try
{
- _varietyRepository = provider.CreateScope().ServiceProvider.GetRequiredService>();
+ _varietyRepository =
+ provider.CreateScope().ServiceProvider.GetRequiredService>();
_palletRepository = provider.CreateScope().ServiceProvider.GetRequiredService>();
- _varietiesCache = new ConcurrentBag(_varietyRepository.GetList());
- _palletsCache = new ConcurrentBag(_palletRepository.GetList());
- }catch(Exception e)
+ _varietiesCache = new List(_varietyRepository.GetList());
+ _palletsCache = new List(_palletRepository.GetList());
+ }
+ catch (Exception e)
{
Console.WriteLine(e.Message);
}
-
}
public async Task GetVarietyByCodeAsync(string code, int? layers = null)
@@ -44,13 +45,33 @@ public class VarietyService : IVarietyService
public async Task AddVarietyAsync(Variety variety)
{
- var entity = await _varietyRepository.InsertReturnEntityAsync(variety.ToEntity());
- _varietiesCache.Add(entity);
+ try
+ {
+ var entity = await _varietyRepository.InsertReturnEntityAsync(variety.ToEntity());
+ _varietiesCache.Add(entity);
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine(e.Message);
+ }
+
+ }
+
+ public async Task UpdateVarietyAsync(Variety variety)
+ {
+ await _varietyRepository.UpdateAsync(variety.ToEntity());
+ _varietiesCache[_varietiesCache.IndexOf(variety.ToEntity())] = variety.ToEntity();
+ }
+
+ public async Task DeleteVarietyAsync(Variety variety)
+ {
+ await _varietyRepository.DeleteAsync(variety.ToEntity());
+ _varietiesCache.Remove(variety.ToEntity());
}
public async Task> GetPalletsAsync()
{
- return _palletsCache.Select(p =>Pallet.FromEntity(p)).ToList();
+ return _palletsCache.Select(Pallet.FromEntity).ToList();
}
public async Task AddPalletAsync(Pallet pallet)
@@ -61,6 +82,6 @@ public class VarietyService : IVarietyService
public List GetAll()
{
- return _varietiesCache.Select(v=>Variety.Create(v, _palletsCache.ToList())).ToList();
+ return _varietiesCache.Select(v => Variety.Create(v, _palletsCache.ToList())).ToList();
}
}
\ No newline at end of file
diff --git a/Seyounth.Hyosung/ViewModels/VarietyViewModel.cs b/Seyounth.Hyosung/ViewModels/VarietyViewModel.cs
index aac7dc3..7cd008d 100644
--- a/Seyounth.Hyosung/ViewModels/VarietyViewModel.cs
+++ b/Seyounth.Hyosung/ViewModels/VarietyViewModel.cs
@@ -1,8 +1,11 @@
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;
@@ -12,13 +15,18 @@ public partial class VarietyViewModel : ObservableObject
[ObservableProperty] private ObservableCollection _pallets;
+
public IEnumerable NeedTypeItems { get; }
- public VarietyViewModel(PalletManagerViewModel palletManagerViewModel)
+ private readonly IVarietyService _varietyService;
+
+ public VarietyViewModel(PalletManagerViewModel palletManagerViewModel, IVarietyService varietyService)
{
Pallets = new ObservableCollection(palletManagerViewModel.Pallets);
- _varieties = new ObservableCollection();
+ _varietyService = varietyService;
+
+ _varieties = new ObservableCollection(varietyService.GetAll());
var needTypes = EnumHelper.GetValues();
NeedTypeItems = needTypes.Select(nt => new NeedTypeEnumItem
{
@@ -26,4 +34,38 @@ public partial class VarietyViewModel : ObservableObject
Description = nt.GetDescription()
});
}
+
+ public class SavePalletCompletedMessage
+ {
+ }
+
+ [RelayCommand]
+ private void OnDeletePallet(object obj)
+ {
+ if (obj is Variety variety)
+ {
+ Varieties.Remove(variety);
+ _varietyService.DeleteVarietyAsync(variety);
+ }
+ else
+ {
+ Console.Write("Object is not a Pallet");
+ }
+ }
+
+ [RelayCommand]
+ private void OnSavePallet(object obj)
+ {
+ if (obj is Variety variety)
+ {
+ if (variety.Id == 0)
+ {
+ _varietyService.AddVarietyAsync(variety);
+ }
+ else
+ _varietyService.UpdateVarietyAsync(variety);
+ }
+
+ WeakReferenceMessenger.Default.Send(new PalletManagerViewModel.SavePalletCompletedMessage());
+ }
}
\ No newline at end of file
diff --git a/Seyounth.Hyosung/Views/Pages/VarietyPage.xaml b/Seyounth.Hyosung/Views/Pages/VarietyPage.xaml
index a068f9c..9e730a4 100644
--- a/Seyounth.Hyosung/Views/Pages/VarietyPage.xaml
+++ b/Seyounth.Hyosung/Views/Pages/VarietyPage.xaml
@@ -61,13 +61,14 @@
ToolTip="托盘/隔板管理" />
-
+ ItemsSource="{Binding ViewModel.Varieties, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
+ CellEditEnding="DataGrid_CellEditEnding">
-
+
@@ -130,10 +132,11 @@
-
+
@@ -141,9 +144,10 @@
-
+
@@ -151,9 +155,10 @@
-
+
@@ -166,19 +171,122 @@
Binding="{Binding StackHeadCount }"
Header="垛头数量"
Width="Auto" />
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Seyounth.Hyosung/Views/Pages/VarietyPage.xaml.cs b/Seyounth.Hyosung/Views/Pages/VarietyPage.xaml.cs
index 4239094..70e7131 100644
--- a/Seyounth.Hyosung/Views/Pages/VarietyPage.xaml.cs
+++ b/Seyounth.Hyosung/Views/Pages/VarietyPage.xaml.cs
@@ -1,5 +1,6 @@
using System.Windows;
using System.Windows.Controls;
+using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using Seyounth.Hyosung.Data.Entities;
using Seyounth.Hyosung.ViewModels;
@@ -19,12 +20,27 @@ public partial class VarietyPage : Page
ViewModel = viewModel;
DataContext = this;
InitializeComponent();
+ WeakReferenceMessenger.Default.Register(this, (r, m) =>
+ {
+ // 提交当前编辑并取消编辑模式
+ VarietyDataGrid.CommitEdit();
+ VarietyDataGrid.CancelEdit();
+ });
}
-
+
private void TrayManagerButton_OnClick(object sender, RoutedEventArgs e)
{
var dialog = _serviceProvider.GetService();
dialog.ShowDialog();
}
+
+ private void DataGrid_CellEditEnding(object? sender, DataGridCellEditEndingEventArgs e)
+ {
+ if (e.EditAction == DataGridEditAction.Commit)
+ {
+ var binding = e.EditingElement.GetBindingExpression(TextBox.TextProperty);
+ binding?.UpdateSource();
+ }
+ }
}
\ No newline at end of file