Shuxia Ning
2024-12-27 f94a4ed8e9f5400e73bad84be023096585cb0498
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using IStation.Model;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
 
namespace IStation.BLL
{
    /// <summary>
    /// 导出 WaterDesk 所需数据
    /// </summary>
    public partial class ExportWaterDeskDataHelper
    {
        public class ExportInfo
        {
            public string Tag { get; set; }
            public int Index { get; set; }
            public List<SignalRecord> SignalRecords { get; set; }
        }
 
        public static bool ExportLast()
        {
            var ms = new BLL.MonitorDataSources().GetAll().Last();
            return Export(ms.ID);
        }
 
        public static bool Export(long monitorDataSourcesId)
        {
            var bllSummary = new BLL.MonitorDataSetSummary();
            var yearMonths = bllSummary.GetYearMonth(monitorDataSourcesId);
            if (yearMonths == null || !yearMonths.Any())
                return false;
 
            var bllEquipment = new BLL.Equipment();
            var enginePumps = bllEquipment.GetAll();
            if (enginePumps == null || !enginePumps.Any())
                return false;
 
 
            var bllMonitorPoint = new BLL.MonitorPoint();
            var bllEquipmentMonitorMapping = new BLL.EquipmentMonitorMapping();
            var bllMonitorDataSet = new BLL.MonitorDataSet();
 
 
            var monitorPointExSignalWithSignalTypes = new BLL.MonitorPoint().GetAllExSignalWithSignalType();
            monitorPointExSignalWithSignalTypes = monitorPointExSignalWithSignalTypes.Where(x => !string.IsNullOrEmpty(x.TagName)).ToList();
            if (monitorPointExSignalWithSignalTypes == null || !monitorPointExSignalWithSignalTypes.Any())
                return false;
 
 
 
 
            var wls = monitorPointExSignalWithSignalTypes.Where(x => x.SignalType == IStation.SignalType.水位).ToList();
            var wls_ids = wls.Select(x => x.SignalID).ToList();
 
            var qs = monitorPointExSignalWithSignalTypes.Where(x => x.SignalType == IStation.SignalType.瞬时流量).ToList();
            var qs_ids = qs.Select(x => x.SignalID).ToList();
 
            var ps = monitorPointExSignalWithSignalTypes.Where(x => x.SignalType == IStation.SignalType.压力).ToList();
            var ps_ids = ps.Select(x => x.SignalID).ToList();
 
            var rs = monitorPointExSignalWithSignalTypes.Where(x => x.SignalType == IStation.SignalType.运行状态).ToList();
            var rs_ids = rs.Select(x => x.SignalID).ToList();
 
            //var hzs = monitorPointExSignalWithSignalTypes.Where(x => x.SignalType == IStation.SignalType.频率).ToList();
            //var hzs_id_list = hzs.Select(x => x.SignalID).ToList();
 
             var speeds = monitorPointExSignalWithSignalTypes.Where(x => x.SignalType == IStation.SignalType.转速).ToList();
             //var speeds_ids = speeds.Where(x=> x.Importance==0).Select(x => x.SignalID).ToList();
             var speeds_ids = speeds.Select(x => x.SignalID).ToList();
 
            var hzs_ids = new List<long>();
            hzs_ids.AddRange(rs_ids);
          //  hzs_ids.AddRange(hzs_id_list);
             hzs_ids.AddRange(speeds_ids);
 
            Dictionary<long, double> temp = new Dictionary<long, double>();
 
            foreach (var enginePump in enginePumps)
            {
                var equipmentMonitorMappings = bllEquipmentMonitorMapping.GetByEquipmentID(enginePump.ID);
                if (equipmentMonitorMappings == null || !equipmentMonitorMappings.Any())
                    continue;
                var pump = bllEquipment.GetChildPumpByEnginePumpID(enginePump.ID);
                foreach (var item in speeds)
                {
                    if (equipmentMonitorMappings.Exists(x => x.MonitorPointID == item.MonitorPointID))
                    {
                        temp.Add(item.SignalID, pump.RatedParas.Nr);
                    }
                }
            }
 
            var root_path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WaterDeskData");
            foreach (var yearMonth in yearMonths)
            {
                var year = yearMonth.Year;
                var month = yearMonth.Month;
                var y_m_path = Path.Combine(root_path, $"{year}年{month}月");
                if (!Directory.Exists(y_m_path))
                {
                    Directory.CreateDirectory(y_m_path);
                }
 
                var wls_packets = bllMonitorDataSet.GetSignalRecordPacket(monitorDataSourcesId, wls_ids, year, month);
                if (wls_packets != null && wls_packets.Any())
                {
                    var infos = new List<ExportInfo>();
                    for (int i = 0; i < wls.Count; i++)
                    {
                        var mp = wls[i];
                        var info = new ExportInfo();
                        info.Tag = mp.TagName;
                        info.Index = i;
                        info.SignalRecords = wls_packets.Where(x => x.SignalID == mp.SignalID).SelectMany(x => x.RecordList).ToList();
                        infos.Add(info);
                    }
 
                    var path = Path.Combine(y_m_path, "水位-时间模式.xls");
                    Export2Excel(path, infos);
                }
 
                var qs_packets = bllMonitorDataSet.GetSignalRecordPacket(monitorDataSourcesId, qs_ids, year, month);
                if (qs_packets != null && qs_packets.Any())
                {
                    var infos = new List<ExportInfo>();
                    for (int i = 0; i < qs.Count; i++)
                    {
                        var mp = qs[i];
                        var info = new ExportInfo();
                        info.Tag = mp.TagName;
                        info.Index = i;
                        info.SignalRecords = qs_packets.Where(x => x.SignalID == mp.SignalID).SelectMany(x => x.RecordList).ToList();
                        infos.Add(info);
                    }
 
                    var path = Path.Combine(y_m_path, "流量-时间模式.xls");
                    Export2Excel(path, infos);
 
                    var path_csv = Path.Combine(y_m_path, "流量-Scada.csv");
                    ExportCSV(path_csv, infos);
                }
 
 
 
                var hzs_packets = bllMonitorDataSet.GetSignalRecordPacket(monitorDataSourcesId, hzs_ids, year, month);
                if (hzs_packets != null && hzs_packets.Any())
                {
                    var infos = new List<ExportInfo>();
                    for (int i = 0; i < hzs_ids.Count; i++)
                    {
                        var mp = monitorPointExSignalWithSignalTypes.Find(x => x.SignalID == hzs_ids[i]);
                        var info = new ExportInfo();
                        info.Tag = mp.TagName;
                        info.Index = i;
                        if (mp.SignalType == IStation.SignalType.转速)
                        {
                            var nr = temp[mp.SignalID];
                            var old_records = hzs_packets.Where(x => x.SignalID == mp.SignalID).SelectMany(x => x.RecordList).ToList();
                            //info.SignalRecords = old_records.Select(x => new Model.SignalRecord(x.Time, x.Value / nr)).ToList();
                            var records = new List<Model.SignalRecord>(); 
                            foreach (var item in old_records)
                            {
                                var time = item.Time;
                                var value = item.Value / nr;
                                value = value < 0 ? 0 : value;
                                value = value > 1 ? 1 : value;
                                records.Add(new SignalRecord(time, value));
                            }
                            info.SignalRecords = records;
                        }
                        else if (mp.SignalType == IStation.SignalType.频率)
                        {
                            var old_records = hzs_packets.Where(x => x.SignalID == mp.SignalID).SelectMany(x => x.RecordList).ToList();
                            var records = new List<Model.SignalRecord>();
                            foreach (var item in old_records)
                            {
                                var time = item.Time;
                                var value = item.Value / 50;
                                value = value < 0 ? 0 : value;
                                value = value > 1 ? 1 : value;
                                records.Add(new SignalRecord(time, value));
                            }
 
                            info.SignalRecords = records;
                        }
                        else
                        {
                            info.SignalRecords = hzs_packets.Where(x => x.SignalID == mp.SignalID).SelectMany(x => x.RecordList).ToList();
                        }
                        infos.Add(info);
                    }
 
                    var path = Path.Combine(y_m_path, "转速比-时间模式.xls");
                    Export2Excel(path, infos);
                }
 
                var ps_packets = bllMonitorDataSet.GetSignalRecordPacket(monitorDataSourcesId, ps_ids, year, month);
                if (ps_packets != null && ps_packets.Any())
                {
                    var infos = new List<ExportInfo>();
                    for (int i = 0; i < ps.Count; i++)
                    {
                        var mp = ps[i];
                      
                        var info = new ExportInfo();
                        info.Tag = mp.TagName;
                        info.Index = i;
 
                        var signalRecords = ps_packets.Where(x => x.SignalID == mp.SignalID).SelectMany(x => x.RecordList).ToList();
                        //if (signalRecords != null)
                        //{
                        //    for (int j = 0; j < signalRecords.Count; j++)
                        //    {
                        //        var r = signalRecords[j];
                        //        r.Value= Unit.UnitHHelper.fromMPa(Unit.eUnitH.M, r.Value); 
                        //    } 
                        //}
                        info.SignalRecords = signalRecords?.Select(x=>new SignalRecord(x.Time, Unit.UnitHHelper.toM(Unit.eUnitH.MPa, x.Value))).ToList();
                        infos.Add(info);
                    }
 
                    var path = Path.Combine(y_m_path, "压力-Scada.csv");
                    ExportCSV(path, infos);
                }
 
            }
 
            return true;
        }
 
 
        public static bool Export2Excel(string fileName, List<ExportInfo> infos)
        {
 
            //try
            //{
                HSSFWorkbook theBook = new HSSFWorkbook();
                var theSheet1 = theBook.CreateSheet("Sheet1");
 
                IRow rowTile = theSheet1.CreateRow(0);
                foreach (ExportInfo info in infos)
                {
                    rowTile.CreateCell(info.Index).SetCellValue(info.Tag);
                }
 
 
            foreach (var item in infos)
            {
                for (int i = 1; i < item.SignalRecords.Count; i++)
                {
                    var record = item.SignalRecords[i];
 
                    IRow row;
                    if (theSheet1.LastRowNum < i)
                    {
                        row = theSheet1.CreateRow(i);
                    }
                    else
                    {
                        row = theSheet1.GetRow(i);
                    }
 
 
 
                    row.CreateCell(item.Index).SetCellValue(record.Value == IStation.Error.Default ? 0 : record.Value);
                }
            }
 
                //强制Excel在打开时重新计算所有公式
                theSheet1.ForceFormulaRecalculation = true;
                using (FileStream fs = File.OpenWrite(fileName))
                {
                    theBook.Write(fs);
                }
            //}
            //catch (Exception ex)
            //{
 
            //    XtraMessageBox.Show(ex.Message);
            //}
 
            return true;
        }
 
        public static void ExportCSV(string fileName, List<ExportInfo> infos)
        {
            using (var fs = new FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write, FileShare.ReadWrite))
            using (var sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
            {
                sw.WriteLine("ScadaID,Time,Value");
                foreach (var item in infos)
                {
                    for (int i = 0; i < item.SignalRecords.Count; i++)
                    {
                        var r = item.SignalRecords[i];
                        var v = r.Value == IStation.Error.Default ? 0 : r.Value;
                        sw.WriteLine($"{item.Tag},{r.Time:yyyy-MM-dd HH:mm:ss},{v}");
                    }
                }
            }
        }
 
    }
}