using HMI.Core.Util;
using HMI.DataViewer.Entity;
using HMI.UI;
using HMI.UI.Util;
using log4net;
using Microsoft.Win32;
using Panuon.WPF.UI;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Controls;
namespace HMI.DataViewer
{
///
/// ModalWindow.xaml 的交互逻辑
///
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 Datasource { get; private set; }
private SqliteHelper helper;
public SqliteHelper Helper
{
get => helper;
set
{
if (helper == value) return;
helper?.Close();
helper = value;
}
}
private ObservableCollection columns = new ObservableCollection();
public ObservableCollection Columns
{
get => columns;
set { Set(ref columns, value); }
}
private ObservableCollection conditions =
new ObservableCollection();
public ObservableCollection 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, $"未知异常!{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 dataSource,
string filePath)
{
FilePath = filePath;
DataIndex = index;
Data = data;
Datasource = dataSource;
programInput.Text = Data.Program;
dbFilePathInput.Text = Data.DBFilePath;
tableNameInput.Text = 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":
break;
default:
break;
}
}
private void OpenSelectDbDialog()
{
string BasePath = PathHelper.IconFolder;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "(*.db)|*.db";
openFileDialog.InitialDirectory = BasePath;
openFileDialog.Title = "选择数据库文件";
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == true)
{
string FileName = openFileDialog.FileName;
dbFilePathInput.Text = FileName;
Helper = new SqliteHelper(FileName);
string[] tableNames = Helper.GetAllTableInFile();
tableNameInput.ItemsSource = tableNames;
}
}
private void SelectAll()
{
columnTable.SelectAll();
AddCondition();
}
private void AddCondition()
{
if (columnTable.SelectedItems == null || columnTable.SelectedItems.Count <= 0) return;
var temp = new ObservableCollection();
foreach (var column in columnTable.SelectedItems)
{
temp.Add(new QueryCondition { Key = column.ToString(), Desc = column.ToString() });
}
Conditions = temp;
}
private void ComboxSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
string[] cols = Helper.GetColumnNameFromTable(e.AddedItems[0].ToString());
Columns = new ObservableCollection(cols);
}
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;
}
}
}
}
}