using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using Dm.util; using Microsoft.Extensions.DependencyInjection; using Sln.Wcs.Function.Functions; using Sln.Wcs.Model.Domain; using SqlSugar; namespace Sln_Wpf.Page.AddWindows { /// /// AddSubtaskWindow.xaml 的交互逻辑 /// public partial class AddSubtaskWindow : Window { /// /// 返回结果:新增的子任务信息 /// public BaseSubtaskTemplate NewSubtask { get; private set; } private ISqlSugarClient? sqlSugarClient; private subtaskSqlFunction AddSqlFunction; private int WorkingType; public AddSubtaskWindow() { InitializeComponent(); AddSqlFunction = new subtaskSqlFunction(App.ServiceProvider); WorkingType = 0; } public AddSubtaskWindow(BaseSubtaskTemplate baseSubtaskTemplate) { NewSubtask = baseSubtaskTemplate; sqlSugarClient = App.ServiceProvider.GetService(); InitializeComponent(); Init(baseSubtaskTemplate); AddSqlFunction = new subtaskSqlFunction(App.ServiceProvider); WorkingType = 1; } public void Init(BaseSubtaskTemplate baseSubtaskTemplate) { TxtSubtaskCode.Text = baseSubtaskTemplate.SubtaskCode; TxtSubtaskName.Text = baseSubtaskTemplate.SubtaskName; SetComboBoxIndexByValue(CmbSubtaskType, baseSubtaskTemplate.SubtaskType); SetComboBoxIndexByValue(CmbDeviceType, baseSubtaskTemplate.DeviceType); SetComboBoxIndexByValue(CmbDeviceAction, baseSubtaskTemplate.DeviceAction); SetComboBoxIndexByValue(CmbSourceLocationType, baseSubtaskTemplate.SourceLocationType); TxtSourceLocationCode.Text = baseSubtaskTemplate.SourceLocationCode; SetComboBoxIndexByValue(CmbTargetLocationType, baseSubtaskTemplate.TargetLocationType); TxtTargetLocationCode.Text = baseSubtaskTemplate.TargetLocationCode; SetComboBoxIndexByValue(CmbMaterialAction, baseSubtaskTemplate.MaterialAction); SetComboBoxIndexByValue(CmbContainerType, baseSubtaskTemplate.ContainerType); SetComboBoxIndexByValue(CmbIsCarryMaterial, baseSubtaskTemplate.IsCarryMaterial.toString()); TxtRequiredParams.Text = baseSubtaskTemplate.RequiredParams; SetComboBoxIndexByValue(CmbStatus, baseSubtaskTemplate.Status.toString()); } /// /// 获取 ComboBox 选中项的 Tag 值 /// private string GetSelectedTagValue(ComboBox comboBox) { var selectedItem = comboBox.SelectedItem as ComboBoxItem; if (selectedItem != null && selectedItem.Tag != null) { return selectedItem.Tag.ToString(); } return ""; } /// /// 获取 ComboBox 选中项的显示文本 /// private string GetSelectedDisplayText(ComboBox comboBox) { var selectedItem = comboBox.SelectedItem as ComboBoxItem; return selectedItem?.Content?.ToString() ?? ""; } /// /// 获取状态值(INT类型:0-禁用 1-启用) /// private int GetStatusValue() { var selectedItem = CmbStatus.SelectedItem as ComboBoxItem; if (selectedItem != null && selectedItem.Tag != null) { return Convert.ToInt32(selectedItem.Tag); } return 1; } /// /// 获取是否携带物料值 /// private bool GetIsCarryMaterialValue() { var selectedItem = CmbIsCarryMaterial.SelectedItem as ComboBoxItem; if (selectedItem != null && selectedItem.Tag != null) { return Convert.ToBoolean(selectedItem.Tag); } return false; } private void CmbSelectFunction(ComboBox comboBox, string content) { foreach (ComboBoxItem item in comboBox.Items) { if (item.Content.ToString() == content) { comboBox.SelectedItem = item; break; } } } private void SetComboBoxIndexByValue(ComboBox comboBox, string value) { foreach (ComboBoxItem item in comboBox.Items) { if (item.Tag.ToString() == value) { comboBox.SelectedItem = item; break; } } } /// /// 确认添加按钮 /// private void BtnConfirm_Click(object sender, RoutedEventArgs e) { // 验证输入 if (!ValidateInput()) return; // 创建子任务对象 NewSubtask = new BaseSubtaskTemplate { SubtaskCode = TxtSubtaskCode.Text.Trim(), SubtaskName = TxtSubtaskName.Text.Trim(), SubtaskType = GetSelectedTagValue(CmbSubtaskType), DeviceType = GetSelectedTagValue(CmbDeviceType), DeviceAction = GetSelectedTagValue(CmbDeviceAction), SourceLocationType = GetSelectedTagValue(CmbSourceLocationType), SourceLocationCode = TxtSourceLocationCode.Text.Trim(), TargetLocationType = GetSelectedTagValue(CmbTargetLocationType), TargetLocationCode = TxtTargetLocationCode.Text.Trim(), MaterialAction = GetSelectedTagValue(CmbMaterialAction), ContainerType = GetSelectedTagValue(CmbContainerType), IsCarryMaterial = GetIsCarryMaterialValue(), RequiredParams = TxtRequiredParams.Text.Trim(), // INT类型字段 Status = GetStatusValue(), // 设置默认值 CreateTime = DateTime.Now, UpdateTime = DateTime.Now }; AddSqlFunction.insertFunction(NewSubtask); DialogResult = true; Close(); } /// /// 取消按钮 /// private void BtnCancel_Click(object sender, RoutedEventArgs e) { DialogResult = false; Close(); } /// /// 验证输入 /// private bool ValidateInput() { // 验证子任务编码 if (string.IsNullOrWhiteSpace(TxtSubtaskCode.Text)) { MessageBox.Show("请输入子任务编码", "验证失败", MessageBoxButton.OK, MessageBoxImage.Warning); TxtSubtaskCode.Focus(); return false; } // 验证子任务名称 if (string.IsNullOrWhiteSpace(TxtSubtaskName.Text)) { MessageBox.Show("请输入子任务名称", "验证失败", MessageBoxButton.OK, MessageBoxImage.Warning); TxtSubtaskName.Focus(); return false; } return true; } /// /// 窗口拖拽 /// private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.ButtonState == MouseButtonState.Pressed) { this.DragMove(); } } /// /// 最小化 /// private void BtnMinimize_Click(object sender, RoutedEventArgs e) { this.WindowState = WindowState.Minimized; } /// /// 关闭 /// private void BtnClose_Click(object sender, RoutedEventArgs e) { DialogResult = false; Close(); } } }