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 DeviceAddWindow : Window { private readonly IBaseDeviceInfoService _deviceInfoService; private readonly DeviceInfoPage _parentPage; public DeviceAddWindow(DeviceInfoPage parentPage) { InitializeComponent(); _deviceInfoService = App.ServiceProvider.GetService(); _parentPage = parentPage; } private void BtnConfirm_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(TxtDeviceCode.Text)) { CustomMessageBox.Show("请输入设备编号", "提示", MessageBoxButton.OK); return; } 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; var device = new BaseDeviceInfo { deviceCode = TxtDeviceCode.Text.Trim(), deviceName = TxtDeviceName.Text.Trim(), deviceType = deviceTypeItem?.Tag != null ? int.Parse(deviceTypeItem.Tag.ToString()) : null, deviceStatus = deviceStatusItem?.Tag != null ? int.Parse(deviceStatusItem.Tag.ToString()) : null, isFlag = isFlagItem?.Tag != null ? int.Parse(isFlagItem.Tag.ToString()) : null, remark = TxtRemark.Text?.Trim(), createdTime = DateTime.Now }; var success = _deviceInfoService.Insert(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() { ClearForm(); _parentPage.LoadData(); Close(); } private void ClearForm() { TxtDeviceCode.Text = ""; TxtDeviceName.Text = ""; TxtRemark.Text = ""; CmbDeviceType.SelectedIndex = 0; CmbDeviceStatus.SelectedIndex = 0; CmbIsFlag.SelectedIndex = 0; } } }