using Microsoft.Extensions.DependencyInjection; using Sln.Wcs.Model.Domain; using Sln.Wcs.Repository.service; using Sln.Wcs.UI.Controls; using System; using System.Windows; using System.Windows.Controls; namespace Sln.Wcs.UI.Page.BasicInfo.DeviceInfo { public partial class DeviceEditWindow : Window { private readonly IBaseDeviceInfoService _deviceInfoService; private readonly DeviceInfoPage _parentPage; private readonly BaseDeviceInfo _device; public DeviceEditWindow(DeviceInfoPage parentPage, BaseDeviceInfo device) { InitializeComponent(); _deviceInfoService = App.ServiceProvider.GetService(); _parentPage = parentPage; _device = device; LoadDeviceData(); } private void LoadDeviceData() { TxtDeviceCode.Text = _device.deviceCode; TxtDeviceName.Text = _device.deviceName; TxtRemark.Text = _device.remark; if (_device.deviceType.HasValue) { SelectComboBoxItem(CmbDeviceType, _device.deviceType.Value); } if (_device.deviceStatus.HasValue) { SelectComboBoxItem(CmbDeviceStatus, _device.deviceStatus.Value); } if (_device.isFlag.HasValue) { SelectComboBoxItem(CmbIsFlag, _device.isFlag.Value); } } private void SelectComboBoxItem(ComboBox comboBox, int value) { foreach (ComboBoxItem item in comboBox.Items) { if (item.Tag != null && int.TryParse(item.Tag.ToString(), out int tagValue) && tagValue == value) { comboBox.SelectedItem = item; break; } } } private void BtnConfirm_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(TxtDeviceName.Text)) { CustomMessageBox.Show("请输入设备名称", "提示", MessageBoxButton.OK); return; } var deviceTypeItem = CmbDeviceType.SelectedItem as ComboBoxItem; var deviceStatusItem = CmbDeviceStatus.SelectedItem as ComboBoxItem; var isFlagItem = CmbIsFlag.SelectedItem as ComboBoxItem; _device.deviceName = TxtDeviceName.Text.Trim(); _device.deviceType = deviceTypeItem?.Tag != null ? int.Parse(deviceTypeItem.Tag.ToString()) : null; _device.deviceStatus = deviceStatusItem?.Tag != null ? int.Parse(deviceStatusItem.Tag.ToString()) : null; _device.isFlag = isFlagItem?.Tag != null ? int.Parse(isFlagItem.Tag.ToString()) : null; _device.remark = TxtRemark.Text?.Trim(); _device.updatedTime = DateTime.Now; var success = _deviceInfoService.Update(_device); if (success) { CustomMessageBox.Show("修改成功", "提示", MessageBoxButton.OK); CloseAndRefresh(); } else { CustomMessageBox.Show("修改失败", "错误", MessageBoxButton.OK); } } private void BtnCancel_Click(object sender, RoutedEventArgs e) { CloseAndRefresh(); } private void BtnClose_Click(object sender, RoutedEventArgs e) { CloseAndRefresh(); } private void CloseAndRefresh() { _parentPage.LoadData(); Close(); } } }