using System;
|
using System.Collections.Generic;
|
using System.Collections.ObjectModel;
|
using System.ComponentModel;
|
using System.Linq;
|
using System.Reflection;
|
using System.Windows;
|
using Microsoft.Win32;
|
using Panuon.WPF.UI;
|
using HMI.Core.Annotation;
|
using HMI.Core.Entity;
|
using HMI.Core.Exceptions;
|
using HMI.Core.Util;
|
using HMI.ParameterManage.Entity;
|
using HMI.UI;
|
|
namespace HMI.ParameterManage
|
{
|
/// <summary>
|
/// ModalWindow.xaml 的交互逻辑
|
/// </summary>
|
public partial class ParameterModal : WindowX
|
{
|
public ParameterModal()
|
{
|
this.DataContext = this;
|
InitializeComponent();
|
}
|
|
public ParameterData Data { get; set; }
|
|
public SqliteHelper sqliteHelper { get; set; }
|
|
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();
|
}
|
catch (Exception ex)
|
{
|
Panuon.WPF.UI.Toast.Show(this, "Error!", ToastPosition.Top);
|
e.Cancel = true;
|
return;
|
}
|
finally
|
{
|
_isProcessing = false;
|
ButtonHelper.SetIsPending(okButton, false);
|
}
|
}
|
base.OnClosing(e);
|
}
|
|
public bool ValidateName(object value, out string err)
|
{
|
err = null;
|
string segment = string.IsNullOrEmpty(Data.Id) ? "" : $" AND id!='{Data.Id}'";
|
string sql = $"SELECT 1 FROM parameter WHERE name='{TbName.Text}' AND productName='{Data.ProductName}'{segment};";
|
if (sqliteHelper.HasData(sql))
|
{
|
err = "参数名称不能重复";
|
return false;
|
}
|
return true;
|
}
|
|
private bool ValidateForm()
|
{
|
bool flag = FormGroupHelper.ValidateForm(formPanel);
|
if (flag)
|
{
|
Data.Name = TbName.Text;
|
Data.Value = TbValue.Text;
|
Data.Unit = TbUnit.Text;
|
Data.Description = TbDescription.Text;
|
Data.ProductName = TbProductName.Text;
|
Data.PGroup = TbPGroup.Text;
|
}
|
return flag;
|
}
|
|
public bool? Open(ParameterData data)
|
{
|
Data = data;
|
TbName.Text = Data.Name;
|
TbValue.Text = Data.Value;
|
TbUnit.Text = Data.Unit;
|
TbProductName.Text = Data.ProductName;
|
TbDescription.Text = Data.Description;
|
TbPGroup.Text = Data.PGroup;
|
return this.ShowDialog();
|
}
|
|
private void SaveOrUpdateData()
|
{
|
if (string.IsNullOrEmpty(Data.Id))
|
{
|
InsertData(Data);
|
}
|
else
|
{
|
UpdateData(Data);
|
}
|
}
|
|
public void InsertData(ParameterData data)
|
{
|
string sql = $@"INSERT INTO parameter (name,value,unit,description,productName,pGroup)
|
VALUES
|
('{data.Name}','{data.Value}','{data.Unit}','{data.Description}','{data.ProductName}','{data.PGroup}');";
|
sqliteHelper.ExecuteSql(sql);
|
}
|
|
public void UpdateData(ParameterData data)
|
{
|
string sql = $@"UPDATE parameter SET name='{data.Name}',value='{data.Value}',unit='{data.Unit}',
|
description='{data.Description}',productName='{data.ProductName}',pGroup='{data.PGroup}' WHERE id='{data.Id}';";
|
sqliteHelper.ExecuteSql(sql);
|
}
|
}
|
}
|