146 lines
4.9 KiB
C#
146 lines
4.9 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;
|
||
|
|
|
||
|
|
namespace YY.Admin.ViewModels.WeightRecord;
|
||
|
|
|
||
|
|
public class WeightRecordEditDialogViewModel : BaseViewModel, IDialogResultable<bool>
|
||
|
|
{
|
||
|
|
private readonly IWeightRecordService _weightRecordService;
|
||
|
|
private readonly IJeecgDictSyncService _dictSyncService;
|
||
|
|
|
||
|
|
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 WeightRecordEditDialogViewModel(
|
||
|
|
IWeightRecordService weightRecordService,
|
||
|
|
IJeecgDictSyncService dictSyncService,
|
||
|
|
IContainerExtension container,
|
||
|
|
IRegionManager regionManager) : base(container, regionManager)
|
||
|
|
{
|
||
|
|
_weightRecordService = weightRecordService;
|
||
|
|
_dictSyncService = dictSyncService;
|
||
|
|
SaveCommand = new DelegateCommand(async () => await SaveAsync());
|
||
|
|
CancelCommand = new DelegateCommand(() => CloseAction?.Invoke());
|
||
|
|
_ = LoadDictOptionsAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
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"
|
||
|
|
};
|
||
|
|
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,
|
||
|
|
GoodsName = record.GoodsName,
|
||
|
|
GrossWeight = record.GrossWeight,
|
||
|
|
TareWeight = record.TareWeight,
|
||
|
|
NetWeight = record.NetWeight,
|
||
|
|
DriverName = record.DriverName,
|
||
|
|
DriverPhone = record.DriverPhone,
|
||
|
|
BillType = record.BillType,
|
||
|
|
TenantId = record.TenantId,
|
||
|
|
UpdateTime = record.UpdateTime
|
||
|
|
};
|
||
|
|
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);
|
||
|
|
|
||
|
|
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}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|