Files
qhmes/yy-admin-master/YY.Admin/ViewModels/WeightRecord/WeightRecordEditDialogViewModel.cs

267 lines
9.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
using YY.Admin.Services.Service;
using YY.Admin.Views.WeightRecord;
namespace YY.Admin.ViewModels.WeightRecord;
public class WeightRecordEditDialogViewModel : BaseViewModel, IDialogResultable<bool>
{
private readonly IWeightRecordService _weightRecordService;
private readonly IJeecgDictSyncService _dictSyncService;
private readonly IMixerMaterialService _mixerMaterialService;
private string? _mixerMaterialIds;
private string? _mixerMaterialNames;
public string MixerMaterialDisplay => !string.IsNullOrWhiteSpace(_mixerMaterialNames)
? _mixerMaterialNames
: "点击右侧「选择」选取密炼物料(可多选)";
public bool HasSelectedMixerMaterials => !string.IsNullOrWhiteSpace(_mixerMaterialNames);
private MesXslWeightRecord? _record;
public MesXslWeightRecord? Record
{
get => _record;
set => SetProperty(ref _record, value);
}
public bool IsAddMode => string.IsNullOrWhiteSpace(Record?.Id);
public string DialogTitle => IsAddMode ? "新增磅单" : "编辑磅单";
/// <summary>毛重输入:变更后自动重算净重</summary>
public double? GrossWeightInput
{
get => Record?.GrossWeight;
set
{
if (Record == null || Nullable.Equals(Record.GrossWeight, value)) return;
Record.GrossWeight = value;
RaisePropertyChanged(nameof(GrossWeightInput));
RecalcNetWeight();
}
}
/// <summary>皮重输入:变更后自动用毛重-皮重填充净重</summary>
public double? TareWeightInput
{
get => Record?.TareWeight;
set
{
if (Record == null || Nullable.Equals(Record.TareWeight, value)) return;
Record.TareWeight = value;
RaisePropertyChanged(nameof(TareWeightInput));
RecalcNetWeight();
}
}
/// <summary>净重(只读展示,由毛重/皮重自动计算)</summary>
public double? NetWeightInput
{
get => Record?.NetWeight;
private set
{
if (Record == null || Nullable.Equals(Record.NetWeight, value)) return;
Record.NetWeight = value;
RaisePropertyChanged(nameof(NetWeightInput));
}
}
public ObservableCollection<KeyValuePair<string, string>> InoutDirectionOptions { get; } = new();
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 OpenMixerMaterialPickerCommand { get; }
public DelegateCommand ClearMixerMaterialCommand { get; }
public WeightRecordEditDialogViewModel(
IWeightRecordService weightRecordService,
IJeecgDictSyncService dictSyncService,
IMixerMaterialService mixerMaterialService,
IContainerExtension container,
IRegionManager regionManager) : base(container, regionManager)
{
_weightRecordService = weightRecordService;
_dictSyncService = dictSyncService;
_mixerMaterialService = mixerMaterialService;
SaveCommand = new DelegateCommand(async () => await SaveAsync());
CancelCommand = new DelegateCommand(() => CloseAction?.Invoke());
OpenMixerMaterialPickerCommand = new DelegateCommand(async () => await OpenMixerMaterialPickerAsync());
ClearMixerMaterialCommand = new DelegateCommand(ClearMixerMaterialSelection);
_ = LoadDictOptionsAsync();
}
private async Task OpenMixerMaterialPickerAsync()
{
MixerMaterialPickerDialogViewModel? pickerVm = null;
bool confirmed;
try
{
confirmed = await HandyControl.Controls.Dialog.Show<MixerMaterialPickerDialogView>()
.Initialize<MixerMaterialPickerDialogViewModel>(vm => { pickerVm = vm; })
.GetResultAsync<bool>();
}
catch { return; }
if (!confirmed || pickerVm == null) return;
var selected = pickerVm.SelectedMaterials;
if (selected.Count == 0) return;
_mixerMaterialIds = string.Join("", selected.Select(m => m.Source.Id ?? ""));
_mixerMaterialNames = string.Join("", selected.Select(m => m.Source.MaterialName ?? ""));
RaisePropertyChanged(nameof(MixerMaterialDisplay));
RaisePropertyChanged(nameof(HasSelectedMixerMaterials));
}
private void ClearMixerMaterialSelection()
{
_mixerMaterialIds = null;
_mixerMaterialNames = null;
RaisePropertyChanged(nameof(MixerMaterialDisplay));
RaisePropertyChanged(nameof(HasSelectedMixerMaterials));
}
private async Task LoadDictOptionsAsync()
{
try
{
var options = await _dictSyncService.GetDictOptionsAsync("xslmes_inout_direction");
InoutDirectionOptions.Clear();
foreach (var item in options) InoutDirectionOptions.Add(item);
if (InoutDirectionOptions.Count == 0) AddDefaultDirectionOptions();
}
catch { AddDefaultDirectionOptions(); }
}
private void AddDefaultDirectionOptions()
{
InoutDirectionOptions.Clear();
InoutDirectionOptions.Add(new KeyValuePair<string, string>("进厂", "1"));
InoutDirectionOptions.Add(new KeyValuePair<string, string>("出厂", "2"));
}
public void InitializeForAdd()
{
Record = new MesXslWeightRecord
{
WeighDate = DateTime.Today,
InoutDirection = "1",
MaterialType = "1"
};
RaisePropertyChanged(nameof(IsAddMode));
RaisePropertyChanged(nameof(DialogTitle));
RaiseWeightInputsChanged();
}
public void InitializeForEdit(MesXslWeightRecord record)
{
Record = new MesXslWeightRecord
{
Id = record.Id,
BillNo = record.BillNo,
WeighDate = record.WeighDate,
InoutDirection = record.InoutDirection,
VehicleId = record.VehicleId,
PlateNumber = record.PlateNumber,
SenderUnit = record.SenderUnit,
ReceiverUnit = record.ReceiverUnit,
GrossWeight = record.GrossWeight,
TareWeight = record.TareWeight,
NetWeight = record.NetWeight,
DriverName = record.DriverName,
DriverPhone = record.DriverPhone,
BillType = record.BillType,
MaterialType = record.MaterialType,
TenantId = record.TenantId,
UpdateTime = record.UpdateTime
};
_mixerMaterialIds = record.MixerMaterialIds;
_mixerMaterialNames = record.MixerMaterialNames;
RaisePropertyChanged(nameof(MixerMaterialDisplay));
RaisePropertyChanged(nameof(HasSelectedMixerMaterials));
RaisePropertyChanged(nameof(IsAddMode));
RaisePropertyChanged(nameof(DialogTitle));
RaiseWeightInputsChanged();
// 编辑打开时若已有毛重+皮重,立即校正净重展示
RecalcNetWeight();
}
private void RaiseWeightInputsChanged()
{
RaisePropertyChanged(nameof(GrossWeightInput));
RaisePropertyChanged(nameof(TareWeightInput));
RaisePropertyChanged(nameof(NetWeightInput));
}
/// <summary>毛重、皮重均有效时:净重 = 毛重 - 皮重(保留两位小数)</summary>
private void RecalcNetWeight()
{
if (Record == null) return;
if (Record.GrossWeight.HasValue && Record.GrossWeight.Value > 0
&& Record.TareWeight.HasValue && Record.TareWeight.Value > 0)
{
NetWeightInput = Math.Round(Record.GrossWeight.Value - Record.TareWeight.Value, 2);
}
else
{
NetWeightInput = null;
}
}
private async Task SaveAsync()
{
if (Record == null) return;
if (string.IsNullOrWhiteSpace(Record.PlateNumber))
{
HandyControl.Controls.MessageBox.Warning("车牌号不能为空!");
return;
}
if (string.IsNullOrWhiteSpace(Record.InoutDirection))
{
HandyControl.Controls.MessageBox.Warning("进出方向不能为空!");
return;
}
// 保存前再算一次,防止未触发属性变更时净重未更新
RecalcNetWeight();
// 写入密炼物料
Record.MixerMaterialIds = _mixerMaterialIds;
Record.MixerMaterialNames = _mixerMaterialNames;
if (string.IsNullOrWhiteSpace(Record.MaterialType))
Record.MaterialType = "1";
try
{
bool ok;
if (IsAddMode)
{
ok = await _weightRecordService.AddAsync(Record);
if (ok) HandyControl.Controls.MessageBox.Success("新增磅单成功!");
else { HandyControl.Controls.MessageBox.Error("新增磅单失败!"); return; }
}
else
{
ok = await _weightRecordService.EditAsync(Record);
if (!ok) { HandyControl.Controls.MessageBox.Error("编辑磅单失败!"); return; }
}
Result = ok;
CloseAction?.Invoke();
}
catch (Exception ex)
{
HandyControl.Controls.MessageBox.Error($"操作失败:{ex.Message}");
}
}
}