AutoAnalysisXml.cs 31 KB

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