AutoAnalysisXml.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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.ToUpper();
  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 = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=" + DB.Rows[i]["ma_user"].ToString() + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + DB.Rows[i]["ma_inneraddress"].ToString() + ")(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  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 = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=" + DB.Rows[i]["ma_user"].ToString() + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + DB.Rows[i]["ma_inneraddress"].ToString() + ")(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  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("sc_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. tipform.startthread("当前线别在" + istepcode + "无可分配序列号", "NG");
  252. return;
  253. }
  254. //-2-NG2017/10/2514:46:29.xml取第二位版号
  255. string combinecode = e.Name.Substring(1, 1);
  256. if (LogicHandler.CheckStepSNAndMacode(ma_code.Text == "" ? makecode : ma_code.Text, isource, sncode, iusercode, out makecode, out oMSID, out oErrMessage))
  257. {
  258. //插入日志
  259. LogicHandler.AutoPassLog(sncode, isource, makecode, test_date, istepcode, ilinecode, e.Name, test_result == "NG" ? "-1" : "0", "-1");
  260. //如果是不良品记录日志,用于测试采集判负
  261. if (test_result == "NG")
  262. {
  263. RecordBadInfo(sncode, makecode, e.Name, combinecode, badcode.ToArray(), badlocation.ToArray());
  264. }
  265. if (!LogicHandler.SetStepResult(makecode, isource, sncode, "自动过站采集", "OK", iusercode, out oErrMessage))
  266. {
  267. OperateResult.AppendText(oErrMessage + "\n");
  268. tipform.startthread(oErrMessage, "NG");
  269. return;
  270. }
  271. tipform.startthread("序列号 " + sncode + "通过检测", test_result);
  272. }
  273. else
  274. {
  275. OperateResult.AppendText(oErrMessage + "\n");
  276. tipform.startthread(oErrMessage, "NG");
  277. return;
  278. }
  279. }
  280. else
  281. {
  282. string status = "";
  283. string errmessage = "";
  284. bool NoteAlready = false;
  285. LogicHandler.GetMakeInfo(sncode, out makecode, out status, out errmessage);
  286. if (ma_code.Text != makecode && makecode != "" && ma_code.Text != "" && status != "2")
  287. {
  288. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号" + sncode + "所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  289. //如果选择不切换赋值当前界面工单
  290. if (ChangeMakeCode != "Yes")
  291. {
  292. makecode = ma_code.Text;
  293. }
  294. else
  295. {
  296. NoteAlready = true;
  297. }
  298. }
  299. if (LogicHandler.CheckStepSNAndMacode(makecode, isource, sncode, iusercode, out makecode, out oMSID, out oErrMessage))
  300. {
  301. if (!NoteAlready)
  302. {
  303. if (makecode != ma_code.Text && ma_code.Text != "")
  304. {
  305. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  306. //如果选择不切换赋值当前界面工单
  307. if (ChangeMakeCode == "Yes")
  308. {
  309. ma_code.Text = makecode;
  310. }
  311. else
  312. {
  313. OperateResult.AppendText(">>请重新采集序列号\n");
  314. tipform.startthread("请重新采集序列号", test_result);
  315. return;
  316. }
  317. }
  318. }
  319. //获取序列号ID最大的工单
  320. dt = (DataTable)dh.ExecuteSql("select ms_makecode,ms_id from makeserial where ms_sncode='" + sncode + "' order by ms_id desc", "select");
  321. if (dt.Rows.Count > 0)
  322. {
  323. ma_code.Text = dt.Rows[0]["ms_makecode"].ToString();
  324. makecode = dt.Rows[0]["ms_makecode"].ToString();
  325. switch (test_result)
  326. {
  327. case "OK":
  328. if (!LogicHandler.SetStepResult(makecode, isource, sncode, "自动过站采集", test_result, iusercode, out oErrMessage))
  329. {
  330. OperateResult.AppendText(oErrMessage + "\n");
  331. tipform.startthread(oErrMessage, test_result);
  332. return;
  333. }
  334. tipform.startthread("序列号 " + sncode + "通过检测", test_result);
  335. break;
  336. case "NG":
  337. RecordBadInfo(sncode, makecode, e.Name, "", badcode.ToArray(), badlocation.ToArray());
  338. //所有的序列号均采集为良品
  339. if (!LogicHandler.SetTestNGDetail(sncode, makecode, iusercode, isource, test_result, out oErrMessage))
  340. {
  341. OperateResult.AppendText(oErrMessage + "\n");
  342. tipform.startthread(oErrMessage, test_result);
  343. return;
  344. }
  345. tipform.startthread("序列号 " + sncode + "未通过检测", test_result);
  346. break;
  347. default:
  348. break;
  349. }
  350. LogicHandler.AutoPassLog(sncode, isource, makecode, test_date, istepcode, ilinecode, e.Name, test_result == "NG" ? "1" : "0", "0");
  351. }
  352. }
  353. else
  354. {
  355. OperateResult.AppendText(oErrMessage + "\n");
  356. tipform.startthread(oErrMessage, "NG");
  357. return;
  358. }
  359. }
  360. FileInfo file = new FileInfo(FolderPath.Text + @"\" + e.Name);
  361. if (file.Exists)
  362. {
  363. try
  364. {
  365. for (int i = 1; i <= 20; i++)
  366. {
  367. if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name))
  368. {
  369. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name);
  370. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  371. break;
  372. }
  373. else if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]))
  374. {
  375. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]);
  376. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  377. break;
  378. }
  379. }
  380. }
  381. catch (Exception ex)
  382. {
  383. OperateResult.AppendText(e.Name + ex.Message + "\n");
  384. }
  385. }
  386. }
  387. /// <summary>
  388. /// 使用进程处理文件,避免界面假死
  389. /// </summary>
  390. private void XmlHandleProcess()
  391. {
  392. }
  393. private void RecordBadInfo(string sncode, string makecode, string filename, string combine, string[] badcode, string[] badlocation)
  394. {
  395. string[] ID = dh.GetSEQ("makebad_seq", badcode.Length);
  396. sql.Clear();
  397. sql.Append("insert into makebad(mb_id,mb_makecode,mb_mscode,mb_sncode,mb_inman,mb_indate,mb_stepcode");
  398. sql.Append(",mb_sourcecode,mb_badcode,mb_bgcode,mb_badtable,mb_status,mb_badremark,mb_stauts) select :ID");
  399. sql.Append(",ma_code,ms_code,ms_sncode,'" + iusercode + "',sysdate,'" + istepcode + "','" + isource + "',");
  400. sql.Append(":bc_code,'" + ibadgroup + "','','0',:location,'-1' from make left join makeSerial on ms_makecode=ma_code ");
  401. sql.Append("where ms_sncode='" + sncode + "' and ms_makecode='" + makecode + "'");
  402. dh.BatchInsert(sql.ToString(), new string[] {"ID", "bc_code", "location" }, ID, badcode, badlocation);
  403. sql.Clear();
  404. sql.Append("update makebad set mb_badname=(select DISTINCT bc_name from badcode where bc_code=mb_badcode),mb_bgname=(select DISTINCT bg_name from badgroup where bg_code=mb_bgcode) ");
  405. sql.Append("where mb_id=:ID");
  406. dh.BatchInsert(sql.ToString(), new string[] { "ID" }, ID);
  407. //记录判断日志
  408. if (dh.getFieldDataByCondition("source", "sc_scantype", "sc_code='" + isource + "'").ToString() == "REJUCE")
  409. {
  410. LogicHandler.AutoPassJudge(sncode, makecode, isource, filename, ilinecode, combine);
  411. }
  412. }
  413. private void StopWatch_Click(object sender, EventArgs e)
  414. {
  415. XmlWatcher.EnableRaisingEvents = false;
  416. StartWatch.Enabled = true;
  417. ma_code.Enabled = true;
  418. Master.Enabled = true;
  419. ChooseFolder.Enabled = true;
  420. ChooseBackUpFolder.Enabled = true;
  421. StopWatch.Enabled = false;
  422. OperateResult.AppendText("停止执行监控\n");
  423. }
  424. private void Clean_Click(object sender, EventArgs e)
  425. {
  426. OperateResult.Clear();
  427. }
  428. private void ChooseFolder_Click(object sender, EventArgs e)
  429. {
  430. FolderBrowserDialog folder = new FolderBrowserDialog();
  431. folder.Description = "选择监控文件夹";
  432. DialogResult result = folder.ShowDialog();
  433. if (result == DialogResult.OK)
  434. {
  435. FolderPath.Text = folder.SelectedPath;
  436. }
  437. }
  438. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  439. {
  440. FolderBrowserDialog folder = new FolderBrowserDialog();
  441. folder.Description = "选择备份文件夹";
  442. DialogResult result = folder.ShowDialog();
  443. if (result == DialogResult.OK)
  444. {
  445. BackUpFolderPath.Text = folder.SelectedPath;
  446. }
  447. }
  448. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  449. {
  450. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  451. if (ExitConfirm != "Yes")
  452. {
  453. WindowState = FormWindowState.Minimized;
  454. e.Cancel = true;
  455. }
  456. }
  457. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  458. {
  459. SetAutoRun();
  460. }
  461. private void SetAutoRun()
  462. {
  463. if (AutoStart.Checked) //设置开机自启动
  464. {
  465. string path = Application.ExecutablePath;
  466. RegistryKey rk = Registry.LocalMachine;
  467. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  468. rk2.SetValue("UAS自动过站解析器.exe", path);
  469. rk2.Close();
  470. rk.Close();
  471. }
  472. else //取消开机自启动
  473. {
  474. string path = Application.ExecutablePath;
  475. RegistryKey rk = Registry.LocalMachine;
  476. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  477. rk2.DeleteValue("UAS自动过站解析器.exe", false);
  478. rk2.Close();
  479. rk.Close();
  480. }
  481. }
  482. private void BatchAnalysus_Click(object sender, EventArgs e)
  483. {
  484. var AllXmls = Directory.GetFiles(FolderPath.Text, "*.xml");
  485. foreach (var Xml in AllXmls)
  486. {
  487. string fileName = Xml.Substring(Xml.LastIndexOf(@"\")+1, Xml.Length- Xml.LastIndexOf(@"\")-1);
  488. string test_date = "";
  489. string test_result = "";
  490. string test_sn = "";
  491. string imageurl = "";
  492. string oMSID = "";
  493. string oErrMessage = "";
  494. XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + fileName);
  495. OperateResult.AppendText("读取文件" + fileName + "\n");
  496. //获取采集的项目名称
  497. List<string> badcode = new List<string>();
  498. //获取采集项目的结果
  499. List<string> badlocation = new List<string>();
  500. int code_or_location = 0;
  501. while (myReader.Read())
  502. {
  503. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "test" && myReader.IsStartElement())
  504. {
  505. test_sn = myReader.GetAttribute("test_sn");
  506. test_result = myReader.GetAttribute("test_result");
  507. test_date = myReader.GetAttribute("test_date");
  508. imageurl = myReader.GetAttribute("imgurl");
  509. }
  510. if (myReader.NodeType == XmlNodeType.Text)
  511. {
  512. if (code_or_location % 2 == 0)
  513. {
  514. badcode.Add(myReader.Value);
  515. code_or_location++;
  516. }
  517. else
  518. {
  519. badlocation.Add(myReader.Value);
  520. code_or_location++;
  521. }
  522. }
  523. }
  524. myReader.Close();
  525. //获取文件名的序列号,如SA123456.xml,如果开头为-表示无条码需要自动获取
  526. string sncode = fileName.Substring(0, 1) == "-" ? "" : fileName.Split('.')[0];
  527. string makecode = "";
  528. //获取序列号ID最大的工单号,所有序列号都测试为良品
  529. if (sncode == "")
  530. {
  531. //获取资源,线别,工序相等,并且状态在线的序列号进行分配
  532. sql.Clear();
  533. sql.Append("select ms_sncode,ms_makecode from makeserial left join source on sc_code=ms_sccode where ");
  534. sql.Append(" sc_linecode='" + ilinecode + "' and ms_nextstepcode='" + istepcode + "' and ms_status=1 order by ms_id");
  535. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  536. if (dt.Rows.Count > 0)
  537. {
  538. makecode = dt.Rows[0]["ms_makecode"].ToString();
  539. sncode = dt.Rows[0]["ms_sncode"].ToString();
  540. }
  541. else
  542. {
  543. OperateResult.AppendText("当前线别在" + istepcode + "无可分配序列号");
  544. continue;
  545. }
  546. //-2-NG2017/10/2514:46:29.xml取第二位版号
  547. string combinecode = fileName.Substring(1, 1);
  548. if (LogicHandler.CheckStepSNAndMacode(ma_code.Text == "" ? makecode : ma_code.Text, isource, sncode, iusercode, out makecode, out oMSID, out oErrMessage))
  549. {
  550. //插入日志
  551. LogicHandler.AutoPassLog(sncode, isource, makecode, test_date, istepcode, ilinecode, fileName, test_result == "NG" ? "-1" : "0", "-1");
  552. //如果是不良品记录日志,用于测试采集判负
  553. if (test_result == "NG")
  554. {
  555. RecordBadInfo(sncode, makecode, fileName, combinecode, badcode.ToArray(), badlocation.ToArray());
  556. }
  557. if (!LogicHandler.SetStepResult(makecode, isource, sncode, "自动过站采集", "OK", iusercode, out oErrMessage))
  558. {
  559. OperateResult.AppendText(oErrMessage + "\n");
  560. continue;
  561. }
  562. }
  563. else
  564. {
  565. OperateResult.AppendText(oErrMessage + "\n");
  566. continue;
  567. }
  568. }
  569. else
  570. {
  571. string status = "";
  572. string errmessage = "";
  573. bool NoteAlready = false;
  574. LogicHandler.GetMakeInfo(sncode, out makecode, out status, out errmessage);
  575. if (ma_code.Text != makecode && makecode != "" && ma_code.Text != "" && status != "2")
  576. {
  577. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号" + sncode + "所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  578. //如果选择不切换赋值当前界面工单
  579. if (ChangeMakeCode != "Yes")
  580. {
  581. makecode = ma_code.Text;
  582. }
  583. else
  584. {
  585. NoteAlready = true;
  586. }
  587. }
  588. if (LogicHandler.CheckStepSNAndMacode(makecode, isource, sncode, iusercode, out makecode, out oMSID, out oErrMessage))
  589. {
  590. if (!NoteAlready)
  591. {
  592. if (makecode != ma_code.Text && ma_code.Text != "")
  593. {
  594. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  595. //如果选择不切换赋值当前界面工单
  596. if (ChangeMakeCode == "Yes")
  597. {
  598. ma_code.Text = makecode;
  599. }
  600. else
  601. {
  602. OperateResult.AppendText(">>请重新采集序列号\n");
  603. continue;
  604. }
  605. }
  606. }
  607. //获取序列号ID最大的工单
  608. dt = (DataTable)dh.ExecuteSql("select ms_makecode,ms_id from makeserial where ms_sncode='" + sncode + "' order by ms_id desc", "select");
  609. if (dt.Rows.Count > 0)
  610. {
  611. ma_code.Text = dt.Rows[0]["ms_makecode"].ToString();
  612. makecode = dt.Rows[0]["ms_makecode"].ToString();
  613. switch (test_result)
  614. {
  615. case "OK":
  616. if (!LogicHandler.SetStepResult(makecode, isource, sncode, "自动过站采集", test_result, iusercode, out oErrMessage))
  617. {
  618. OperateResult.AppendText(oErrMessage + "\n");
  619. continue;
  620. }
  621. break;
  622. case "NG":
  623. RecordBadInfo(sncode, makecode, fileName, "", badcode.ToArray(), badlocation.ToArray());
  624. //所有的序列号均采集为良品
  625. if (!LogicHandler.SetTestNGDetail(sncode, makecode, iusercode, isource, test_result, out oErrMessage))
  626. {
  627. OperateResult.AppendText(oErrMessage + "\n");
  628. continue;
  629. }
  630. break;
  631. default:
  632. break;
  633. }
  634. LogicHandler.AutoPassLog(sncode, isource, makecode, test_date, istepcode, ilinecode, fileName, test_result == "NG" ? "1" : "0", "0");
  635. }
  636. }
  637. else
  638. {
  639. OperateResult.AppendText(oErrMessage + "\n");
  640. continue;
  641. }
  642. }
  643. FileInfo file = new FileInfo(FolderPath.Text + @"\" + fileName);
  644. if (file.Exists)
  645. {
  646. try
  647. {
  648. for (int i = 1; i <= 20; i++)
  649. {
  650. if (!File.Exists(BackUpFolderPath.Text + @"\" + fileName))
  651. {
  652. file.MoveTo(BackUpFolderPath.Text + @"\" + fileName);
  653. OperateResult.AppendText("成功解析文件" + fileName + "\n");
  654. break;
  655. }
  656. else if (!File.Exists(BackUpFolderPath.Text + @"\" + fileName.Split('.')[0] + "(" + i + ")" + "." + fileName.Split('.')[1]))
  657. {
  658. file.MoveTo(BackUpFolderPath.Text + @"\" + fileName.Split('.')[0] + "(" + i + ")" + "." + fileName.Split('.')[1]);
  659. OperateResult.AppendText("成功解析文件" + fileName + "\n");
  660. break;
  661. }
  662. }
  663. }
  664. catch (Exception ex)
  665. {
  666. OperateResult.AppendText(fileName + ex.Message + "\n");
  667. }
  668. }
  669. }
  670. }
  671. }
  672. }