222 lines
8.1 KiB
C#
222 lines
8.1 KiB
C#
|
|
using HandyControl.Controls;
|
||
|
|
using HandyControl.Tools.Extension;
|
||
|
|
using Prism.Events;
|
||
|
|
using System.Collections.ObjectModel;
|
||
|
|
using System.Diagnostics;
|
||
|
|
using System.Windows;
|
||
|
|
using YY.Admin.Core;
|
||
|
|
using YY.Admin.Core.Entity;
|
||
|
|
using YY.Admin.Core.Events;
|
||
|
|
using YY.Admin.Core.Helper;
|
||
|
|
using YY.Admin.Core.Services;
|
||
|
|
using YY.Admin.Views.MixerMaterialTareStrategy;
|
||
|
|
|
||
|
|
namespace YY.Admin.ViewModels.MixerMaterialTareStrategy;
|
||
|
|
|
||
|
|
public class MixerMaterialTareStrategyListViewModel : BaseViewModel
|
||
|
|
{
|
||
|
|
private readonly IMixerMaterialTareStrategyService _tareStrategyService;
|
||
|
|
private SubscriptionToken? _changedToken;
|
||
|
|
private SubscriptionToken? _syncConflictToken;
|
||
|
|
|
||
|
|
private ObservableCollection<MesXslMixerMaterialTareStrategy> _strategies = new();
|
||
|
|
public ObservableCollection<MesXslMixerMaterialTareStrategy> Strategies
|
||
|
|
{
|
||
|
|
get => _strategies;
|
||
|
|
set => SetProperty(ref _strategies, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
private long _total;
|
||
|
|
public long Total { get => _total; set => SetProperty(ref _total, value); }
|
||
|
|
|
||
|
|
private int _pageNo = 1;
|
||
|
|
public int PageNo { get => _pageNo; set => SetProperty(ref _pageNo, value); }
|
||
|
|
|
||
|
|
private int _pageSize = 20;
|
||
|
|
public int PageSize { get => _pageSize; set => SetProperty(ref _pageSize, value); }
|
||
|
|
|
||
|
|
private string? _filterMixerMaterialName;
|
||
|
|
public string? FilterMixerMaterialName
|
||
|
|
{
|
||
|
|
get => _filterMixerMaterialName;
|
||
|
|
set => SetProperty(ref _filterMixerMaterialName, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
private string? _filterSupplierName;
|
||
|
|
public string? FilterSupplierName
|
||
|
|
{
|
||
|
|
get => _filterSupplierName;
|
||
|
|
set => SetProperty(ref _filterSupplierName, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
public DelegateCommand SearchCommand { get; }
|
||
|
|
public DelegateCommand ResetCommand { get; }
|
||
|
|
public DelegateCommand AddCommand { get; }
|
||
|
|
public DelegateCommand<MesXslMixerMaterialTareStrategy> EditCommand { get; }
|
||
|
|
public DelegateCommand<MesXslMixerMaterialTareStrategy> DeleteCommand { get; }
|
||
|
|
public DelegateCommand PrevPageCommand { get; }
|
||
|
|
public DelegateCommand NextPageCommand { get; }
|
||
|
|
|
||
|
|
public MixerMaterialTareStrategyListViewModel(
|
||
|
|
IMixerMaterialTareStrategyService tareStrategyService,
|
||
|
|
IContainerExtension container,
|
||
|
|
IRegionManager regionManager) : base(container, regionManager)
|
||
|
|
{
|
||
|
|
_tareStrategyService = tareStrategyService;
|
||
|
|
|
||
|
|
SearchCommand = new DelegateCommand(async () => { PageNo = 1; await LoadAsync(); });
|
||
|
|
ResetCommand = new DelegateCommand(async () =>
|
||
|
|
{
|
||
|
|
FilterMixerMaterialName = null;
|
||
|
|
FilterSupplierName = null;
|
||
|
|
PageNo = 1;
|
||
|
|
await LoadAsync();
|
||
|
|
});
|
||
|
|
AddCommand = new DelegateCommand(async () => await ShowAddDialogAsync());
|
||
|
|
EditCommand = new DelegateCommand<MesXslMixerMaterialTareStrategy>(async s => await ShowEditDialogAsync(s));
|
||
|
|
DeleteCommand = new DelegateCommand<MesXslMixerMaterialTareStrategy>(async s => await DeleteAsync(s));
|
||
|
|
PrevPageCommand = new DelegateCommand(async () => { if (PageNo > 1) { PageNo--; await LoadAsync(); } });
|
||
|
|
NextPageCommand = new DelegateCommand(async () => { if ((long)PageNo * PageSize < Total) { PageNo++; await LoadAsync(); } });
|
||
|
|
|
||
|
|
_changedToken = _eventAggregator.GetEvent<MixerMaterialTareStrategyChangedEvent>()
|
||
|
|
.Subscribe(async p => await OnChangedAsync(p), ThreadOption.UIThread);
|
||
|
|
_syncConflictToken = _eventAggregator.GetEvent<SyncConflictEvent>()
|
||
|
|
.Subscribe(OnSyncConflict, ThreadOption.UIThread);
|
||
|
|
|
||
|
|
_ = InitializeAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task OnChangedAsync(MixerMaterialTareStrategyChangedPayload payload)
|
||
|
|
{
|
||
|
|
if (payload.Action == "edit" && !string.IsNullOrWhiteSpace(payload.TareStrategyId))
|
||
|
|
await RefreshSingleAsync(payload.TareStrategyId!);
|
||
|
|
else
|
||
|
|
await LoadAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task RefreshSingleAsync(string id)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var updated = await _tareStrategyService.GetByIdAsync(id);
|
||
|
|
if (updated == null) return;
|
||
|
|
var idx = Strategies.ToList().FindIndex(s => string.Equals(s.Id, id, StringComparison.OrdinalIgnoreCase));
|
||
|
|
if (idx >= 0) Strategies[idx] = updated;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
Debug.WriteLine($"[密炼物料皮重策略] 单条刷新失败: {ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnSyncConflict(SyncConflictPayload payload)
|
||
|
|
{
|
||
|
|
if (!string.Equals(payload.EntityName, "密炼物料皮重策略", StringComparison.OrdinalIgnoreCase)) return;
|
||
|
|
|
||
|
|
var parts = new List<string>();
|
||
|
|
if (payload.PushedCount > 0)
|
||
|
|
parts.Add($"已同步 {payload.PushedCount} 条本地改动到服务器");
|
||
|
|
if (payload.NewRecordsPushed > 0)
|
||
|
|
parts.Add($"已上传 {payload.NewRecordsPushed} 条本地新增记录");
|
||
|
|
if (payload.ConflictCount > 0)
|
||
|
|
parts.Add($"{payload.ConflictCount} 条记录与服务器版本冲突,已保留服务器版本");
|
||
|
|
|
||
|
|
if (parts.Count == 0) return;
|
||
|
|
var message = string.Join("\n", parts);
|
||
|
|
if (payload.ConflictCount > 0) Growl.Warning(message);
|
||
|
|
else Growl.Success(message);
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task InitializeAsync()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
await UIHelper.WaitForRenderAsync();
|
||
|
|
await LoadAsync();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
Debug.WriteLine($"密炼物料皮重策略列表初始化失败: {ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task LoadAsync()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
IsLoading = true;
|
||
|
|
var result = await _tareStrategyService.PageAsync(
|
||
|
|
PageNo, PageSize, FilterMixerMaterialName, FilterSupplierName);
|
||
|
|
Strategies = new ObservableCollection<MesXslMixerMaterialTareStrategy>(result.Records);
|
||
|
|
Total = result.Total;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
Growl.Error($"加载密炼物料皮重策略失败:{ex.Message}");
|
||
|
|
}
|
||
|
|
finally
|
||
|
|
{
|
||
|
|
IsLoading = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task ShowAddDialogAsync()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var result = await HandyControl.Controls.Dialog.Show<MixerMaterialTareStrategyEditDialogView>()
|
||
|
|
.Initialize<MixerMaterialTareStrategyEditDialogViewModel>(vm => vm.InitializeForAdd())
|
||
|
|
.GetResultAsync<bool>();
|
||
|
|
if (result) await LoadAsync();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
Growl.Error($"打开新增对话框失败:{ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task ShowEditDialogAsync(MesXslMixerMaterialTareStrategy strategy)
|
||
|
|
{
|
||
|
|
if (strategy == null) return;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var result = await HandyControl.Controls.Dialog.Show<MixerMaterialTareStrategyEditDialogView>()
|
||
|
|
.Initialize<MixerMaterialTareStrategyEditDialogViewModel>(vm => vm.InitializeForEdit(strategy))
|
||
|
|
.GetResultAsync<bool>();
|
||
|
|
if (result) await LoadAsync();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
Growl.Error($"打开编辑对话框失败:{ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task DeleteAsync(MesXslMixerMaterialTareStrategy strategy)
|
||
|
|
{
|
||
|
|
if (strategy?.Id == null) return;
|
||
|
|
var confirm = System.Windows.MessageBox.Show(
|
||
|
|
$"确定删除【{strategy.MixerMaterialName} / {strategy.SupplierName}】的包装物重量策略?此操作不可恢复!",
|
||
|
|
"确认删除", MessageBoxButton.OKCancel, MessageBoxImage.Question);
|
||
|
|
if (confirm != System.Windows.MessageBoxResult.OK) return;
|
||
|
|
|
||
|
|
var ok = await _tareStrategyService.DeleteAsync(strategy.Id);
|
||
|
|
if (ok) { Growl.Success("删除成功!"); await LoadAsync(); }
|
||
|
|
else Growl.Error("删除失败!");
|
||
|
|
}
|
||
|
|
|
||
|
|
protected override void CleanUp()
|
||
|
|
{
|
||
|
|
base.CleanUp();
|
||
|
|
if (_changedToken != null)
|
||
|
|
{
|
||
|
|
_eventAggregator.GetEvent<MixerMaterialTareStrategyChangedEvent>().Unsubscribe(_changedToken);
|
||
|
|
_changedToken = null;
|
||
|
|
}
|
||
|
|
if (_syncConflictToken != null)
|
||
|
|
{
|
||
|
|
_eventAggregator.GetEvent<SyncConflictEvent>().Unsubscribe(_syncConflictToken);
|
||
|
|
_syncConflictToken = null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|