Shuxia Ning
2024-11-06 adf8dc1c7cae1b12f486dcdb3d7daf4a5a59ec52
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
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
 
namespace IStation.WinFrmUI
{
    /// <summary>
    /// 导出 WaterDesk 所需数据
    /// </summary>
    public partial class ExportTestHelper
    {
        
        public class TimeValue
        {
            public TimeValue() { }
            public TimeValue(DateTime t,double v) 
            {
                this.Time= t;
                this.Value= v;
            }
            public DateTime Time { get; set; }
            public double Value { get; set; }
 
        }
 
        public class DataColumn
        {
            public string Title { get; set; }
            public int Index { get; set; }
            public List<TimeValue> TimeValueList { get; set; }
        }
         
      static   BLL.StationSignalRecordPacket _packet = new BLL.StationSignalRecordPacket();
 
 
        /// <summary>
        /// 导出全部
        /// </summary>
        /// <param name="monitorDataSourcesId"></param>
        /// <param name="stationId"></param>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="timeStep"></param>
        /// <returns></returns>
        public static bool Export(long monitorDataSourcesId, List<long> monitor_ids, int year, int month, int timeStep = 300)
        {
 
 
            var bllMonitorPoint = new BLL.MonitorPoint();
            var bllMonitorDataSet = new BLL.MonitorDataSet();
 
            var dataColumnDict = new Dictionary<string, List<TimeValue>>(); 
            var stationMonitorPointList = new List<Model.MonitorPointExSignalWithSignalType>();
            var signalIds = new List<long>();
 
            var monitorPoints_station = bllMonitorPoint.GetExSignalWithSignalTypeByIds(monitor_ids);
            //monitorPoints_station = monitorPoints_station?.Where(x => !string.IsNullOrEmpty(x.TagName)).ToList();
             //stationMonitorPointList = monitorPoints_station?.Where(x => x.Flags != null && x.Flags.Contains(IStation.Flags.总站)).ToList();
            stationMonitorPointList = monitorPoints_station;
            if (stationMonitorPointList != null && stationMonitorPointList.Any())
            {
                signalIds.AddRange(stationMonitorPointList.Select(x => x.SignalID));
                foreach (var item in stationMonitorPointList)
                {
                    dataColumnDict[item.Name] = new List<TimeValue>();
                }
            }
 
            signalIds = signalIds.Distinct().ToList();
 
            var bllSummary = new BLL.MonitorDataSetSummary();
            var summaries = bllSummary.GetAllByDate(monitorDataSourcesId, year, month);
            if (summaries == null || !summaries.Any())
                return default;
 
            var minTime = summaries.Min(x => x.MinTime);
            var maxTime = summaries.Max(x => x.MaxTime);
            var totalSeconds = (int)(maxTime - minTime).TotalSeconds;
 
            var startDay = minTime.AddDays(1);
            var days = (maxTime - minTime).Days;
            var capacity = _packet.CalcuCapacity(days, timeStep);// 每个月的数据量
 
 
            var findIndexDict = new Dictionary<long, int>();// 查找数据下标
            var recordDict = new Dictionary<long, double>();// 数据字典
            signalIds.ForEach(x =>
            {
                findIndexDict.Add(x, 0);
                recordDict.Add(x, IStation.Error.Default);
            });
 
            var signalRecordPackets = bllMonitorDataSet.GetSignalRecordPacket(monitorDataSourcesId, signalIds, year, month);
            if (signalRecordPackets == null || !signalRecordPackets.Any())
                return default;
 
 
            for (int i = 0; i <= days; i++)  //按天整理数据
            {
                var day = startDay.AddDays(i);
                for (int seconds = 0; seconds < _packet.TotalSecondsOfDay; seconds += timeStep) //00点的数据不要
                {
                    var realTime = day.AddSeconds(seconds);
                    _packet.GetRecordDict(realTime, signalRecordPackets, findIndexDict, recordDict, timeStep, out bool existRecord);
                    if (!existRecord)
                        continue;
                
                    if (stationMonitorPointList != null && stationMonitorPointList.Any())
                    {
                        foreach (var stationMonitorPoint in stationMonitorPointList)
                        {
                            var time = realTime;
                            var value = recordDict[stationMonitorPoint.SignalID];
                            //if (stationMonitorPoint.SignalType == IStation.SignalType.压力)
                            //{
                            //    value = Model.CurveCalcuHelper.Mpa2M(value);
                            //}
                            //else if (stationMonitorPoint.SignalType == IStation.SignalType.瞬时流量)
                            //{
                            //    value = value < 0 ? 0 : value;
                            //}
                            var timeValue = new TimeValue(time, value);
                            dataColumnDict[stationMonitorPoint.Name].Add(timeValue);
                        }
                    }
 
                }
            }
 
            var mode_excel_data_column_list = new List<DataColumn>();
            var pressure_scada_data_column_list = new List<DataColumn>();
            var flow_scada_data_column_list = new List<DataColumn>();
            var index = 1;
            foreach (var item in dataColumnDict)
            {
                var dataColumn = new DataColumn();
                dataColumn.Title = item.Key;
                dataColumn.Index = index;
                dataColumn.TimeValueList = item.Value; 
                mode_excel_data_column_list.Add(dataColumn);
                index++;
            }
 
            var root_folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "问题排查");
            if (!Directory.Exists(root_folder))
            {
                Directory.CreateDirectory(root_folder);
            }
            var mode_excel_file_name = root_folder + "\\" + $"陈行数据.xls";
            Export2Excel(mode_excel_file_name, mode_excel_data_column_list);
            return true;
        }
 
 
        public static bool Export2Excel(string fileName, List<DataColumn> dataColumnList)
        { 
            //try
            //{
            HSSFWorkbook theBook = new HSSFWorkbook();
            var theSheet1 = theBook.CreateSheet("Sheet1");
 
            IRow rowTile = theSheet1.CreateRow(0);
            foreach (var info in dataColumnList)
            {
                rowTile.CreateCell(info.Index).SetCellValue(info.Title);
            }
             
            foreach (var item in dataColumnList)
            {
                for (int i = 1; i < item.TimeValueList.Count; i++)
                {
                    var record = item.TimeValueList[i - 1];
 
                    IRow row;
                    if (theSheet1.LastRowNum < i)
                    {
                        row = theSheet1.CreateRow(i);
                        row.CreateCell(0).SetCellValue(record.Time.ToString("G"));
                    }
                    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);
            } 
 
            return true;
        }
 
 
        public static void ExportCSV(string fileName, List<DataColumn> dataColumnList)
        {
            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 dataColumnList)
                {
                    for (int i = 0; i < item.TimeValueList.Count; i++)
                    {
                        var r = item.TimeValueList[i];
                        var v = r.Value == IStation.Error.Default ? 0 : r.Value;
                        sw.WriteLine($"{item.Title},{r.Time:yyyy-MM-dd HH:mm:ss},{v}");
                    }
                }
            }
        }
 
    }
}