Sample_Information_Enter.xaml.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.SQLite;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Forms;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Shapes;
  19. namespace AIRS
  20. {
  21. /// <summary>
  22. /// Sample_Information_Enter.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class Sample_Information_Enter : Window
  25. {
  26. public Sample_Information_Enter()
  27. {
  28. InitializeComponent();
  29. }
  30. public delegate void MB();
  31. /// <summary>
  32. /// 移除蒙版
  33. /// </summary>
  34. public static event MB MBEvent;
  35. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  36. private string path = Directory.GetCurrentDirectory();//工作路径
  37. //数据库
  38. SQLiteConnection conn = new SQLiteConnection();
  39. SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder();
  40. SQLiteDataAdapter da = new SQLiteDataAdapter();
  41. private int count = 10;//设定count几个就删除多余的以外的。假设设置为5那么大于5条的都会被删除
  42. List<string> list_Image_Names = new List<string>();//图片路径名称列表(绝对路径)
  43. public string lbl_Inspection_project;//检验项目
  44. public string comb_Number_prefix;//编号前缀
  45. public string comb_Number_suffix;//编号后缀
  46. public string comb_Inspection_instrument;//检验仪器
  47. public string txt_Picture_catalog;//图片目录 //E:/images/img1.png....
  48. public string grade_steel;//钢种
  49. public string grade_standard;//标准
  50. public string test_item;//检验项目
  51. public void BLFZ()
  52. {
  53. if(input_Number_prefix.Text!=""&&input_Inspection_project.Content.ToString()!=""&&input_Inspection_instrument.Text != "" && input_Picture_catalog.Text !="")
  54. {
  55. lbl_Inspection_project = input_Inspection_project.Content.ToString();
  56. // comb_Number_prefix = input_Number_prefix.Text;
  57. comb_Number_prefix = input_Number_prefix.Text;
  58. comb_Number_suffix = input_Number_suffix.Text;
  59. comb_Inspection_instrument = input_Inspection_instrument.Text;
  60. txt_Picture_catalog = Common.removeControlChars(input_Picture_catalog.Text);//lbf 2020/10/28 去除控制字符
  61. //编号前缀
  62. BHQZ();
  63. //编号后缀
  64. BHHZ();
  65. //检验仪器
  66. JYYQ();
  67. MBEvent();
  68. this.DialogResult = true;//或者等于fa
  69. }
  70. else
  71. {
  72. System.Windows.MessageBox.Show("请完善信息后点击确定!","警告",MessageBoxButton.OK,MessageBoxImage.Warning);
  73. }
  74. }
  75. /// <summary>
  76. /// 取消
  77. /// </summary>
  78. /// <param name="sender"></param>
  79. /// <param name="e"></param>
  80. private void btnCanel_Click(object sender, RoutedEventArgs e)
  81. {
  82. MBEvent();
  83. Clear_SampleInfo();
  84. this.DialogResult = false;
  85. // this.Close();
  86. }
  87. /// <summary>
  88. /// 确定
  89. /// </summary>
  90. /// <param name="sender"></param>
  91. /// <param name="e"></param>
  92. private void btnOK_Click(object sender, RoutedEventArgs e)
  93. {
  94. BLFZ();
  95. }
  96. /// <summary>
  97. /// 编号前缀 编辑删除增加
  98. /// </summary>
  99. public void BHQZ()
  100. {
  101. if (input_Number_prefix.Text != "")
  102. {
  103. //input_Inspection_instrument.Text = SelectDataWhereValue(lblnumbers.Content.ToString(), input_Number_suffix.Text).ToString();
  104. int id = SelectDataWhereValue(lblnumberp.Content.ToString(), input_Number_prefix.Text);
  105. //Sample_Information_Enter.MBEvent();
  106. if (id > -1)
  107. {
  108. FuncUpdateData(id);
  109. //System.Windows.MessageBox.Show("做更新");
  110. }
  111. else
  112. {
  113. FuncInsertData(lblnumberp.Content.ToString(), input_Number_prefix.Text);
  114. //System.Windows.MessageBox.Show("做插入");
  115. }
  116. //table = getDataTable(lblnumbers.Content.ToString());
  117. //deleteDataTable(table);
  118. deleteDataTable(getDataTable(lblnumberp.Content.ToString()));
  119. //input_Number_prefix.ItemsSource = DataTableSelectData(lblnumberp.Content.ToString());
  120. input_Number_prefix.ItemsSource = DataTableSelectData(lblnumberp.Content.ToString());
  121. }
  122. //else
  123. //{
  124. // //input_Number_prefix.ItemsSource = DataTableSelectData(lblnumberp.Content.ToString());
  125. // // System.Windows.MessageBox.Show("请输入" + lblnumberp.Content.ToString().Substring(0, lblnumberp.Content.ToString().Length - 1));
  126. //}
  127. }
  128. /// <summary>
  129. /// 编号后缀 编辑删除增加
  130. /// </summary>
  131. public void BHHZ()
  132. {
  133. if (input_Number_suffix.Text != "")
  134. {
  135. //input_Inspection_instrument.Text = SelectDataWhereValue(lblnumbers.Content.ToString(), input_Number_suffix.Text).ToString();
  136. int id = SelectDataWhereValue(lblnumbers.Content.ToString(), input_Number_suffix.Text);
  137. //Sample_Information_Enter.MBEvent();
  138. if (id > -1)
  139. {
  140. FuncUpdateData(id);
  141. //System.Windows.MessageBox.Show("做更新");
  142. }
  143. else
  144. {
  145. FuncInsertData(lblnumbers.Content.ToString(), input_Number_suffix.Text);
  146. //System.Windows.MessageBox.Show("做插入");
  147. }
  148. //table = getDataTable(lblnumbers.Content.ToString());
  149. //deleteDataTable(table);
  150. deleteDataTable(getDataTable(lblnumbers.Content.ToString()));
  151. input_Number_suffix.ItemsSource = DataTableSelectData(lblnumbers.Content.ToString());
  152. }
  153. //else
  154. //{
  155. // input_Number_suffix.ItemsSource = DataTableSelectData(lblnumbers.Content.ToString());
  156. // System.Windows.MessageBox.Show("请输入" + lblnumbers.Content.ToString().Substring(0, lblnumbers.Content.ToString().Length - 1));
  157. //}
  158. }
  159. /// <summary>
  160. /// 检验仪器 编辑删除增加
  161. /// </summary>
  162. public void JYYQ()
  163. {
  164. if (input_Inspection_instrument.Text != "")
  165. {
  166. //input_Inspection_instrument.Text = SelectDataWhereValue(lblnumbers.Content.ToString(), input_Number_suffix.Text).ToString();
  167. int id = SelectDataWhereValue(lblInspection_instrument.Content.ToString(), input_Inspection_instrument.Text);
  168. //Sample_Information_Enter.MBEvent();
  169. if (id > -1)
  170. {
  171. FuncUpdateData(id);
  172. //System.Windows.MessageBox.Show("做更新");
  173. }
  174. else
  175. {
  176. FuncInsertData(lblInspection_instrument.Content.ToString(), input_Inspection_instrument.Text);
  177. //System.Windows.MessageBox.Show("做插入");
  178. }
  179. //table = getDataTable(lblnumbers.Content.ToString());
  180. //deleteDataTable(table);
  181. deleteDataTable(getDataTable(lblInspection_instrument.Content.ToString()));
  182. input_Inspection_instrument.ItemsSource = DataTableSelectData(lblInspection_instrument.Content.ToString());
  183. }
  184. else
  185. {
  186. input_Inspection_instrument.ItemsSource = DataTableSelectData(lblInspection_instrument.Content.ToString());
  187. System.Windows.MessageBox.Show("请输入" + lblInspection_instrument.Content.ToString().Substring(0, lblInspection_instrument.Content.ToString().Length - 1));
  188. }
  189. }
  190. /// <summary>
  191. /// 查询数据
  192. /// </summary>
  193. /// <param name="key"></param>
  194. /// <param name="value"></param>
  195. public void SelectDataCompare(string key, string value)
  196. {
  197. //connstr.DataSource = path + "\\datas.db";
  198. //conn.ConnectionString = connstr.ToString();
  199. try
  200. {
  201. conn.Open();
  202. SQLiteCommand cmd = new SQLiteCommand(conn);
  203. cmd.CommandText = "SELECT * FROM t_Parameter WHERE key='" + key + "'";
  204. SQLiteDataReader sr = cmd.ExecuteReader();
  205. while (sr.Read())
  206. {
  207. // Console.WriteLine($"{sr.GetString(1)},{sr.GetDateTime(5).ToString()}");
  208. // Console.WriteLine($"{sr.GetInt32(0)},{sr.GetString(1)},{sr.GetString(2)},{sr.GetString(3)}");
  209. }
  210. conn.Close();
  211. }
  212. catch (Exception e)
  213. {
  214. System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  215. }
  216. }
  217. DataTable table = new DataTable();
  218. /// <summary>
  219. /// combox绑定数据
  220. /// </summary>
  221. public List<string> DataTableSelectData(string key)
  222. {
  223. List<string> comboxlist = new List<string>();
  224. try
  225. {
  226. conn.Open();
  227. //查询钢种
  228. da = new SQLiteDataAdapter("SELECT * FROM t_Parameter WHERE key='" + key + "'", conn);
  229. DataTable ds = new DataTable();
  230. da.Fill(ds);
  231. DataView dataView = ds.DefaultView;
  232. // dataView.Sort = "time asc";//正序从小到大
  233. dataView.Sort = "time desc";//逆序从大到小
  234. ds = dataView.ToTable();
  235. // deleteDataTable(ds);
  236. for (int i = 0; i < ds.Rows.Count; i++)
  237. {
  238. comboxlist.Add(ds.Rows[i][2].ToString());
  239. // Console.WriteLine("看看" + i + " " + ds.Rows[i][0] + " " + ds.Rows[i][1] + " " + ds.Rows[i][2] + " " + ds.Rows[i][3]);
  240. }
  241. conn.Close();
  242. table = ds;
  243. }
  244. catch (Exception e)
  245. {
  246. System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  247. }
  248. return comboxlist;
  249. }
  250. /// <summary>
  251. /// 根据key(编号前缀/编号后缀/检验仪器)条件返回的DataTable
  252. /// </summary>
  253. /// <param name="key"></param>
  254. /// <returns></returns>
  255. public DataTable getDataTable(string key)
  256. {
  257. DataTable ds = new DataTable();
  258. try
  259. {
  260. conn.Open();
  261. //查询钢种
  262. da = new SQLiteDataAdapter("SELECT * FROM t_Parameter WHERE key='" + key + "'", conn);
  263. da.Fill(ds);
  264. conn.Close();
  265. DataView dataView = ds.DefaultView;
  266. // dataView.Sort = "time asc";//正序从小到大
  267. dataView.Sort = "time desc";//逆序从大到小
  268. ds = dataView.ToTable();
  269. }
  270. catch (Exception e)
  271. {
  272. System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  273. }
  274. return ds;
  275. }
  276. /// <summary>
  277. /// 查询数据返回编号
  278. /// </summary>
  279. public int SelectDataWhereValue(string key, string value)
  280. {
  281. int id = -1;
  282. try
  283. {
  284. conn.Open();
  285. da = new SQLiteDataAdapter("SELECT * FROM t_Parameter WHERE key='" + key + "'and value='" + value + "'", conn);
  286. DataTable ds = new DataTable();
  287. da.Fill(ds);
  288. for (int i = 0; i < ds.Rows.Count; i++)
  289. {
  290. id = Convert.ToInt32(ds.Rows[i][0]);
  291. }
  292. conn.Close();
  293. }
  294. catch (Exception e)
  295. {
  296. System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  297. }
  298. return id;
  299. }
  300. /// <summary>
  301. /// 执行SQL语句,返回影响的记录数
  302. /// </summary>
  303. /// <param name="SQLString">SQL语句</param>
  304. /// <returns>影响的记录数</returns>
  305. public int ExecuteSql(string SQLString)
  306. {
  307. int rows=-1;
  308. try
  309. {
  310. using (SQLiteCommand cmd = new SQLiteCommand(SQLString, conn))
  311. {
  312. try
  313. {
  314. conn.Open();
  315. rows = cmd.ExecuteNonQuery();
  316. //conn.Dispose();
  317. conn.Close();
  318. return rows;
  319. }
  320. catch (System.Data.SQLite.SQLiteException E)
  321. {
  322. // conn.Close();
  323. throw new Exception(E.Message);
  324. }
  325. }
  326. }
  327. catch (Exception e)
  328. {
  329. System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  330. }
  331. return rows;
  332. }
  333. /// <summary>
  334. /// 添加数据
  335. /// </summary>
  336. /// <param name="key"></param>
  337. /// <param name="value"></param>
  338. public void FuncInsertData(string key, string value)
  339. {
  340. //Random random = new Random();
  341. //int id = random.Next();
  342. try
  343. {
  344. if (value != "")
  345. {
  346. //string sqlstr = "INSERT INTO t_Parameter (key,value,time) VALUES ('" + key + "','" + value + "','" + DateTime.Now.ToString() + "')";
  347. string sqlstr = "INSERT INTO t_Parameter (key,value,time) VALUES ('" + key + "','" + value + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')";
  348. int i = ExecuteSql(sqlstr);
  349. if (i > 0)
  350. {
  351. //System.Windows.MessageBox.Show("添加了一条数据 " + key + " " + value);
  352. }
  353. }
  354. }
  355. catch (Exception e)
  356. {
  357. System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  358. }
  359. }
  360. /// <summary>
  361. /// 更新数据
  362. /// </summary>
  363. /// <param name="id"></param>
  364. public void FuncUpdateData(int id)
  365. {
  366. try
  367. {
  368. // string sqlstr = "update t_Parameter set time='" + DateTime.Now.ToString() + "' where id='" + id + "'";
  369. string sqlstr = "update t_Parameter set time='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where id='" + id + "'";
  370. int i = ExecuteSql(sqlstr);
  371. if (i > 0)
  372. {
  373. //System.Windows.MessageBox.Show("更新了 " + id + " 的时间");
  374. }
  375. }
  376. catch (Exception e)
  377. {
  378. System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  379. }
  380. }
  381. /// <summary>
  382. /// 删除数据当大于10条数据就删除
  383. /// </summary>
  384. /// <param name="id"></param>
  385. public void FuncDeleteData(int id)
  386. {
  387. try
  388. {
  389. string sqlstr = "delete from t_Parameter where id='" + id + "'";
  390. int i = ExecuteSql(sqlstr);
  391. if (i > 0)
  392. {
  393. // System.Windows.MessageBox.Show("删除了 " + id);
  394. }
  395. }
  396. catch (Exception e)
  397. {
  398. System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  399. }
  400. }
  401. /// <summary>
  402. ///
  403. /// 删除多出10条以上的所有
  404. /// </summary>
  405. /// <param name="dt"></param>
  406. public void deleteDataTable(DataTable dt)
  407. {
  408. try
  409. { //设定count几个就删除多余的以外的。假设设置为5那么大于5条的都会被删除,显示最多也是五个
  410. if (dt.Rows.Count > count)
  411. {
  412. DataView dataView = dt.DefaultView;
  413. // dataView.Sort = "time asc";//正序从小到大
  414. dataView.Sort = "time desc";//逆序从大到小
  415. dt = dataView.ToTable();
  416. for (int i = 0; i < dt.Rows.Count; i++)
  417. {
  418. // i = i + count;
  419. if (i >= count)
  420. {
  421. FuncDeleteData(Convert.ToInt32(dt.Rows[i][0]));
  422. }
  423. }
  424. }
  425. }
  426. catch (Exception e)
  427. {
  428. System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  429. }
  430. }
  431. /// <summary>
  432. /// 清空输入信息
  433. /// </summary>
  434. private void Clear_SampleInfo()
  435. {
  436. input_Inspection_project.Content = "";
  437. input_Number_prefix.Text = "";
  438. input_Number_suffix.Text = "";
  439. input_Inspection_instrument.Text = "";
  440. input_Picture_catalog.Text = "";
  441. }
  442. /// <summary>
  443. /// 打开文件
  444. /// </summary>
  445. /// <param name="sender"></param>
  446. /// <param name="e"></param>
  447. private void openfiles()
  448. {
  449. try
  450. {
  451. List<string> piclist = new List<string>();
  452. System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
  453. dialog.SelectedPath = txt_Picture_catalog;
  454. if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
  455. {
  456. txt_Picture_catalog = dialog.SelectedPath;
  457. WriteApp("folderpath", txt_Picture_catalog);
  458. DirectoryInfo directoryInfo = new DirectoryInfo(txt_Picture_catalog);
  459. string jianyangname = directoryInfo.Name;
  460. input_Number_prefix.Text = jianyangname;
  461. DirectoryInfo TheFolder = new DirectoryInfo(txt_Picture_catalog);
  462. FileSystemInfo fileinfo1 = TheFolder as FileSystemInfo;
  463. input_Picture_catalog.Text = fileinfo1.ToString();//图片目录文件夹名
  464. //遍历文件
  465. foreach (FileInfo NextFile in TheFolder.GetFiles())
  466. {
  467. if (System.IO.Path.GetExtension(NextFile.FullName).ToLower() == ".jpg" || System.IO.Path.GetExtension(NextFile.FullName).ToLower() == ".bmp")
  468. {
  469. //Console.WriteLine("111" + NextFile.Name);//图片名
  470. //将文件夹下的图像文件添加到文件列表中
  471. piclist.Add(NextFile.FullName);
  472. // Console.WriteLine("000"+NextFile.FullName);//带路径的图片名
  473. }
  474. }
  475. if (piclist.Count == 0)
  476. {
  477. input_Picture_catalog.Text ="";//图片目录文件夹名
  478. System.Windows.MessageBox.Show("您选择了没有图片的文件夹哦!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
  479. return;
  480. }
  481. }
  482. list_Image_Names = piclist;
  483. //if (piclist.Count == 0)
  484. //{
  485. // System.Windows.MessageBox.Show("您选择了没有图片的文件夹哦!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
  486. //}
  487. }
  488. catch (Exception e)
  489. {
  490. input_Picture_catalog.Text = "";
  491. System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  492. }
  493. }
  494. /// <summary>
  495. /// 浏览文件夹
  496. /// </summary>
  497. /// <param name="sender"></param>
  498. /// <param name="e"></param>
  499. private void btnBrowseFolders_Click(object sender, RoutedEventArgs e)
  500. {
  501. openfiles();
  502. }
  503. /// <summary>
  504. /// 试样信息绑定数据源
  505. /// </summary>
  506. public void BangdingComboxDataSource()
  507. {
  508. input_Number_prefix.ItemsSource = DataTableSelectData(lblnumberp.Content.ToString());
  509. //input_Number_prefix.ItemsSource = DataTableSelectData(lblnumberp.Content.ToString());
  510. input_Number_prefix.SelectedIndex = 0;
  511. input_Number_suffix.ItemsSource = DataTableSelectData(lblnumbers.Content.ToString());
  512. input_Number_suffix.SelectedIndex = 0;
  513. input_Inspection_instrument.ItemsSource = DataTableSelectData(lblInspection_instrument.Content.ToString());
  514. input_Inspection_instrument.SelectedIndex = 0;
  515. }
  516. /// <summary>
  517. /// 写入文件夹
  518. /// </summary>
  519. /// <param name="NodeName"></param>
  520. /// <param name="value"></param>
  521. public void WriteApp(string NodeName, string value)
  522. {
  523. config.AppSettings.Settings[NodeName].Value = value;
  524. config.Save(ConfigurationSaveMode.Modified);// 保存在APP文件中的更改
  525. ConfigurationManager.RefreshSection("appSettings");//强制重新加载更改的部分
  526. }
  527. /// <summary>
  528. /// 装载事件
  529. /// </summary>
  530. /// <param name="sender"></param>
  531. /// <param name="e"></param>
  532. private void Window_Loaded(object sender, RoutedEventArgs e)
  533. {
  534. txt_Picture_catalog =Common.removeControlChars( ConfigurationManager.AppSettings["folderpath"]);
  535. connstr.DataSource = path + "\\datas.db";
  536. conn.ConnectionString = connstr.ToString();
  537. input_Inspection_project.Content = grade_standard + " " + test_item;
  538. BangdingComboxDataSource();
  539. }
  540. }
  541. }