2026-05-06 15:30:31 +08:00
|
|
|
|
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;
|
2026-05-07 17:53:48 +08:00
|
|
|
|
using YY.Admin.Views.WeightRecord;
|
2026-05-06 15:30:31 +08:00
|
|
|
|
|
|
|
|
|
|
namespace YY.Admin.ViewModels.WeightRecord;
|
|
|
|
|
|
|
|
|
|
|
|
public class WeightRecordEditDialogViewModel : BaseViewModel, IDialogResultable<bool>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IWeightRecordService _weightRecordService;
|
|
|
|
|
|
private readonly IJeecgDictSyncService _dictSyncService;
|
2026-05-07 17:53:48 +08:00
|
|
|
|
private readonly IMixerMaterialService _mixerMaterialService;
|
|
|
|
|
|
|
|
|
|
|
|
private string? _mixerMaterialIds;
|
|
|
|
|
|
private string? _mixerMaterialNames;
|
|
|
|
|
|
|
|
|
|
|
|
public string MixerMaterialDisplay => !string.IsNullOrWhiteSpace(_mixerMaterialNames)
|
|
|
|
|
|
? _mixerMaterialNames
|
|
|
|
|
|
: "点击右侧「选择」选取密炼物料(可多选)";
|
|
|
|
|
|
|
|
|
|
|
|
public bool HasSelectedMixerMaterials => !string.IsNullOrWhiteSpace(_mixerMaterialNames);
|
2026-05-06 15:30:31 +08:00
|
|
|
|
|
|
|
|
|
|
private MesXslWeightRecord? _record;
|
|
|
|
|
|
public MesXslWeightRecord? Record
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _record;
|
|
|
|
|
|
set => SetProperty(ref _record, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsAddMode => string.IsNullOrWhiteSpace(Record?.Id);
|
|
|
|
|
|
public string DialogTitle => IsAddMode ? "新增磅单" : "编辑磅单";
|
|
|
|
|
|
|
|
|
|
|
|
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; }
|
2026-05-07 17:53:48 +08:00
|
|
|
|
public DelegateCommand OpenMixerMaterialPickerCommand { get; }
|
|
|
|
|
|
public DelegateCommand ClearMixerMaterialCommand { get; }
|
2026-05-06 15:30:31 +08:00
|
|
|
|
|
|
|
|
|
|
public WeightRecordEditDialogViewModel(
|
|
|
|
|
|
IWeightRecordService weightRecordService,
|
|
|
|
|
|
IJeecgDictSyncService dictSyncService,
|
2026-05-07 17:53:48 +08:00
|
|
|
|
IMixerMaterialService mixerMaterialService,
|
2026-05-06 15:30:31 +08:00
|
|
|
|
IContainerExtension container,
|
|
|
|
|
|
IRegionManager regionManager) : base(container, regionManager)
|
|
|
|
|
|
{
|
|
|
|
|
|
_weightRecordService = weightRecordService;
|
|
|
|
|
|
_dictSyncService = dictSyncService;
|
2026-05-07 17:53:48 +08:00
|
|
|
|
_mixerMaterialService = mixerMaterialService;
|
2026-05-06 15:30:31 +08:00
|
|
|
|
SaveCommand = new DelegateCommand(async () => await SaveAsync());
|
|
|
|
|
|
CancelCommand = new DelegateCommand(() => CloseAction?.Invoke());
|
2026-05-07 17:53:48 +08:00
|
|
|
|
OpenMixerMaterialPickerCommand = new DelegateCommand(async () => await OpenMixerMaterialPickerAsync());
|
|
|
|
|
|
ClearMixerMaterialCommand = new DelegateCommand(ClearMixerMaterialSelection);
|
2026-05-06 15:30:31 +08:00
|
|
|
|
_ = LoadDictOptionsAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-07 17:53:48 +08:00
|
|
|
|
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));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-06 15:30:31 +08:00
|
|
|
|
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,
|
2026-05-07 17:53:48 +08:00
|
|
|
|
InoutDirection = "1",
|
|
|
|
|
|
MaterialType = "1"
|
2026-05-06 15:30:31 +08:00
|
|
|
|
};
|
|
|
|
|
|
RaisePropertyChanged(nameof(IsAddMode));
|
|
|
|
|
|
RaisePropertyChanged(nameof(DialogTitle));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2026-05-07 17:53:48 +08:00
|
|
|
|
MaterialType = record.MaterialType,
|
2026-05-06 15:30:31 +08:00
|
|
|
|
TenantId = record.TenantId,
|
|
|
|
|
|
UpdateTime = record.UpdateTime
|
|
|
|
|
|
};
|
2026-05-07 17:53:48 +08:00
|
|
|
|
_mixerMaterialIds = record.MixerMaterialIds;
|
|
|
|
|
|
_mixerMaterialNames = record.MixerMaterialNames;
|
|
|
|
|
|
RaisePropertyChanged(nameof(MixerMaterialDisplay));
|
|
|
|
|
|
RaisePropertyChanged(nameof(HasSelectedMixerMaterials));
|
2026-05-06 15:30:31 +08:00
|
|
|
|
RaisePropertyChanged(nameof(IsAddMode));
|
|
|
|
|
|
RaisePropertyChanged(nameof(DialogTitle));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 净重自动计算
|
|
|
|
|
|
if (Record.GrossWeight.HasValue && Record.TareWeight.HasValue)
|
|
|
|
|
|
Record.NetWeight = Math.Round(Record.GrossWeight.Value - Record.TareWeight.Value, 2);
|
|
|
|
|
|
|
2026-05-07 17:53:48 +08:00
|
|
|
|
// 写入密炼物料
|
|
|
|
|
|
Record.MixerMaterialIds = _mixerMaterialIds;
|
|
|
|
|
|
Record.MixerMaterialNames = _mixerMaterialNames;
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(Record.MaterialType))
|
|
|
|
|
|
Record.MaterialType = "1";
|
|
|
|
|
|
|
2026-05-06 15:30:31 +08:00
|
|
|
|
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}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|