296 lines
11 KiB
C#
296 lines
11 KiB
C#
|
|
using HandyControl.Tools.Extension;
|
||
|
|
using System.Collections.ObjectModel;
|
||
|
|
using YY.Admin.Core;
|
||
|
|
using YY.Admin.Core.Entity;
|
||
|
|
using YY.Admin.Core.Services;
|
||
|
|
using YY.Admin.Core.Session;
|
||
|
|
using YY.Admin.ViewModels.RawMaterialEntry;
|
||
|
|
using YY.Admin.ViewModels.WeightRecord;
|
||
|
|
using YY.Admin.Views.RawMaterialEntry;
|
||
|
|
using YY.Admin.Views.WeightRecord;
|
||
|
|
|
||
|
|
namespace YY.Admin.ViewModels.MixerMaterialTareStrategy;
|
||
|
|
|
||
|
|
public class MixerMaterialTareStrategyEditDialogViewModel : BaseViewModel, IDialogResultable<bool>
|
||
|
|
{
|
||
|
|
private readonly IMixerMaterialTareStrategyService _tareStrategyService;
|
||
|
|
|
||
|
|
private MesXslMixerMaterialTareStrategy? _strategy;
|
||
|
|
public MesXslMixerMaterialTareStrategy? Strategy
|
||
|
|
{
|
||
|
|
get => _strategy;
|
||
|
|
set => SetProperty(ref _strategy, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool IsAddMode => string.IsNullOrWhiteSpace(Strategy?.Id) || (Strategy?.Id?.StartsWith("local-") ?? false);
|
||
|
|
public string DialogTitle => IsAddMode ? "新增密炼物料皮重策略" : "编辑密炼物料皮重策略";
|
||
|
|
|
||
|
|
public ObservableCollection<MesXslUnit> UnitOptions { get; } = new();
|
||
|
|
|
||
|
|
private MesMixerMaterial? _selectedMaterial;
|
||
|
|
public MesMixerMaterial? SelectedMaterial
|
||
|
|
{
|
||
|
|
get => _selectedMaterial;
|
||
|
|
set
|
||
|
|
{
|
||
|
|
SetProperty(ref _selectedMaterial, value);
|
||
|
|
RaisePropertyChanged(nameof(SelectedMaterialDisplay));
|
||
|
|
RaisePropertyChanged(nameof(HasSelectedMaterial));
|
||
|
|
if (Strategy != null && value != null)
|
||
|
|
{
|
||
|
|
Strategy.MixerMaterialId = value.Id;
|
||
|
|
Strategy.MixerMaterialName = value.MaterialName;
|
||
|
|
RaisePropertyChanged(nameof(Strategy));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public string SelectedMaterialDisplay => _selectedMaterial != null
|
||
|
|
? $"[{_selectedMaterial.MaterialCode}] {_selectedMaterial.MaterialName}"
|
||
|
|
: (string.IsNullOrWhiteSpace(Strategy?.MixerMaterialName) ? "请选择密炼物料" : Strategy!.MixerMaterialName!);
|
||
|
|
|
||
|
|
public bool HasSelectedMaterial =>
|
||
|
|
_selectedMaterial != null || !string.IsNullOrWhiteSpace(Strategy?.MixerMaterialId);
|
||
|
|
|
||
|
|
private MesXslSupplier? _selectedSupplier;
|
||
|
|
public MesXslSupplier? SelectedSupplier
|
||
|
|
{
|
||
|
|
get => _selectedSupplier;
|
||
|
|
set
|
||
|
|
{
|
||
|
|
SetProperty(ref _selectedSupplier, value);
|
||
|
|
RaisePropertyChanged(nameof(SelectedSupplierDisplay));
|
||
|
|
RaisePropertyChanged(nameof(HasSelectedSupplier));
|
||
|
|
if (Strategy != null && value != null)
|
||
|
|
{
|
||
|
|
Strategy.SupplierId = value.Id;
|
||
|
|
Strategy.SupplierName = value.SupplierName;
|
||
|
|
RaisePropertyChanged(nameof(Strategy));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public string SelectedSupplierDisplay => _selectedSupplier != null
|
||
|
|
? $"[{_selectedSupplier.SupplierCode}] {_selectedSupplier.SupplierName}"
|
||
|
|
: (string.IsNullOrWhiteSpace(Strategy?.SupplierName) ? "请选择供应商" : Strategy!.SupplierName!);
|
||
|
|
|
||
|
|
public bool HasSelectedSupplier =>
|
||
|
|
_selectedSupplier != null || !string.IsNullOrWhiteSpace(Strategy?.SupplierId);
|
||
|
|
|
||
|
|
private bool _result;
|
||
|
|
public bool Result { get => _result; set => SetProperty(ref _result, value); }
|
||
|
|
public Action? CloseAction { get; set; }
|
||
|
|
|
||
|
|
public DelegateCommand SaveCommand { get; }
|
||
|
|
public DelegateCommand CancelCommand { get; }
|
||
|
|
public DelegateCommand OpenMaterialPickerCommand { get; }
|
||
|
|
public DelegateCommand ClearMaterialCommand { get; }
|
||
|
|
public DelegateCommand OpenSupplierPickerCommand { get; }
|
||
|
|
public DelegateCommand ClearSupplierCommand { get; }
|
||
|
|
|
||
|
|
public MixerMaterialTareStrategyEditDialogViewModel(
|
||
|
|
IMixerMaterialTareStrategyService tareStrategyService,
|
||
|
|
IContainerExtension container,
|
||
|
|
IRegionManager regionManager) : base(container, regionManager)
|
||
|
|
{
|
||
|
|
_tareStrategyService = tareStrategyService;
|
||
|
|
SaveCommand = new DelegateCommand(async () => await SaveAsync());
|
||
|
|
CancelCommand = new DelegateCommand(() => CloseAction?.Invoke());
|
||
|
|
OpenMaterialPickerCommand = new DelegateCommand(async () => await OpenMaterialPickerAsync());
|
||
|
|
ClearMaterialCommand = new DelegateCommand(ClearMaterialSelection);
|
||
|
|
OpenSupplierPickerCommand = new DelegateCommand(async () => await OpenSupplierPickerAsync());
|
||
|
|
ClearSupplierCommand = new DelegateCommand(ClearSupplierSelection);
|
||
|
|
_ = LoadUnitsAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task LoadUnitsAsync()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var units = await _tareStrategyService.GetUnitsAsync();
|
||
|
|
UnitOptions.Clear();
|
||
|
|
foreach (var unit in units.Where(u => !string.IsNullOrWhiteSpace(u.Id)))
|
||
|
|
UnitOptions.Add(unit);
|
||
|
|
}
|
||
|
|
catch
|
||
|
|
{
|
||
|
|
UnitOptions.Clear();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void InitializeForAdd()
|
||
|
|
{
|
||
|
|
Strategy = new MesXslMixerMaterialTareStrategy();
|
||
|
|
_selectedMaterial = null;
|
||
|
|
_selectedSupplier = null;
|
||
|
|
RaisePropertyChanged(nameof(IsAddMode));
|
||
|
|
RaisePropertyChanged(nameof(DialogTitle));
|
||
|
|
RaisePropertyChanged(nameof(SelectedMaterialDisplay));
|
||
|
|
RaisePropertyChanged(nameof(SelectedSupplierDisplay));
|
||
|
|
RaisePropertyChanged(nameof(HasSelectedMaterial));
|
||
|
|
RaisePropertyChanged(nameof(HasSelectedSupplier));
|
||
|
|
}
|
||
|
|
|
||
|
|
public void InitializeForEdit(MesXslMixerMaterialTareStrategy strategy)
|
||
|
|
{
|
||
|
|
Strategy = new MesXslMixerMaterialTareStrategy
|
||
|
|
{
|
||
|
|
Id = strategy.Id,
|
||
|
|
TenantId = strategy.TenantId,
|
||
|
|
MixerMaterialId = strategy.MixerMaterialId,
|
||
|
|
MixerMaterialName = strategy.MixerMaterialName,
|
||
|
|
SupplierId = strategy.SupplierId,
|
||
|
|
SupplierName = strategy.SupplierName,
|
||
|
|
MaterialSpec = strategy.MaterialSpec,
|
||
|
|
TareWeight = strategy.TareWeight,
|
||
|
|
PalletWeight = strategy.PalletWeight,
|
||
|
|
UnitId = strategy.UnitId,
|
||
|
|
UnitName = strategy.UnitName,
|
||
|
|
EffectiveStartDate = strategy.EffectiveStartDate,
|
||
|
|
EffectiveEndDate = strategy.EffectiveEndDate,
|
||
|
|
MaintainBy = strategy.MaintainBy,
|
||
|
|
UpdateTime = strategy.UpdateTime
|
||
|
|
};
|
||
|
|
_selectedMaterial = null;
|
||
|
|
_selectedSupplier = null;
|
||
|
|
RaisePropertyChanged(nameof(IsAddMode));
|
||
|
|
RaisePropertyChanged(nameof(DialogTitle));
|
||
|
|
RaisePropertyChanged(nameof(SelectedMaterialDisplay));
|
||
|
|
RaisePropertyChanged(nameof(SelectedSupplierDisplay));
|
||
|
|
RaisePropertyChanged(nameof(HasSelectedMaterial));
|
||
|
|
RaisePropertyChanged(nameof(HasSelectedSupplier));
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task OpenMaterialPickerAsync()
|
||
|
|
{
|
||
|
|
RawMaterialEntryMaterialPickerDialogViewModel? pickerVm = null;
|
||
|
|
bool confirmed;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
confirmed = await HandyControl.Controls.Dialog.Show<RawMaterialEntryMaterialPickerDialogView>()
|
||
|
|
.Initialize<RawMaterialEntryMaterialPickerDialogViewModel>(vm =>
|
||
|
|
{
|
||
|
|
pickerVm = vm;
|
||
|
|
vm.Initialize(Strategy?.MixerMaterialId, Strategy?.MixerMaterialName);
|
||
|
|
})
|
||
|
|
.GetResultAsync<bool>();
|
||
|
|
}
|
||
|
|
catch { return; }
|
||
|
|
|
||
|
|
if (!confirmed || pickerVm?.SelectedMaterial == null) return;
|
||
|
|
SelectedMaterial = pickerVm.SelectedMaterial;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ClearMaterialSelection()
|
||
|
|
{
|
||
|
|
_selectedMaterial = null;
|
||
|
|
RaisePropertyChanged(nameof(SelectedMaterial));
|
||
|
|
RaisePropertyChanged(nameof(SelectedMaterialDisplay));
|
||
|
|
RaisePropertyChanged(nameof(HasSelectedMaterial));
|
||
|
|
if (Strategy == null) return;
|
||
|
|
Strategy.MixerMaterialId = null;
|
||
|
|
Strategy.MixerMaterialName = null;
|
||
|
|
RaisePropertyChanged(nameof(Strategy));
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task OpenSupplierPickerAsync()
|
||
|
|
{
|
||
|
|
SupplierPickerDialogViewModel? pickerVm = null;
|
||
|
|
bool confirmed;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
confirmed = await HandyControl.Controls.Dialog.Show<SupplierPickerDialogView>()
|
||
|
|
.Initialize<SupplierPickerDialogViewModel>(vm => pickerVm = vm)
|
||
|
|
.GetResultAsync<bool>();
|
||
|
|
}
|
||
|
|
catch { return; }
|
||
|
|
|
||
|
|
if (!confirmed || pickerVm?.SelectedSupplier == null) return;
|
||
|
|
SelectedSupplier = pickerVm.SelectedSupplier;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ClearSupplierSelection()
|
||
|
|
{
|
||
|
|
_selectedSupplier = null;
|
||
|
|
RaisePropertyChanged(nameof(SelectedSupplier));
|
||
|
|
RaisePropertyChanged(nameof(SelectedSupplierDisplay));
|
||
|
|
RaisePropertyChanged(nameof(HasSelectedSupplier));
|
||
|
|
if (Strategy == null) return;
|
||
|
|
Strategy.SupplierId = null;
|
||
|
|
Strategy.SupplierName = null;
|
||
|
|
RaisePropertyChanged(nameof(Strategy));
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task SaveAsync()
|
||
|
|
{
|
||
|
|
if (Strategy == null) return;
|
||
|
|
|
||
|
|
if (string.IsNullOrWhiteSpace(Strategy.MixerMaterialId))
|
||
|
|
{
|
||
|
|
HandyControl.Controls.MessageBox.Warning("请选择密炼物料!");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (string.IsNullOrWhiteSpace(Strategy.SupplierId))
|
||
|
|
{
|
||
|
|
HandyControl.Controls.MessageBox.Warning("请选择供应商!");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (!Strategy.TareWeight.HasValue)
|
||
|
|
{
|
||
|
|
HandyControl.Controls.MessageBox.Warning("请填写包装物重量!");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (Strategy.PalletWeight.HasValue && Strategy.PalletWeight.Value < 0)
|
||
|
|
{
|
||
|
|
HandyControl.Controls.MessageBox.Warning("托盘重量不能为负数!");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (string.IsNullOrWhiteSpace(Strategy.UnitId))
|
||
|
|
{
|
||
|
|
HandyControl.Controls.MessageBox.Warning("请选择单位!");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (!Strategy.EffectiveStartDate.HasValue || !Strategy.EffectiveEndDate.HasValue)
|
||
|
|
{
|
||
|
|
HandyControl.Controls.MessageBox.Warning("请填写完整的生效日期!");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (Strategy.EffectiveStartDate.Value.Date > Strategy.EffectiveEndDate.Value.Date)
|
||
|
|
{
|
||
|
|
HandyControl.Controls.MessageBox.Warning("生效开始日期不能晚于截止日期!");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var selectedUnit = UnitOptions.FirstOrDefault(u => string.Equals(u.Id, Strategy.UnitId, StringComparison.OrdinalIgnoreCase));
|
||
|
|
if (selectedUnit != null)
|
||
|
|
Strategy.UnitName = selectedUnit.UnitName;
|
||
|
|
|
||
|
|
Strategy.MaintainBy = AppSession.CurrentUser?.Account ?? Strategy.MaintainBy;
|
||
|
|
Strategy.MaterialSpec = string.IsNullOrWhiteSpace(Strategy.MaterialSpec)
|
||
|
|
? null
|
||
|
|
: Strategy.MaterialSpec.Trim();
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
bool ok;
|
||
|
|
if (IsAddMode)
|
||
|
|
{
|
||
|
|
ok = await _tareStrategyService.AddAsync(Strategy);
|
||
|
|
if (ok) HandyControl.Controls.MessageBox.Success("新增成功!");
|
||
|
|
else { HandyControl.Controls.MessageBox.Error("新增失败!"); return; }
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
ok = await _tareStrategyService.EditAsync(Strategy);
|
||
|
|
if (!ok) { HandyControl.Controls.MessageBox.Error("编辑失败!"); return; }
|
||
|
|
}
|
||
|
|
Result = ok;
|
||
|
|
CloseAction?.Invoke();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
HandyControl.Controls.MessageBox.Error($"操作失败:{ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|