alielie
2025-03-04 2b1a6417285597bc58ef28aa3f01aeee2532cd19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
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.MenuManage.Entity;
using HMI.MenuManage.Util;
 
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)
                {
                    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 user WHERE username='{TbCode.Text}'{segment};";
                if (string.IsNullOrEmpty(TbCode.Text))
                {
                    ex.Result.Add(new FormGroupValidateResult("请输入角色编号", FgCode));
                }
                else if (sqliteHelper.HasData(sql))
                {
                    ex.Result.Add(new FormGroupValidateResult("角色编号不能重复", FgCode));
                }
                else
                {
                    FgCode.ValidateResult = ValidateResult.None;
                    FgCode.Message = null;
                }
 
                if (string.IsNullOrEmpty(TbName.Text))
                {
                    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.Code = TbCode.Text;
            Data.Name = TbName.Text;
            return true;
        }
 
        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);
        }
    }
}