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.

189 lines
5.8 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using Sln.Wcs.Model.Domain;
using Sln.Wcs.Repository.service;
using Sln.Wcs.UI.Controls;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace Sln.Wcs.UI.Page.BasicInfo.LocationInfo
{
public partial class LocationInfoPage : UserControl
{
private readonly IBaseLocationInfoService _locationService;
private string _searchLocationCode = "";
private string _searchLocationName = "";
private int? _searchLocationStatus = null;
public LocationInfoPage()
{
InitializeComponent();
_locationService = App.ServiceProvider.GetService<IBaseLocationInfoService>();
LoadData();
}
private void LoadData()
{
try
{
var list = _locationService.Query()
.Where(x => string.IsNullOrEmpty(_searchLocationCode) || (x.locationCode != null && x.locationCode.Contains(_searchLocationCode)))
.Where(x => string.IsNullOrEmpty(_searchLocationName) || (x.locationName != null && x.locationName.Contains(_searchLocationName)))
.Where(x => !_searchLocationStatus.HasValue || x.locationStatus == _searchLocationStatus)
.OrderByDescending(x => x.updatedTime)
.ToList();
for (int i = 0; i < list.Count; i++)
{
list[i].RowIndex = i + 1;
}
dataGrid.ItemsSource = list;
}
catch (Exception ex)
{
CustomMessageBox.Show($"加载数据失败: {ex.Message}", "错误");
}
}
private void BtnSearch_Click(object sender, RoutedEventArgs e)
{
_searchLocationCode = txtLocationCode.Text.Trim();
_searchLocationName = txtLocationName.Text.Trim();
_searchLocationStatus = GetSelectedStatus();
LoadData();
}
private int? GetSelectedStatus()
{
if (cmbLocationStatus.SelectedItem is ComboBoxItem item && item.Tag != null)
{
if (int.TryParse(item.Tag.ToString(), out int status))
{
return status;
}
}
return null;
}
private void BtnReset_Click(object sender, RoutedEventArgs e)
{
txtLocationCode.Text = "";
txtLocationName.Text = "";
cmbLocationStatus.SelectedIndex = 0;
_searchLocationCode = "";
_searchLocationName = "";
_searchLocationStatus = null;
LoadData();
}
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
var window = new LocationAddWindow
{
Owner = Window.GetWindow(this)
};
if (window.ShowDialog() == true)
{
LoadData();
}
}
private void BtnEdit_Click(object sender, RoutedEventArgs e)
{
if (dataGrid.SelectedItem is BaseLocationInfo location)
{
var window = new LocationEditWindow(location)
{
Owner = Window.GetWindow(this)
};
if (window.ShowDialog() == true)
{
LoadData();
}
}
}
private void BtnDelete_Click(object sender, RoutedEventArgs e)
{
if (dataGrid.SelectedItem is BaseLocationInfo location)
{
var result = CustomMessageBox.Show(
$"确定要删除库位 \"{location.locationCode}\" 吗?",
"确认删除",
MessageBoxButton.YesNo
);
if (result == MessageBoxResult.Yes)
{
try
{
_locationService.Delete(location);
CustomMessageBox.Show("删除成功", "提示");
LoadData();
}
catch (Exception ex)
{
CustomMessageBox.Show($"删除失败: {ex.Message}", "错误");
}
}
}
}
private void DataGrid_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
BtnEdit_Click(sender, e);
}
}
public class LocationStatusConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is int status)
{
return status switch
{
0 => "未使用",
1 => "已使用",
2 => "锁库",
3 => "异常",
_ => "未知"
};
}
return "未知";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class IsFlagConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is int isFlag)
{
return isFlag == 1 ? "是" : "否";
}
return "否";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}