MainView.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using LocalhostMES.Api.Hosting;
  2. using LocalhostMES.Api.Services;
  3. using LocalhostMES.Core;
  4. using LocalhostMES.DataBase;
  5. using LocalhostMES.Models;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  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.Maximized;
  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. private readonly bool _isEditMode;
  103. public WorkOrderDialog()
  104. : this(null)
  105. {
  106. }
  107. public WorkOrderDialog(WorkOrderInfo source)
  108. {
  109. _isEditMode = source != null;
  110. WorkOrder = source == null
  111. ? new WorkOrderInfo
  112. {
  113. WorkOrderNo = "WO-" + DateTime.Now.ToString("yyyyMMddHHmmss"),
  114. Status = "2",
  115. IsLocalhost = true,
  116. LineCode = "RSC01",
  117. CreateTime = DateTime.Now,
  118. PlannedQuantity = 100,
  119. StartTime = DateTime.Now,
  120. EndTime = DateTime.Now
  121. }
  122. : new WorkOrderInfo
  123. {
  124. WorkOrderNo = source.WorkOrderNo,
  125. OrderNo = source.OrderNo,
  126. MaterialCode = source.MaterialCode,
  127. MaterialName = source.MaterialName,
  128. PlannedQuantity = source.PlannedQuantity,
  129. CompletedQuantity = source.CompletedQuantity,
  130. Status = source.Status,
  131. LineCode = source.LineCode,
  132. CreateTime = source.CreateTime,
  133. StartTime = source.StartTime,
  134. EndTime = source.EndTime,
  135. IsLocalhost = source.IsLocalhost
  136. };
  137. InitializeComponent();
  138. DataContext = WorkOrder;
  139. }
  140. private void InitializeComponent()
  141. {
  142. if (WorkOrder == null)
  143. {
  144. WorkOrder = new WorkOrderInfo();
  145. }
  146. Width = 400;
  147. Height = 450;
  148. WindowStartupLocation = WindowStartupLocation.CenterOwner;
  149. Title = _isEditMode ? "修改工单" : "创建新工单";
  150. var grid = new Grid();
  151. grid.ColumnDefinitions.Add(new ColumnDefinition()); grid.ColumnDefinitions.Add(new ColumnDefinition());
  152. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  153. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  154. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  155. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  156. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  157. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  158. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  159. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  160. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  161. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  162. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  163. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  164. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  165. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  166. grid.RowDefinitions.Add(new RowDefinition());
  167. // 添加控件
  168. int row = 0;
  169. AddLabeledTextBox(grid, "工单号:", nameof(WorkOrder.WorkOrderNo), row++);
  170. AddLabeledTextBox(grid, "物料编码:", nameof(WorkOrder.MaterialCode), row++);
  171. AddLabeledTextBox(grid, "物料名称:", nameof(WorkOrder.MaterialName), row++);
  172. AddLabeledTextBox(grid, "计划数量:", nameof(WorkOrder.PlannedQuantity), row++);
  173. AddLabeledTextBox(grid, "线体编码:", nameof(WorkOrder.LineCode), row++);
  174. // 工单状态选择
  175. var statusLabel = new Label { Content = "工单状态:" };
  176. Grid.SetRow(statusLabel, row);
  177. Grid.SetColumn(statusLabel, 0);
  178. var statusComboBox = new ComboBox
  179. {
  180. ItemsSource = new[]
  181. {
  182. new { Value = "0", Display = "已创建" },
  183. new { Value = "1", Display = "已排产" },
  184. new { Value = "2", Display = "已发布" },
  185. new { Value = "3", Display = "关闭" },
  186. new { Value = "4", Display = "已锁定" },
  187. new { Value = "5", Display = "已开工" },
  188. new { Value = "6", Display = "已完成" },
  189. new { Value = "7", Display = "异常完工" }
  190. },
  191. DisplayMemberPath = "Display",
  192. SelectedValuePath = "Value",
  193. SelectedValue = WorkOrder.Status
  194. };
  195. statusComboBox.SetBinding(ComboBox.SelectedValueProperty, "Status");
  196. Grid.SetRow(statusComboBox, row);
  197. Grid.SetColumn(statusComboBox, 1);
  198. grid.Children.Add(statusLabel);
  199. grid.Children.Add(statusComboBox);
  200. row++;
  201. // 按钮
  202. var buttonPanel = new StackPanel
  203. {
  204. Orientation = Orientation.Horizontal,
  205. HorizontalAlignment = HorizontalAlignment.Right,
  206. Margin = new Thickness(0, 20, 0, 0)
  207. };
  208. var okButton = new Button
  209. {
  210. Content = "确定",
  211. Width = 80,
  212. Margin = new Thickness(5)
  213. };
  214. okButton.Click += (s, e) => { DialogResult = true; Close(); };
  215. var cancelButton = new Button
  216. {
  217. Content = "取消",
  218. Width = 80,
  219. Margin = new Thickness(5)
  220. };
  221. cancelButton.Click += (s, e) => { DialogResult = false; Close(); };
  222. buttonPanel.Children.Add(okButton);
  223. buttonPanel.Children.Add(cancelButton);
  224. Grid.SetRow(buttonPanel, row);
  225. Grid.SetColumnSpan(buttonPanel, 2);
  226. grid.Children.Add(buttonPanel);
  227. Content = grid;
  228. }
  229. private void AddLabeledTextBox(Grid grid, string label, string bindingPath, int row)
  230. {
  231. var labelControl = new Label { Content = label };
  232. Grid.SetRow(labelControl, row);
  233. Grid.SetColumn(labelControl, 0);
  234. var textBox = new TextBox();
  235. textBox.SetBinding(TextBox.TextProperty, bindingPath);
  236. Grid.SetRow(textBox, row);
  237. Grid.SetColumn(textBox, 1);
  238. grid.Children.Add(labelControl);
  239. grid.Children.Add(textBox);
  240. }
  241. }
  242. public partial class SnDialog : Window
  243. {
  244. public SnInfo SnInfo { get; private set; }
  245. public SnDialog(SnInfo source)
  246. {
  247. SnInfo = new SnInfo
  248. {
  249. Sn = source?.Sn,
  250. WorkOrderNo = source?.WorkOrderNo,
  251. PrintType = source?.PrintType,
  252. GenerateTime = source?.GenerateTime ?? DateTime.Now,
  253. IsUsed = source?.IsUsed ?? false
  254. };
  255. InitializeComponent();
  256. DataContext = SnInfo;
  257. }
  258. private void InitializeComponent()
  259. {
  260. Width = 420;
  261. Height = 320;
  262. WindowStartupLocation = WindowStartupLocation.CenterOwner;
  263. Title = "修改SN";
  264. var grid = new Grid();
  265. grid.ColumnDefinitions.Add(new ColumnDefinition());
  266. grid.ColumnDefinitions.Add(new ColumnDefinition());
  267. for (var i = 0; i < 6; i++)
  268. {
  269. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  270. }
  271. grid.RowDefinitions.Add(new RowDefinition());
  272. var row = 0;
  273. AddLabeledTextBox(grid, "SN条码:", nameof(SnInfo.Sn), row++, false);
  274. AddLabeledTextBox(grid, "工单号:", nameof(SnInfo.WorkOrderNo), row++);
  275. AddLabeledTextBox(grid, "类型:", nameof(SnInfo.PrintType), row++);
  276. AddLabeledTextBox(grid, "生成时间:", nameof(SnInfo.GenerateTime), row++);
  277. var usedLabel = new Label { Content = "已使用:" };
  278. Grid.SetRow(usedLabel, row);
  279. Grid.SetColumn(usedLabel, 0);
  280. var usedCheck = new CheckBox { VerticalAlignment = VerticalAlignment.Center };
  281. usedCheck.SetBinding(CheckBox.IsCheckedProperty, nameof(SnInfo.IsUsed));
  282. Grid.SetRow(usedCheck, row);
  283. Grid.SetColumn(usedCheck, 1);
  284. grid.Children.Add(usedLabel);
  285. grid.Children.Add(usedCheck);
  286. row++;
  287. var buttonPanel = new StackPanel
  288. {
  289. Orientation = Orientation.Horizontal,
  290. HorizontalAlignment = HorizontalAlignment.Right,
  291. Margin = new Thickness(0, 16, 0, 0)
  292. };
  293. var okButton = new Button
  294. {
  295. Content = "确定",
  296. Width = 80,
  297. Margin = new Thickness(5)
  298. };
  299. okButton.Click += (s, e) => { DialogResult = true; Close(); };
  300. var cancelButton = new Button
  301. {
  302. Content = "取消",
  303. Width = 80,
  304. Margin = new Thickness(5)
  305. };
  306. cancelButton.Click += (s, e) => { DialogResult = false; Close(); };
  307. buttonPanel.Children.Add(okButton);
  308. buttonPanel.Children.Add(cancelButton);
  309. Grid.SetRow(buttonPanel, row);
  310. Grid.SetColumnSpan(buttonPanel, 2);
  311. grid.Children.Add(buttonPanel);
  312. Content = grid;
  313. }
  314. private static void AddLabeledTextBox(Grid grid, string label, string bindingPath, int row, bool enabled = true)
  315. {
  316. var labelControl = new Label { Content = label };
  317. Grid.SetRow(labelControl, row);
  318. Grid.SetColumn(labelControl, 0);
  319. var textBox = new TextBox { IsEnabled = enabled };
  320. textBox.SetBinding(TextBox.TextProperty, bindingPath);
  321. Grid.SetRow(textBox, row);
  322. Grid.SetColumn(textBox, 1);
  323. grid.Children.Add(labelControl);
  324. grid.Children.Add(textBox);
  325. }
  326. }
  327. }