204 lines
7.4 KiB
C#
204 lines
7.4 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;
|
||
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 ? "新增磅单" : "编辑磅单";
|
||
|
||
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));
|
||
}
|
||
|
||
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));
|
||
}
|
||
|
||
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);
|
||
|
||
// 写入密炼物料
|
||
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}");
|
||
}
|
||
}
|
||
}
|