using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Reflection.PortableExecutable;
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
{
///
/// AddLocationWindow.xaml 的交互逻辑
///
public partial class AddLocationWindow : Window
{
///
/// 返回结果:新增的库位信息
///
public BaseLocation NewLocation { get; private set; }
private ISqlSugarClient? sqlSugarClient;
private locationSqlFunction AddSqlFunction;
private int WorkingType;
public AddLocationWindow()
{
InitializeComponent();
AddSqlFunction = new locationSqlFunction(App.ServiceProvider);
WorkingType = 0;
}
public AddLocationWindow(BaseLocation baseLocation)
{
InitializeComponent();
AddSqlFunction = new locationSqlFunction(App.ServiceProvider);
sqlSugarClient = App.ServiceProvider.GetService();
SetComboBoxIndexByValue(CmbWorkshopID, baseLocation.WorkshopId.ToString());
SetComboBoxIndexByValue(CmbWorkshopLevel, baseLocation.WorkshopLevel.ToString());
SetComboBoxIndexByValue(CmbActiveFlag, baseLocation.ActiveFlag.ToString());
SetComboBoxIndexByValue(CmbLocationStatus, baseLocation.LocationStatus.ToString());
TxtWarehouseID.Text = baseLocation.WarehouseId.ToString();
TxtMachineID.Text = baseLocation.MachineId.ToString();
TxtLocationCode.Text = baseLocation.LocationCode;
TxtAgvPositionCode.Text = baseLocation.AgvPositionCode;
TxtContainerCode.Text = baseLocation.ContainerCode;
WorkingType = 1;
}
///
/// 获取 ComboBox 选中项的 Tag 值(INT类型)
///
private int GetSelectedTagValue(ComboBox comboBox)
{
var selectedItem = comboBox.SelectedItem as ComboBoxItem;
if (selectedItem != null && selectedItem.Tag != null)
{
return Convert.ToInt32(selectedItem.Tag);
}
return 0;
}
private void SetComboBoxIndexByValue(ComboBox comboBox, string value)
{
foreach (ComboBoxItem item in comboBox.Items)
{
if (item.Tag.ToString() == value)
{
comboBox.SelectedItem = item;
break;
}
}
}
///
/// 获取 ComboBox 选中项的显示文本
///
private string GetSelectedDisplayText(ComboBox comboBox)
{
var selectedItem = comboBox.SelectedItem as ComboBoxItem;
return selectedItem?.Content?.ToString() ?? "";
}
///
/// 确认添加按钮
///
private void BtnConfirm_Click(object sender, RoutedEventArgs e)
{
if (WorkingType == 0)
{
// 验证输入
if (!ValidateInput())
return;
// 创建库位信息对象
NewLocation = new BaseLocation
{
WarehouseId = Convert.ToInt32(TxtWarehouseID.Text.Trim()),
MachineId = TxtMachineID.Text.Trim(),
LocationCode = TxtLocationCode.Text.Trim(),
AgvPositionCode = TxtAgvPositionCode.Text.Trim(),
ContainerCode = TxtContainerCode.Text.Trim(),
WorkshopId = GetSelectedTagValue(CmbWorkshopID),
WorkshopLevel = GetSelectedTagValue(CmbWorkshopLevel),
ActiveFlag = GetSelectedTagValue(CmbActiveFlag),
LocationStatus = GetSelectedTagValue(CmbLocationStatus),
};
AddSqlFunction.insertFunction(NewLocation);
DialogResult = true;
Close();
}
else
{
if (!ValidateInput())
return;
sqlSugarClient.Updateable().Where(it => it.LocationCode == TxtLocationCode.Text.Trim()).SetColumns(it => new BaseLocation()
{
WarehouseId = Convert.ToInt32(TxtWarehouseID.Text.Trim()),
MachineId = TxtMachineID.Text.Trim(),
LocationCode = TxtLocationCode.Text.Trim(),
AgvPositionCode = TxtAgvPositionCode.Text.Trim(),
ContainerCode = TxtContainerCode.Text.Trim(),
WorkshopId = GetSelectedTagValue(CmbWorkshopID),
WorkshopLevel = GetSelectedTagValue(CmbWorkshopLevel),
ActiveFlag = GetSelectedTagValue(CmbActiveFlag),
LocationStatus = GetSelectedTagValue(CmbLocationStatus),
}).ExecuteCommand();
//传递之前页面
NewLocation = new BaseLocation
{
WarehouseId = Convert.ToInt32(TxtWarehouseID.Text.Trim()),
MachineId = TxtMachineID.Text.Trim(),
LocationCode = TxtLocationCode.Text.Trim(),
AgvPositionCode = TxtAgvPositionCode.Text.Trim(),
ContainerCode = TxtContainerCode.Text.Trim(),
WorkshopId = GetSelectedTagValue(CmbWorkshopID),
WorkshopLevel = GetSelectedTagValue(CmbWorkshopLevel),
ActiveFlag = GetSelectedTagValue(CmbActiveFlag),
LocationStatus = GetSelectedTagValue(CmbLocationStatus),
};
DialogResult = true;
Close();
}
}
///
/// 取消按钮
///
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
///
/// 验证输入
///
private bool ValidateInput()
{
// 验证仓库编号
if (string.IsNullOrWhiteSpace(TxtWarehouseID.Text))
{
MessageBox.Show("请输入仓库编号", "验证失败", MessageBoxButton.OK, MessageBoxImage.Warning);
TxtWarehouseID.Focus();
return false;
}
// 验证机台编号
if (string.IsNullOrWhiteSpace(TxtMachineID.Text))
{
MessageBox.Show("请输入机台编号", "验证失败", MessageBoxButton.OK, MessageBoxImage.Warning);
TxtMachineID.Focus();
return false;
}
// 验证位置编号
if (string.IsNullOrWhiteSpace(TxtLocationCode.Text))
{
MessageBox.Show("请输入位置编号", "验证失败", MessageBoxButton.OK, MessageBoxImage.Warning);
TxtLocationCode.Focus();
return false;
}
// 验证AGV位置编号
if (string.IsNullOrWhiteSpace(TxtAgvPositionCode.Text))
{
MessageBox.Show("请输入AGV位置编号", "验证失败", MessageBoxButton.OK, MessageBoxImage.Warning);
TxtAgvPositionCode.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();
}
}
}