using HMI.Core.Util;
|
using HMI.MenuManage.Entity;
|
using HMI.MenuManage.Util;
|
using HMI.UI.DpUtil;
|
using HMI.UI.Util;
|
using Panuon.WPF.UI;
|
using System;
|
using System.Collections.ObjectModel;
|
using System.ComponentModel;
|
|
namespace HMI.MenuManage
|
{
|
/// <summary>
|
/// ModalWindow.xaml 的交互逻辑
|
/// </summary>
|
public partial class RoleModal : WindowX
|
{
|
public RoleModal()
|
{
|
this.DataContext = this;
|
InitializeComponent();
|
}
|
|
public RoleData Data { get; set; }
|
|
private ObservableCollection<RoleMenuRel> _TreeDataSource;
|
|
public ObservableCollection<RoleMenuRel> TreeDataSource
|
{
|
get { return _TreeDataSource; }
|
set { Set(ref _TreeDataSource, value); }
|
}
|
|
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)
|
{
|
ToastHelper.Error(this, $"Error!{ex.Message}");
|
e.Cancel = true;
|
return;
|
}
|
finally
|
{
|
_isProcessing = false;
|
ButtonHelper.SetIsPending(okButton, false);
|
}
|
}
|
base.OnClosing(e);
|
}
|
|
private bool ValidateCode(object value, out string err)
|
{
|
err = null;
|
string segment = string.IsNullOrEmpty(Data.Id) ? "" : $" AND id!='{Data.Id}'";
|
string sql = $"SELECT 1 FROM user WHERE username='{TbCode.Text}'{segment};";
|
if (sqliteHelper.HasData(sql))
|
{
|
err = this.lang("App.MustUnique", this.lang("HMI.Role.Code"));
|
return false;
|
}
|
return true;
|
}
|
|
private bool ValidateForm()
|
{
|
bool flag = FormGroupHelper.ValidateForm(formPanel);
|
if (flag)
|
{
|
Data.Code = TbCode.Text;
|
Data.Name = TbName.Text;
|
}
|
return flag;
|
}
|
|
public bool? Open(RoleData data)
|
{
|
Data = data;
|
TbCode.Text = Data.Code;
|
TbName.Text = Data.Name;
|
TreeDataSource = RoleMenuHelper.SelectRoleMenuTreeData(sqliteHelper, Data.Code);
|
return this.ShowDialog();
|
}
|
|
private void SaveOrUpdateData()
|
{
|
if (string.IsNullOrEmpty(Data.Id))
|
{
|
InsertData(Data);
|
}
|
else
|
{
|
UpdateData(Data);
|
}
|
}
|
|
public void InsertData(RoleData data)
|
{
|
string sql = $@"INSERT INTO role (code,name)
|
VALUES
|
('{data.Code}','{data.Name}');";
|
RoleMenuHelper.SaveRelData(sqliteHelper, TreeDataSource, data.Code, sql, null);
|
}
|
|
public void UpdateData(RoleData data)
|
{
|
string sql = $@"UPDATE role SET code='{data.Code}',name='{data.Name}' WHERE id='{data.Id}';";
|
RoleMenuHelper.SaveRelData(sqliteHelper, TreeDataSource, data.Code, sql, null);
|
}
|
}
|
}
|