AutoAnalysisXml.cs 31 KB

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