MainEventHandlers.cs
9.75 KB
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
using Hh.Mes.Common.Exel;
using Hh.Mes.POJO.ApiEntity;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.WebEntity.equipment;
using HH.Data.Excel.ColumnMapConfig;
using HH.Data.Excel.SqlHelp;
using NPOI.HPSF;
using NPOI.OpenXmlFormats.Dml.Chart;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Hh.Mes.Common.Exel.NpoiExcelImportHelper;
namespace HH.Data.Excel
{
/// <summary>
/// 事件处理
/// </summary>
public partial class MainEventHandlers
{
#region 属性
private MainWindow _main;
private Dictionary<string, Dictionary<string, SysDtAllCls>> _sysSource { get; set; }
private List<base_equipment> _base_Equipment { get; set; }
private List<daq_equipment> _daq_Equipment { get; set; }
private List<daq_equipment_prop> _equipment_Props { get; set; }
private readonly DatabaseService _service;
/// <summary>
/// 输送线报警Conveyor_Fault_ALRM、输送线系统报警System_Fault_ALRM、wcs站台是否报错WCS_Display、输送线状态地址
/// 值的顺序禁止修改 除非业务有变动
/// </summary>
private readonly string[] excelAllSheets = { "Conveyor_Fault_ALRM", "System_Fault_ALRM", "WCS_Display", "输送线状态地址" };
/// <summary>
/// 输送线类型名称 PLCSystemFault ,PLC系统报警
/// </summary>
public readonly string ccTypeCode = "PLCSystemFault";
/// <summary>
/// 输送线类型id
/// </summary>
public readonly int ccTypeId = 19;
#endregion
public MainEventHandlers(MainWindow excelMain)
{
_main = excelMain;
_sysSource = new Dictionary<string, Dictionary<string, SysDtAllCls>>();
_base_Equipment = new List<base_equipment>();
_daq_Equipment = new List<daq_equipment>();
_equipment_Props = new List<daq_equipment_prop>();
_service = DatabaseService.Instance;
InitRegisterEvent();
}
/// <summary>
/// 订阅事件
/// </summary>
void InitRegisterEvent()
{
_main._tabControl1.SelectedIndexChanged += tabControl1_SelectedIndexChanged;
_main._cb_project.SelectedIndexChanged += cb_project_SelectedIndexChanged;
_main._cb_factory.SelectedIndexChanged += cb_factory_SelectedIndexChanged;
_main._btn_read.Click += btn_read_Click;
_main._btn_read_save.Click += btn_read_save_Click;
_main._btnDefaule.Click += _btnDefaule_Click;
}
/// <summary>
/// 获取当前选中的 Tab 索引
/// </summary>
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = _main._tabControl1.SelectedIndex;
if (selectedIndex == 1)
{
_main._btn_read_save.Hide();
_main._btn_read.Hide();
_main._btnDefaule.Show();
_main._appService.InitEqType(_main._ccb_type, _main._panel2, false);
return;
}
_main._btnDefaule.Hide();
_main._btn_read_save.Show();
_main._btn_read.Show();
_main._appService.InitEqType(_main._ccb_type, _main._panel2);
}
/// <summary>
/// 项目下拉框选择事件
/// </summary>
public void cb_project_SelectedIndexChanged(object sender, EventArgs e)
{
if (_main._cb_project.SelectedItem == null)
{
Program.SysMsgShow("请选择项目列表!");
return;
}
// 将 SelectedItem 转换为 KeyValuePair<Guid, string>
var selectedItem = (KeyValuePair<Guid, string>)_main._cb_project.SelectedItem;
_main.selectProjectKey = selectedItem.Key;
_main._appService.InitFactory(_main._cb_factory, _main.selectProjectKey);
}
/// <summary>
/// 厂房下拉框选择实事件
/// </summary>
public void cb_factory_SelectedIndexChanged(object sender, EventArgs e)
{
if (_main._cb_factory.SelectedItem == null) return;
var selectedItem = (KeyValuePair<string, string>)_main._cb_factory.SelectedItem;
_main.selectFactoryCode = selectedItem.Key;
}
/// <summary>
/// 读取并保存 Excel 数据
/// </summary>
private void btn_read_save_Click(object? sender, EventArgs e)
{
try
{
_main._btn_read_save.Enabled = false;
var files = OpenFileDialogExcel(1, "", true, false);
if (files == null) return;
_sysSource.Clear();
foreach (string file in files.FileNames)
{
var fileSuffix = Path.GetExtension(file);
var fileName = Path.GetFileName(file);
using FileStream fileStream = new(file, FileMode.Open, FileAccess.Read);
var dic = _.ExcelToDataTable(fileStream, fileSuffix, out var isSuccess, out var resultMsg, 1);
if (!isSuccess)
{
Program.SysMsgShow($"{fileName}文件读取失败:" + resultMsg);
return;
}
if (dic != null)
{
_sysSource.Add(fileName, dic);
ResponseNew response = _service.JudgeData(dic);
string resMsg = $"【{fileName}】{response.message}";
if (response.status)
{
Program.SysMsgShow(resMsg, "");
}
else
{
Program.SysMsgShow(resMsg);
}
}
}
_main._btn_read_save.Enabled = true;
}
catch (Exception ex)
{
Program.SysMsgShow(ex.Message);
_main._btn_read_save.Enabled = true;
return;
}
finally
{
_main._btn_read_save.Enabled = true;
}
}
/// <summary>
/// 导入/导出 Excel 文件 主要针对输送线
/// </summary>
private void btn_read_Click(object? sender, EventArgs e)
{
var files = OpenFileDialogExcel(2, "上传的excel需要包含【IP地址表.xlsx】,且附件至少要2个文件以上!", true);
if (files == null) return;
_sysSource.Clear();
foreach (string file in files.FileNames)
{
var fileSuffix = Path.GetExtension(file);
var fileName = Path.GetFileName(file);
using FileStream fileStream = new(file, FileMode.Open, FileAccess.Read);
var dic = _.ExcelToDataTable(fileStream, fileSuffix, out var isSuccess, out var resultMsg, 1);
if (!isSuccess)
{
Program.SysMsgShow($"{fileName}文件读取失败:" + resultMsg);
return;
}
_sysSource.Add(fileName, dic);
}
var isValid = CheckRequiredFilesCC();
if (!isValid) return;
HandlersCCDataAndExport();
}
/// <summary>
/// 默认设备 堆垛机设备 设备属性生成
/// </summary>
private void _btnDefaule_Click(object? sender, EventArgs e)
{
var selectValue = GetCheckedItems(_main._ccb_type, true);
if (selectValue.Count == 0)
{
Program.SysMsgShow("请选择设备类型!");
return;
}
var files = OpenFileDialogExcel(1, $"上传的excel需要包含有【{_main._sysIpFileName}】文件,且只能上传一个的附件!");
if (files == null) return;
var fileName = Path.GetFileName(files.FileName);
var fileSuffix = Path.GetExtension(files.FileName);
using FileStream fileStream = new(files.FileName, FileMode.Open, FileAccess.Read);
var dic = _.ExcelToDataTable(fileStream, fileSuffix, out var isSuccess, out var resultMsg, 1);
if (!isSuccess)
{
Program.SysMsgShow($"{fileName}文件读取失败:" + resultMsg);
return;
}
if (dic.Count != 1)
{
Program.SysMsgShow($"{fileName}的页签数只能是一个,当前页签个数:" + dic.Count);
return;
}
var eqDt = dic.FirstOrDefault().Value.dtData;
if (eqDt == null || eqDt.Rows.Count == 0)
{
Program.SysMsgShow($"【{files.FileName}】文件不存数据!");
return;
}
var columnName = "类型";
var result = AllValuesExist(eqDt, columnName, selectValue);
if (result.Item1 == -1)
{
Program.SysMsgShow($"{fileName}文件中不存在列【{columnName}】");
return;
}
if (result.Item1 == 0)
{
Program.SysMsgShow($"{fileName}文件中列【{columnName}】的值【{result.Item2}】和设备类型复选框选中的值【{result.Item3}】不是全匹配");
return;
}
DefaultSaveData(eqDt, files.FileName);
}
}
}