SelectSampleInfo.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Configuration;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace Metis.AutoAnalysis
  15. {
  16. public partial class SelectSampleInfo : Form
  17. {
  18. public SelectSampleInfo()
  19. {
  20. InitializeComponent();
  21. }
  22. private void SelectSampleInfo_Load(object sender, EventArgs e)
  23. {
  24. lvSampleInfo.Columns.Add("检测项目");
  25. lvSampleInfo.Columns.Add("检验批号");
  26. lvSampleInfo.Columns.Add("试批顺序号");
  27. lvSampleInfo.Columns.Add("试样号");
  28. lvSampleInfo.Columns.Add("收样时间");
  29. lvSampleInfo.Columns.Add("牌号(钢级)");
  30. LoadSampleInfoData("");
  31. for (int i = 0; i < lvSampleInfo.Columns.Count; i++)
  32. {
  33. lvSampleInfo.Columns[i].Width = -2;
  34. }
  35. }
  36. private void LoadSampleInfoData(string condition)
  37. {
  38. string dir = ConfigurationManager.AppSettings["sampleinfo_dir"];
  39. string[] files = GetLatestFiles(dir, 1);
  40. if (files.Length <= 0)
  41. {
  42. MessageBox.Show("指定文件夹下的文件不存在");
  43. return;
  44. }
  45. lvSampleInfo.Items.Clear();
  46. try
  47. {
  48. StreamReader file = File.OpenText(files[0]);
  49. JsonTextReader reader = new JsonTextReader(file);
  50. JArray arr = (JArray)JToken.ReadFrom(reader);
  51. foreach (var json in arr)
  52. {
  53. if(string.IsNullOrEmpty(condition) == false)
  54. {
  55. string sampleId = json["SAMPLEID"].ToString();
  56. if (sampleId.Contains(condition) == false)
  57. continue;
  58. }
  59. ListViewItem item = new ListViewItem();
  60. item.SubItems[0].Text = json["TESTCODE"].ToString();
  61. item.SubItems.Add(json["SAMPLELOTNO"].ToString());
  62. item.SubItems.Add(json["SAMPLEID"].ToString());
  63. item.SubItems.Add(json["FD_SAMPLE_NO"].ToString());
  64. item.SubItems.Add(json["RECVTIME"].ToString());
  65. item.SubItems.Add(json["FD_SG_SIGN"].ToString());
  66. lvSampleInfo.Items.Add(item);
  67. }
  68. reader.Close();
  69. file.Close();
  70. }
  71. catch (Exception ex)
  72. {
  73. MessageBox.Show("读取json文件出错 " + ex.Message);
  74. }
  75. }
  76. public string TESTCODE { get; set; }
  77. public string SAMPLELOTNO { get; set; }
  78. public string SAMPLEID { get; set; }
  79. public string FD_SAMPLE_NO { get; set; }
  80. public string RECVTIME { get; set; }
  81. public string FD_SG_SIGN { get; set; }
  82. private string[] GetLatestFiles(string dir, int count)
  83. {
  84. try
  85. {
  86. var query = (from f in Directory.GetFiles(dir)
  87. let fi = new FileInfo(f)
  88. orderby fi.CreationTime descending
  89. select fi.FullName).Take(count);
  90. return query.ToArray();
  91. }
  92. catch (Exception) { }
  93. return new string[0];
  94. }
  95. private void btnSearch_Click(object sender, EventArgs e)
  96. {
  97. string cond = txtCond.Text;
  98. LoadSampleInfoData(cond);
  99. }
  100. private void btnClose_Click(object sender, EventArgs e)
  101. {
  102. this.DialogResult = DialogResult.Cancel;
  103. this.Close();
  104. }
  105. private void btnOK_Click(object sender, EventArgs e)
  106. {
  107. TESTCODE = lvSampleInfo.SelectedItems[0].SubItems[0].Text;
  108. SAMPLELOTNO = lvSampleInfo.SelectedItems[0].SubItems[1].Text;
  109. SAMPLEID = lvSampleInfo.SelectedItems[0].SubItems[2].Text;
  110. FD_SAMPLE_NO = lvSampleInfo.SelectedItems[0].SubItems[3].Text;
  111. RECVTIME = lvSampleInfo.SelectedItems[0].SubItems[4].Text;
  112. FD_SG_SIGN = lvSampleInfo.SelectedItems[0].SubItems[5].Text;
  113. this.DialogResult = DialogResult.OK;
  114. }
  115. }
  116. }