187 lines
6.1 KiB
C#
187 lines
6.1 KiB
C#
using HandyControl.Controls;
|
|
using HandyControl.Tools.Extension;
|
|
using System.Collections.ObjectModel;
|
|
using YY.Admin.Core;
|
|
using YY.Admin.Core.Entity;
|
|
using YY.Admin.Core.Services;
|
|
|
|
namespace YY.Admin.ViewModels.MixerMaterial;
|
|
|
|
public class MixerMaterialEditDialogViewModel : BaseViewModel, IDialogResultable<bool>
|
|
{
|
|
private readonly IMixerMaterialService _mixerMaterialService;
|
|
|
|
private MesMixerMaterial? _material;
|
|
public MesMixerMaterial? Material
|
|
{
|
|
get => _material;
|
|
set => SetProperty(ref _material, value);
|
|
}
|
|
|
|
public bool IsAddMode => string.IsNullOrWhiteSpace(Material?.Id);
|
|
public string DialogTitle => IsAddMode ? "新增密炼物料" : "编辑密炼物料";
|
|
|
|
public ObservableCollection<KeyValuePair<string, string>> MajorCategoryOptions { get; } = new();
|
|
public ObservableCollection<KeyValuePair<string, string>> MinorCategoryOptions { get; } = new();
|
|
public ObservableCollection<KeyValuePair<string, int>> FeedManageStatusOptions { get; } =
|
|
[
|
|
new("在投管", 1),
|
|
new("未投管", 0)
|
|
];
|
|
public ObservableCollection<KeyValuePair<string, int>> UseStatusOptions { get; } =
|
|
[
|
|
new("使用中", 1),
|
|
new("停用", 0)
|
|
];
|
|
|
|
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 MajorCategoryChangedCommand { get; }
|
|
|
|
public MixerMaterialEditDialogViewModel(
|
|
IMixerMaterialService mixerMaterialService,
|
|
IContainerExtension container,
|
|
IRegionManager regionManager) : base(container, regionManager)
|
|
{
|
|
_mixerMaterialService = mixerMaterialService;
|
|
SaveCommand = new DelegateCommand(async () => await SaveAsync());
|
|
CancelCommand = new DelegateCommand(() => CloseAction?.Invoke());
|
|
MajorCategoryChangedCommand = new DelegateCommand(async () => await OnMajorCategoryChangedAsync());
|
|
_ = LoadMajorCategoryOptionsAsync();
|
|
}
|
|
|
|
public void InitializeForAdd()
|
|
{
|
|
Material = new MesMixerMaterial
|
|
{
|
|
FeedManageStatus = 1,
|
|
UseStatus = 1
|
|
};
|
|
MinorCategoryOptions.Clear();
|
|
RaisePropertyChanged(nameof(IsAddMode));
|
|
RaisePropertyChanged(nameof(DialogTitle));
|
|
}
|
|
|
|
public void InitializeForEdit(MesMixerMaterial material)
|
|
{
|
|
Material = new MesMixerMaterial
|
|
{
|
|
Id = material.Id,
|
|
MaterialCode = material.MaterialCode,
|
|
MaterialName = material.MaterialName,
|
|
ErpCode = material.ErpCode,
|
|
MajorCategoryId = material.MajorCategoryId,
|
|
MinorCategoryId = material.MinorCategoryId,
|
|
MaterialDesc = material.MaterialDesc,
|
|
AliasName = material.AliasName,
|
|
FeedManageStatus = material.FeedManageStatus,
|
|
UseStatus = material.UseStatus,
|
|
SpecificGravity = material.SpecificGravity,
|
|
ShelfLifeDays = material.ShelfLifeDays,
|
|
MinBakeMinutes = material.MinBakeMinutes,
|
|
TotalSafetyStockKg = material.TotalSafetyStockKg,
|
|
QualifiedSafetyStockKg = material.QualifiedSafetyStockKg,
|
|
Remark = material.Remark,
|
|
TenantId = material.TenantId,
|
|
};
|
|
_ = LoadMinorCategoryOptionsAsync(Material.MajorCategoryId);
|
|
RaisePropertyChanged(nameof(IsAddMode));
|
|
RaisePropertyChanged(nameof(DialogTitle));
|
|
}
|
|
|
|
private async Task LoadMajorCategoryOptionsAsync()
|
|
{
|
|
try
|
|
{
|
|
MajorCategoryOptions.Clear();
|
|
var options = await _mixerMaterialService.GetMajorCategoryOptionsAsync();
|
|
foreach (var item in options)
|
|
{
|
|
MajorCategoryOptions.Add(item);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
private async Task LoadMinorCategoryOptionsAsync(string? majorCategoryId)
|
|
{
|
|
MinorCategoryOptions.Clear();
|
|
if (string.IsNullOrWhiteSpace(majorCategoryId))
|
|
{
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
var options = await _mixerMaterialService.GetMinorCategoryOptionsAsync(majorCategoryId);
|
|
foreach (var item in options)
|
|
{
|
|
MinorCategoryOptions.Add(item);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
private async Task OnMajorCategoryChangedAsync()
|
|
{
|
|
if (Material == null) return;
|
|
Material.MinorCategoryId = null;
|
|
RaisePropertyChanged(nameof(Material));
|
|
await LoadMinorCategoryOptionsAsync(Material.MajorCategoryId);
|
|
}
|
|
|
|
private async Task SaveAsync()
|
|
{
|
|
if (Material == null) return;
|
|
if (string.IsNullOrWhiteSpace(Material.MaterialCode))
|
|
{
|
|
HandyControl.Controls.MessageBox.Warning("物料编码不能为空!");
|
|
return;
|
|
}
|
|
if (string.IsNullOrWhiteSpace(Material.MaterialName))
|
|
{
|
|
HandyControl.Controls.MessageBox.Warning("物料名称不能为空!");
|
|
return;
|
|
}
|
|
if (string.IsNullOrWhiteSpace(Material.MajorCategoryId))
|
|
{
|
|
HandyControl.Controls.MessageBox.Warning("物料大类不能为空!");
|
|
return;
|
|
}
|
|
if (string.IsNullOrWhiteSpace(Material.MinorCategoryId))
|
|
{
|
|
HandyControl.Controls.MessageBox.Warning("物料小类不能为空!");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
bool ok = IsAddMode
|
|
? await _mixerMaterialService.AddAsync(Material)
|
|
: await _mixerMaterialService.EditAsync(Material);
|
|
if (!ok)
|
|
{
|
|
HandyControl.Controls.MessageBox.Error($"{(IsAddMode ? "新增" : "编辑")}密炼物料失败!");
|
|
return;
|
|
}
|
|
|
|
Result = true;
|
|
CloseAction?.Invoke();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
HandyControl.Controls.MessageBox.Error($"操作失败:{ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|