using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq.Expressions; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Sln.Wcs.Model.Domain; using Sln.Wcs.Repository.service; using Sln.Wcs.UI.ViewModels.Base; using Sln.Wcs.UI.Views.Base; namespace Sln.Wcs.UI.ViewModels.Base; public partial class StoreInfoViewModel : CrudPageViewModel { private readonly IBaseLocationInfoService _locationService; private BaseStoreInfo? _currentStore; [ObservableProperty] private bool _isPanelOpen; [ObservableProperty] private ObservableCollection _storeLocations = new(); [ObservableProperty] private string _slidePanelTitle = string.Empty; public StoreInfoViewModel(IBaseStoreInfoService service, IBaseLocationInfoService locationService) : base(service) { _locationService = locationService; PageTitle = "仓库信息管理"; } public override List FieldConfigs => new() { new() { PropertyName = "storeCode", DisplayName = "仓库编号", IsRequired = true }, new() { PropertyName = "storeName", DisplayName = "仓库名称" }, new() { PropertyName = "storeType", DisplayName = "仓库类型", FieldType = FieldType.Number }, new() { PropertyName = "isFlag", DisplayName = "启用", FieldType = FieldType.CheckBox }, new() { PropertyName = "remark", DisplayName = "备注" }, }; protected override Expression>? BuildSearchExpression(string search) => x => x.storeCode.Contains(search) || (x.storeName != null && x.storeName.Contains(search)); public override Avalonia.Controls.Control CreateView() => new Sln.Wcs.UI.Views.Base.StoreInfoListView(); public void LoadLocationsData(BaseStoreInfo store) { _currentStore = store; LoadLocations(); SlidePanelTitle = $"库位列表 - {store.storeCode}"; } [RelayCommand] public void ClosePanel() { IsPanelOpen = false; } [RelayCommand] private async System.Threading.Tasks.Task AddLocation() { if (_currentStore is null) return; var entity = new BaseLocationInfo { storeCode = _currentStore.storeCode }; var editor = new EntityEditWindow(); var result = await editor.ShowDialog(entity, LocationFieldConfigs, false, GetMainWindow()); if (result) { _locationService.Insert(entity); LoadLocations(); } } public async System.Threading.Tasks.Task EditLocationAsync(BaseLocationInfo location) { var editor = new EntityEditWindow(); var result = await editor.ShowDialog(location, LocationFieldConfigs, true, GetMainWindow()); if (result) { _locationService.Update(location); LoadLocations(); } } public async System.Threading.Tasks.Task DeleteLocationAsync(BaseLocationInfo location) { var dlg = new Sln.Wcs.UI.Views.Base.ConfirmDialog(); if (!await dlg.ShowDialog("确定要删除该库位吗?", GetMainWindow())) return; _locationService.DeleteById(location.objId); LoadLocations(); } private void LoadLocations() { if (_currentStore is null) return; var list = _locationService.Query(x => x.storeCode == _currentStore.storeCode); for (int i = 0; i < list.Count; i++) list[i].RowIndex = i + 1; StoreLocations = new ObservableCollection(list); } public List LocationFieldConfigs => new() { new() { PropertyName = "locationCode", DisplayName = "库位编号", IsRequired = true }, new() { PropertyName = "storeCode", DisplayName = "仓库编号", IsRequired = true, IsReadOnly = true }, new() { PropertyName = "locationName", DisplayName = "库位名称" }, new() { PropertyName = "locationArea", DisplayName = "库位区域" }, new() { PropertyName = "locationRows", DisplayName = "排", FieldType = FieldType.Number }, new() { PropertyName = "locationColumns", DisplayName = "列", FieldType = FieldType.Number }, new() { PropertyName = "locationLayers", DisplayName = "层", FieldType = FieldType.Number }, new() { PropertyName = "locationStatus", DisplayName = "库位状态", FieldType = FieldType.Combo, Options = StandardOptions.LocationStatus }, new() { PropertyName = "agvPosition", DisplayName = "AGV定位" }, new() { PropertyName = "materialCode", DisplayName = "物料编号" }, new() { PropertyName = "palletBarcode", DisplayName = "托盘条码" }, new() { PropertyName = "stackCount", DisplayName = "库存数量" }, new() { PropertyName = "isFlag", DisplayName = "启用", FieldType = FieldType.CheckBox }, new() { PropertyName = "remark", DisplayName = "备注" }, }; private Avalonia.Controls.Window GetMainWindow() { return (Avalonia.Controls.Window)Avalonia.Application.Current! .ApplicationLifetime!.GetType() .GetProperty("MainWindow")! .GetValue(Avalonia.Application.Current.ApplicationLifetime)!; } }