AutoAnalysisXml.cs 32 KB

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