SelectDataViewModel.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using Aitex.Core.RT.Log;
  9. using MECF.Framework.Common.DataCenter;
  10. using MECF.Framework.Common.Utilities;
  11. using MECF.Framework.UI.Client.ClientBase;
  12. using MECF.Framework.UI.Client.ClientBase.Tree;
  13. using OpenSEMI.ClientBase;
  14. namespace MECF.Framework.UI.Client.CenterViews.DataLogs.ProcessHistory
  15. {
  16. public class SelectDataViewModel : UiViewModelBase
  17. {
  18. #region
  19. private const int MAX_PARAMETERS = 20;
  20. public DateTime BeginDate { get; set; }
  21. public DateTime StartDateTime { get; set; }
  22. public DateTime EndDateTime { get; set; }
  23. public ObservableCollection<RecipeItem> Recipes { get; set; }
  24. public ObservableCollection<RecipeItem> SelectedRecipes { get; set; }
  25. private ObservableCollection<TreeNode> _ParameterNodes;
  26. public ObservableCollection<TreeNode> ParameterNodes
  27. {
  28. get => _ParameterNodes;
  29. set { _ParameterNodes = value; NotifyOfPropertyChange(nameof(ParameterNodes)); }
  30. }
  31. public ObservableCollection<string> SelectedParameters { get; set; }
  32. object _lockSelection = new object();
  33. public ObservableCollection<string> SourcePM { get; set; }
  34. public string SelectedValuePM { get; set; }
  35. public ObservableCollection<string> SourceSLot { get; set; }
  36. public string SelectedValueSLot { get; set; }
  37. public string RecipeName { get; set; }
  38. public string LotID { get; set; }
  39. public bool CheckAllRecipeFlag = false;
  40. public ObservableCollection<TreeNode> ParameterNodes1 = new ObservableCollection<TreeNode>();
  41. #endregion
  42. #region Popup window
  43. public SelectDataViewModel()
  44. {
  45. var now = DateTime.Now;
  46. this.StartDateTime = now;
  47. this.BeginDate = now;
  48. this.StartDateTime = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, 0);
  49. this.EndDateTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999);
  50. Recipes = new ObservableCollection<RecipeItem>();
  51. SelectedRecipes = new ObservableCollection<RecipeItem>();
  52. SelectedParameters = new ObservableCollection<string>();
  53. ParameterNodes = new ObservableCollection<TreeNode>(ProcessHistoryProvider.Instance.GetTreeNodeParameters());
  54. SourcePM = new ObservableCollection<string>(new[] { "PM1", "PM2", "PM3", "PM4", "PM5" });
  55. SourceSLot = new ObservableCollection<string>();
  56. for (int i = 0; i < 25; i++)
  57. {
  58. SourceSLot.Add((i + 1).ToString());
  59. }
  60. }
  61. protected override void OnViewLoaded(object _view)
  62. {
  63. base.OnViewLoaded(_view);
  64. this.view = (SelectDataView)_view;
  65. this.view.wfTimeFrom.Value = this.StartDateTime;
  66. this.view.wfTimeTo.Value = this.EndDateTime;
  67. }
  68. protected override void OnActivate()
  69. {
  70. base.OnActivate();
  71. ParameterNodes = new ObservableCollection<TreeNode>(ProcessHistoryProvider.Instance.GetTreeNodeParameters());
  72. foreach (var recipeItem in Recipes)
  73. {
  74. recipeItem.Selected = false;
  75. }
  76. }
  77. private SelectDataView view;
  78. public void SearchRecipe()
  79. {
  80. Recipes.Clear();
  81. //ObservableCollection<RecipeItem> items = ProcessHistoryProvider.Instance.SearchRecipe();
  82. //foreach (RecipeItem item in items)
  83. // Recipes.Add(item);
  84. this.StartDateTime = this.view.wfTimeFrom.Value;
  85. this.EndDateTime = this.view.wfTimeTo.Value;
  86. if (StartDateTime > EndDateTime)
  87. {
  88. MessageBox.Show("Time range invalid, start time should be early than end time");
  89. return;
  90. }
  91. try
  92. {
  93. //string sql = $"SELECT * FROM \"process_data\",\"cj_data\",\"pj_data\",\"wafer_data\" where \"process_data\".\"wafer_data_guid\"=\"wafer_data\".\"guid\"" +
  94. //$" and \"wafer_data\".\"pj_data_guid\" =\"pj_data\".\"guid\" and \"cj_data\".\"guid\"=\"pj_data\".\"cj_data_guid\" and \"process_data\".\"process_begin_time\" >='{StartDateTime:yyyyMMdd HHmmss}' and \"process_data\".\"process_end_time\" <='{EndDateTime:yyyyMMdd HHmmss}'";
  95. string sql = $"SELECT * FROM \"process_data\",\"wafer_data\"" +
  96. $"left join \"pj_data\" on \"wafer_data\".\"pj_data_guid\" =\"pj_data\".\"guid\"" +
  97. $"left join \"cj_data\" on \"cj_data\".\"guid\"=\"pj_data\".\"cj_data_guid\"" +
  98. $"left join \"carrier_data\" on \"wafer_data\".\"carrier_data_guid\"=\"carrier_data\".\"guid\"" +
  99. $"where \"process_data\".\"wafer_data_guid\"=\"wafer_data\".\"guid\" and \"process_data\".\"process_begin_time\" >='{StartDateTime:yyyyMMdd HHmmss}' and \"process_data\".\"process_end_time\" <='{EndDateTime:yyyyMMdd HHmmss}'";
  100. if (!string.IsNullOrEmpty(SelectedValuePM))
  101. {
  102. string[] pms = SelectedValuePM.Split(',');
  103. if (pms.Length > 0)
  104. {
  105. sql += " and (FALSE ";
  106. foreach (var pm in pms)
  107. {
  108. sql += $" OR \"process_data\".\"process_in\"='{pm}' ";
  109. }
  110. sql += " ) ";
  111. }
  112. ParameterNodes = new ObservableCollection<TreeNode>(ProcessHistoryProvider.Instance.GetTreeNodeParameters());
  113. bool pm1 = ((System.Collections.IList)pms).Contains("PMA") || ((System.Collections.IList)pms).Contains("PM1");//True
  114. bool pm2 = ((System.Collections.IList)pms).Contains("PMB") || ((System.Collections.IList)pms).Contains("PM2");//True
  115. bool pm3 = ((System.Collections.IList)pms).Contains("PMC") || ((System.Collections.IList)pms).Contains("PM3");//True
  116. bool pm4 = ((System.Collections.IList)pms).Contains("PMD") || ((System.Collections.IList)pms).Contains("PM4");//True
  117. bool pm5 = ((System.Collections.IList)pms).Contains("PME") || ((System.Collections.IList)pms).Contains("PM5");//True
  118. ParameterNodes1.Clear();
  119. if (pm1)
  120. ParameterNodes1.Add(ParameterNodes[0]);
  121. if (pm2)
  122. ParameterNodes1.Add(ParameterNodes[1]);
  123. if (pm3)
  124. ParameterNodes1.Add(ParameterNodes[2]);
  125. if (pm4)
  126. ParameterNodes1.Add(ParameterNodes[3]);
  127. if (pm5)
  128. ParameterNodes1.Add(ParameterNodes[4]);
  129. ParameterNodes.Clear();
  130. ParameterNodes = ParameterNodes1;
  131. }
  132. else
  133. {
  134. ParameterNodes = new ObservableCollection<TreeNode>(ProcessHistoryProvider.Instance.GetTreeNodeParameters());
  135. }
  136. if (!string.IsNullOrEmpty(RecipeName))
  137. {
  138. sql += string.Format(" and lower( \"process_data\".\"recipe_name\") like '%{0}%'", RecipeName.ToLower());
  139. }
  140. if (!string.IsNullOrEmpty(LotID))
  141. {
  142. sql += string.Format(" and lower(\"wafer_data\".\"lot_id\") like '%{0}%'", LotID.ToLower());
  143. }
  144. if (!string.IsNullOrEmpty(SelectedValueSLot))
  145. {
  146. string[] pms = SelectedValueSLot.Split(',');
  147. if (pms.Length > 0)
  148. {
  149. sql += " and (FALSE ";
  150. foreach (var pm in pms)
  151. {
  152. sql += $" OR \"wafer_data\".\"create_slot\"='{pm}' ";
  153. }
  154. sql += " ) ";
  155. }
  156. }
  157. sql += " order by \"process_data\".\"process_begin_time\" ASC;";
  158. DataTable dbData = QueryDataClient.Instance.Service.QueryData(sql);
  159. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  160. {
  161. if (dbData == null || dbData.Rows.Count == 0)
  162. return;
  163. for (int i = 0; i < dbData.Rows.Count; i++)
  164. {
  165. RecipeItem item = new RecipeItem();
  166. item.GUID = dbData.Rows[i]["guid"].ToString();
  167. item.Recipe = dbData.Rows[i]["recipe_name"].ToString();
  168. item.LotID = dbData.Rows[i]["lot_id"].ToString();
  169. item.SlotID = dbData.Rows[i]["create_slot"].ToString();
  170. item.CjName = dbData.Rows[i]["name"].ToString();
  171. item.PjName = dbData.Rows[i]["name1"].ToString();
  172. item.Chamber = dbData.Rows[i]["process_in"].ToString();
  173. item.Status = dbData.Rows[i]["process_status"].ToString();
  174. if (!dbData.Rows[i]["process_begin_time"].Equals(DBNull.Value))
  175. item.StartTime = ((DateTime)dbData.Rows[i]["process_begin_time"]).ToString("yyyy/MM/dd HH:mm:ss.fff");
  176. if (!dbData.Rows[i]["process_end_time"].Equals(DBNull.Value))
  177. item.EndTime = ((DateTime)dbData.Rows[i]["process_end_time"]).ToString("yyyy/MM/dd HH:mm:ss.fff");
  178. Recipes.Add(item);
  179. }
  180. }));
  181. }
  182. catch (Exception e)
  183. {
  184. LOG.Write(e);
  185. }
  186. }
  187. public void CheckAllRecipe()
  188. {
  189. SelectedRecipes.Clear();
  190. CheckAllRecipeFlag = !CheckAllRecipeFlag;
  191. if (CheckAllRecipeFlag)
  192. {
  193. foreach (var recipe in Recipes)
  194. {
  195. recipe.Selected = true;
  196. SelectedRecipes.Add(recipe);
  197. }
  198. }
  199. else
  200. {
  201. foreach (var recipe in Recipes)
  202. {
  203. recipe.Selected = false;
  204. }
  205. }
  206. }
  207. public void CheckRecipe(RecipeItem recipe)
  208. {
  209. if (recipe.Selected)
  210. {
  211. SelectedRecipes.Add(recipe);
  212. }
  213. else
  214. {
  215. RecipeItem item = SelectedRecipes.FirstOrDefault(t => t.Recipe == recipe.Recipe);
  216. if (item != null)
  217. SelectedRecipes.Remove(item);
  218. }
  219. }
  220. public void Preset()
  221. {
  222. }
  223. public void UnSelect()
  224. {
  225. }
  226. //public void ParameterCheck(TreeNode node)
  227. //{
  228. // bool result = RefreshTreeStatusToChild(node);
  229. // if (!result)
  230. // {
  231. // node.Selected = !node.Selected;
  232. // DialogBox.ShowWarning($"The max number of parameters is {MAX_PARAMETERS}.");
  233. // }
  234. // else
  235. // {
  236. // RefreshTreeStatusToParent(node);
  237. // }
  238. //}
  239. ///// <summary>
  240. ///// Refresh tree node status from current to children, and add data to SelectedData
  241. ///// </summary>
  242. //private bool RefreshTreeStatusToChild(TreeNode node)
  243. //{
  244. // if (node.ChildNodes.Count > 0)
  245. // {
  246. // for (int i = 0; i < node.ChildNodes.Count; i++)
  247. // {
  248. // TreeNode n = node.ChildNodes[i];
  249. // n.Selected = node.Selected;
  250. // if (!RefreshTreeStatusToChild(n))
  251. // {
  252. // //uncheck left node
  253. // for (int j = i; j < node.ChildNodes.Count; j++)
  254. // {
  255. // node.ChildNodes[j].Selected = !node.Selected;
  256. // }
  257. // node.Selected = !node.Selected;
  258. // return false;
  259. // }
  260. // }
  261. // }
  262. // else //leaf node
  263. // {
  264. // lock (_lockSelection)
  265. // {
  266. // bool flag = SelectedParameters.Contains(node.Name);
  267. // if (node.Selected && !flag)
  268. // {
  269. // if (SelectedParameters.Count < MAX_PARAMETERS)
  270. // {
  271. // SelectedParameters.Add(node.Name);
  272. // }
  273. // else
  274. // {
  275. // return false;
  276. // }
  277. // }
  278. // else if (!node.Selected && flag)
  279. // {
  280. // SelectedParameters.Remove(node.Name);
  281. // }
  282. // }
  283. // }
  284. // return true;
  285. //}
  286. ///// <summary>
  287. ///// Refresh tree node status from current to parent
  288. ///// </summary>
  289. ///// <param name="node"></param>
  290. ///// <returns></returns>
  291. //private void RefreshTreeStatusToParent(TreeNode node)
  292. //{
  293. // if (node.ParentNode != null)
  294. // {
  295. // if (node.Selected)
  296. // {
  297. // bool flag = true;
  298. // for (int i = 0; i < node.ParentNode.ChildNodes.Count; i++)
  299. // {
  300. // if (!node.ParentNode.ChildNodes[i].Selected)
  301. // {
  302. // flag = false; //as least one child is unselected
  303. // break;
  304. // }
  305. // }
  306. // if (flag)
  307. // node.ParentNode.Selected = true;
  308. // }
  309. // else
  310. // {
  311. // node.ParentNode.Selected = false;
  312. // }
  313. // RefreshTreeStatusToParent(node.ParentNode);
  314. // }
  315. //}
  316. public void OnTreeSelectedChanged(object obj)
  317. {
  318. }
  319. public void OK()
  320. {
  321. this.TryClose(true);
  322. }
  323. public void Cancel()
  324. {
  325. this.TryClose(false);
  326. }
  327. //public void SelectColor(ChartParameter cp)
  328. //{
  329. // if (cp == null)
  330. // return;
  331. // var dlg = new System.Windows.Forms.ColorDialog();
  332. // if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  333. // {
  334. // var newColor = new System.Windows.Media.Color() { A = dlg.Color.A, B = dlg.Color.B, G = dlg.Color.G, R = dlg.Color.R };
  335. // cp.Color = new SolidColorBrush(newColor);
  336. // }
  337. //}
  338. public bool GetDbData(string chamber, DateTime from, DateTime to, IEnumerable<string> dataIdList,
  339. out Dictionary<string, List<Tuple<DateTime, double>>> returnDatas, ref double min, ref double max)
  340. {
  341. returnDatas = new Dictionary<string, List<Tuple<DateTime, double>>>();
  342. for (DateTime dfrom = new DateTime(from.Year, from.Month, from.Day); dfrom < to; dfrom += new TimeSpan(1, 0, 0, 0))
  343. {
  344. DateTime begin = (dfrom.Year == from.Year && dfrom.Month == from.Month && dfrom.Day == from.Day) ? from : new DateTime(dfrom.Year, dfrom.Month, dfrom.Day, 0, 0, 0, 0);
  345. DateTime end = (dfrom.Date == to.Date) ? to : new DateTime(dfrom.Year, dfrom.Month, dfrom.Day, 23, 59, 59, 999);
  346. //if (begin.Date > DateTime.Today)
  347. // continue;
  348. try
  349. {
  350. string sql = "select time AS InternalTimeStamp";
  351. foreach (var dataId in dataIdList)
  352. {
  353. if (!returnDatas.Keys.Contains(dataId) && dataId.StartsWith($"{chamber}."))
  354. {
  355. returnDatas[dataId] = new List<Tuple<DateTime, double>>();
  356. sql += "," + string.Format("\"{0}\"", dataId);
  357. }
  358. }
  359. sql += string.Format(" from \"{0}\" where time > {1} and time <= {2} order by time asc;",
  360. begin.ToString("yyyyMMdd") + "." + chamber, begin.Ticks, end.Ticks);
  361. using (var table = QueryDataClient.Instance.Service.QueryData(sql))
  362. {
  363. if (table == null || table.Rows.Count == 0)
  364. continue;
  365. DateTime dt = new DateTime();
  366. Dictionary<int, string> colName = new Dictionary<int, string>();
  367. for (int colNo = 0; colNo < table.Columns.Count; colNo++)
  368. colName.Add(colNo, table.Columns[colNo].ColumnName);
  369. for (int rowNo = 0; rowNo < table.Rows.Count; rowNo++)
  370. {
  371. var row = table.Rows[rowNo];
  372. for (int i = 0; i < table.Columns.Count; i++)
  373. {
  374. if (i == 0)
  375. {
  376. long ticks = (long)row[i];
  377. dt = new DateTime(ticks);
  378. }
  379. else
  380. {
  381. string dataId = colName[i];
  382. double value = 0.0;
  383. if (row[i] is DBNull || row[i] == null)
  384. {
  385. value = 0.0;
  386. }
  387. else if (row[i] is bool)
  388. {
  389. value = (bool)row[i] ? 1.0 : 0;
  390. }
  391. else
  392. {
  393. value = (double)float.Parse(row[i].ToString());
  394. }
  395. returnDatas[dataId].Add(Tuple.Create(dt, value));
  396. if (value < min)
  397. min = value;
  398. if (value > max)
  399. max = value;
  400. }
  401. }
  402. }
  403. table.Clear();
  404. }
  405. }
  406. catch (Exception ex)
  407. {
  408. LOG.Write(ex);
  409. return false;
  410. }
  411. }
  412. return true;
  413. }
  414. public string SelectDataExport(string path)
  415. {
  416. try
  417. {
  418. double valueMin = double.MaxValue;
  419. double valueMax = double.MinValue;
  420. DateTime dtMin = DateTime.MaxValue;
  421. DateTime dtMax = DateTime.MinValue;
  422. foreach (var recipe in SelectedRecipes)
  423. {
  424. if (Convert.ToDateTime(recipe.StartTime) < dtMin)
  425. dtMin = Convert.ToDateTime(recipe.StartTime);
  426. if (!string.IsNullOrEmpty(recipe.EndTime))
  427. {
  428. if (Convert.ToDateTime(recipe.EndTime) > dtMax)
  429. dtMax = Convert.ToDateTime(recipe.EndTime);
  430. }
  431. DateTime dtFrom = Convert.ToDateTime(recipe.StartTime);
  432. DateTime dtTo = dtFrom.AddMinutes(10);
  433. if (!string.IsNullOrEmpty(recipe.EndTime))
  434. {
  435. dtTo = Convert.ToDateTime(recipe.EndTime);
  436. }
  437. Dictionary<string, List<Tuple<DateTime, double>>> result;
  438. Dictionary<DateTime, double[]> timeValue = new Dictionary<DateTime, double[]>();
  439. System.Data.DataSet ds = new System.Data.DataSet();
  440. var array = recipe.Recipe.Split(new char[] { '\\' });
  441. string fileName = $"{array[array.Length - 1]}_ST{ConvertFileName(recipe.StartTime)}_ED{ConvertFileName(recipe.EndTime)}";
  442. //ds.Tables.Add(new System.Data.DataTable(fileName));
  443. ds.Tables.Add(new System.Data.DataTable($"{array[array.Length - 1]}_Export_{DateTime.Now:yyyyMMdd_HHmmss}"));
  444. ds.Tables[0].Columns.Add("Time");
  445. ds.Tables[0].Columns[0].DataType = typeof(DateTime);
  446. if (GetDbData(recipe.Chamber, dtFrom, dtTo, SelectedParameters, out result, ref valueMin, ref valueMax))
  447. {
  448. int i = 0;
  449. foreach (var data in result)
  450. {
  451. List<Tuple<DateTime, double>> x = result[data.Key];
  452. foreach (var tuple in x)
  453. {
  454. if (!timeValue.ContainsKey(tuple.Item1))
  455. timeValue[tuple.Item1] = new double[result.Values.Count];
  456. timeValue[tuple.Item1][i] = tuple.Item2;
  457. }
  458. ds.Tables[0].Columns.Add(data.Key);
  459. ds.Tables[0].Columns[i + 1].DataType = typeof(double);
  460. i++;
  461. }
  462. }
  463. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  464. dlg.DefaultExt = ".xlsx"; // Default file extension
  465. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  466. //dlg.FileName = $"{path}\\{recipe.Chamber}_{fileName}.xlsx"; //导出的名称
  467. dlg.FileName = $"{path}\\{recipe.Chamber}_{DateTime.Now:yyyyMMdd_HHmmss}.xlsx";
  468. foreach (var item in timeValue)
  469. {
  470. var row = ds.Tables[0].NewRow();
  471. row[0] = item.Key;
  472. for (int j = 0; j < item.Value.Length; j++)
  473. {
  474. row[j + 1] = item.Value[j];
  475. }
  476. ds.Tables[0].Rows.Add(row);
  477. }
  478. if (!ExcelHelper.ExportToExcel(dlg.FileName, ds, out string reason))
  479. {
  480. return $"{dlg.FileName} export failed, {reason}";
  481. }
  482. System.Threading.Thread.Sleep(10);
  483. }
  484. return null;
  485. }
  486. catch (Exception ex)
  487. {
  488. LOG.Write(ex);
  489. return $"Write failed,{ex.Message}";
  490. }
  491. }
  492. private string ConvertFileName(string str)
  493. {
  494. string ret = null;
  495. string specialkey = "[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#?……&*()——|{}??‘;:”“'?,?? -]";
  496. ret = System.Text.RegularExpressions.Regex.Replace(str, specialkey, "");
  497. return ret;
  498. }
  499. public async void Export()
  500. {
  501. System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
  502. if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
  503. return;
  504. string ret = await Task.Run(() => SelectDataExport(dialog.SelectedPath));
  505. if (ret != null)
  506. {
  507. MessageBox.Show(ret, "Export", MessageBoxButton.OK,
  508. MessageBoxImage.Information);
  509. }
  510. else
  511. {
  512. MessageBox.Show("Export succeed", "Export", MessageBoxButton.OK,
  513. MessageBoxImage.Information);
  514. }
  515. }
  516. #endregion
  517. }
  518. }