using HMI.Core.Util;
|
using HMI.DataViewer.Entity;
|
using HMI.UI.DpUtil;
|
using HMI.UI.Util;
|
using log4net;
|
using Microsoft.Win32;
|
using Panuon.WPF.UI;
|
using System;
|
using System.Collections.Generic;
|
using System.Collections.ObjectModel;
|
using System.ComponentModel;
|
using System.IO;
|
using System.Linq;
|
using System.Windows.Controls;
|
|
namespace HMI.DataViewer
|
{
|
/// <summary>
|
/// ModalWindow.xaml 的交互逻辑
|
/// </summary>
|
public partial class DbSettingModal : WindowX
|
{
|
public DbSettingModal(ILog log)
|
{
|
this.log = log;
|
this.DataContext = this;
|
InitializeComponent();
|
}
|
private ILog log;
|
public string FilePath { get; private set; }
|
public int DataIndex { get; private set; }
|
public DataBase_Setting Data { get; private set; }
|
public ObservableCollection<DataBase_Setting> Datasource { get; private set; }
|
|
private SqliteHelper helper;
|
public SqliteHelper Helper
|
{
|
get => helper;
|
set
|
{
|
if (helper == value) return;
|
helper?.Close();
|
helper = value;
|
}
|
}
|
|
private ObservableCollection<string> columns = new ObservableCollection<string>();
|
public ObservableCollection<string> Columns
|
{
|
get => columns;
|
set { Set(ref columns, value); }
|
}
|
|
private ObservableCollection<QueryCondition> conditions =
|
new ObservableCollection<QueryCondition>();
|
public ObservableCollection<QueryCondition> Conditions
|
{
|
get => conditions;
|
set { Set(ref conditions, value); }
|
}
|
|
|
private bool _isProcessing;
|
|
protected override void OnClosing(CancelEventArgs e)
|
{
|
//Press Continue Button
|
if (DialogResult == true)
|
{
|
if (_isProcessing)
|
{
|
e.Cancel = true;
|
return;
|
}
|
var okButton = WindowXModalDialog.GetOKButton(this);
|
try
|
{
|
_isProcessing = true;
|
ButtonHelper.SetIsPending(okButton, true);
|
if (!ValidateForm())
|
{
|
e.Cancel = true;
|
return;
|
}
|
|
SaveOrUpdateData();
|
Helper?.Close();
|
}
|
catch (Exception ex)
|
{
|
log.Error("IPManage Form Submit Failed.", ex);
|
ToastHelper.Error(this, $"{this.lang("App.App.UnhandledException")}: {ex.Message}");
|
e.Cancel = true;
|
return;
|
}
|
finally
|
{
|
_isProcessing = false;
|
ButtonHelper.SetIsPending(okButton, false);
|
}
|
}
|
base.OnClosing(e);
|
}
|
|
public bool ValidateDBAddress(object value, out string err)
|
{
|
err = null;
|
|
return true;
|
}
|
|
private bool ValidateForm()
|
{
|
bool flag = FormGroupHelper.ValidateForm(formPanel);
|
if (flag)
|
{
|
Data.Program = programInput.Text;
|
Data.DBFilePath = dbFilePathInput.Text;
|
Data.TableName = tableNameInput.Text;
|
Data.TimeCol = Conditions.FirstOrDefault(c => c.IsTimeCol)?.Key;
|
Data.Condition = Conditions;
|
}
|
return flag;
|
}
|
|
public bool? Open(int index, DataBase_Setting data,
|
ObservableCollection<DataBase_Setting> dataSource,
|
string filePath)
|
{
|
FilePath = filePath;
|
DataIndex = index;
|
Data = data;
|
Datasource = dataSource;
|
|
programInput.Text = Data.Program;
|
dbFilePathInput.Text = Data.DBFilePath;
|
tableNameInput.Text = Data.TableName;
|
Conditions = data?.Condition ?? new ObservableCollection<QueryCondition>();
|
|
if (!string.IsNullOrEmpty(Data.DBFilePath) && File.Exists(Data.DBFilePath))
|
{
|
UpdateDbHelper(Data.DBFilePath);
|
UpdateTableSource();
|
tableNameInput.SelectedItem = Data.TableName;
|
UpdateColumnSource(Data.TableName);
|
}
|
|
return this.ShowDialog();
|
}
|
|
private void SaveOrUpdateData()
|
{
|
if (DataIndex == -1)
|
{
|
InsertData(Data);
|
}
|
else
|
{
|
UpdateData(DataIndex, Data);
|
}
|
}
|
|
public void InsertData(DataBase_Setting data)
|
{
|
Datasource.Add(data);
|
XmlHelper.SerializeToXml(FilePath, Datasource);
|
}
|
|
public void UpdateData(int index, DataBase_Setting data)
|
{
|
Datasource[index] = data;
|
XmlHelper.SerializeToXml(FilePath, Datasource);
|
}
|
|
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
|
{
|
Button btn = sender as Button;
|
switch (btn.Name)
|
{
|
case "searchBtn":
|
OpenSelectDbDialog();
|
break;
|
case "selectAllBtn":
|
SelectAll();
|
break;
|
case "addBtn":
|
AddCondition();
|
break;
|
case "delBtn":
|
RemoveCondition();
|
break;
|
default:
|
break;
|
}
|
|
|
}
|
|
private void UpdateDbHelper(string filePath)
|
{
|
if (Helper == null)
|
{
|
Helper = new SqliteHelper(filePath);
|
}
|
else if (Helper.DataBaseFile != filePath)
|
{
|
Helper.Close();
|
Helper = new SqliteHelper(filePath);
|
}
|
}
|
|
private void UpdateTableSource()
|
{
|
string[] tableNames = Helper.GetAllTableInFile();
|
tableNameInput.ItemsSource = tableNames;
|
}
|
|
private void UpdateColumnSource(string tableName)
|
{
|
string[] cols = Helper.GetColumnNameFromTable(tableName);
|
Columns = new ObservableCollection<string>(cols);
|
}
|
|
private void OpenSelectDbDialog()
|
{
|
string BasePath = PathHelper.ConfigFolder;
|
|
OpenFileDialog openFileDialog = new OpenFileDialog();
|
openFileDialog.Filter = "(*.db)|*.db";
|
openFileDialog.InitialDirectory = BasePath;
|
openFileDialog.Title = this.lang("Dv.SelectDbFile");
|
openFileDialog.Multiselect = false;
|
|
if (openFileDialog.ShowDialog() == true)
|
{
|
string FileName = openFileDialog.FileName;
|
dbFilePathInput.Text = FileName;
|
UpdateDbHelper(FileName);
|
UpdateTableSource();
|
}
|
}
|
|
private void SelectAll()
|
{
|
columnTable.SelectAll();
|
AddCondition();
|
}
|
|
private void AddCondition()
|
{
|
if (columnTable.SelectedItems == null || columnTable.SelectedItems.Count <= 0) return;
|
var temp = new ObservableCollection<QueryCondition>();
|
foreach (var column in columnTable.SelectedItems)
|
{
|
temp.Add(new QueryCondition { Key = column.ToString(), Desc = column.ToString() });
|
}
|
Conditions = temp;
|
}
|
|
private void RemoveCondition()
|
{
|
if (conditionTable.SelectedItems == null || conditionTable.SelectedItems.Count <= 0) return;
|
Conditions = new ObservableCollection<QueryCondition>(
|
Conditions.Where(d => !conditionTable.SelectedItems.Contains(d)).ToList());
|
}
|
|
private void ComboxSelectionChanged(object sender, SelectionChangedEventArgs e)
|
{
|
UpdateColumnSource(e.AddedItems[0].ToString());
|
Conditions = new ObservableCollection<QueryCondition>();
|
}
|
|
private void RadioButton_Click(object sender, System.Windows.RoutedEventArgs e)
|
{
|
RadioButton btn = sender as RadioButton;
|
var condition = btn.DataContext as QueryCondition;
|
if (condition.IsTimeCol == true)
|
{
|
condition.IsTimeCol = false;
|
}
|
else
|
{
|
condition.IsTimeCol = true;
|
foreach (var c in Conditions)
|
{
|
if (c != condition) c.IsTimeCol = false;
|
}
|
}
|
}
|
}
|
}
|