151 lines
4.5 KiB
C#
151 lines
4.5 KiB
C#
|
|
using Avalonia.Controls.Notifications;
|
|||
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|||
|
|
using CommunityToolkit.Mvvm.Input;
|
|||
|
|
using Cowain.Base.Helpers;
|
|||
|
|
using Cowain.Base.ViewModels;
|
|||
|
|
using Ke.Bee.Localization.Localizer.Abstractions;
|
|||
|
|
using Microsoft.Extensions.DependencyInjection;
|
|||
|
|
using Plugin.Cowain.Wcs.IServices;
|
|||
|
|
using Plugin.Cowain.Wcs.Services;
|
|||
|
|
using System.Collections.ObjectModel;
|
|||
|
|
using Ursa.Controls;
|
|||
|
|
|
|||
|
|
namespace Plugin.Cowain.Wcs.ViewModels;
|
|||
|
|
|
|||
|
|
public partial class RgvManagement6180ViewModel : PageViewModelBase
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
[ObservableProperty]
|
|||
|
|
private ObservableCollection<Rgv6180ViewModel>? _rgvList;
|
|||
|
|
[ObservableProperty]
|
|||
|
|
private ObservableCollection<StationViewModel>? _stations;
|
|||
|
|
[ObservableProperty]
|
|||
|
|
private int _totals;
|
|||
|
|
[ObservableProperty]
|
|||
|
|
private int _pageSize;
|
|||
|
|
[ObservableProperty]
|
|||
|
|
private int _pageIndex;
|
|||
|
|
|
|||
|
|
|
|||
|
|
private readonly ILocalizer _l;
|
|||
|
|
private IStationService _stationService;
|
|||
|
|
private readonly IRgv6180Service _rgvService;
|
|||
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
|||
|
|
public RgvManagement6180ViewModel(ILocalizer localizer, IRgv6180Service rgvService, IStationService stationService, IServiceScopeFactory scopeFactory)
|
|||
|
|
{
|
|||
|
|
_l = localizer;
|
|||
|
|
_scopeFactory = scopeFactory;
|
|||
|
|
_rgvService = rgvService;
|
|||
|
|
_stationService = stationService;
|
|||
|
|
PageSize = 10;
|
|||
|
|
PageIndex = 1;
|
|||
|
|
RgvList = new ObservableCollection<Rgv6180ViewModel>();
|
|||
|
|
// 异步调用刷新
|
|||
|
|
RefreshCommand.ExecuteAsync(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
[RelayCommand]
|
|||
|
|
private async Task RefreshAsync(int pageIndex)
|
|||
|
|
{
|
|||
|
|
if (Stations == null)
|
|||
|
|
{
|
|||
|
|
var stationList = await _stationService.GetAllAsync();
|
|||
|
|
Stations = new ObservableCollection<StationViewModel>(stationList);
|
|||
|
|
StationViewModel nullStation = new StationViewModel { Id = 0, StationName = "Null", StationCode = "Null" };
|
|||
|
|
Stations.Insert(0, nullStation);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var (data, count) = await _rgvService.GetAllAsync(pageIndex, PageSize);
|
|||
|
|
Totals = count;
|
|||
|
|
if (count > 0)
|
|||
|
|
{
|
|||
|
|
RgvList?.Clear();
|
|||
|
|
foreach (var item in data)
|
|||
|
|
{
|
|||
|
|
RgvList?.Add(item);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[RelayCommand]
|
|||
|
|
private void Add()
|
|||
|
|
{
|
|||
|
|
RgvList?.Add(new Rgv6180ViewModel { StationName = "请命名", StationCode = "请命名" });
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[RelayCommand]
|
|||
|
|
private async Task DeleteAsync(Rgv6180ViewModel? rgv)
|
|||
|
|
{
|
|||
|
|
if (rgv == null)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
var result = await MessageBox.ShowOverlayAsync(_l["DeleteDialog"], _l["Message.Info.Title"], button: MessageBoxButton.YesNo);
|
|||
|
|
if (result != MessageBoxResult.Yes)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
var delete = await _rgvService.DeleteAsync(rgv.Id);
|
|||
|
|
if (delete.IsSuccess)
|
|||
|
|
{
|
|||
|
|
NotificationHelper.ShowNormal(NotificationType.Success, _l["RgvManagement.Delete.Success"]);
|
|||
|
|
RgvList?.Remove(rgv);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["RgvManagement.Delete.Error"] + ":" + delete.ErrorMessage);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[RelayCommand]
|
|||
|
|
private async Task SaveAsync(Rgv6180ViewModel? rgv)
|
|||
|
|
{
|
|||
|
|
if (rgv == null)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
var result = await MessageBox.ShowOverlayAsync(_l["SaveDialog"], _l["Message.Info.Title"], button: MessageBoxButton.YesNo);
|
|||
|
|
if (result != MessageBoxResult.Yes)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (rgv.Id == 0)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
var addResult = await _rgvService.AddRgvAsync(rgv);
|
|||
|
|
if (addResult.IsSuccess)
|
|||
|
|
{
|
|||
|
|
NotificationHelper.ShowNormal(NotificationType.Success, _l["RgvManagement.Add.Success"]);
|
|||
|
|
// 新增成功后刷新列表
|
|||
|
|
await RefreshAsync(PageIndex);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["RgvManagement.Add.Error"] + ":" + addResult.ErrorMessage);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
var update = await _rgvService.UpdateAsync(rgv);
|
|||
|
|
if (update.IsSuccess)
|
|||
|
|
{
|
|||
|
|
await RefreshAsync(PageIndex);
|
|||
|
|
NotificationHelper.ShowNormal(NotificationType.Success, _l["RgvManagement.Save.Success"]);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
NotificationHelper.ShowNormal(NotificationType.Error, _l["RgvManagement.Save.Error"]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|