alielie
2025-10-10 15bbd62b0b24b72c717ae91ab45348a2083ad04e
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using HMI.Core.Util;
using HMI.DataViewer.Entity;
using HMI.UI.DpUtil;
using HMI.UI.Util;
using log4net;
using Microsoft.Win32;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows.Controls;
 
namespace HMI.DataViewer
{
    /// <summary>
    /// ModalWindow.xaml 的交互逻辑
    /// </summary>
    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<DataBase_Setting> Datasource { get; private set; }
 
        private SqliteHelper helper;
        public SqliteHelper Helper
        {
            get => helper;
            set
            {
                if (helper == value) return;
                helper?.Close();
                helper = value;
            }
        }
 
        private ObservableCollection<string> columns = new ObservableCollection<string>();
        public ObservableCollection<string> Columns
        {
            get => columns;
            set { Set(ref columns, value); }
        }
 
        private ObservableCollection<QueryCondition> conditions =
            new ObservableCollection<QueryCondition>();
        public ObservableCollection<QueryCondition> 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, $"{this.lang("App.App.UnhandledException")}: {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<DataBase_Setting> dataSource,
            string filePath)
        {
            FilePath = filePath;
            DataIndex = index;
            Data = data;
            Datasource = dataSource;
 
            programInput.Text = Data.Program;
            dbFilePathInput.Text = Data.DBFilePath;
            tableNameInput.Text = Data.TableName;
            Conditions = data?.Condition ?? new ObservableCollection<QueryCondition>();
 
            if (!string.IsNullOrEmpty(Data.DBFilePath) && File.Exists(Data.DBFilePath))
            {
                UpdateDbHelper(Data.DBFilePath);
                UpdateTableSource();
                tableNameInput.SelectedItem = Data.TableName;
                UpdateColumnSource(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":
                    RemoveCondition();
                    break;
                default:
                    break;
            }
 
 
        }
 
        private void UpdateDbHelper(string filePath)
        {
            if (Helper == null)
            {
                Helper = new SqliteHelper(filePath);
            }
            else if (Helper.DataBaseFile != filePath)
            {
                Helper.Close();
                Helper = new SqliteHelper(filePath);
            }
        }
 
        private void UpdateTableSource()
        {
            string[] tableNames = Helper.GetAllTableInFile();
            tableNameInput.ItemsSource = tableNames;
        }
 
        private void UpdateColumnSource(string tableName)
        {
            string[] cols = Helper.GetColumnNameFromTable(tableName);
            Columns = new ObservableCollection<string>(cols);
        }
 
        private void OpenSelectDbDialog()
        {
            string BasePath = PathHelper.ConfigFolder;
 
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "(*.db)|*.db";
            openFileDialog.InitialDirectory = BasePath;
            openFileDialog.Title = this.lang("Dv.SelectDbFile");
            openFileDialog.Multiselect = false;
 
            if (openFileDialog.ShowDialog() == true)
            {
                string FileName = openFileDialog.FileName;
                dbFilePathInput.Text = FileName;
                UpdateDbHelper(FileName);
                UpdateTableSource();
            }
        }
 
        private void SelectAll()
        {
            columnTable.SelectAll();
            AddCondition();
        }
 
        private void AddCondition()
        {
            if (columnTable.SelectedItems == null || columnTable.SelectedItems.Count <= 0) return;
            var temp = new ObservableCollection<QueryCondition>();
            foreach (var column in columnTable.SelectedItems)
            {
                temp.Add(new QueryCondition { Key = column.ToString(), Desc = column.ToString() });
            }
            Conditions = temp;
        }
 
        private void RemoveCondition()
        {
            if (conditionTable.SelectedItems == null || conditionTable.SelectedItems.Count <= 0) return;
            Conditions = new ObservableCollection<QueryCondition>(
                Conditions.Where(d => !conditionTable.SelectedItems.Contains(d)).ToList());
        }
 
        private void ComboxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UpdateColumnSource(e.AddedItems[0].ToString());
            Conditions = new ObservableCollection<QueryCondition>();
        }
 
        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;
                }
            }
        }
    }
}