You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
6.6 KiB
C#

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
{
/// <summary>
/// AddTaskWindow.xaml 的交互逻辑
/// </summary>
public partial class AddTaskWindow : Window
{
/// <summary>
/// 返回结果:新增的任务实例
/// </summary>
public WcsTaskInstance NewTask { get; private set; }
private List<BaseTasktypeInfo> _taskTypes;
private ISqlSugarClient? sqlSugarClient;
private wcstaskSqlFunction AddSqlFunction;
public AddTaskWindow()
{
InitializeComponent();
AddSqlFunction = new wcstaskSqlFunction(App.ServiceProvider);
sqlSugarClient = App.ServiceProvider.GetService<ISqlSugarClient>();
_taskTypes = sqlSugarClient.Queryable<BaseTasktypeInfo>().ToList();
LoadTaskTypes();
}
/// <summary>
/// 加载任务类型下拉框
/// </summary>
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;
}
}
/// <summary>
/// 任务类型选择变化事件
/// </summary>
private void CmbTaskType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// 待定
}
/// <summary>
/// 获取 ComboBox 选中项的 Tag 值
/// </summary>
private string GetSelectedTagValue(ComboBox comboBox)
{
var selectedItem = comboBox.SelectedItem as ComboBoxItem;
if (selectedItem != null && selectedItem.Tag != null)
{
return selectedItem.Tag.ToString();
}
return "";
}
/// <summary>
/// 获取 ComboBox 选中项的显示文本
/// </summary>
private string GetSelectedDisplayText(ComboBox comboBox)
{
var selectedItem = comboBox.SelectedItem as ComboBoxItem;
return selectedItem?.Content?.ToString() ?? "";
}
/// <summary>
/// 获取优先级值
/// </summary>
private int? GetPriorityValue()
{
var tagValue = GetSelectedTagValue(CmbPriority);
if (int.TryParse(tagValue, out int result))
{
return result;
}
return 0;
}
/// <summary>
/// 确认添加按钮
/// </summary>
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();
}
/// <summary>
/// 取消按钮
/// </summary>
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
/// <summary>
/// 验证输入
/// </summary>
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;
}
/// <summary>
/// 窗口拖拽
/// </summary>
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ButtonState == MouseButtonState.Pressed)
{
this.DragMove();
}
}
/// <summary>
/// 最小化
/// </summary>
private void BtnMinimize_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
/// <summary>
/// 关闭
/// </summary>
private void BtnClose_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
}
}