using System; using System.Collections.ObjectModel; using System.Data; using System.Linq; using System.Windows; using Aitex.Core.RT.Log; using MECF.Framework.Common.DataCenter; using OpenSEMI.ClientBase; using VirgoUI.Client.Models.Sys; namespace VirgoUI.Client.Models.History.ProcessHistory { public class SelectDataViewModel : UiViewModelBase { #region private const int MAX_PARAMETERS = 50; public DateTime BeginDate { get; set; } public DateTime StartDateTime { get; set; } public DateTime EndDateTime { get; set; } public ObservableCollection Recipes { get; set; } public ObservableCollection SelectedRecipes { get; set; } private ObservableCollection _ParameterNodes; public ObservableCollection ParameterNodes { get { return _ParameterNodes; } set { _ParameterNodes = value; NotifyOfPropertyChange("ParameterNodes"); } } public ObservableCollection SelectedParameters { get; set; } object _lockSelection = new object(); public ObservableCollection SourcePM { get; set; } public string SelectedValuePM { get; set; } public string RecipeName { get; set; } #endregion #region Popup window public SelectDataViewModel() { var now = DateTime.Now; this.StartDateTime = now; this.BeginDate = now; this.StartDateTime = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, 0); this.EndDateTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999); Recipes = new ObservableCollection(); SelectedRecipes = new ObservableCollection(); SelectedParameters = new ObservableCollection(); ParameterNodes = ProcessHistoryProvider.Instance.GetParameters(); SourcePM = new ObservableCollection(new[] { "PM2", "PM4", "PM6" }); } protected override void OnActivate() { base.OnActivate(); ParameterNodes = ProcessHistoryProvider.Instance.GetParameters(); foreach (var recipeItem in Recipes) { recipeItem.Selected = false; } } public void SearchRecipe() { Recipes.Clear(); //ObservableCollection items = ProcessHistoryProvider.Instance.SearchRecipe(); //foreach (RecipeItem item in items) // Recipes.Add(item); try { string sql = $"SELECT * FROM \"process_data\" where \"process_begin_time\" >='{StartDateTime:yyyyMMdd HHmmss}' and \"process_end_time\" <='{EndDateTime:yyyyMMdd HHmmss}' "; if (!string.IsNullOrEmpty(SelectedValuePM)) { string[] pms = SelectedValuePM.Split(','); if (pms.Length > 0) { sql += " and (FALSE "; foreach (var pm in pms) { sql += $" OR \"process_in\"='{pm}' "; } sql += " ) "; } } if (!string.IsNullOrEmpty(RecipeName)) { sql += string.Format(" and lower(\"recipe_name\") like '%{0}%'", RecipeName.ToLower()); } sql += " order by \"process_begin_time\" ASC;"; DataTable dbData = QueryDataClient.Instance.Service.QueryData(sql); Application.Current.Dispatcher.BeginInvoke(new Action(() => { if (dbData == null || dbData.Rows.Count == 0) return; for (int i = 0; i < dbData.Rows.Count; i++) { RecipeItem item = new RecipeItem(); item.Recipe = dbData.Rows[i]["recipe_name"].ToString(); item.Chamber = dbData.Rows[i]["process_in"].ToString(); item.Status = dbData.Rows[i]["process_status"].ToString(); if (!dbData.Rows[i]["process_begin_time"].Equals(DBNull.Value)) item.StartTime = ((DateTime)dbData.Rows[i]["process_begin_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff"); if (!dbData.Rows[i]["process_end_time"].Equals(DBNull.Value)) item.EndTime = ((DateTime)dbData.Rows[i]["process_end_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff"); Recipes.Add(item); } })); } catch (Exception e) { LOG.Write(e); } } public void CheckAllRecipe() { } public void CheckRecipe(RecipeItem recipe) { if (recipe.Selected) { SelectedRecipes.Add(recipe); } else { RecipeItem item = SelectedRecipes.FirstOrDefault(t => t.Recipe == recipe.Recipe); if (item != null) SelectedRecipes.Remove(item); } } public void Preset() { } public void UnSelect() { } public void ParameterCheck(ParameterNode node) { bool result = RefreshTreeStatusToChild(node); if (!result) node.Selected = !node.Selected; else RefreshTreeStatusToParent(node); } /// /// Refresh tree node status from current to children, and add data to SelectedData /// private bool RefreshTreeStatusToChild(ParameterNode node) { if (node.ChildNodes.Count > 0) { for (int i = 0; i < node.ChildNodes.Count; i++) { ParameterNode n = node.ChildNodes[i]; n.Selected = node.Selected; if (!RefreshTreeStatusToChild(n)) { //uncheck left node for (int j = i; j < node.ChildNodes.Count; j++) { node.ChildNodes[j].Selected = !node.Selected; } node.Selected = !node.Selected; return false; } } } else //leaf node { lock (_lockSelection) { bool flag = SelectedParameters.Contains(node.Name); if (node.Selected && !flag) { if (SelectedParameters.Count < MAX_PARAMETERS) { SelectedParameters.Add(node.Name); } else { DialogBox.ShowWarning("The max number of parameters is 20."); return false; } } else if (!node.Selected && flag) { SelectedParameters.Remove(node.Name); } } } return true; } /// /// Refresh tree node status from current to parent /// /// /// private void RefreshTreeStatusToParent(ParameterNode node) { if (node.ParentNode != null) { if (node.Selected) { bool flag = true; for (int i = 0; i < node.ParentNode.ChildNodes.Count; i++) { if (!node.ParentNode.ChildNodes[i].Selected) { flag = false; //as least one child is unselected break; } } if (flag) node.ParentNode.Selected = true; } else { node.ParentNode.Selected = false; } RefreshTreeStatusToParent(node.ParentNode); } } public void OnTreeSelectedChanged(object obj) { } public void OK() { this.TryClose(true); } public void Cancel() { this.TryClose(false); } //public void SelectColor(ChartParameter cp) //{ // if (cp == null) // return; // var dlg = new System.Windows.Forms.ColorDialog(); // if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) // { // var newColor = new System.Windows.Media.Color() { A = dlg.Color.A, B = dlg.Color.B, G = dlg.Color.G, R = dlg.Color.R }; // cp.Color = new SolidColorBrush(newColor); // } //} #endregion } }