Shuxia Ning
2025-02-13 2f1cbec203dcff25df7a5c2b51b13ec558f2c3db
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
using System.Collections.Generic;
using System.IO;
using System.Linq;
 
namespace IStation.DAL
{
    /// <summary>
    /// 监测月数据集
    /// </summary>    
    public partial class StationMonthSignalRecordPacket
    {
        #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 stationMonthDataFolder = Path.Combine(monitorDataSourcesFolder, Settings.File.StationMonthDataFolder);
            if (!Directory.Exists(stationMonthDataFolder))
                Directory.CreateDirectory(stationMonthDataFolder);
            return stationMonthDataFolder;
        }
 
        /// <summary>
        /// 查询日期文件夹
        /// </summary>
        public string GetYearMonthFolder(long monitorDataSourcesId, int year, int month)
        {
            var monitorDataSetFolder = GetRootFolder(monitorDataSourcesId);
            var timeFolderName = "";
            if (month < 10)
            {
                timeFolderName = $"{year}{Settings.File.FileNameSpacer}0{month}";
            }
            else
            {
                timeFolderName = $"{year}{Settings.File.FileNameSpacer}{month}";
            }
            var timeFolderPath = Path.Combine(monitorDataSetFolder, timeFolderName);
            if (!Directory.Exists(timeFolderPath))
                Directory.CreateDirectory(timeFolderPath);
            return timeFolderPath;
        }
 
        /// <summary>
        /// 查询泵站月记录文件路径
        /// </summary>
        public bool GetStationMonthSignalRecordPacketFile(long monitorDataSourcesId, long stationId, int year, int month, out string filePath)
        {
            var timeFolder = GetYearMonthFolder(monitorDataSourcesId, year, month);
            var stationFolderPath = Path.Combine(timeFolder, stationId.ToString());
            if (!Directory.Exists(stationFolderPath))
                Directory.CreateDirectory(stationFolderPath);
            filePath = Path.Combine(stationFolderPath, stationId.ToString() + Settings.File.SignalRecordFileExtension);
            return File.Exists(filePath);
        }
 
        #endregion
 
        #region Path  
 
        /// <summary>
        /// 查询泵站月信号记录文件夹
        /// </summary> 
        public string GetRootFolder(long monitorDataSourcesId, long stationId)
        {
 
            var monitorDataSetFolder = Path.Combine(monitorDataSourcesFolder, Settings.File.StationMonthDataFolder, stationId.ToString());
            if (!Directory.Exists(monitorDataSetFolder))
                Directory.CreateDirectory(monitorDataSetFolder);
            return monitorDataSetFolder;
        }
 
        /// <summary>
        /// 查询泵站月信号记录文件路径
        /// </summary>
        public string GetStationMonthSignalRecordFile(long monitorDataSourcesId, long stationId, int year, int month, int recordCount)
        {
            var monitorPointFolder = GetRootFolder(monitorDataSourcesId, stationId);
            var timeFolderName = "";
            if (month < 10)
            {
                timeFolderName = $"{year}{Settings.File.FileNameSpacer}0{month}{Settings.File.FileNameSpacer}{recordCount}";
            }
            else
            {
                timeFolderName = $"{year}{Settings.File.FileNameSpacer}{month}{Settings.File.FileNameSpacer}{recordCount}";
            }
            var fileName = Path.Combine(monitorPointFolder, timeFolderName + Settings.File.FileExtension);
            return fileName;
        }
 
 
        #endregion
 
        #region Get
 
        /// <summary>
        /// 查询监测数据集列表
        /// </summary> 
        public List<Model.StationMonthSignalRecordPacket> Get(long monitorDataSourcesId, long stationId)
        {
            var rootFolder = this.GetRootFolder(monitorDataSourcesId, stationId);
            var fileInfoList = this.GetMonthSignalRecordFileInfoList(rootFolder);
            if (fileInfoList == null || fileInfoList.Count() < 1)
                return default;
 
            var monthSignalRecordPackets = new List<Model.StationMonthSignalRecordPacket>(fileInfoList.Count);
            foreach (var fileInfo in fileInfoList)
            {
                var packet = Get(monitorDataSourcesId, stationId, fileInfo.Year, fileInfo.Month, fileInfo.RecordCount);
                if (packet == null)
                    continue;
                monthSignalRecordPackets.Add(packet);
            }
            return monthSignalRecordPackets;
        }
 
 
        /// <summary>
        /// 查询监测数据集
        /// </summary> 
        public Model.StationMonthSignalRecordPacket Get(long monitorDataSourcesId, long stationId, int year, int month, int recordCount)
        {
            var filePath = this.GetStationMonthSignalRecordFile(monitorDataSourcesId, stationId, year, month, recordCount);
            if (!File.Exists(filePath))
                return default;
            var json = File.ReadAllText(filePath);
            var monthSignalRecordPacket = JsonHelper.Json2Object<Model.StationMonthSignalRecordPacket>(json);
            return monthSignalRecordPacket;
        }
 
 
        #endregion
 
        #region Save
 
        /// <summary>
        /// 保存
        /// </summary>
        public bool Save(long monitorDataSourcesId, long stationId, IEnumerable<Model.StationMonthSignalRecordPacket> 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.StationMonthSignalRecordPacket rhs)
        {
            if (rhs == null)
                return default;
            if (rhs.StationSignalRecords == null || rhs.StationSignalRecords.Count < 1)
                return default;
            var filePath = GetStationMonthSignalRecordFile(monitorDataSourcesId, stationId, rhs.Year, rhs.Month, rhs.StationSignalRecords.Count);
            var json = JsonHelper.Object2Json(rhs);
            File.WriteAllText(filePath, json);
            return true;
        }
 
        #endregion
 
        /// <summary>
        /// 保存
        /// </summary>
        public void Clear(long monitorDataSourcesId, long stationId)
        {
            var folder = GetRootFolder(monitorDataSourcesId, stationId);
            if (Directory.Exists(folder))
            {
                Directory.Delete(folder, true);
            }
        }
 
    }
}