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.Vehicle; public class VehicleEditDialogViewModel : BaseViewModel, IDialogResultable { private readonly IVehicleService _vehicleService; private readonly IJeecgDictSyncService _dictSyncService; private MesXslVehicle? _vehicle; public MesXslVehicle? Vehicle { get => _vehicle; set => SetProperty(ref _vehicle, value); } public bool IsAddMode => string.IsNullOrWhiteSpace(Vehicle?.Id); public string DialogTitle => IsAddMode ? "新增车辆" : "编辑车辆"; public ObservableCollection> VehicleBelongOptions { get; } = new(); public ObservableCollection> StatusOptions { 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 VehicleEditDialogViewModel( IVehicleService vehicleService, IJeecgDictSyncService dictSyncService, IContainerExtension container, IRegionManager regionManager) : base(container, regionManager) { _vehicleService = vehicleService; _dictSyncService = dictSyncService; SaveCommand = new DelegateCommand(async () => await SaveAsync()); CancelCommand = new DelegateCommand(() => CloseAction?.Invoke()); _ = LoadDictOptionsAsync(); } private async Task LoadDictOptionsAsync() { try { var belongOptions = await _dictSyncService.GetDictOptionsAsync("xslmes_vehicle_belong"); var statusOptions = await _dictSyncService.GetDictOptionsAsync("xslmes_vehicle_status"); VehicleBelongOptions.Clear(); foreach (var item in belongOptions) { VehicleBelongOptions.Add(item); } StatusOptions.Clear(); foreach (var item in statusOptions) { StatusOptions.Add(item); } if (VehicleBelongOptions.Count == 0) { VehicleBelongOptions.Add(new KeyValuePair("客户", "1")); VehicleBelongOptions.Add(new KeyValuePair("供应商", "2")); VehicleBelongOptions.Add(new KeyValuePair("本公司", "3")); } if (StatusOptions.Count == 0) { StatusOptions.Add(new KeyValuePair("启用", "0")); StatusOptions.Add(new KeyValuePair("停用", "1")); } } catch { VehicleBelongOptions.Clear(); VehicleBelongOptions.Add(new KeyValuePair("客户", "1")); VehicleBelongOptions.Add(new KeyValuePair("供应商", "2")); VehicleBelongOptions.Add(new KeyValuePair("本公司", "3")); StatusOptions.Clear(); StatusOptions.Add(new KeyValuePair("启用", "0")); StatusOptions.Add(new KeyValuePair("停用", "1")); } } public void InitializeForAdd() { Vehicle = new MesXslVehicle { Status = "0", VehicleBelong = "1" }; RaisePropertyChanged(nameof(IsAddMode)); RaisePropertyChanged(nameof(DialogTitle)); } public void InitializeForEdit(MesXslVehicle vehicle) { Vehicle = new MesXslVehicle { Id = vehicle.Id, PlateNumber = vehicle.PlateNumber, VehicleBelong = vehicle.VehicleBelong, TareWeightKg = vehicle.TareWeightKg, LoadCapacity = vehicle.LoadCapacity, UnitId = vehicle.UnitId, LoadUnit = vehicle.LoadUnit, CustomerIds = vehicle.CustomerIds, CustomerShortName = vehicle.CustomerShortName, SupplierId = vehicle.SupplierId, SupplierName = vehicle.SupplierName, SupplierShortName = vehicle.SupplierShortName, VehicleLength = vehicle.VehicleLength, VehicleWidth = vehicle.VehicleWidth, VehicleHeight = vehicle.VehicleHeight, DriverName = vehicle.DriverName, DriverPhone = vehicle.DriverPhone, Status = vehicle.Status, TenantId = vehicle.TenantId, }; RaisePropertyChanged(nameof(IsAddMode)); RaisePropertyChanged(nameof(DialogTitle)); } private async Task SaveAsync() { if (Vehicle == null) return; if (string.IsNullOrWhiteSpace(Vehicle.PlateNumber)) { HandyControl.Controls.MessageBox.Warning("车牌号不能为空!"); return; } if (string.IsNullOrWhiteSpace(Vehicle.VehicleBelong)) { HandyControl.Controls.MessageBox.Warning("车辆归属不能为空!"); return; } try { bool ok; if (IsAddMode) { ok = await _vehicleService.AddAsync(Vehicle); if (ok) { HandyControl.Controls.MessageBox.Success("新增车辆成功!"); } else { HandyControl.Controls.MessageBox.Error("新增车辆失败!"); return; } } else { ok = await _vehicleService.EditAsync(Vehicle); if (!ok) { HandyControl.Controls.MessageBox.Error("编辑车辆失败!"); return; } } Result = ok; CloseAction?.Invoke(); } catch (Exception ex) { HandyControl.Controls.MessageBox.Error($"操作失败:{ex.Message}"); } } }