ningshuxia
2025-03-31 d321346f204a69b1929cc39098a5d88f44509e7b
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
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
 
namespace IStation.DAL
{
    /// <summary>
    /// 泵站监测数据集
    /// </summary>    
    public partial class StationStatisticalRecordPacket
    {
        #region Path
 
        /// <summary>
        /// 查询泵站监测数据集文件夹
        /// </summary> 
        public string GetRootFolder(long monitorDataSourcesId)
        {
            var monitorDataSourcesFolder = FileHelper.GetMonitorDataSourcesFolder(Settings.Project.ID, monitorDataSourcesId);
            if (!Directory.Exists(monitorDataSourcesFolder))
                Directory.CreateDirectory(monitorDataSourcesFolder);
            var stationMonitorDataFolder = Path.Combine(monitorDataSourcesFolder, Settings.File.StationMonitorDataFolder);
            if (!Directory.Exists(stationMonitorDataFolder))
                Directory.CreateDirectory(stationMonitorDataFolder);
            return stationMonitorDataFolder;
        }
 
        /// <summary>
        /// 查询泵站文件夹
        /// </summary>
        public string GetStationFolder(long monitorDataSourcesId, long stationId)
        {
            var rootFolder = GetRootFolder(monitorDataSourcesId);
            var stationFolderPath = Path.Combine(rootFolder, stationId.ToString());
            if (!Directory.Exists(stationFolderPath))
                Directory.CreateDirectory(stationFolderPath);
            return stationFolderPath;
        }
 
        /// <summary>
        /// 查询泵站记录文件路径
        /// </summary>
        public bool GetStationStatisticalRecordPacketFile(long monitorDataSourcesId, long stationId, int year, int month, out string filePath)
        {
            var stationFolderPath = GetStationFolder(monitorDataSourcesId, stationId);
            var fileName = "";
            if (month < 10)
            {
                fileName = $"{year}{Settings.File.FileNameSpacer}0{month}";
            }
            else
            {
                fileName = $"{year}{Settings.File.FileNameSpacer}{month}";
            }
            filePath = Path.Combine(stationFolderPath, fileName + Settings.File.SignalRecordFileExtension);
            return File.Exists(filePath);
        }
 
        #endregion 
 
        #region Get
 
        /// <summary>
        /// 查询泵站月数据列表
        /// </summary> 
        public List<Model.StationStatisticalRecordPacket> Get(long monitorDataSourcesId, long stationId)
        {
            var stationFolder = GetStationFolder(monitorDataSourcesId, stationId);
            var fileInfoList = GetStationStatisticalRecordPacketFileInfoList(stationFolder);
            if (fileInfoList == null || fileInfoList.Count() < 1)
                return default;
 
            var stationSignalRecordPackets = new List<Model.StationStatisticalRecordPacket>(fileInfoList.Count);
            foreach (var fileInfo in fileInfoList)
            {
                var packet = Get(monitorDataSourcesId, stationId, fileInfo.Year, fileInfo.Month);
                if (packet == null)
                    continue;
                stationSignalRecordPackets.Add(packet);
            }
            return stationSignalRecordPackets;
        }
 
 
        /// <summary>
        /// 查询监测数据集
        /// </summary> 
        public Model.StationStatisticalRecordPacket Get(long monitorDataSourcesId, long stationId, int year, int month)
        {
            var valid = GetStationStatisticalRecordPacketFile(monitorDataSourcesId, stationId, year, month, out string filePath);
            if (!valid)
                return default;
            var packet = new Model.StationStatisticalRecordPacket();
            packet.StationID = stationId;
            packet.Year = year;
            packet.Month = month;
            packet.StationStatisticalRecords = new List<Model.StationStatisticalRecord>();
 
            using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            using (var sr = new StreamReader(fs, Encoding.UTF8))
            {
                var strLine = string.Empty;
                while (!string.IsNullOrEmpty(strLine = sr.ReadLine()))
                {
                    var content = FromDsString(strLine);
                    packet.StationStatisticalRecords.Add(content);
                }
            }
            return packet;
        }
 
 
 
        #endregion
 
        #region Save
 
        /// <summary>
        /// 保存
        /// </summary>
        public bool Save(long monitorDataSourcesId, long stationId, IEnumerable<Model.StationStatisticalRecordPacket> list)
        {
            if (list == null || list.Count() < 1)
                return default;
            for (int i = 0; i < list.Count(); i++)
            {
                var dataSet = list.ElementAt(i);
                if (!Save(monitorDataSourcesId, stationId, dataSet))
                {
                    //未做判断
                }
            }
            return true;
        }
 
 
        /// <summary>
        /// 保存
        /// </summary>
        public bool Save(long monitorDataSourcesId, long stationId, Model.StationStatisticalRecordPacket rhs)
        {
            if (rhs == null)
                return default;
            if (rhs.StationStatisticalRecords == null || rhs.StationStatisticalRecords.Count < 1)
                return default;
            GetStationStatisticalRecordPacketFile(monitorDataSourcesId, stationId, rhs.Year, rhs.Month, out string filePath);
            using (var fs = new FileStream(filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, FileShare.ReadWrite))
            using (var sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
            {
                foreach (var record in rhs.StationStatisticalRecords)
                {
                    var strLine = ToDsString(record);
                    sw.WriteLine(strLine);
                }
            }
            return true;
        }
 
        #endregion
 
        /// <summary>
        /// 保存
        /// </summary>
        public void Clear(long monitorDataSourcesId, long stationId)
        {
            var folder = GetStationFolder(monitorDataSourcesId, stationId);
            if (Directory.Exists(folder))
            {
                Directory.Delete(folder, true);
            }
        }
 
    }
}