using System; using System.Text; using System.Windows; using System.Windows.Controls; using System.Data; using SSWMS.Common; using System.Windows.Input; using System.Collections.Generic; namespace SSWMS.Client.UC { public partial class ucQuickQuery : UserControl { public delegate void U_QueryEventHandler(string sQueryWhere); public event U_QueryEventHandler U_Query; public string U_XmlTableName = string.Empty; public string U_WindowName = string.Empty; public Dictionary U_DefaultValue = new Dictionary(); private DataTable FormStyles = null; public ucQuickQuery() { InitializeComponent(); } public void U_InitControl() { try { this.wpQuery.Children.Clear(); if (this.FormStyles == null) { this.FormStyles = XmlStyle.GetDataTable(this.U_WindowName, this.U_XmlTableName); } foreach (DataRow rowField in this.FormStyles.Select("(Header<>'' and Query<>'0')")) { StackPanel sp = new StackPanel { Tag = rowField, Style = Application.Current.Resources["CommonStackPanel"] as Style }; sp.Children.Add(new TextBlock { VerticalAlignment = VerticalAlignment.Center, FontSize = 14, Margin = new Thickness(2, 0, 2, 0), Text = XmlStyle.GetHeader(rowField) }); string sControlType = rowField["Control"].ToString(); string sColumn = rowField["Column"].ToString(); string sQuery = rowField["Query"].ToString(); switch (sControlType) { case "TextBox": TextBox tb = new TextBox { Tag = rowField, Width = 120, Margin = new Thickness(5, 0, 5, 0), Text = rowField["Default"].ToString(), FontSize = 14, DataContext = sQuery }; if (U_DefaultValue.ContainsKey(sColumn)) { string sValue; U_DefaultValue.TryGetValue(sColumn, out sValue); tb.Text = sValue; } sp.Children.Add(tb); break; case "CheckBox": sp.Children.Add(new CheckBox { Tag = rowField, VerticalAlignment = VerticalAlignment.Center, IsThreeState = false, Margin = new Thickness(10, 0, 5, 0) }); break; default: ComboBox cb = new ComboBox { Tag = rowField, Width = 120, Height = 25, FontSize = 14, Margin = new Thickness(5, 0, 5, 0), DisplayMemberPath = "NAME", SelectedValuePath = "VALUE", ItemsSource = SystemCodeData.DicDataView[sControlType], SelectedIndex = -1, DataContext = sQuery }; if (U_DefaultValue.ContainsKey(sColumn)) { string sValue; U_DefaultValue.TryGetValue(sColumn, out sValue); cb.SelectedValue = sValue; } sp.Children.Add(cb); break; } this.wpQuery.Children.Add(sp); } StackPanel spQuery = new StackPanel() { Style = Application.Current.Resources["CommonStackPanel"] as Style }; Button bQuery = new Button() { Content = "查 询", Style = Application.Current.Resources["CommonButton"] as Style }; bQuery.Click += (sender, e) => { MainWindow.mainWin.Cursor = Cursors.Wait; this.U_Query(this.GetQuerySQL(0)); MainWindow.mainWin.Cursor = Cursors.Arrow; }; spQuery.Children.Add(bQuery); Button bRefresh = new Button() { Content = "重 置", Style = Application.Current.Resources["CommonButton"] as Style }; bRefresh.Click += (sender, e) => { foreach (UIElement uie in this.wpQuery.Children) { StackPanel sp = uie as StackPanel; if (sp == null) { continue; } DataRow dr = sp.Tag as DataRow; if (dr == null) { continue; } if (sp.Children[1] is TextBox) { (sp.Children[1] as TextBox).Clear(); } else if (sp.Children[1] is ComboBox) { (sp.Children[1] as ComboBox).SelectedIndex = -1; } else if (sp.Children[1] is CheckBox) { (sp.Children[1] as CheckBox).IsChecked = false; } } }; spQuery.Children.Add(bRefresh); this.wpQuery.Children.Add(spQuery); if (U_XmlTableName == "V_STORAGE_LIST" || U_XmlTableName == "V_STORAGE_LIST_OUT") { StackPanel spExpire = new StackPanel() { Style = Application.Current.Resources["CommonStackPanel"] as Style }; spExpire.Children.Add(new TextBlock() { Text = "超期天数", Style = Application.Current.Resources["CommonTextBlock"] as Style }); TextBox tbExpire = new TextBox() { Text = "30", Width = 30, Style = Application.Current.Resources["CommonTextBox"] as Style }; spExpire.Children.Add(tbExpire); Button bExpire = new Button() { Content = "超期查询", Style = Application.Current.Resources["CommonButton"] as Style }; bExpire.Click += (sender, e) => { int iExpire = 0; try { iExpire = Convert.ToInt32(tbExpire.Text); if (iExpire <= 0) { MessageDialog.ShowException($"超期天数 {tbExpire.Text} 少于1天"); } } catch { MessageDialog.ShowException($"超期天数 {tbExpire.Text} 格式错误"); } MainWindow.mainWin.Cursor = Cursors.Wait; this.U_Query(this.GetQuerySQL(iExpire)); MainWindow.mainWin.Cursor = Cursors.Arrow; }; spExpire.Children.Add(bExpire); this.wpQuery.Children.Add(spExpire); } } catch (Exception ex) { MessageDialog.ShowException(ex); } } private string GetQuerySQL(int iExpire) { StringBuilder sb = new StringBuilder(); sb.Append("1=1"); foreach (UIElement uie in this.wpQuery.Children) { StackPanel sp = uie as StackPanel; if (sp == null) { continue; } DataRow dr = sp.Tag as DataRow; if (dr == null) { continue; } string sColumn = dr["Column"].ToString(); if (sp.Children[1] is CheckBox) { CheckBox cb = sp.Children[1] as CheckBox; if (cb.IsChecked != null) { sb.Append($" and {sColumn}='{Convert.ToInt32((sp.Children[1] as CheckBox).IsChecked)}'"); } } else if (sp.Children[1] is ComboBox) { ComboBox cb = sp.Children[1] as ComboBox; if (cb.SelectedIndex > -1) { if (cb.DataContext == null || cb.DataContext.ToString() == "1") { sb.Append($" and {sColumn} like '%{cb.SelectedValue}%'"); } else { sb.Append($" and {sColumn}='{cb.SelectedValue}'"); } } } else if (sp.Children[1] is TextBox) { TextBox tb = sp.Children[1] as TextBox; if (!string.IsNullOrWhiteSpace(tb.Text)) { if (tb.DataContext == null || tb.DataContext.ToString() == "1") { sb.Append($" and {sColumn} like '%{tb.Text}%'"); } else { sb.Append($" and {sColumn}='{tb.Text}'"); } } } } if (iExpire > 0) { sb.Append($" and ENTRY_TIME<='{StringUtils.GetExpireDay(iExpire)}'"); } return sb.ToString(); } } }