| | |
| | | using System.Windows; |
| | | using HMI.Base; |
| | | using HMI.Core.Util; |
| | | using HMI.DataViewer.Entity; |
| | | using HMI.UI.Util; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Collections.ObjectModel; |
| | | using System.Data; |
| | | using System.IO; |
| | | using System.Linq; |
| | | using System.Windows; |
| | | using System.Windows.Controls; |
| | | using HMI.Base; |
| | | using System.Windows.Data; |
| | | |
| | | namespace HMI.DataViewer |
| | | { |
| | |
| | | { |
| | | public DataViewer() |
| | | { |
| | | DataContext = this; |
| | | InitializeComponent(); |
| | | } |
| | | |
| | | private string SourceFilePath; |
| | | |
| | | private ObservableCollection<DataBase_Setting> settings = new ObservableCollection<DataBase_Setting>(); |
| | | public ObservableCollection<DataBase_Setting> Settings |
| | | { |
| | | get => settings; |
| | | set { Set(ref settings, value); } |
| | | } |
| | | |
| | | private ObservableCollection<QueryData> datas = new ObservableCollection<QueryData>(); |
| | | public ObservableCollection<QueryData> Datas |
| | | { |
| | | get => datas; |
| | | set { Set(ref datas, value); } |
| | | } |
| | | |
| | | private Dictionary<string, DataBase_Setting> Map = new Dictionary<string, DataBase_Setting>(); |
| | | |
| | | private void Button_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | Button btn = sender as Button; |
| | | switch (btn.Name) |
| | | { |
| | | case "addBtn": |
| | | OpenSettingModal(new DataBase_Setting()); |
| | | break; |
| | | case "rfhBtn": |
| | | break; |
| | | case "delBtn": |
| | | break; |
| | | case "srhBtn": |
| | | SearchData(); |
| | | break; |
| | | case "resetBtn": |
| | | ResetDataTable(); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | |
| | | |
| | | private void OpenSettingModal(DataBase_Setting data) |
| | | { |
| | | DbSettingModal modal = new DbSettingModal(log) |
| | | { |
| | | Title = "新增数据源", |
| | | Owner = Window.GetWindow(this) |
| | | }; |
| | | if (modal.Open(-1, data, Settings, SourceFilePath) == true) |
| | | { |
| | | LoadData(); |
| | | } |
| | | } |
| | | |
| | | private void SearchData() |
| | | { |
| | | string program = programInput.Text; |
| | | if (!Map.ContainsKey(program)) |
| | | { |
| | | ToastHelper.Error(Window.GetWindow(this), "参数异常,请刷新重试"); |
| | | return; |
| | | } |
| | | |
| | | var datas = Datas?.Where(d => !string.IsNullOrEmpty(d.Value)).ToList(); |
| | | if (datas == null || datas.Count <= 0) |
| | | { |
| | | ToastHelper.Warning(Window.GetWindow(this), "请设置查询条件值"); |
| | | return; |
| | | } |
| | | |
| | | DataBase_Setting setting = Map[program]; |
| | | |
| | | |
| | | string fields = string.Join(",", Datas?.Select(d => $"[{d.Key}]").ToList()); |
| | | |
| | | string conditon = string.Join(" AND ", datas.Select(d => $"[{d.Key}] {d.Condition} '{d.Value}'").ToList()); |
| | | string timeCol = setting.TimeCol; |
| | | if (string.IsNullOrEmpty(timeCol)) |
| | | { |
| | | timeCol = "time"; |
| | | } |
| | | if (!string.IsNullOrEmpty(startTimeInput.Text)) |
| | | { |
| | | conditon += $" AND {timeCol} >= '{startTimeInput.Text}'"; |
| | | } |
| | | if (!string.IsNullOrEmpty(endTimeInput.Text)) |
| | | { |
| | | conditon += $" AND {timeCol} < '{endTimeInput.Text}'"; |
| | | } |
| | | conditon = string.IsNullOrEmpty(conditon) ? string.Empty : $" WHERE {conditon}"; |
| | | |
| | | SqliteHelper helper = null; |
| | | string sql = string.Empty; |
| | | try |
| | | { |
| | | helper = new SqliteHelper(setting.DBFilePath); |
| | | sql = $"SELECT {fields} FROM {setting.TableName} {conditon}"; |
| | | DataTable dt = helper.GetDataTable(sql); |
| | | |
| | | dataTable.ItemsSource = dt.DefaultView; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | log.Error($"Select Data Failed. SQL: {sql}", ex); |
| | | ToastHelper.Error(Window.GetWindow(this), $"查询失败!{ex.Message}"); |
| | | |
| | | } |
| | | finally |
| | | { |
| | | helper?.Close(); |
| | | } |
| | | } |
| | | |
| | | private void ResetDataTable() |
| | | { |
| | | startTimeInput.SelectedDateTime = null; |
| | | endTimeInput.SelectedDateTime = null; |
| | | dataTable.ItemsSource = null; |
| | | foreach (var data in Datas) |
| | | { |
| | | data.Value = null; |
| | | } |
| | | } |
| | | |
| | | public override void SetBaseFolder(string folder) |
| | | { |
| | | base.SetBaseFolder(folder); |
| | | SourceFilePath = Path.Combine(BaseFolder, "SourceData.setting"); |
| | | } |
| | | |
| | | private void PluginControl_Loaded(object sender, RoutedEventArgs e) |
| | | { |
| | | LoadData(); |
| | | Loaded -= PluginControl_Loaded; |
| | | } |
| | | |
| | | private void LoadData() |
| | | { |
| | | if (File.Exists(SourceFilePath)) |
| | | { |
| | | List<string> names = new List<string>(); |
| | | Settings = XmlHelper.DeserializeFromXml<ObservableCollection<DataBase_Setting>>(SourceFilePath); |
| | | Map.Clear(); |
| | | foreach (var data in Settings) |
| | | { |
| | | Map.Add(data.Program, data); |
| | | names.Add(data.Program); |
| | | } |
| | | programInput.ItemsSource = names; |
| | | } |
| | | } |
| | | |
| | | private void programInput_SelectionChanged(object sender, SelectionChangedEventArgs e) |
| | | { |
| | | var key = e.AddedItems[0].ToString(); |
| | | if (!Map.ContainsKey(key)) |
| | | { |
| | | ToastHelper.Error(Window.GetWindow(this), "参数异常,请刷新重试"); |
| | | return; |
| | | } |
| | | DataBase_Setting setting = Map[key]; |
| | | Datas = new ObservableCollection<QueryData>( |
| | | setting?.Condition?.Select(d => new QueryData { Key = d.Key, Name = d.Desc, Condition = "=" }).ToList()); |
| | | |
| | | var temp = dataTable.Columns[0]; |
| | | dataTable.Columns.Clear(); |
| | | dataTable.Columns.Add(temp); |
| | | foreach (var data in Datas) |
| | | { |
| | | AddDataGridTextColumn(data.Name, data.Key); |
| | | } |
| | | } |
| | | |
| | | private void AddDataGridTextColumn(string header, string bindingPath) |
| | | { |
| | | DataGridTextColumn textColumn = new DataGridTextColumn |
| | | { |
| | | Header = header, |
| | | Binding = new Binding(bindingPath) |
| | | , |
| | | Width = new DataGridLength(1, DataGridLengthUnitType.Star) |
| | | }; |
| | | dataTable.Columns.Add(textColumn); |
| | | } |
| | | } |
| | | } |