CNAS取数仪器端升级
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1114 lines
48KB

  1. using CnasSynchronousCommon;
  2. using CnasSynchronusClient;
  3. using CnasSynchrousModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Windows.Forms;
  11. using System.Xml.Serialization;
  12. namespace CNAS_BalanceClient
  13. {
  14. public partial class frmBalanceMain : Form
  15. {
  16. private LaboratoryTestBLL laboratoryTestBLL = new LaboratoryTestBLL();
  17. private BalanceMainOperation operation = new BalanceMainOperation();
  18. private string currentDataType = "ARB";//ARB全水分;Moisture水分;Ash灰分;Volatile挥发分;Crucible坩埚;Heat发热量:Sulfur测硫;Hydrogen测氢
  19. private string currentCrucibleDataType = "Crucible|Heat";//坩埚称样数据类型(热量;硫;元素)
  20. private Dictionary<string, DataGridView> DstCrucibleGrids = new Dictionary<string, DataGridView>();
  21. private List<LaboratoryTest> laboratoryTests = new List<LaboratoryTest>(); //数据源
  22. private List<LaboratoryTest> cruciblelaboratoryTests = new List<LaboratoryTest>(); //坩埚数据样品编号类型(界面中左侧表单数据,不存储)
  23. private List<SyncBalanceItem> lstBalanceData = new List<SyncBalanceItem>(); //天平端映射字段相关信息
  24. private DataBaseInfo targetdataBase = new DataBaseInfo(); //cnas目标数据库
  25. private SerialPortConfigInfo serialPortConfig; //连接天平的串口的相关配置信息,从本地数据库中读取
  26. private List<string> lstReadTxt = new List<string>(); //天平传输过来的数据
  27. private double OldMad1Value = 0; //用于记录未开始编辑时的旧值,便于编辑出错后的返回
  28. private double OldMad2Value = 0; //用于记录未开始编辑时的旧值,便于编辑出错后的返回
  29. public frmBalanceMain()
  30. {
  31. InitializeComponent();
  32. //初始化各个datagridview控件
  33. InitializeDatagridview(dgvQuanShuiFen);
  34. InitializeDatagridview(dgvShuiFen);
  35. InitializeDatagridview(dgvHuiFen);
  36. InitializeDatagridview(dgvHuiFaFen);
  37. InitializeDatagridview(dgvWeighing);
  38. InitializeDatagridview(dgvCrucibleSulfur);
  39. InitializeDatagridview(dgvCrucibleElement);
  40. InitializeDatagridview(dgvCrucibleShelf);
  41. InitializeDatagridview(dgvShelfFind);
  42. if (GlobalCommonOperation.strUserName.Contains("admin"))
  43. {
  44. this.tsmMapping.Enabled = true;
  45. this.tsmPortConfig.Enabled = true;
  46. }
  47. else
  48. {
  49. this.tsmMapping.Enabled = false;
  50. this.tsmPortConfig.Enabled = false;
  51. }
  52. }
  53. private void frmBalanceMain_Load(object sender, EventArgs e)
  54. {
  55. //加载天平端字段信息
  56. lstBalanceData = FileOperation.GetLocalBalanceData();
  57. //加载CNAS数据库配置
  58. targetdataBase = FileOperation.GetLocalPlatFormDB();
  59. if (!CnasDataOperationFact.CnasDataOperation().TestConnect(targetdataBase.DBHost, targetdataBase.DBName, targetdataBase.DBUser, targetdataBase.DBPwd, targetdataBase.DBPort))
  60. {
  61. this.lblCNASDb.Visible = true;
  62. this.lblCNASDb.Text = "CNAS数据库无法正常连接...";
  63. }
  64. else
  65. {
  66. this.lblCNASDb.Visible = false;
  67. }
  68. #region 注释------已经不再使用通过读取上次上传时间获取显示数据
  69. //从目标库的每个表中获取中读取上次上传的最大时间
  70. //Dictionary<string, string> dictMaxTime = new Dictionary<string, string>();
  71. //foreach (var item in lstBalanceData)
  72. //{
  73. // string strCompareTime = "";
  74. // var query = item.syncParamasInfos.Where(s => s.IfDateField == true).ToList<SyncParamasInfo>();
  75. // if (query.Count == 1)
  76. // {
  77. // DataTable dtTime = CnasDataOperationFact.CnasDataOperation().GetMaxTimeByTableName(targetdataBase, item.syncParamasInfos[0].TargetTable, query[0].TargetField);
  78. // if (dtTime == null || dtTime.Rows.Count <= 0)
  79. // {
  80. // strCompareTime = "2019-01-01";
  81. // }
  82. // else
  83. // strCompareTime = Convert.ToDateTime(dtTime.Rows[0][0].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
  84. // }
  85. // dictMaxTime.Add(GetLaboratoryValue(item.Type), strCompareTime);
  86. //}
  87. #endregion
  88. //从本地数据库中读取串口配置信息
  89. serialPortConfig = new SericalPortConfigBLL().GetSerialPortConfigInfo();
  90. //从本地数据库中读取所有数据
  91. laboratoryTests = laboratoryTestBLL.GetLaboratoryTestDataByNoUpLoad();
  92. if (laboratoryTests.Count <= 0) return;
  93. //绑定显示的datagridview
  94. BindData();
  95. //初始化后面常用的一个参数(避免频繁swtich)
  96. DstCrucibleGrids = new Dictionary<string, DataGridView>() {
  97. { "Crucible|Heat",dgvWeighing},
  98. { "Crucible|Sulfur",dgvCrucibleSulfur},
  99. { "Crucible|Hydrogen",dgvCrucibleElement}
  100. };
  101. }
  102. private void tabBalance_Selected(object sender, TabControlEventArgs e)
  103. {
  104. //ARB全水分;Moisture水分;Ash灰分;Volatile挥发分;Crucible坩埚;Heat发热量:Sulfur测硫;Hydrogen测氢
  105. operation.UpdateCurrentDataType(e, ref currentDataType);
  106. BindData();
  107. }
  108. private void tsbDelete_Click(object sender, EventArgs e)
  109. {
  110. DeleteData();
  111. }
  112. private void tsbSave_Click(object sender, EventArgs e)
  113. {
  114. //当前选项卡下的所有数据
  115. List<LaboratoryTest> currentquery = new List<LaboratoryTest>();
  116. if(currentDataType== "Crucible")
  117. currentquery = laboratoryTests.Where(s => s.DataType.StartsWith(currentDataType) && s.OperationType != null && s.OperationType != "").ToList<LaboratoryTest>();
  118. else
  119. currentquery = laboratoryTests.Where(s => s.DataType == currentDataType&&s.OperationType!=null&& s.OperationType != "").ToList<LaboratoryTest>();
  120. //执行存储
  121. ReturnValue<LaboratoryTest> returnValue=laboratoryTestBLL.SaveLaboratoryData(currentquery);
  122. if (returnValue.StrErrorMsg!=null&&returnValue.StrErrorMsg != "")
  123. {
  124. AppLog.Error($"未保存成功:{returnValue.StrErrorMsg}");
  125. MessageBox.Show(returnValue.StrErrorMsg);
  126. }
  127. else if (returnValue.StrMsg!=null&&returnValue.StrMsg != "")
  128. {
  129. MessageBox.Show(returnValue.StrMsg);
  130. //清空操作列的所有内容,避免重复操作
  131. //int deletecount=currentquery.RemoveAll(s => (s.OperationType != null && s.OperationType.ToLower() == "delete"));
  132. //foreach (var item in currentquery)
  133. //{
  134. // item.OperationType = "";
  135. //}
  136. foreach (var item in currentquery)
  137. {
  138. if (item.OperationType == null) continue;
  139. if (item.OperationType == "") continue;
  140. if (item.OperationType.ToLower() == "delete")
  141. {
  142. laboratoryTests.Remove(item);
  143. }
  144. else
  145. {
  146. item.OperationType = "";
  147. }
  148. }
  149. }
  150. }
  151. private void tsbAllSave_Click(object sender, EventArgs e)
  152. {
  153. //保存操作列不为空的数据行
  154. var currentquery = laboratoryTests.Where(s =>s.OperationType != null && s.OperationType != "").ToList<LaboratoryTest>();
  155. ReturnValue<LaboratoryTest> returnValue = laboratoryTestBLL.SaveLaboratoryData(currentquery);
  156. if (returnValue.StrErrorMsg != null && returnValue.StrErrorMsg != "")
  157. {
  158. MessageBox.Show(returnValue.StrErrorMsg);
  159. AppLog.Error($"未保存成功:{returnValue.StrErrorMsg}");
  160. }
  161. else if (returnValue.StrMsg != null && returnValue.StrMsg != "")
  162. {
  163. MessageBox.Show(returnValue.StrMsg);
  164. //清空操作列的所有内容,避免重复操作
  165. //int deletecount = currentquery.RemoveAll(s => (s.OperationType != null && s.OperationType.ToLower() == "delete"));
  166. //foreach (var item in currentquery)
  167. //{
  168. // item.OperationType = "";
  169. //}
  170. foreach (var item in currentquery)
  171. {
  172. if (item.OperationType == null) continue;
  173. if (item.OperationType == "") continue;
  174. if (item.OperationType.ToLower() == "delete")
  175. {
  176. laboratoryTests.Remove(item);
  177. }
  178. else
  179. {
  180. item.OperationType = "";
  181. }
  182. }
  183. }
  184. }
  185. private void tsbOpenPort_Click(object sender, EventArgs e)
  186. {
  187. try
  188. {
  189. if (sPortBalance.IsOpen)
  190. {
  191. sPortBalance.Close();
  192. tsbOpenPort.Text = "打开串口";
  193. lblPort.Text = "串口已关闭...";
  194. }
  195. else
  196. {
  197. sPortBalance.PortName = serialPortConfig.PortName;
  198. sPortBalance.BaudRate = serialPortConfig.BaudRate;
  199. sPortBalance.DataBits = serialPortConfig.DataBits;
  200. switch (serialPortConfig.Parity)
  201. {
  202. case "None":
  203. sPortBalance.Parity = System.IO.Ports.Parity.None;
  204. break;
  205. case "Odd":
  206. sPortBalance.Parity = System.IO.Ports.Parity.Odd;
  207. break;
  208. case "Even":
  209. sPortBalance.Parity = System.IO.Ports.Parity.Even;
  210. break;
  211. case "Mark":
  212. sPortBalance.Parity = System.IO.Ports.Parity.Mark;
  213. break;
  214. case "Space":
  215. sPortBalance.Parity = System.IO.Ports.Parity.Space;
  216. break;
  217. }
  218. switch (serialPortConfig.StopBits)
  219. {
  220. case "1":
  221. sPortBalance.StopBits = System.IO.Ports.StopBits.One;
  222. break;
  223. case "1.5":
  224. sPortBalance.StopBits = System.IO.Ports.StopBits.OnePointFive;
  225. break;
  226. case "2":
  227. sPortBalance.StopBits = System.IO.Ports.StopBits.Two;
  228. break;
  229. }
  230. switch (serialPortConfig.Handshake)
  231. {
  232. case "None":
  233. sPortBalance.Handshake = System.IO.Ports.Handshake.None;
  234. break;
  235. case "XOnXOff":
  236. sPortBalance.Handshake = System.IO.Ports.Handshake.XOnXOff;
  237. break;
  238. case "RequestToSend":
  239. sPortBalance.Handshake = System.IO.Ports.Handshake.RequestToSend;
  240. break;
  241. case "RequestToSendXOnXOff":
  242. sPortBalance.Handshake = System.IO.Ports.Handshake.RequestToSendXOnXOff;
  243. break;
  244. }
  245. sPortBalance.Open();
  246. tsbOpenPort.Text = "关闭串口";
  247. lblPort.Text = "串口已打开...";
  248. }
  249. }
  250. catch (Exception ex)
  251. {
  252. sPortBalance = new System.IO.Ports.SerialPort();
  253. System.Media.SystemSounds.Beep.Play();
  254. tsbOpenPort.Text = "打开串口";
  255. lblPort.Text = "串口已关闭...";
  256. MessageBox.Show(ex.Message);
  257. AppLog.Error($"打开串口失败:{ex.Message}");
  258. }
  259. }
  260. private void sPortBalance_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  261. {
  262. //读取缓冲区三次数据,类似{N +} {0.0000}{ g},这里只要第二部分的内容
  263. try
  264. {
  265. //读取数据
  266. this.Invoke((EventHandler)(delegate
  267. {
  268. string strReadTxt = sPortBalance.ReadExisting();
  269. if (strReadTxt.StartsWith("N"))
  270. {
  271. lstReadTxt.Clear();
  272. }
  273. lstReadTxt.Add(strReadTxt);
  274. if (lstReadTxt.Count == 3)
  275. {
  276. UpdateBalanceValue(lstReadTxt[1]);
  277. }
  278. //UpdateBalanceValue(strReadTxt);
  279. }
  280. )
  281. );
  282. }
  283. catch (Exception ex)
  284. {
  285. AppLog.Error($"接收数据失败:{ex.Message}");
  286. throw ex;
  287. }
  288. }
  289. /// <summary>
  290. /// 初始化datagridview(补充)
  291. /// </summary>
  292. /// <param name="dgv"></param>
  293. private void InitializeDatagridview(DataGridView dgv)
  294. {
  295. dgv.AutoGenerateColumns = false;
  296. }
  297. /// <summary>
  298. /// 选中最后一行
  299. /// </summary>
  300. private void SelectLastRow()
  301. {
  302. //ARB 全水分;Moisture 水分;Ash 灰分;Volatile 挥发分;Crucible 坩埚;Heat 发热量:Sulfur 测硫;Hydrogen 测氢
  303. switch (currentDataType)
  304. {
  305. case "ARB":
  306. dgvQuanShuiFen.CurrentCell = dgvQuanShuiFen.Rows[dgvQuanShuiFen.Rows.Count - 1].Cells[1];
  307. break;
  308. case "Moisture":
  309. dgvShuiFen.CurrentCell = dgvShuiFen.Rows[dgvShuiFen.Rows.Count - 1].Cells[1];
  310. break;
  311. case "Ash":
  312. dgvHuiFen.CurrentCell = dgvHuiFen.Rows[dgvHuiFen.Rows.Count - 1].Cells[1];
  313. break;
  314. case "Volatile":
  315. dgvHuiFaFen.CurrentCell = dgvHuiFaFen.Rows[dgvHuiFaFen.Rows.Count - 1].Cells[1];
  316. break;
  317. }
  318. }
  319. /// <summary>
  320. /// 绑定数据
  321. /// </summary>
  322. private void BindData()
  323. {
  324. try
  325. {
  326. //ARB 全水分;Moisture 水分;Ash 灰分;Volatile 挥发分;Crucible 坩埚;Heat 发热量:Sulfur 测硫;Hydrogen 测氢
  327. switch (currentDataType)
  328. {
  329. case "ARB":
  330. operation.BindData(dgvQuanShuiFen, laboratoryTests, currentDataType);
  331. break;
  332. case "Moisture":
  333. operation.BindData(dgvShuiFen, laboratoryTests, currentDataType);
  334. break;
  335. case "Ash":
  336. operation.BindData(dgvHuiFen, laboratoryTests, currentDataType);
  337. break;
  338. case "Volatile":
  339. operation.BindData(dgvHuiFaFen, laboratoryTests, currentDataType);
  340. break;
  341. case "Crucible":
  342. BindCrucibleData();
  343. break;
  344. case "Heat":
  345. break;
  346. case "Sulfur":
  347. break;
  348. case "Hydrogen":
  349. break;
  350. }
  351. }
  352. catch (Exception ex)
  353. {
  354. AppLog.Error("删除数据时出错:" + ex.Message.ToString());
  355. }
  356. }
  357. /// <summary>
  358. /// 删除数据
  359. /// </summary>
  360. private void DeleteData()
  361. {
  362. try
  363. {
  364. switch (currentDataType)
  365. {
  366. case "ARB":
  367. operation.DeleteData(dgvQuanShuiFen, laboratoryTests, currentDataType);
  368. break;
  369. case "Moisture":
  370. operation.DeleteData(dgvShuiFen, laboratoryTests, currentDataType);
  371. break;
  372. case "Ash":
  373. operation.DeleteData(dgvHuiFen, laboratoryTests, currentDataType);
  374. break;
  375. case "Volatile":
  376. operation.DeleteData(dgvHuiFaFen, laboratoryTests, currentDataType);
  377. break;
  378. }
  379. }
  380. catch (Exception ex)
  381. {
  382. AppLog.Error("删除数据时出错:"+ex.Message.ToString());
  383. }
  384. }
  385. private void tsmMapping_Click(object sender, EventArgs e)
  386. {
  387. frmBalanceField frmBalanceField = new frmBalanceField(lstBalanceData,targetdataBase);
  388. if (frmBalanceField.ShowDialog() == DialogResult.OK)
  389. {
  390. this.lstBalanceData = frmBalanceField.syncBalanceItems;
  391. }
  392. }
  393. /// <summary>
  394. /// 上传数据
  395. /// </summary>
  396. /// <param name="sender"></param>
  397. /// <param name="e"></param>
  398. private void tsbUpload_Click(object sender, EventArgs e)
  399. {
  400. //1.确定当前准备上传的数据(只上传选中部分)
  401. AppLog.Info("获取当前选中数据");
  402. List<string> lstSelectID = new List<string>();
  403. //ARB 全水分;Moisture 水分;Ash 灰分;Volatile 挥发分;Crucible 坩埚;Heat 发热量:Sulfur 测硫;Hydrogen 测氢
  404. switch (currentDataType)
  405. {
  406. case "ARB":
  407. lstSelectID = operation.GetSelectItemID(dgvQuanShuiFen);
  408. break;
  409. case "Moisture":
  410. lstSelectID = operation.GetSelectItemID(dgvShuiFen);
  411. break;
  412. case "Ash":
  413. lstSelectID = operation.GetSelectItemID(dgvHuiFen);
  414. break;
  415. case "Volatile":
  416. lstSelectID = operation.GetSelectItemID(dgvHuiFaFen);
  417. break;
  418. }
  419. if (lstSelectID.Count <= 0)
  420. {
  421. AppLog.Info("未选中任何一条化验数据。上传结束。");
  422. MessageBox.Show("请至少选择一条化验数据进行上传。");
  423. return;
  424. }
  425. var currentbalanceconfig = lstBalanceData.Where(x => GetLaboratoryValue(x.Type) == currentDataType).ToList<SyncBalanceItem>();
  426. if (currentbalanceconfig.Count <= 0)
  427. {
  428. MessageBox.Show("请先配置上传映射字段。");
  429. return;
  430. }
  431. List<DataRow> lstError = new List<DataRow>();
  432. foreach (var banlanceitem in currentbalanceconfig)
  433. {
  434. if (currentDataType != GetLaboratoryValue(banlanceitem.Type)) continue;
  435. //2.获取准备上传到数据库表结构
  436. AppLog.Info("获取准备上传到数据库表结构");
  437. DataTable dtTarget = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(banlanceitem.syncParamasInfos[0].TargetTable, targetdataBase);
  438. //3.将上传数据转换为上传数据的形式
  439. AppLog.Info("将上传数据转换为上传数据的形式");
  440. string strErrorMsg = "";
  441. var query = laboratoryTests.Where(s => s.DataType == GetLaboratoryValue(banlanceitem.Type)).ToList<LaboratoryTest>();
  442. foreach (var laboratoryitem in query)
  443. {
  444. if (!lstSelectID.Contains(laboratoryitem.GUID)) continue;
  445. DataRow drNewTarget = dtTarget.NewRow();
  446. //遍历映射表,把所有字段插入到目标表中
  447. foreach (var item in banlanceitem.syncParamasInfos)
  448. {
  449. var strSourceField = item.SourceField;
  450. var strTargetField = item.TargetField;
  451. //当列名存在于数据表中时才能继续
  452. int i = laboratoryitem.GetType().GetProperties().Where(sb => sb.Name== strSourceField).Count();
  453. if (laboratoryitem.GetType().GetProperties().Where(sb => sb.Name== strSourceField).Count() > 0 && dtTarget.Columns.Contains(strTargetField))
  454. {
  455. strErrorMsg = TestConvertValueToTarget(drNewTarget, laboratoryitem, strTargetField, strSourceField);
  456. if (strErrorMsg != "")
  457. {
  458. MessageBox.Show(string.Format("发生数据转换错误,请重新配置映射字段或修改字段值。错误信息如下:列【{0}】,值【{1}】,详情【{2}】", strSourceField, laboratoryitem.GetType().GetProperty(strSourceField).GetValue(laboratoryitem, null), strErrorMsg));
  459. break;
  460. }
  461. }
  462. }
  463. //dtTarget.Rows.Add(drNewTarget);
  464. AppLog.Info("检查上传数据合法性");
  465. if (strErrorMsg != "") break;
  466. if (!CheckInputDatallegal(ConvertDataRowToTable(drNewTarget), banlanceitem)) break;
  467. AppLog.Info("执行上传单条数据");
  468. int iReturn = CnasDataOperationFact.CnasDataOperation().InsertDataToCNASTable(ConvertDataRowToTable(drNewTarget), targetdataBase, banlanceitem.syncParamasInfos,"");
  469. if (iReturn <= 0) //此时出现问题
  470. {
  471. AppLog.Error("上传插入操作未能正常完成:" + TransConvert.DataTableToJSON(ConvertDataRowToTable(drNewTarget)));
  472. lstError.Add(drNewTarget);
  473. }
  474. else //此时没有出现问题,1.如果本条数据未保存,将本条数据保存;2.向上传数据记录表中插入一条数据
  475. {
  476. if (!string.IsNullOrEmpty(laboratoryitem.OperationType))
  477. {
  478. ReturnValue<LaboratoryTest> returnValue = laboratoryTestBLL.SaveLaboratoryData(laboratoryitem);
  479. if (!string.IsNullOrEmpty(returnValue.StrErrorMsg))
  480. {
  481. //写入日志
  482. AppLog.Error("上传完成后,没能正常存储数据到本地:" + returnValue.StrErrorMsg);
  483. }
  484. }
  485. UpLoadRecordBLL upLoadRecord = new UpLoadRecordBLL();
  486. int ireturn=upLoadRecord.AddRecordToDatabase(
  487. new UpLoadRecord()
  488. {
  489. GUID = Guid.NewGuid().ToString(),
  490. LaboratoryTestGUID = laboratoryitem.GUID,
  491. UpLoadUser = GlobalCommonOperation.strUserName,
  492. UpLoadTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm"))
  493. }
  494. );
  495. if (ireturn <= 0)
  496. {
  497. //写入日志
  498. AppLog.Error($"上传完成后,没能正常存储上传记录数据到本地:LaboratoryTestGUID={laboratoryitem.GUID},UpLoadUser={GlobalCommonOperation.strUserName},UpLoadTime={Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm"))}");
  499. }
  500. AppLog.Info("执行上传单条数据完成");
  501. }
  502. }
  503. }
  504. if (lstError.Count <= 0)
  505. {
  506. MessageBox.Show("上传完成!");
  507. //数据源中清空已经上传的项
  508. foreach (var item in lstSelectID)
  509. {
  510. laboratoryTests.RemoveAll(s => s.GUID == item);
  511. }
  512. BindData();
  513. }
  514. else
  515. {
  516. MessageBox.Show("上传过程中发生异常,存在部分数据未成功上传,请联系管理员!");
  517. }
  518. }
  519. public bool CheckInputDatallegal(DataTable dtTarget,SyncBalanceItem banlanceitem)
  520. {
  521. bool bIfSuccess = true;
  522. CnasDataCheck check = new CnasDataCheck
  523. {
  524. DtReadyInsert = dtTarget,
  525. TargetDataBase = targetdataBase,
  526. StrTableName = banlanceitem.syncParamasInfos[0].TargetTable
  527. };
  528. check.CheckData();
  529. if (check.LstIllegalMsg != null && check.LstIllegalMsg.Count > 0)
  530. {
  531. var item = banlanceitem.syncParamasInfos.Where(s => s.TargetField == check.LstIllegalMsg[0].ColumnName).ToList<SyncParamasInfo>();
  532. if (item.Count == 1)
  533. {
  534. MessageBox.Show(string.Format("数据不满足上传条件,请修改后再上传。错误数据:当前列名【{0}】,列值【{1}】", item[0].SourceField, check.LstIllegalMsg[0].ColumnValue));
  535. bIfSuccess = false;
  536. }
  537. else
  538. {
  539. MessageBox.Show(string.Format("数据不满足上传条件,请修改后再上传。错误数据:CNAS列名【{0}】,列值【{1}】", check.LstIllegalMsg[0].ColumnName, check.LstIllegalMsg[0].ColumnValue));
  540. bIfSuccess = false;
  541. }
  542. }
  543. return bIfSuccess;
  544. }
  545. private string TestConvertValueToTarget(DataRow drTarget, LaboratoryTest laboratory, string strTargetColumn, string strSourceColumn)
  546. {
  547. string strErrorMsg = "";
  548. try
  549. {
  550. drTarget[strTargetColumn] = laboratory.GetType().GetProperty(strSourceColumn).GetValue(laboratory, null);
  551. }
  552. catch (Exception ex)
  553. {
  554. strErrorMsg = ex.Message;
  555. AppLog.Error(strErrorMsg);
  556. }
  557. return strErrorMsg;
  558. }
  559. private DataTable ConvertDataRowToTable(DataRow dr)
  560. {
  561. DataTable dt = dr.Table.Clone();
  562. DataRow drNew = dt.NewRow();
  563. drNew.ItemArray = dr.ItemArray;
  564. dt.Rows.Add(drNew);
  565. return dt;
  566. }
  567. private string GetLaboratoryValue(string strShowName)
  568. {
  569. string strReturn = "";
  570. switch (strShowName)
  571. {
  572. case "全水分":
  573. strReturn = "ARB";
  574. break;
  575. case "水分":
  576. strReturn = "Moisture";
  577. break;
  578. case "灰分":
  579. strReturn = "Ash";
  580. break;
  581. case "挥发分":
  582. strReturn = "Volatile";
  583. break;
  584. }
  585. return strReturn;
  586. }
  587. /// <summary>
  588. /// 此时新增一条样品化验记录
  589. /// </summary>
  590. /// <param name="sender"></param>
  591. /// <param name="e"></param>
  592. private void btnCodeOK_Click(object sender, EventArgs e)
  593. {
  594. //判断化验编号是否合法
  595. if (this.txtCode.Text.Trim().Length <= 0)
  596. {
  597. MessageBox.Show("当条码为空时不能新增化验记录");
  598. return;
  599. }
  600. //根据当前Tab页,增加新的数据
  601. ReturnValue<LaboratoryTest> returnValue=operation.AddNewData(this.txtCode.Text.Trim(),currentDataType);
  602. if ((returnValue.StrErrorMsg == null || returnValue.StrErrorMsg == "") && returnValue.LstValue.Count == 1)
  603. {
  604. //填充自动编码数据
  605. returnValue.LstValue[0].Auto_Code = operation.GetAutoCode(laboratoryTests);
  606. //显示页中新增一条数据
  607. laboratoryTests.Add(returnValue.LstValue[0]);
  608. BindData();
  609. //选中最后一行
  610. SelectLastRow();
  611. }
  612. else
  613. {
  614. MessageBox.Show("新增数据时出错:"+returnValue.StrErrorMsg);
  615. AppLog.Error("新增数据时出错:" + returnValue.StrErrorMsg);
  616. }
  617. }
  618. /// <summary>
  619. /// 将天平端传入的值传入当前显示界面
  620. /// </summary>
  621. /// <param name="strValue"></param>
  622. private void UpdateBalanceValue(string strValue)
  623. {
  624. try
  625. {
  626. AppLog.Info($"接收串口信息:{strValue}");
  627. double defaultdouble = 0;
  628. if (double.TryParse(strValue, out defaultdouble)) //接收到的数据必须可以转换成数字
  629. {
  630. List<string> lstIndex = new List<string>();
  631. //ARB 全水分;Moisture 水分;Ash 灰分;Volatile 挥发分;Crucible 坩埚;Heat 发热量:Sulfur 测硫;Hydrogen 测氢
  632. //全水分,水分,灰分天平端上传列相同,使用同一个方法。
  633. switch (currentDataType)
  634. {
  635. case "ARB":
  636. operation.UpdateQuanShuiFenBalanceWeight(dgvQuanShuiFen, lstIndex, defaultdouble, laboratoryTests, currentDataType);
  637. break;
  638. case "Moisture":
  639. operation.UpdateQuanShuiFenBalanceWeight(dgvShuiFen, lstIndex, defaultdouble, laboratoryTests, currentDataType);
  640. break;
  641. case "Ash":
  642. operation.UpdateQuanShuiFenBalanceWeight(dgvHuiFen, lstIndex, defaultdouble, laboratoryTests, currentDataType);
  643. break;
  644. case "Volatile":
  645. operation.UpdateVolatileBalanceWeight(dgvHuiFaFen, lstIndex, defaultdouble, laboratoryTests);
  646. break;
  647. case "Crucible":
  648. operation.UpdateCrucibleBalanceWeight(DstCrucibleGrids, dgvCrucibleShelf, defaultdouble, currentCrucibleDataType, laboratoryTests);
  649. break;
  650. }
  651. }
  652. }
  653. catch (Exception ex)
  654. {
  655. MessageBox.Show("无法正常插入天平值," + ex.Message);
  656. AppLog.Error("无法正常插入天平值," + ex.Message);
  657. }
  658. }
  659. #region 绘制序号列
  660. private void dgvQuanShuiFen_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  661. {
  662. operation.DrawFirstColumnIndex(dgvQuanShuiFen,e);
  663. }
  664. private void dgvShuiFen_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  665. {
  666. operation.DrawFirstColumnIndex(dgvShuiFen, e);
  667. }
  668. private void dgvHuiFen_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  669. {
  670. operation.DrawFirstColumnIndex(dgvHuiFen, e);
  671. }
  672. private void dgvHuiFaFen_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  673. {
  674. operation.DrawFirstColumnIndex(dgvHuiFaFen, e);
  675. }
  676. private void dgvWeighing_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  677. {
  678. operation.DrawFirstColumnIndex(dgvWeighing, e);
  679. }
  680. private void dgvCrucibleSulfur_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  681. {
  682. operation.DrawFirstColumnIndex(dgvCrucibleSulfur, e);
  683. }
  684. private void dgvCrucibleElement_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  685. {
  686. operation.DrawFirstColumnIndex(dgvCrucibleElement, e);
  687. }
  688. private void dgvCrucibleShelf_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  689. {
  690. operation.DrawFirstColumnIndex(dgvCrucibleShelf, e);
  691. }
  692. private void dgvShelfFind_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  693. {
  694. operation.DrawFirstColumnIndex(dgvShelfFind, e);
  695. }
  696. #endregion
  697. #region 编辑提交仪器编号,坩埚号
  698. private void dgvQuanShuiFen_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  699. {
  700. operation.CommitEditColumn(dgvQuanShuiFen, laboratoryTests, e);
  701. }
  702. private void dgvShuiFen_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  703. {
  704. operation.CommitEditColumn(dgvShuiFen, laboratoryTests, e);
  705. if (dgvShuiFen.CurrentCell == null) return;
  706. if (dgvShuiFen.CurrentCell.Value == null) return;
  707. if (e.ColumnIndex == 11) //MAD编号
  708. {
  709. string strInputValue = dgvShuiFen.CurrentCell.Value.ToString();
  710. string strCurrentID = dgvShuiFen.Rows[dgvShuiFen.CurrentCell.RowIndex].Cells[0].Value.ToString();
  711. var query = laboratoryTests.Where(s => s.GUID == strCurrentID).ToList<LaboratoryTest>();
  712. if (query.Count == 1)
  713. {
  714. if (operation.CheckInputPercent(strInputValue))
  715. {
  716. query[0].Mad = Convert.ToDouble(strInputValue);
  717. if (query[0].OperationType == null || query[0].OperationType == "")
  718. query[0].OperationType = "update";
  719. }
  720. else
  721. {
  722. //将显示值改回初始值
  723. dgvShuiFen.CurrentCell.Value = OldMad1Value;
  724. }
  725. }
  726. }
  727. }
  728. private void dgvHuiFen_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  729. {
  730. operation.CommitEditColumn(dgvHuiFen, laboratoryTests, e);
  731. }
  732. private void dgvHuiFaFen_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  733. {
  734. operation.CommitEditColumn(dgvHuiFaFen, laboratoryTests, e);
  735. if (dgvHuiFaFen.CurrentCell == null) return;
  736. if (dgvHuiFaFen.CurrentCell.Value == null) return;
  737. if (e.ColumnIndex == 9) //MAD编号
  738. {
  739. string strInputValue = dgvHuiFaFen.CurrentCell.Value.ToString();
  740. string strCurrentID = dgvHuiFaFen.Rows[dgvHuiFaFen.CurrentCell.RowIndex].Cells[0].Value.ToString();
  741. var query = laboratoryTests.Where(s => s.GUID == strCurrentID).ToList<LaboratoryTest>();
  742. if (query.Count == 1)
  743. {
  744. if (operation.CheckInputPercent(strInputValue))
  745. {
  746. query[0].Mad = Convert.ToDouble(strInputValue);
  747. if (query[0].OperationType == null || query[0].OperationType == "")
  748. query[0].OperationType = "update";
  749. }
  750. else
  751. {
  752. //将显示值改回初始值
  753. dgvHuiFaFen.CurrentCell.Value = OldMad2Value;
  754. }
  755. }
  756. }
  757. if (e.ColumnIndex == 11) //焦特征
  758. {
  759. string strInputValue = dgvHuiFaFen.CurrentCell.Value.ToString();
  760. string strCurrentID = dgvHuiFaFen.Rows[dgvHuiFaFen.CurrentCell.RowIndex].Cells[0].Value.ToString();
  761. var query = laboratoryTests.Where(s => s.GUID == strCurrentID).ToList<LaboratoryTest>();
  762. if (query.Count == 1)
  763. {
  764. query[0].Cinder_Characteristics = strInputValue;
  765. if (query[0].OperationType == null || query[0].OperationType == "")
  766. query[0].OperationType = "update";
  767. }
  768. }
  769. }
  770. #endregion
  771. private void btnAddReady_Click(object sender, EventArgs e)
  772. {
  773. //在左侧增加一条数据
  774. //判断化验编号是否合法
  775. string strSampleCode = this.txtSampleCode.Text.Trim();
  776. if (this.txtSampleCode.Text.Trim().Length <= 0)
  777. {
  778. MessageBox.Show("样品编码不能为空!");
  779. return;
  780. }
  781. //存在相同样品编码的化验数据,处理相同名称
  782. int index = 1;
  783. while (true)
  784. {
  785. var query = cruciblelaboratoryTests.Where(s => s.Sample_Number == strSampleCode && s.DataType == currentCrucibleDataType).Count();
  786. if (query > 0)
  787. {
  788. strSampleCode = this.txtSampleCode.Text.Trim() + "_" + index;
  789. index++;
  790. }
  791. else
  792. {
  793. break;
  794. }
  795. }
  796. //根据当前Tab页,增加新的数据
  797. ReturnValue<LaboratoryTest> returnValue = operation.AddNewData(strSampleCode, currentCrucibleDataType);
  798. if ((returnValue.StrErrorMsg == null || returnValue.StrErrorMsg == "") && returnValue.LstValue.Count == 1)
  799. {
  800. //填充自动编码数据
  801. returnValue.LstValue[0].Auto_Code = operation.GetAutoCode(laboratoryTests);
  802. //显示页中新增一条数据
  803. cruciblelaboratoryTests.Add(returnValue.LstValue[0]);
  804. //绑定
  805. BindCrucibleData();
  806. }
  807. }
  808. /// <summary>
  809. /// 绑定坩埚侧数据
  810. /// </summary>
  811. private void BindCrucibleData()
  812. {
  813. operation.BindCrucibleData(DstCrucibleGrids[currentCrucibleDataType], cruciblelaboratoryTests, currentCrucibleDataType);
  814. }
  815. private void tabCrucibleType_Selected(object sender, TabControlEventArgs e)
  816. {
  817. switch (e.TabPage.Name)
  818. {
  819. case "tabHeatValue":
  820. currentCrucibleDataType = "Crucible|Heat";
  821. break;
  822. case "tabSulfurValue":
  823. currentCrucibleDataType = "Crucible|Sulfur";
  824. break;
  825. case "tabElementAynic":
  826. currentCrucibleDataType = "Crucible|Hydrogen";
  827. break;
  828. }
  829. BindCrucibleData();
  830. //需要清空右侧列表,避免发生错误输入
  831. dgvCrucibleShelf.Rows.Clear();
  832. }
  833. private void btnCrucible_A_Click(object sender, EventArgs e)
  834. {
  835. operation.AddCrucibleShelfData(DstCrucibleGrids, dgvCrucibleShelf,currentCrucibleDataType,"A",laboratoryTests);
  836. }
  837. private void btnCrucible_B_Click(object sender, EventArgs e)
  838. {
  839. operation.AddCrucibleShelfData(DstCrucibleGrids, dgvCrucibleShelf, currentCrucibleDataType, "B", laboratoryTests);
  840. }
  841. private void btnCrucible_C_Click(object sender, EventArgs e)
  842. {
  843. operation.AddCrucibleShelfData(DstCrucibleGrids, dgvCrucibleShelf, currentCrucibleDataType, "C", laboratoryTests);
  844. }
  845. private void btnCrucible_D_Click(object sender, EventArgs e)
  846. {
  847. operation.AddCrucibleShelfData(DstCrucibleGrids, dgvCrucibleShelf, currentCrucibleDataType, "D", laboratoryTests);
  848. }
  849. private void tabShelf_Selected(object sender, TabControlEventArgs e)
  850. {
  851. //清空坩埚架数据
  852. dgvCrucibleShelf.Rows.Clear();
  853. }
  854. private void tsbHistory_Click(object sender, EventArgs e)
  855. {
  856. new frmHistory().Show();
  857. }
  858. private void btnFind_Click(object sender, EventArgs e)
  859. {
  860. try
  861. {
  862. List<LaboratoryTest> lstFilterData = new List<LaboratoryTest>();
  863. string strWeighingType = currentCrucibleDataType == "Crucible|Heat" ? "FRL" : (currentCrucibleDataType == "Crucible|Sulfur" ? "CLY" : "CHN");
  864. lstFilterData = laboratoryTests.Where(s => s.WeighingType == strWeighingType).ToList<LaboratoryTest>();
  865. if (dtpEndDate.Value >= dtpStartTime.Value)
  866. lstFilterData = lstFilterData.Where(s => s.OperaDateTime != null && s.OperaDateTime > dtpStartTime.Value && s.OperaDateTime < dtpEndDate.Value).ToList<LaboratoryTest>();
  867. if (this.txtShelfFind.Text.Trim() != "")
  868. lstFilterData = lstFilterData.Where(s => s.Sample_Number == this.txtShelfFind.Text.Trim()).ToList<LaboratoryTest>();
  869. dgvShelfFind.DataSource = new BindingList<LaboratoryTest>();
  870. dgvShelfFind.DataSource = new BindingList<LaboratoryTest>(lstFilterData);
  871. }
  872. catch (Exception ex)
  873. {
  874. MessageBox.Show("查找时发生错误:" + ex.Message);
  875. AppLog.Error("查找时发生错误:" + ex.Message);
  876. }
  877. }
  878. private void dgvQuanShuiFen_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  879. {
  880. if (e.ColumnIndex == 10)//百分比字段
  881. {
  882. new QuanShuiFenUIOperation().UpdateARBPercentColumn(dgvQuanShuiFen, laboratoryTests,e.RowIndex);
  883. }
  884. }
  885. private void dgvShuiFen_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  886. {
  887. if (e.ColumnIndex == 10) //减量,百分比字段更新
  888. {
  889. new ShuiFenUIOperation().UpdateMoisturePercentColumn(dgvShuiFen, laboratoryTests, e.RowIndex);
  890. }
  891. }
  892. private void dgvHuiFen_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  893. {
  894. if (e.ColumnIndex == 10) //减量,百分比字段更新
  895. {
  896. new HuiFenUIOperation().UpdateAshPercentColumn(dgvHuiFen, laboratoryTests, e.RowIndex);
  897. }
  898. }
  899. private void dgvHuiFaFen_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  900. {
  901. if (e.ColumnIndex == 10) //减量,百分比字段更新
  902. {
  903. new HuiFaFenUIOperation().UpdateVolatilePercentColumn(dgvHuiFaFen, laboratoryTests, e.RowIndex);
  904. }
  905. }
  906. private void dgvShuiFen_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
  907. {
  908. if (e.ColumnIndex == 11) //MAD编号
  909. {
  910. string strCurrentID = dgvShuiFen.Rows[dgvShuiFen.CurrentCell.RowIndex].Cells[0].Value.ToString();
  911. var query = laboratoryTests.Where(s => s.GUID == strCurrentID).ToList<LaboratoryTest>();
  912. if (query.Count == 1)
  913. {
  914. OldMad1Value = query[0].Mad;
  915. }
  916. }
  917. }
  918. private void dgvHuiFaFen_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
  919. {
  920. if (e.ColumnIndex == 9) //MAD编号
  921. {
  922. string strCurrentID = dgvHuiFaFen.Rows[dgvHuiFaFen.CurrentCell.RowIndex].Cells[0].Value.ToString();
  923. var query = laboratoryTests.Where(s => s.GUID == strCurrentID).ToList<LaboratoryTest>();
  924. if (query.Count == 1)
  925. {
  926. OldMad2Value = query[0].Mad;
  927. }
  928. }
  929. }
  930. private void dgvShuiFen_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  931. {
  932. if (dgvShuiFen.CurrentCell.ColumnIndex == 11) //MAD列,手动输入只能是数字或百分比
  933. {
  934. e.Control.KeyPress -= new KeyPressEventHandler(EditingControl_KeyPress);
  935. e.Control.KeyPress += new KeyPressEventHandler(EditingControl_KeyPress);
  936. }
  937. }
  938. private void dgvHuiFaFen_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  939. {
  940. if (dgvHuiFaFen.CurrentCell.ColumnIndex == 9) //MAD列,手动输入只能是数字或百分比
  941. {
  942. e.Control.KeyPress -= new KeyPressEventHandler(EditingControl_KeyPress);
  943. e.Control.KeyPress += new KeyPressEventHandler(EditingControl_KeyPress);
  944. }
  945. }
  946. private void EditingControl_KeyPress(object sender, KeyPressEventArgs e)
  947. {
  948. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
  949. {
  950. e.Handled = true;
  951. MessageBox.Show("请输入数字与小数点");
  952. }
  953. }
  954. //用户catch发生的错误
  955. private void dgvShuiFen_DataError(object sender, DataGridViewDataErrorEventArgs e)
  956. {
  957. }
  958. //用户catch发生的错误
  959. private void dgvHuiFaFen_DataError(object sender, DataGridViewDataErrorEventArgs e)
  960. {
  961. }
  962. private void btnDeleteShelf_Click(object sender, EventArgs e)
  963. {
  964. //当前选中项i
  965. if (dgvCrucibleShelf.CurrentCell == null) return;
  966. if (dgvCrucibleShelf.Rows[dgvCrucibleShelf.CurrentCell.RowIndex].Cells[0].Value == null) return;
  967. string strID = dgvCrucibleShelf.Rows[dgvCrucibleShelf.CurrentCell.RowIndex].Cells[0].Value.ToString();
  968. //删除数据源数据
  969. var queryitem = laboratoryTests.Where(s => s.GUID == strID).ToList<LaboratoryTest>();
  970. if (queryitem.Count == 1)
  971. {
  972. queryitem[0].OperationType = "delete";
  973. //删除显示数据
  974. dgvCrucibleShelf.Rows[dgvCrucibleShelf.CurrentCell.RowIndex].Cells["Shelf_ID"].Value = "";
  975. dgvCrucibleShelf.Rows[dgvCrucibleShelf.CurrentCell.RowIndex].Cells["Shelf_SampleNumber"].Value = "";
  976. dgvCrucibleShelf.Rows[dgvCrucibleShelf.CurrentCell.RowIndex].Cells["Shelf_WeighingType"].Value = "";
  977. dgvCrucibleShelf.Rows[dgvCrucibleShelf.CurrentCell.RowIndex].Cells["Shelf_SampleWeight"].Value = "";
  978. dgvCrucibleShelf.Rows[dgvCrucibleShelf.CurrentCell.RowIndex].Cells["Shelf_Operator"].Value = "";
  979. }
  980. }
  981. private void tsmPortConfig_Click(object sender, EventArgs e)
  982. {
  983. frmConfigPort frmConfig = new frmConfigPort(serialPortConfig);
  984. if (frmConfig.ShowDialog() == DialogResult.OK)
  985. {
  986. this.serialPortConfig = frmConfig.serialPortConfig;
  987. }
  988. }
  989. private void frmBalanceMain_FormClosing(object sender, FormClosingEventArgs e)
  990. {
  991. var currentquery = laboratoryTests.Where(s => s.OperationType != null && s.OperationType != "").ToList<LaboratoryTest>();
  992. if (currentquery.Count() > 0)
  993. {
  994. DialogResult dr = MessageBox.Show("检测到未保存数据,是否保存?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  995. if (dr == DialogResult.Yes)
  996. {
  997. e.Cancel = false;
  998. laboratoryTestBLL.SaveLaboratoryData(currentquery);
  999. }
  1000. else if (dr == DialogResult.Cancel)
  1001. {
  1002. e.Cancel = true;
  1003. }
  1004. else
  1005. {
  1006. e.Cancel = false;
  1007. }
  1008. }
  1009. }
  1010. }
  1011. }