AutoAnalysisXml.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. using System.Xml;
  9. using UAS_AutoPass.ToolClass;
  10. using BenQGuru.eMES.DLLService;
  11. using System.Text;
  12. namespace UAS_AutoPass
  13. {
  14. public partial class AutoAnalysisXml : Form
  15. {
  16. DataHelper dh;
  17. DataTable dt;
  18. DataTable DB;
  19. MESHelper helper = new MESHelper();
  20. /// <summary>
  21. /// 用户编号
  22. /// </summary>
  23. string iusercode;
  24. /// <summary>
  25. /// 岗位资源
  26. /// </summary>
  27. string isource;
  28. Tip tipform ;
  29. Thread InitDB;
  30. /// <summary>
  31. /// 线别
  32. /// </summary>
  33. string ilinecode;
  34. /// <summary>
  35. /// 不良代码组
  36. /// </summary>
  37. string ibadgroup;
  38. /// <summary>
  39. /// 当前工序
  40. /// </summary>
  41. string istepcode;
  42. StringBuilder sql = new StringBuilder();
  43. /// <summary>
  44. /// 缓存的文件
  45. /// </summary>
  46. public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  47. /// <summary>
  48. /// 缓存的文件夹
  49. /// </summary>
  50. public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  51. /// <summary>
  52. /// 需要解析的文件名
  53. /// </summary>
  54. List<string> _FileName = new List<string>();
  55. public AutoAnalysisXml()
  56. {
  57. tipform = new Tip();
  58. InitializeComponent();
  59. StartPosition = FormStartPosition.CenterScreen;
  60. }
  61. public AutoAnalysisXml(string iUserName, string iSource)
  62. {
  63. tipform = new Tip();
  64. InitializeComponent();
  65. iusercode = iUserName;
  66. isource = iSource;
  67. StartPosition = FormStartPosition.CenterScreen;
  68. }
  69. private void Form1_Load(object sender, EventArgs e)
  70. {
  71. tipform.Show();
  72. CheckForIllegalCrossThreadCalls = false;
  73. FormBorderStyle = FormBorderStyle.FixedSingle;
  74. InitDB = new Thread(ConnectDB);
  75. //添加监控事件
  76. XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
  77. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  78. stw.StartPosition = FormStartPosition.CenterScreen;
  79. stw.ShowDialog();
  80. List<string> CacheInf = new List<string>();
  81. DB = (DataTable)dh.ExecuteSql("select ms_pwd,ma_user,ma_address,ma_inneraddress from master", "select");
  82. Master.DataSource = DB;
  83. Master.DisplayMember = "ma_user";
  84. Master.ValueMember = "ma_user";
  85. //获取缓存信息
  86. try
  87. {
  88. FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString();
  89. BackUpFolderPath.Text = BaseUtil.GetCacheData("BackUpFolderPath").ToString();
  90. Master.Text = BaseUtil.GetCacheData("Master").ToString();
  91. AutoStart.Checked = (bool)BaseUtil.GetCacheData("AutoStart");
  92. }
  93. catch (Exception ex) { Console.WriteLine(ex.Message); }
  94. for (int i = 0; i < DB.Rows.Count; i++)
  95. {
  96. if (Master.Text == DB.Rows[i]["ma_user"].ToString())
  97. {
  98. DataHelper.DBConnectionString = "Data Source=" + DB.Rows[i]["ma_inneraddress"] + ";User ID=" + DB.Rows[i]["ma_user"] + ";PassWord=" + DB.Rows[i]["ms_pwd"]; ;
  99. dh = new DataHelper();
  100. }
  101. }
  102. //获取岗位资源相关信息
  103. DataTable dt = (DataTable)dh.ExecuteSql("select sc_linecode,sc_stepcode,st_badgroupcode from source left join step on sc_stepcode=st_code where sc_code='" + isource + "'", "select");
  104. if (dt.Rows.Count > 0)
  105. {
  106. ilinecode = dt.Rows[0]["sc_linecode"].ToString();
  107. istepcode = dt.Rows[0]["sc_stepcode"].ToString();
  108. ibadgroup = dt.Rows[0]["st_badgroupcode"].ToString();
  109. }
  110. }
  111. private void ConnectDB()
  112. {
  113. dh = new DataHelper();
  114. }
  115. private void StartWatch_Click(object sender, EventArgs e)
  116. {
  117. if (FolderPath.Text == "" || BackUpFolderPath.Text == "")
  118. {
  119. OperateResult.AppendText("请选择监控文件夹和备份文件夹\n");
  120. return;
  121. }
  122. else
  123. {
  124. if (!Directory.Exists(FolderPath.Text))
  125. {
  126. OperateResult.AppendText("监控文件夹不存在\n");
  127. return;
  128. }
  129. if (!Directory.Exists(BackUpFolderPath.Text))
  130. {
  131. OperateResult.AppendText("备份文件夹不存在\n");
  132. return;
  133. }
  134. }
  135. if (FolderPath.Text == BackUpFolderPath.Text)
  136. {
  137. OperateResult.AppendText("监控文件夹和备份文件夹不能相同\n");
  138. return;
  139. }
  140. for (int i = 0; i < DB.Rows.Count; i++)
  141. {
  142. if (Master.Text == DB.Rows[i]["ma_user"].ToString())
  143. {
  144. DataHelper.DBConnectionString = "Data Source=" + DB.Rows[i]["ma_inneraddress"] + ";User ID=" + DB.Rows[i]["ma_user"] + ";PassWord=" + DB.Rows[i]["ms_pwd"]; ;
  145. dh = new DataHelper();
  146. LogicHandler.dh = new DataHelper();
  147. }
  148. }
  149. //存在工单必须是已下放状态
  150. if (!dh.CheckExist("make", "ma_statuscode='STARTED' and ma_code='" + ma_code.Text + "'") && ma_code.Text != "")
  151. {
  152. OperateResult.AppendText("工单不存在或者未下放\n");
  153. return;
  154. }
  155. XmlWatcher.Path = FolderPath.Text;
  156. XmlWatcher.Filter = "*.xml";
  157. XmlWatcher.EnableRaisingEvents = true;
  158. //设置缓存数据
  159. BaseUtil.SetCacheData("FolderPath", FolderPath.Text);
  160. BaseUtil.SetCacheData("BackUpFolderPath", BackUpFolderPath.Text);
  161. BaseUtil.SetCacheData("Master", Master.Text);
  162. BaseUtil.SetCacheData("AutoStart", AutoStart.Checked);
  163. //设置按钮不可点击
  164. StartWatch.Enabled = false;
  165. ChooseFolder.Enabled = false;
  166. Master.Enabled = false;
  167. ma_code.Enabled = false;
  168. ChooseBackUpFolder.Enabled = false;
  169. StopWatch.Enabled = true;
  170. OperateResult.AppendText("开始执行监控\n");
  171. }
  172. public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
  173. {
  174. XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
  175. node.InnerText = value;
  176. parentNode.AppendChild(node);
  177. }
  178. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  179. {
  180. while (true)
  181. {
  182. try
  183. {
  184. using (Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
  185. {
  186. if (stream != null)
  187. break;
  188. }
  189. }
  190. catch (Exception ex)
  191. {
  192. Console.WriteLine(ex.Message);
  193. }
  194. }
  195. string test_date = "";
  196. string test_result = "";
  197. string test_sn = "";
  198. string imageurl = "";
  199. string oMSID = "";
  200. string oErrMessage = "";
  201. XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + e.Name);
  202. OperateResult.AppendText("读取文件" + e.Name + "\n");
  203. //获取采集的项目名称
  204. List<string> badcode = new List<string>();
  205. //获取采集项目的结果
  206. List<string> badlocation = new List<string>();
  207. int code_or_location = 0;
  208. while (myReader.Read())
  209. {
  210. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "test" && myReader.IsStartElement())
  211. {
  212. test_sn = myReader.GetAttribute("test_sn");
  213. test_result = myReader.GetAttribute("test_result");
  214. test_date = myReader.GetAttribute("test_date");
  215. imageurl = myReader.GetAttribute("imgurl");
  216. }
  217. if (myReader.NodeType == XmlNodeType.Text)
  218. {
  219. if (code_or_location % 2 == 0)
  220. {
  221. badcode.Add(myReader.Value);
  222. code_or_location++;
  223. }
  224. else
  225. {
  226. badlocation.Add(myReader.Value);
  227. code_or_location++;
  228. }
  229. }
  230. }
  231. myReader.Close();
  232. //获取文件名的序列号,如SA123456.xml,如果开头为-表示无条码需要自动获取
  233. string sncode = e.Name.Substring(0, 1) == "-" ? "" : e.Name.Split('.')[0];
  234. string makecode = "";
  235. //获取序列号ID最大的工单号,所有序列号都测试为良品
  236. if (sncode == "")
  237. {
  238. //获取资源,线别,工序相等,并且状态在线的序列号进行分配
  239. sql.Clear();
  240. sql.Append("select ms_sncode,ms_makecode from makeserial left join source on sc_code=ms_sccode where ");
  241. sql.Append("ms_linecode='" + ilinecode + "' and ms_nextstepcode='" + istepcode + "' and ms_status=1 order by ms_id");
  242. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  243. if (dt.Rows.Count > 0)
  244. {
  245. makecode = dt.Rows[0]["ms_makecode"].ToString();
  246. sncode = dt.Rows[0]["ms_sncode"].ToString();
  247. }
  248. else
  249. {
  250. OperateResult.AppendText("当前线别在" + istepcode + "无可分配序列号");
  251. return;
  252. }
  253. //-2-NG2017/10/2514:46:29.xml取第二位版号
  254. string combinecode = e.Name.Substring(1, 1);
  255. if (LogicHandler.CheckStepSNAndMacode(ma_code.Text == "" ? makecode : ma_code.Text, isource, sncode, iusercode, out makecode, out oMSID, out oErrMessage))
  256. {
  257. //插入日志
  258. LogicHandler.AutoPassLog(sncode, isource, makecode, test_date, istepcode, ilinecode, e.Name, test_result == "NG" ? "-1" : "0", "-1");
  259. //如果是不良品记录日志,用于测试采集判负
  260. if (test_result == "NG")
  261. {
  262. RecordBadInfo(sncode, makecode, e.Name, combinecode, badcode.ToArray(), badlocation.ToArray());
  263. }
  264. if (!LogicHandler.SetStepResult(makecode, isource, sncode, "自动过站采集", "OK", iusercode, out oErrMessage))
  265. {
  266. OperateResult.AppendText(oErrMessage + "\n");
  267. return;
  268. }
  269. }
  270. else
  271. {
  272. OperateResult.AppendText(oErrMessage + "\n");
  273. return;
  274. }
  275. }
  276. else
  277. {
  278. string status = "";
  279. string errmessage = "";
  280. bool NoteAlready = false;
  281. LogicHandler.GetMakeInfo(sncode, out makecode, out status, out errmessage);
  282. if (ma_code.Text != makecode && makecode != "" && ma_code.Text != "" && status != "2")
  283. {
  284. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号" + sncode + "所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  285. //如果选择不切换赋值当前界面工单
  286. if (ChangeMakeCode != "Yes")
  287. {
  288. makecode = ma_code.Text;
  289. }
  290. else
  291. {
  292. NoteAlready = true;
  293. }
  294. }
  295. if (LogicHandler.CheckStepSNAndMacode(makecode, isource, sncode, iusercode, out makecode, out oMSID, out oErrMessage))
  296. {
  297. if (!NoteAlready)
  298. {
  299. if (makecode != ma_code.Text && ma_code.Text != "")
  300. {
  301. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  302. //如果选择不切换赋值当前界面工单
  303. if (ChangeMakeCode == "Yes")
  304. {
  305. ma_code.Text = makecode;
  306. }
  307. else
  308. {
  309. OperateResult.AppendText(">>请重新采集序列号\n");
  310. return;
  311. }
  312. }
  313. }
  314. //获取序列号ID最大的工单
  315. dt = (DataTable)dh.ExecuteSql("select ms_makecode,ms_id from makeserial where ms_sncode='" + sncode + "' order by ms_id desc", "select");
  316. if (dt.Rows.Count > 0)
  317. {
  318. ma_code.Text = dt.Rows[0]["ms_makecode"].ToString();
  319. makecode = dt.Rows[0]["ms_makecode"].ToString();
  320. switch (test_result)
  321. {
  322. case "OK":
  323. if (!LogicHandler.SetStepResult(makecode, isource, sncode, "自动过站采集", test_result, iusercode, out oErrMessage))
  324. {
  325. OperateResult.AppendText(oErrMessage + "\n");
  326. tipform.startthread(oErrMessage, test_result);
  327. return;
  328. }
  329. tipform.startthread("序列号 " + sncode + "通过检测", test_result);
  330. break;
  331. case "NG":
  332. RecordBadInfo(sncode, makecode, e.Name, "", badcode.ToArray(), badlocation.ToArray());
  333. //所有的序列号均采集为良品
  334. if (!LogicHandler.SetTestNGDetail(sncode, makecode, iusercode, isource, test_result, out oErrMessage))
  335. {
  336. OperateResult.AppendText(oErrMessage + "\n");
  337. tipform.startthread(oErrMessage, test_result);
  338. return;
  339. }
  340. tipform.startthread("序列号 " + sncode + "未通过检测", test_result);
  341. break;
  342. default:
  343. break;
  344. }
  345. LogicHandler.AutoPassLog(sncode, isource, makecode, test_date, istepcode, ilinecode, e.Name, test_result == "NG" ? "1" : "0", "0");
  346. }
  347. }
  348. else
  349. {
  350. OperateResult.AppendText(oErrMessage + "\n");
  351. return;
  352. }
  353. }
  354. FileInfo file = new FileInfo(FolderPath.Text + @"\" + e.Name);
  355. if (file.Exists)
  356. {
  357. try
  358. {
  359. for (int i = 1; i <= 20; i++)
  360. {
  361. if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name))
  362. {
  363. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name);
  364. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  365. break;
  366. }
  367. else if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]))
  368. {
  369. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]);
  370. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  371. break;
  372. }
  373. }
  374. }
  375. catch (Exception ex)
  376. {
  377. OperateResult.AppendText(e.Name + ex.Message + "\n");
  378. }
  379. }
  380. }
  381. /// <summary>
  382. /// 使用进程处理文件,避免界面假死
  383. /// </summary>
  384. private void XmlHandleProcess()
  385. {
  386. }
  387. private void RecordBadInfo(string sncode, string makecode, string filename, string combine, string[] badcode, string[] badlocation)
  388. {
  389. sql.Clear();
  390. sql.Append("insert into makebad(mb_id,mb_makecode,mb_mscode,mb_sncode,mb_inman,mb_indate,mb_stepcode");
  391. sql.Append(",mb_sourcecode,mb_badcode,mb_bgcode,mb_badtable,mb_status,mb_badremark) select makebad_seq.nextval");
  392. sql.Append(",ma_code,ms_code,ms_sncode,'" + iusercode + "',sysdate,'" + istepcode + "','" + isource + "',");
  393. sql.Append(":bc_code,'" + ibadgroup + "','','0',:location from make left join makeSerial on ms_makecode=ma_code ");
  394. sql.Append("where ms_sncode='" + sncode + "' and ms_makecode='" + makecode + "'");
  395. dh.BatchInsert(sql.ToString(), new string[] { "bc_code", "location" }, badcode, badlocation);
  396. //记录判断日志
  397. if (dh.getFieldDataByCondition("source", "sc_scantype", "sc_code='" + isource + "'").ToString() == "REJUCE")
  398. {
  399. LogicHandler.AutoPassJudge(sncode, makecode, isource, filename, ilinecode, combine);
  400. }
  401. }
  402. private void StopWatch_Click(object sender, EventArgs e)
  403. {
  404. XmlWatcher.EnableRaisingEvents = false;
  405. StartWatch.Enabled = true;
  406. ma_code.Enabled = true;
  407. Master.Enabled = true;
  408. ChooseFolder.Enabled = true;
  409. ChooseBackUpFolder.Enabled = true;
  410. StopWatch.Enabled = false;
  411. OperateResult.AppendText("停止执行监控\n");
  412. }
  413. private void Clean_Click(object sender, EventArgs e)
  414. {
  415. OperateResult.Clear();
  416. }
  417. private void ChooseFolder_Click(object sender, EventArgs e)
  418. {
  419. FolderBrowserDialog folder = new FolderBrowserDialog();
  420. folder.Description = "选择监控文件夹";
  421. DialogResult result = folder.ShowDialog();
  422. if (result == DialogResult.OK)
  423. {
  424. FolderPath.Text = folder.SelectedPath;
  425. }
  426. }
  427. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  428. {
  429. FolderBrowserDialog folder = new FolderBrowserDialog();
  430. folder.Description = "选择备份文件夹";
  431. DialogResult result = folder.ShowDialog();
  432. if (result == DialogResult.OK)
  433. {
  434. BackUpFolderPath.Text = folder.SelectedPath;
  435. }
  436. }
  437. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  438. {
  439. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  440. if (ExitConfirm != "Yes")
  441. {
  442. WindowState = FormWindowState.Minimized;
  443. e.Cancel = true;
  444. }
  445. }
  446. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  447. {
  448. SetAutoRun();
  449. }
  450. private void SetAutoRun()
  451. {
  452. if (AutoStart.Checked) //设置开机自启动
  453. {
  454. string path = Application.ExecutablePath;
  455. RegistryKey rk = Registry.LocalMachine;
  456. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  457. rk2.SetValue("UAS自动过站解析器.exe", path);
  458. rk2.Close();
  459. rk.Close();
  460. }
  461. else //取消开机自启动
  462. {
  463. string path = Application.ExecutablePath;
  464. RegistryKey rk = Registry.LocalMachine;
  465. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  466. rk2.DeleteValue("UAS自动过站解析器.exe", false);
  467. rk2.Close();
  468. rk.Close();
  469. }
  470. }
  471. private void BatchAnalysus_Click(object sender, EventArgs e)
  472. {
  473. var AllXmls = Directory.GetFiles(FolderPath.Text, "*.xml");
  474. foreach (var Xml in AllXmls)
  475. {
  476. string fileName = Xml.Substring(Xml.LastIndexOf(@"\")+1, Xml.Length- Xml.LastIndexOf(@"\")-1);
  477. string test_date = "";
  478. string test_result = "";
  479. string test_sn = "";
  480. string imageurl = "";
  481. string oMSID = "";
  482. string oErrMessage = "";
  483. XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + fileName);
  484. OperateResult.AppendText("读取文件" + fileName + "\n");
  485. //获取采集的项目名称
  486. List<string> badcode = new List<string>();
  487. //获取采集项目的结果
  488. List<string> badlocation = new List<string>();
  489. int code_or_location = 0;
  490. while (myReader.Read())
  491. {
  492. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "test" && myReader.IsStartElement())
  493. {
  494. test_sn = myReader.GetAttribute("test_sn");
  495. test_result = myReader.GetAttribute("test_result");
  496. test_date = myReader.GetAttribute("test_date");
  497. imageurl = myReader.GetAttribute("imgurl");
  498. }
  499. if (myReader.NodeType == XmlNodeType.Text)
  500. {
  501. if (code_or_location % 2 == 0)
  502. {
  503. badcode.Add(myReader.Value);
  504. code_or_location++;
  505. }
  506. else
  507. {
  508. badlocation.Add(myReader.Value);
  509. code_or_location++;
  510. }
  511. }
  512. }
  513. myReader.Close();
  514. //获取文件名的序列号,如SA123456.xml,如果开头为-表示无条码需要自动获取
  515. string sncode = fileName.Substring(0, 1) == "-" ? "" : fileName.Split('.')[0];
  516. string makecode = "";
  517. //获取序列号ID最大的工单号,所有序列号都测试为良品
  518. if (sncode == "")
  519. {
  520. //获取资源,线别,工序相等,并且状态在线的序列号进行分配
  521. sql.Clear();
  522. sql.Append("select ms_sncode,ms_makecode from makeserial left join source on sc_code=ms_sccode where ");
  523. sql.Append("ms_linecode='" + ilinecode + "' and ms_nextstepcode='" + istepcode + "' and ms_status=1 order by ms_id");
  524. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  525. if (dt.Rows.Count > 0)
  526. {
  527. makecode = dt.Rows[0]["ms_makecode"].ToString();
  528. sncode = dt.Rows[0]["ms_sncode"].ToString();
  529. }
  530. else
  531. {
  532. OperateResult.AppendText("当前线别在" + istepcode + "无可分配序列号");
  533. continue;
  534. }
  535. //-2-NG2017/10/2514:46:29.xml取第二位版号
  536. string combinecode = fileName.Substring(1, 1);
  537. if (LogicHandler.CheckStepSNAndMacode(ma_code.Text == "" ? makecode : ma_code.Text, isource, sncode, iusercode, out makecode, out oMSID, out oErrMessage))
  538. {
  539. //插入日志
  540. LogicHandler.AutoPassLog(sncode, isource, makecode, test_date, istepcode, ilinecode, fileName, test_result == "NG" ? "-1" : "0", "-1");
  541. //如果是不良品记录日志,用于测试采集判负
  542. if (test_result == "NG")
  543. {
  544. RecordBadInfo(sncode, makecode, fileName, combinecode, badcode.ToArray(), badlocation.ToArray());
  545. }
  546. if (!LogicHandler.SetStepResult(makecode, isource, sncode, "自动过站采集", "OK", iusercode, out oErrMessage))
  547. {
  548. OperateResult.AppendText(oErrMessage + "\n");
  549. continue;
  550. }
  551. }
  552. else
  553. {
  554. OperateResult.AppendText(oErrMessage + "\n");
  555. continue;
  556. }
  557. }
  558. else
  559. {
  560. string status = "";
  561. string errmessage = "";
  562. bool NoteAlready = false;
  563. LogicHandler.GetMakeInfo(sncode, out makecode, out status, out errmessage);
  564. if (ma_code.Text != makecode && makecode != "" && ma_code.Text != "" && status != "2")
  565. {
  566. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号" + sncode + "所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  567. //如果选择不切换赋值当前界面工单
  568. if (ChangeMakeCode != "Yes")
  569. {
  570. makecode = ma_code.Text;
  571. }
  572. else
  573. {
  574. NoteAlready = true;
  575. }
  576. }
  577. if (LogicHandler.CheckStepSNAndMacode(makecode, isource, sncode, iusercode, out makecode, out oMSID, out oErrMessage))
  578. {
  579. if (!NoteAlready)
  580. {
  581. if (makecode != ma_code.Text && ma_code.Text != "")
  582. {
  583. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  584. //如果选择不切换赋值当前界面工单
  585. if (ChangeMakeCode == "Yes")
  586. {
  587. ma_code.Text = makecode;
  588. }
  589. else
  590. {
  591. OperateResult.AppendText(">>请重新采集序列号\n");
  592. continue;
  593. }
  594. }
  595. }
  596. //获取序列号ID最大的工单
  597. dt = (DataTable)dh.ExecuteSql("select ms_makecode,ms_id from makeserial where ms_sncode='" + sncode + "' order by ms_id desc", "select");
  598. if (dt.Rows.Count > 0)
  599. {
  600. ma_code.Text = dt.Rows[0]["ms_makecode"].ToString();
  601. makecode = dt.Rows[0]["ms_makecode"].ToString();
  602. switch (test_result)
  603. {
  604. case "OK":
  605. if (!LogicHandler.SetStepResult(makecode, isource, sncode, "自动过站采集", test_result, iusercode, out oErrMessage))
  606. {
  607. OperateResult.AppendText(oErrMessage + "\n");
  608. continue;
  609. }
  610. break;
  611. case "NG":
  612. RecordBadInfo(sncode, makecode, fileName, "", badcode.ToArray(), badlocation.ToArray());
  613. //所有的序列号均采集为良品
  614. if (!LogicHandler.SetTestNGDetail(sncode, makecode, iusercode, isource, test_result, out oErrMessage))
  615. {
  616. OperateResult.AppendText(oErrMessage + "\n");
  617. continue;
  618. }
  619. break;
  620. default:
  621. break;
  622. }
  623. LogicHandler.AutoPassLog(sncode, isource, makecode, test_date, istepcode, ilinecode, fileName, test_result == "NG" ? "1" : "0", "0");
  624. }
  625. }
  626. else
  627. {
  628. OperateResult.AppendText(oErrMessage + "\n");
  629. continue;
  630. }
  631. }
  632. FileInfo file = new FileInfo(FolderPath.Text + @"\" + fileName);
  633. if (file.Exists)
  634. {
  635. try
  636. {
  637. for (int i = 1; i <= 20; i++)
  638. {
  639. if (!File.Exists(BackUpFolderPath.Text + @"\" + fileName))
  640. {
  641. file.MoveTo(BackUpFolderPath.Text + @"\" + fileName);
  642. OperateResult.AppendText("成功解析文件" + fileName + "\n");
  643. break;
  644. }
  645. else if (!File.Exists(BackUpFolderPath.Text + @"\" + fileName.Split('.')[0] + "(" + i + ")" + "." + fileName.Split('.')[1]))
  646. {
  647. file.MoveTo(BackUpFolderPath.Text + @"\" + fileName.Split('.')[0] + "(" + i + ")" + "." + fileName.Split('.')[1]);
  648. OperateResult.AppendText("成功解析文件" + fileName + "\n");
  649. break;
  650. }
  651. }
  652. }
  653. catch (Exception ex)
  654. {
  655. OperateResult.AppendText(fileName + ex.Message + "\n");
  656. }
  657. }
  658. }
  659. }
  660. }
  661. }