Files
AohCopyRou/MainWindow.xaml.cs
2025-12-18 20:21:02 +08:00

284 lines
9.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MessageBox = System.Windows.MessageBox;
namespace CopyRou
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly IFileService _fileService;
private readonly ILogger _logger;
private AppConfig _config;
private bool _isProcessing = false;
public MainWindow()
{
InitializeComponent();
_fileService = new FileService();
_logger = new Logger();
_config = AppConfig.Load();
InitializeUI();
SubscribeToEvents();
}
private void InitializeUI()
{
// 加载配置到UI
SourcePathsTextBox.Text = _config.GetSourcePathsText();
DestPathTextBox.Text = _config.DestPath;
// 设置初始状态
StartButton.IsEnabled = true;
ProgressBar.Value = 0;
}
private void SubscribeToEvents()
{
// 订阅日志事件
_logger.MessageLogged += (sender, message) =>
{
Dispatcher.Invoke(() =>
{
LogTextBox.AppendText(message + Environment.NewLine);
LogTextBox.ScrollToEnd();
});
};
}
private async void StartButton_Click(object sender, RoutedEventArgs e)
{
if (_isProcessing)
return;
var codesText = CodesTextBox.Text;
if (string.IsNullOrWhiteSpace(codesText))
{
System.Windows.MessageBox.Show("请输入要处理的料号", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
var sourcePaths = GetSourcePaths();
if (!sourcePaths.Any())
{
System.Windows.MessageBox.Show("请至少配置一个源路径", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
var destPath = DestPathTextBox.Text;
if (string.IsNullOrWhiteSpace(destPath))
{
System.Windows.MessageBox.Show("请配置目标路径", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
_isProcessing = true;
StartButton.IsEnabled = false;
ProgressBar.Value = 0;
LogTextBox.Clear();
try
{
_logger.Log("=== 开始处理 ===");
_logger.Log($"源路径: {string.Join(", ", sourcePaths)}");
_logger.Log($"目标路径: {destPath}");
var materialNumbers = codesText.Split(new[] { Environment.NewLine, "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries)
.ToList();
var progress = new Progress<int>(value =>
{
ProgressBar.Value = value;
});
await _fileService.ProcessCodes(materialNumbers, sourcePaths, destPath, progress, _logger);
_logger.Log("=== 处理完成 ===");
System.Windows.MessageBox.Show("处理完成!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
_logger.LogError($"处理过程中发生错误: {ex.Message}");
System.Windows.MessageBox.Show($"处理过程中发生错误: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
_isProcessing = false;
StartButton.IsEnabled = true;
}
}
private void ClearButton_Click(object sender, RoutedEventArgs e)
{
CodesTextBox.Clear();
LogTextBox.Clear();
ProgressBar.Value = 0;
}
private void SaveConfigButton_Click(object sender, RoutedEventArgs e)
{
_config.SetSourcePathsFromText(SourcePathsTextBox.Text);
_config.DestPath = DestPathTextBox.Text;
_config.Save();
System.Windows.MessageBox.Show("配置已保存", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
private void AddSourceButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new System.Windows.Forms.FolderBrowserDialog();
dialog.Description = "选择源路径";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var currentPaths = GetSourcePaths();
if (!currentPaths.Contains(dialog.SelectedPath))
{
currentPaths.Add(dialog.SelectedPath);
SourcePathsTextBox.Text = string.Join(Environment.NewLine, currentPaths);
}
}
}
private void BrowseDestButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new System.Windows.Forms.FolderBrowserDialog();
dialog.Description = "选择目标路径";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
DestPathTextBox.Text = dialog.SelectedPath;
}
}
private List<string> GetSourcePaths()
{
var paths = new List<string>();
if (!string.IsNullOrWhiteSpace(SourcePathsTextBox.Text))
{
paths = SourcePathsTextBox.Text.Split(new[] { Environment.NewLine, "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries)
.Select(p => p.Trim())
.Where(p => !string.IsNullOrEmpty(p))
.ToList();
}
return paths;
}
private void SingleCodeTextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter)
{
ProcessSingleCode();
}
}
private async void AddSingleCodeButton_Click(object sender, RoutedEventArgs e)
{
await ProcessSingleCodeAsync();
}
private void ProcessSingleCode()
{
if (_isProcessing)
return;
var code = SingleCodeTextBox.Text.Trim();
if (string.IsNullOrWhiteSpace(code))
{
System.Windows.MessageBox.Show("请输入要处理的料号", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
// 异步处理单个料号
_ = ProcessSingleCodeAsync();
}
private async Task ProcessSingleCodeAsync()
{
if (_isProcessing)
return;
var code = SingleCodeTextBox.Text.Trim();
if (string.IsNullOrWhiteSpace(code))
{
System.Windows.MessageBox.Show("请输入要处理的料号", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
var sourcePaths = GetSourcePaths();
if (!sourcePaths.Any())
{
System.Windows.MessageBox.Show("请至少配置一个源路径", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
var destPath = DestPathTextBox.Text;
if (string.IsNullOrWhiteSpace(destPath))
{
System.Windows.MessageBox.Show("请配置目标路径", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
_isProcessing = true;
AddSingleCodeButton.IsEnabled = false;
SingleCodeTextBox.IsEnabled = false;
try
{
_logger.Log($"=== 开始处理单个料号: {code} ===");
_logger.Log($"源路径: {string.Join(", ", sourcePaths)}");
_logger.Log($"目标路径: {destPath}");
var materialNumbers = new List<string> { code };
// 创建一个简单的进度报告器单个料号处理时直接设置为100%
var progress = new Progress<int>(value =>
{
// 对于单个料号,我们可以直接设置进度条
ProgressBar.Value = value;
});
await _fileService.ProcessCodes(materialNumbers, sourcePaths, destPath, progress, _logger);
_logger.Log("=== 单个料号处理完成 ===");
// 清空输入框
SingleCodeTextBox.Clear();
// 将焦点返回到输入框,方便继续输入
SingleCodeTextBox.Focus();
}
catch (Exception ex)
{
_logger.LogError($"处理单个料号时发生错误: {ex.Message}");
System.Windows.MessageBox.Show($"处理单个料号时发生错误: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
_isProcessing = false;
AddSingleCodeButton.IsEnabled = true;
SingleCodeTextBox.IsEnabled = true;
}
}
protected override void OnClosed(EventArgs e)
{
// 自动保存配置
_config.SetSourcePathsFromText(SourcePathsTextBox.Text);
_config.DestPath = DestPathTextBox.Text;
_config.Save();
base.OnClosed(e);
}
}
}