MainView.xaml.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using LocalhostMES.Controller;
  2. using LocalhostMES.Core;
  3. using LocalhostMES.DataBase;
  4. using LocalhostMES.Models;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Shapes;
  19. using Unity;
  20. namespace LocalhostMES.Views
  21. {
  22. /// <summary>
  23. /// MainView.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class MainView : Window
  26. {
  27. private TrayIconManager trayIconManager;
  28. public MainView()
  29. {
  30. InitializeComponent();
  31. // 设置窗体不可见
  32. this.WindowState = WindowState.Minimized;
  33. this.ShowInTaskbar = false;
  34. this.Visibility = Visibility.Hidden;
  35. InitializeManagers();
  36. }
  37. private void InitializeManagers()
  38. {
  39. try
  40. {
  41. // 初始化托盘图标
  42. trayIconManager = new TrayIconManager();
  43. trayIconManager.OnExit += OnExit;
  44. trayIconManager.OnSettings += OnSettings;
  45. trayIconManager.Show();
  46. }
  47. catch ( Exception ex )
  48. {
  49. LogHelper.WriteLogError("启动托盘失败", ex);
  50. }
  51. }
  52. private void OnSettings(object sender, EventArgs e)
  53. {
  54. this.WindowState = WindowState.Normal;
  55. this.Show();
  56. }
  57. private void OnExit(object sender, EventArgs e)
  58. {
  59. trayIconManager?.Dispose();
  60. this.Close();
  61. }
  62. #region Tab 1: 接口测试
  63. //private async void BtnTestWorkOrder_Click(object sender, RoutedEventArgs e)
  64. //{
  65. // try
  66. // {
  67. // AddLog("=== 测试工单下发接口 ===");
  68. // var request = new WorkOrderRequest
  69. // {
  70. // WorkOrderNo = "TEST-" + DateTime.Now.ToString("yyyyMMddHHmmss"),
  71. // MaterialCode = "T03GCD",
  72. // MaterialName = "T03贯穿灯",
  73. // OrderNo = "ORD-" + DateTime.Now.ToString("yyyyMMdd"),
  74. // SequenceNo = "001",
  75. // WorkOrderNum = "100",
  76. // PlanOnlineTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  77. // PlanOfflineTime = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"),
  78. // Status = "2", // 已发布
  79. // FrozenStatus = "0",
  80. // ReleaseStatus = "1",
  81. // LineCode = "RSC01"
  82. // };
  83. // AddLog("请求参数:");
  84. // AddLog(JsonConvert.SerializeObject(request, Formatting.Indented));
  85. // var response = await _apiClient.SendWorkOrderAsync(request);
  86. // AddLog("响应结果:");
  87. // AddLog(JsonConvert.SerializeObject(response, Formatting.Indented));
  88. // ShowMessage("工单下发测试完成", false);
  89. // }
  90. // catch ( Exception ex )
  91. // {
  92. // AddLog($"错误: {ex.Message}");
  93. // ShowMessage($"测试失败: {ex.Message}", true);
  94. // }
  95. //}
  96. #endregion
  97. }
  98. // 工单对话框
  99. public partial class WorkOrderDialog : Window
  100. {
  101. public WorkOrderInfo WorkOrder { get; private set; }
  102. public WorkOrderDialog()
  103. {
  104. InitializeComponent();
  105. WorkOrder = new WorkOrderInfo
  106. {
  107. WorkOrderNo = "WO-" + DateTime.Now.ToString("yyyyMMddHHmmss"),
  108. Status = "2",
  109. LineCode = "RSC01",
  110. CreateTime = DateTime.Now,
  111. PlannedQuantity = 100,
  112. StartTime=DateTime.Now,
  113. EndTime=DateTime.Now
  114. };
  115. DataContext = WorkOrder;
  116. }
  117. private void InitializeComponent()
  118. {
  119. if (WorkOrder == null)
  120. {
  121. WorkOrder = new WorkOrderInfo();
  122. }
  123. Width = 400;
  124. Height = 450;
  125. WindowStartupLocation = WindowStartupLocation.CenterOwner;
  126. Title = "创建新工单";
  127. var grid = new Grid();
  128. grid.ColumnDefinitions.Add(new ColumnDefinition()); grid.ColumnDefinitions.Add(new ColumnDefinition());
  129. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  130. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  131. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  132. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  133. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  134. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  135. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  136. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  137. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  138. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  139. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  140. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  141. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  142. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  143. grid.RowDefinitions.Add(new RowDefinition());
  144. // 添加控件
  145. int row = 0;
  146. AddLabeledTextBox(grid, "工单号:", nameof(WorkOrder.WorkOrderNo), row++);
  147. AddLabeledTextBox(grid, "物料编码:", nameof(WorkOrder.MaterialCode), row++);
  148. AddLabeledTextBox(grid, "物料名称:", nameof(WorkOrder.MaterialName), row++);
  149. AddLabeledTextBox(grid, "计划数量:", nameof(WorkOrder.PlannedQuantity), row++);
  150. AddLabeledTextBox(grid, "线体编码:", nameof(WorkOrder.LineCode), row++);
  151. // 工单状态选择
  152. var statusLabel = new Label { Content = "工单状态:" };
  153. Grid.SetRow(statusLabel, row);
  154. Grid.SetColumn(statusLabel, 0);
  155. var statusComboBox = new ComboBox
  156. {
  157. ItemsSource = new[]
  158. {
  159. new { Value = "0", Display = "已创建" },
  160. new { Value = "1", Display = "已排产" },
  161. new { Value = "2", Display = "已发布" },
  162. new { Value = "3", Display = "关闭" },
  163. new { Value = "4", Display = "已锁定" },
  164. new { Value = "5", Display = "已开工" },
  165. new { Value = "6", Display = "已完成" },
  166. new { Value = "7", Display = "异常完工" }
  167. },
  168. DisplayMemberPath = "Display",
  169. SelectedValuePath = "Value",
  170. SelectedValue = WorkOrder.Status
  171. };
  172. statusComboBox.SetBinding(ComboBox.SelectedValueProperty, "Status");
  173. Grid.SetRow(statusComboBox, row);
  174. Grid.SetColumn(statusComboBox, 1);
  175. grid.Children.Add(statusLabel);
  176. grid.Children.Add(statusComboBox);
  177. row++;
  178. // 按钮
  179. var buttonPanel = new StackPanel
  180. {
  181. Orientation = Orientation.Horizontal,
  182. HorizontalAlignment = HorizontalAlignment.Right,
  183. Margin = new Thickness(0, 20, 0, 0)
  184. };
  185. var okButton = new Button
  186. {
  187. Content = "确定",
  188. Width = 80,
  189. Margin = new Thickness(5)
  190. };
  191. okButton.Click += (s, e) => { DatabaseHelper.InsertWorkOrderInfo(WorkOrder); DialogResult = true; Close(); };
  192. var cancelButton = new Button
  193. {
  194. Content = "取消",
  195. Width = 80,
  196. Margin = new Thickness(5)
  197. };
  198. cancelButton.Click += (s, e) => { DialogResult = false; Close(); };
  199. buttonPanel.Children.Add(okButton);
  200. buttonPanel.Children.Add(cancelButton);
  201. Grid.SetRow(buttonPanel, row);
  202. Grid.SetColumnSpan(buttonPanel, 2);
  203. grid.Children.Add(buttonPanel);
  204. Content = grid;
  205. }
  206. private void AddLabeledTextBox(Grid grid, string label, string bindingPath, int row)
  207. {
  208. var labelControl = new Label { Content = label };
  209. Grid.SetRow(labelControl, row);
  210. Grid.SetColumn(labelControl, 0);
  211. var textBox = new TextBox();
  212. textBox.SetBinding(TextBox.TextProperty, bindingPath);
  213. Grid.SetRow(textBox, row);
  214. Grid.SetColumn(textBox, 1);
  215. grid.Children.Add(labelControl);
  216. grid.Children.Add(textBox);
  217. }
  218. }
  219. // 结果颜色转换器
  220. public class ResultColorConverter : System.Windows.Data.IValueConverter
  221. {
  222. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  223. {
  224. if ( value is string result )
  225. {
  226. return result == "OK" ? Brushes.Green : Brushes.Red;
  227. }
  228. return Brushes.Black;
  229. }
  230. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  231. {
  232. throw new NotImplementedException();
  233. }
  234. }
  235. }