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 ProductModal : WindowX
|
{
|
public ProductModal()
|
{
|
this.DataContext = this;
|
InitializeComponent();
|
}
|
|
public ProductData 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);
|
}
|
|
private bool ValidateForm()
|
{
|
try
|
{
|
|
//
|
|
FormValidateException ex = new FormValidateException();
|
string segment = string.IsNullOrEmpty(Data.Id) ? "" : $" AND id!='{Data.Id}'";
|
string sql = $"SELECT 1 FROM product WHERE name='{TbName.Text}'{segment};";
|
if (string.IsNullOrEmpty(TbName.Text))
|
{
|
ex.Result.Add(new FormGroupValidateResult("请输入产品名称", FgName));
|
}
|
else if (sqliteHelper.HasData(sql))
|
{
|
ex.Result.Add(new FormGroupValidateResult("产品名称不能重复", FgName));
|
}
|
else
|
{
|
FgName.ValidateResult = ValidateResult.None;
|
FgName.Message = null;
|
}
|
|
if (!ex.Success)
|
{
|
throw ex;
|
}
|
}
|
catch (FormValidateException ex)
|
{
|
foreach (var data in ex.Result)
|
{
|
var fg = (FormGroup)data.Sender;
|
fg.ValidateResult = ValidateResult.Error;
|
fg.Message = data.Message;
|
}
|
return false;
|
}
|
|
Data.Name = TbName.Text;
|
Data.IsMaster = TbIsMaster.IsChecked == true;
|
Data.ParentId = TbParentId.Text;
|
return true;
|
}
|
|
public bool? Open(ProductData data)
|
{
|
Data = data;
|
TbName.Text = Data.Name;
|
TbIsMaster.IsChecked = Data.IsMaster;
|
TbParentId.Text = Data.ParentId;
|
|
return this.ShowDialog();
|
}
|
|
private void SaveOrUpdateData()
|
{
|
if (string.IsNullOrEmpty(Data.Id))
|
{
|
InsertData(Data);
|
}
|
else
|
{
|
UpdateData(Data);
|
}
|
}
|
|
public void InsertData(ProductData data)
|
{
|
string sql = $@"INSERT INTO product (name,isMaster,parentId)
|
VALUES
|
('{data.Name}',{(data.IsMaster ? 1 : 0)} ,{data.ParentId} );";
|
sqliteHelper.ExecuteSql(sql);
|
}
|
|
|
|
public void UpdateData(ProductData data)
|
{
|
string sql = $@"UPDATE product SET name='{data.Name}',isMaster='{(data.IsMaster ? 1 : 0)}',parentId = {data.ParentId}
|
WHERE id='{data.Id}';";
|
sqliteHelper.ExecuteSql(sql);
|
}
|
}
|
}
|