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 Microsoft.Extensions.DependencyInjection; using Sln.Wcs.Function.Functions; using Sln.Wcs.Model.Domain; using SqlSugar; namespace Sln_Wpf.Page.AddWindows { /// /// AddTaskWindow.xaml 的交互逻辑 /// public partial class AddTaskWindow : Window { /// /// 返回结果:新增的任务实例 /// public WcsTaskInstance NewTask { get; private set; } private List _taskTypes; private ISqlSugarClient? sqlSugarClient; private wcstaskSqlFunction AddSqlFunction; public AddTaskWindow() { InitializeComponent(); AddSqlFunction = new wcstaskSqlFunction(App.ServiceProvider); sqlSugarClient = App.ServiceProvider.GetService(); _taskTypes = sqlSugarClient.Queryable().ToList(); LoadTaskTypes(); } /// /// 加载任务类型下拉框 /// private void LoadTaskTypes() { CmbTaskType.Items.Clear(); if (_taskTypes != null && _taskTypes.Any()) { foreach (var taskType in _taskTypes) { if (taskType.UseFlag == 1) // 只显示启用的任务类型 { var item = new ComboBoxItem { Content = taskType.TaskTypeName, Tag = taskType.TaskTypeKey }; CmbTaskType.Items.Add(item); } } } if (CmbTaskType.Items.Count > 0) { CmbTaskType.SelectedIndex = 0; } } /// /// 任务类型选择变化事件 /// private void CmbTaskType_SelectionChanged(object sender, SelectionChangedEventArgs e) { // 待定 } /// /// 获取 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() ?? ""; } /// /// 获取优先级值 /// private int? GetPriorityValue() { var tagValue = GetSelectedTagValue(CmbPriority); if (int.TryParse(tagValue, out int result)) { return result; } return 0; } /// /// 确认添加按钮 /// private void BtnConfirm_Click(object sender, RoutedEventArgs e) { // 验证输入 if (!ValidateInput()) return; // 获取任务类型信息 string taskTypeKey = GetSelectedTagValue(CmbTaskType); string taskTypeName = GetSelectedDisplayText(CmbTaskType); // 创建任务实例对象 NewTask = new WcsTaskInstance { TaskCode = TxtTaskCode.Text.Trim(), TaskName = TxtTaskName.Text.Trim(), TaskTypeKey = taskTypeKey, TaskTypeName = taskTypeName, Priority = GetPriorityValue(), Status = "RUNNING", SourceLocation = TxtSourceLocation.Text.Trim(), TargetLocation = TxtTargetLocation.Text.Trim(), Remark = TxtRemark.Text.Trim(), // 默认值 IsDeleted = false, CreateTime = DateTime.Now, UpdateTime = DateTime.Now }; AddSqlFunction.insertFunction(NewTask); DialogResult = true; Close(); } /// /// 取消按钮 /// private void BtnCancel_Click(object sender, RoutedEventArgs e) { DialogResult = false; Close(); } /// /// 验证输入 /// private bool ValidateInput() { // 验证任务编码 if (string.IsNullOrWhiteSpace(TxtTaskCode.Text)) { MessageBox.Show("请输入任务编码", "验证失败", MessageBoxButton.OK, MessageBoxImage.Warning); TxtTaskCode.Focus(); return false; } // 验证任务名称 if (string.IsNullOrWhiteSpace(TxtTaskName.Text)) { MessageBox.Show("请输入任务名称", "验证失败", MessageBoxButton.OK, MessageBoxImage.Warning); TxtTaskName.Focus(); return false; } // 验证任务类型 if (CmbTaskType.SelectedItem == null || GetSelectedTagValue(CmbTaskType) == "") { MessageBox.Show("请选择任务类型", "验证失败", MessageBoxButton.OK, MessageBoxImage.Warning); CmbTaskType.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(); } } }