BatchListForm.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Model;
  2. using PlcUiForm;
  3. using Sunny.UI;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace YangjieTester.机台日志表现
  14. {
  15. [FormDescriptionAttribute("批次报告画面")]
  16. public partial class BatchListForm : PlcBaseForm
  17. {
  18. public BatchListForm()
  19. {
  20. InitializeComponent();
  21. }
  22. private void BatchListForm_Load(object sender, EventArgs e)
  23. {
  24. ShowBatchList();
  25. }
  26. void ShowBatchList()
  27. {
  28. var batches = BatchService.GetFinishedBatches(DateTime.MinValue, DateTime.Now, null, null);
  29. listBox1.DisplayMember = "BatchID"; // 显示 BatchID
  30. //listBox1.ValueMember = "BatchID"; // (可选)
  31. listBox1.DataSource = batches; // 直接绑定 List<Batch>
  32. }
  33. private void txt批次查询输入_TextChanged(object sender, EventArgs e)
  34. {
  35. var batches = BatchService.GetFinishedBatches(DateTime.MinValue, DateTime.Now, txt批次查询输入.Text, null);
  36. listBox1.DisplayMember = "BatchID"; // 显示 BatchID
  37. //listBox1.ValueMember = "BatchID"; // (可选)
  38. listBox1.DataSource = batches; // 直接绑定 List<Batch>
  39. }
  40. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  41. {
  42. if (listBox1.SelectedItem is Batch selectedBatch)
  43. {
  44. txtBatchReport.Text = BatchReport.GenerateBatchReport(selectedBatch);
  45. }
  46. }
  47. private void btn删除选中批次_Click(object sender, EventArgs e)
  48. {
  49. if (listBox1.SelectedItem is Batch selectedBatch)
  50. {
  51. var dr=MessageBox.Show($"确认删除批次{selectedBatch.BatchID}?此操作不可恢复!","确认删除",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
  52. if(dr!= DialogResult.Yes)
  53. {
  54. return;
  55. }
  56. if (!BatchService.DeleteBatchByBatchId(selectedBatch.BatchID,out string err))
  57. {
  58. MessageBox.Show($"删除批次{selectedBatch.BatchID}失败:{err}");
  59. return;
  60. }
  61. ShowBatchList();
  62. }
  63. }
  64. private void btn导出选中_Click(object sender, EventArgs e)
  65. {
  66. if (listBox1.SelectedItem is Batch selectedBatch)
  67. {
  68. using (var saveFileDialog = new SaveFileDialog())
  69. {
  70. saveFileDialog.Filter = "txt 文件 (*.txt)|*.txt";
  71. saveFileDialog.FileName = $"{selectedBatch.BatchID}.txt";
  72. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  73. {
  74. try
  75. {
  76. ExportData.ExportDataToTxt(BatchReport.GenerateBatchReport(selectedBatch), saveFileDialog.FileName);
  77. MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  78. }
  79. catch (Exception ex)
  80. {
  81. MessageBox.Show($"导出失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }