using LocalhostMES.Api.Hosting; using LocalhostMES.Api.Services; using LocalhostMES.Core; using LocalhostMES.DataBase; using LocalhostMES.Models; using Newtonsoft.Json; 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.Imaging; using System.Windows.Shapes; using Unity; namespace LocalhostMES.Views { /// /// MainView.xaml 的交互逻辑 /// public partial class MainView : Window { private TrayIconManager trayIconManager; public MainView() { InitializeComponent(); // 设置窗体不可见 this.WindowState = WindowState.Minimized; this.ShowInTaskbar = false; this.Visibility = Visibility.Hidden; InitializeManagers(); } private void InitializeManagers() { try { // 初始化托盘图标 trayIconManager = new TrayIconManager(); trayIconManager.OnExit += OnExit; trayIconManager.OnSettings += OnSettings; trayIconManager.Show(); } catch ( Exception ex ) { LogHelper.WriteLogError("启动托盘失败", ex); } } private void OnSettings(object sender, EventArgs e) { this.WindowState = WindowState.Maximized; this.Show(); } private void OnExit(object sender, EventArgs e) { trayIconManager?.Dispose(); this.Close(); } #region Tab 1: 接口测试 //private async void BtnTestWorkOrder_Click(object sender, RoutedEventArgs e) //{ // try // { // AddLog("=== 测试工单下发接口 ==="); // var request = new WorkOrderRequest // { // WorkOrderNo = "TEST-" + DateTime.Now.ToString("yyyyMMddHHmmss"), // MaterialCode = "T03GCD", // MaterialName = "T03贯穿灯", // OrderNo = "ORD-" + DateTime.Now.ToString("yyyyMMdd"), // SequenceNo = "001", // WorkOrderNum = "100", // PlanOnlineTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), // PlanOfflineTime = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"), // Status = "2", // 已发布 // FrozenStatus = "0", // ReleaseStatus = "1", // LineCode = "RSC01" // }; // AddLog("请求参数:"); // AddLog(JsonConvert.SerializeObject(request, Formatting.Indented)); // var response = await _apiClient.SendWorkOrderAsync(request); // AddLog("响应结果:"); // AddLog(JsonConvert.SerializeObject(response, Formatting.Indented)); // ShowMessage("工单下发测试完成", false); // } // catch ( Exception ex ) // { // AddLog($"错误: {ex.Message}"); // ShowMessage($"测试失败: {ex.Message}", true); // } //} #endregion } // 工单对话框 public partial class WorkOrderDialog : Window { public WorkOrderInfo WorkOrder { get; private set; } private readonly bool _isEditMode; public WorkOrderDialog() : this(null) { } public WorkOrderDialog(WorkOrderInfo source) { _isEditMode = source != null; WorkOrder = source == null ? new WorkOrderInfo { WorkOrderNo = "WO-" + DateTime.Now.ToString("yyyyMMddHHmmss"), Status = "2", IsLocalhost = true, LineCode = "RSC01", CreateTime = DateTime.Now, PlannedQuantity = 100, StartTime = DateTime.Now, EndTime = DateTime.Now } : new WorkOrderInfo { WorkOrderNo = source.WorkOrderNo, OrderNo = source.OrderNo, MaterialCode = source.MaterialCode, MaterialName = source.MaterialName, PlannedQuantity = source.PlannedQuantity, CompletedQuantity = source.CompletedQuantity, Status = source.Status, LineCode = source.LineCode, CreateTime = source.CreateTime, StartTime = source.StartTime, EndTime = source.EndTime, IsLocalhost = source.IsLocalhost }; InitializeComponent(); DataContext = WorkOrder; } private void InitializeComponent() { if (WorkOrder == null) { WorkOrder = new WorkOrderInfo(); } Width = 400; Height = 450; WindowStartupLocation = WindowStartupLocation.CenterOwner; Title = _isEditMode ? "修改工单" : "创建新工单"; var grid = new Grid(); grid.ColumnDefinitions.Add(new ColumnDefinition()); grid.ColumnDefinitions.Add(new ColumnDefinition()); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition()); // 添加控件 int row = 0; AddLabeledTextBox(grid, "工单号:", nameof(WorkOrder.WorkOrderNo), row++); AddLabeledTextBox(grid, "物料编码:", nameof(WorkOrder.MaterialCode), row++); AddLabeledTextBox(grid, "物料名称:", nameof(WorkOrder.MaterialName), row++); AddLabeledTextBox(grid, "计划数量:", nameof(WorkOrder.PlannedQuantity), row++); AddLabeledTextBox(grid, "线体编码:", nameof(WorkOrder.LineCode), row++); // 工单状态选择 var statusLabel = new Label { Content = "工单状态:" }; Grid.SetRow(statusLabel, row); Grid.SetColumn(statusLabel, 0); var statusComboBox = new ComboBox { ItemsSource = new[] { new { Value = "0", Display = "已创建" }, new { Value = "1", Display = "已排产" }, new { Value = "2", Display = "已发布" }, new { Value = "3", Display = "关闭" }, new { Value = "4", Display = "已锁定" }, new { Value = "5", Display = "已开工" }, new { Value = "6", Display = "已完成" }, new { Value = "7", Display = "异常完工" } }, DisplayMemberPath = "Display", SelectedValuePath = "Value", SelectedValue = WorkOrder.Status }; statusComboBox.SetBinding(ComboBox.SelectedValueProperty, "Status"); Grid.SetRow(statusComboBox, row); Grid.SetColumn(statusComboBox, 1); grid.Children.Add(statusLabel); grid.Children.Add(statusComboBox); row++; // 按钮 var buttonPanel = new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right, Margin = new Thickness(0, 20, 0, 0) }; var okButton = new Button { Content = "确定", Width = 80, Margin = new Thickness(5) }; okButton.Click += (s, e) => { DialogResult = true; Close(); }; var cancelButton = new Button { Content = "取消", Width = 80, Margin = new Thickness(5) }; cancelButton.Click += (s, e) => { DialogResult = false; Close(); }; buttonPanel.Children.Add(okButton); buttonPanel.Children.Add(cancelButton); Grid.SetRow(buttonPanel, row); Grid.SetColumnSpan(buttonPanel, 2); grid.Children.Add(buttonPanel); Content = grid; } private void AddLabeledTextBox(Grid grid, string label, string bindingPath, int row) { var labelControl = new Label { Content = label }; Grid.SetRow(labelControl, row); Grid.SetColumn(labelControl, 0); var textBox = new TextBox(); textBox.SetBinding(TextBox.TextProperty, bindingPath); Grid.SetRow(textBox, row); Grid.SetColumn(textBox, 1); grid.Children.Add(labelControl); grid.Children.Add(textBox); } } public partial class SnDialog : Window { public SnInfo SnInfo { get; private set; } public SnDialog(SnInfo source) { SnInfo = new SnInfo { Sn = source?.Sn, WorkOrderNo = source?.WorkOrderNo, PrintType = source?.PrintType, GenerateTime = source?.GenerateTime ?? DateTime.Now, IsUsed = source?.IsUsed ?? false }; InitializeComponent(); DataContext = SnInfo; } private void InitializeComponent() { Width = 420; Height = 320; WindowStartupLocation = WindowStartupLocation.CenterOwner; Title = "修改SN"; var grid = new Grid(); grid.ColumnDefinitions.Add(new ColumnDefinition()); grid.ColumnDefinitions.Add(new ColumnDefinition()); for (var i = 0; i < 6; i++) { grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); } grid.RowDefinitions.Add(new RowDefinition()); var row = 0; AddLabeledTextBox(grid, "SN条码:", nameof(SnInfo.Sn), row++, false); AddLabeledTextBox(grid, "工单号:", nameof(SnInfo.WorkOrderNo), row++); AddLabeledTextBox(grid, "类型:", nameof(SnInfo.PrintType), row++); AddLabeledTextBox(grid, "生成时间:", nameof(SnInfo.GenerateTime), row++); var usedLabel = new Label { Content = "已使用:" }; Grid.SetRow(usedLabel, row); Grid.SetColumn(usedLabel, 0); var usedCheck = new CheckBox { VerticalAlignment = VerticalAlignment.Center }; usedCheck.SetBinding(CheckBox.IsCheckedProperty, nameof(SnInfo.IsUsed)); Grid.SetRow(usedCheck, row); Grid.SetColumn(usedCheck, 1); grid.Children.Add(usedLabel); grid.Children.Add(usedCheck); row++; var buttonPanel = new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right, Margin = new Thickness(0, 16, 0, 0) }; var okButton = new Button { Content = "确定", Width = 80, Margin = new Thickness(5) }; okButton.Click += (s, e) => { DialogResult = true; Close(); }; var cancelButton = new Button { Content = "取消", Width = 80, Margin = new Thickness(5) }; cancelButton.Click += (s, e) => { DialogResult = false; Close(); }; buttonPanel.Children.Add(okButton); buttonPanel.Children.Add(cancelButton); Grid.SetRow(buttonPanel, row); Grid.SetColumnSpan(buttonPanel, 2); grid.Children.Add(buttonPanel); Content = grid; } private static void AddLabeledTextBox(Grid grid, string label, string bindingPath, int row, bool enabled = true) { var labelControl = new Label { Content = label }; Grid.SetRow(labelControl, row); Grid.SetColumn(labelControl, 0); var textBox = new TextBox { IsEnabled = enabled }; textBox.SetBinding(TextBox.TextProperty, bindingPath); Grid.SetRow(textBox, row); Grid.SetColumn(textBox, 1); grid.Children.Add(labelControl); grid.Children.Add(textBox); } } }