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);
|
}
|
}
|
|
}
|
}
|