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.Device; public partial class DeviceInfoViewModel : CrudPageViewModel { private readonly IBaseDeviceParamService _paramService; private BaseDeviceInfo? _currentDevice; [ObservableProperty] private bool _isPanelOpen; [ObservableProperty] private ObservableCollection _deviceParams = new(); [ObservableProperty] private string _slidePanelTitle = string.Empty; public DeviceInfoViewModel(IBaseDeviceInfoService service, IBaseDeviceParamService paramService) : base(service) { _paramService = paramService; PageTitle = "设备信息管理"; } public override List FieldConfigs => new() { new() { PropertyName = "deviceCode", DisplayName = "设备编号", IsRequired = true }, new() { PropertyName = "deviceName", DisplayName = "设备名称" }, new() { PropertyName = "deviceAlias", DisplayName = "设备别名" }, new() { PropertyName = "deviceSerialNo", DisplayName = "设备序号", FieldType = FieldType.Number }, new() { PropertyName = "deviceType", DisplayName = "设备类型", FieldType = FieldType.Number }, new() { PropertyName = "deviceStatus", DisplayName = "设备状态", FieldType = FieldType.Number }, new() { PropertyName = "hostCode", DisplayName = "主机编号", IsRequired = true }, new() { PropertyName = "isFlag", DisplayName = "启用", FieldType = FieldType.CheckBox }, new() { PropertyName = "remark", DisplayName = "备注" }, }; protected override Expression>? BuildSearchExpression(string search) => x => x.deviceCode.Contains(search) || (x.deviceName != null && x.deviceName.Contains(search)); public override Avalonia.Controls.Control CreateView() => new Sln.Wcs.UI.Views.Device.DeviceInfoListView(); public void LoadParamsData(BaseDeviceInfo device) { _currentDevice = device; LoadParams(); SlidePanelTitle = $"设备参数 - {device.deviceCode}"; } [RelayCommand] public void ClosePanel() { IsPanelOpen = false; } [RelayCommand] private async System.Threading.Tasks.Task AddParam() { if (_currentDevice is null) return; var entity = new BaseDeviceParam { deviceCode = _currentDevice.deviceCode }; var editor = new EntityEditWindow(); var result = await editor.ShowDialog(entity, ParamFieldConfigs, false, GetMainWindow()); if (result) { _paramService.Insert(entity); LoadParams(); } } public async System.Threading.Tasks.Task EditParamAsync(BaseDeviceParam param) { var editor = new EntityEditWindow(); var result = await editor.ShowDialog(param, ParamFieldConfigs, true, GetMainWindow()); if (result) { _paramService.Update(param); LoadParams(); } } public void DeleteParam(BaseDeviceParam param) { _paramService.DeleteById(param.objId); LoadParams(); } private void LoadParams() { if (_currentDevice is null) return; var list = _paramService.Query(x => x.deviceCode == _currentDevice.deviceCode); DeviceParams = new ObservableCollection(list); } public List ParamFieldConfigs => new() { new() { PropertyName = "paramCode", DisplayName = "参数编号", IsRequired = true }, new() { PropertyName = "deviceCode", DisplayName = "设备编号", IsRequired = true, IsReadOnly = true }, new() { PropertyName = "paramName", DisplayName = "参数名称", IsRequired = true }, new() { PropertyName = "paramAddress", DisplayName = "参数地址", IsRequired = true }, new() { PropertyName = "paramType", DisplayName = "参数类型" }, new() { PropertyName = "paramValue", DisplayName = "参数值", FieldType = FieldType.Number }, new() { PropertyName = "operationType", DisplayName = "操作类型" }, new() { PropertyName = "operationFrequency", DisplayName = "操作频率(ms)" }, 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)!; } }