using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Text;
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
{
///
/// AddMaterialWindow.xaml 的交互逻辑
///
public partial class AddMaterialWindow : Window
{
///
/// 返回结果:新增的物料信息
///
public BaseMaterialInfo NewMaterial { get; private set; }
private ISqlSugarClient? sqlSugarClient;
private materialinfoSqlFunction AddSqlFunction;
private int WorkingType;
public AddMaterialWindow()
{
InitializeComponent();
AddSqlFunction = new materialinfoSqlFunction(App.ServiceProvider);
WorkingType = 0;
}
public AddMaterialWindow(BaseMaterialInfo baseMaterialInfo)
{
InitializeComponent();
NewMaterial = baseMaterialInfo;
AddSqlFunction = new materialinfoSqlFunction(App.ServiceProvider);
sqlSugarClient = App.ServiceProvider.GetService();
SetComboBoxIndexByValue(CmbMaterialType, NewMaterial.MaterialType);
SetComboBoxIndexByValue(CmbMaterialCategory, NewMaterial.MaterialCategory);
SetComboBoxIndexByValue(CmbStatus,NewMaterial.Status.ToString());
TxtMaterialCode.Text = NewMaterial.MaterialCode;
TxtMaterialName.Text = NewMaterial.MaterialName;
WorkingType = 1;
}
private void SetComboBoxIndexByValue(ComboBox comboBox, string value)
{
foreach (ComboBoxItem item in comboBox.Items)
{
if (item.Tag.ToString() == value)
{
comboBox.SelectedItem = item;
break;
}
}
}
///
/// 获取 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 string GetStatusText()
{
return GetSelectedDisplayText(CmbStatus);
}
///
/// 确认添加按钮
///
private void BtnConfirm_Click(object sender, RoutedEventArgs e)
{
if(WorkingType == 0)
{
// 验证输入
if (!ValidateInput())
return;
// 创建物料信息对象
NewMaterial = new BaseMaterialInfo
{
// 字符串类型字段
MaterialCode = TxtMaterialCode.Text.Trim(),
MaterialName = TxtMaterialName.Text.Trim(),
MaterialType = GetSelectedTagValue(CmbMaterialType),
MaterialCategory = GetSelectedTagValue(CmbMaterialCategory),
// INT类型字段(数据库中存储的是INT值)
Status = GetStatusValue(),
// 设置默认值(新增时自动填充)
IsDeleted = false,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now
};
AddSqlFunction.insertFunction(NewMaterial);
DialogResult = true;
Close();
}
else
{
// 验证输入
if (!ValidateInput())
return;
sqlSugarClient.Updateable().Where(it => it.MaterialCode == TxtMaterialCode.Text.Trim()).SetColumns(it => new BaseMaterialInfo()
{
MaterialCode = TxtMaterialCode.Text.Trim(),
MaterialName = TxtMaterialName.Text.Trim(),
MaterialType = GetSelectedTagValue(CmbMaterialType),
MaterialCategory = GetSelectedTagValue(CmbMaterialCategory),
// INT类型字段(数据库中存储的是INT值)
Status = GetStatusValue(),
// 设置默认值(新增时自动填充)
IsDeleted = false,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now
}).ExecuteCommand();
DialogResult = true;
Close();
}
}
///
/// 取消按钮
///
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
///
/// 验证输入
///
private bool ValidateInput()
{
// 验证物料编号
if (string.IsNullOrWhiteSpace(TxtMaterialCode.Text))
{
MessageBox.Show("请输入物料编号", "验证失败", MessageBoxButton.OK, MessageBoxImage.Warning);
TxtMaterialCode.Focus();
return false;
}
// 验证物料名称
if (string.IsNullOrWhiteSpace(TxtMaterialName.Text))
{
MessageBox.Show("请输入物料名称", "验证失败", MessageBoxButton.OK, MessageBoxImage.Warning);
TxtMaterialName.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();
}
}
}