2023-05-26 16:18:28 +08:00
|
|
|
|
using Aitex.Core.RT.DBCore;
|
2023-08-10 16:18:48 +08:00
|
|
|
|
using MECF.Framework.Common.Aitex.Core.Common.DeviceData;
|
2023-08-08 21:15:05 +08:00
|
|
|
|
using MECF.Framework.Common.DataCenter;
|
2023-08-10 16:18:48 +08:00
|
|
|
|
using MECF.Framework.Common.Equipment;
|
2023-05-26 16:18:28 +08:00
|
|
|
|
using Sicentury.Core;
|
|
|
|
|
using System;
|
2023-05-25 19:08:06 +08:00
|
|
|
|
using System.Collections.Generic;
|
2023-05-26 16:18:28 +08:00
|
|
|
|
using System.Data;
|
2023-05-25 19:08:06 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2023-07-11 17:46:32 +08:00
|
|
|
|
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.GasFlow
|
2023-05-25 19:08:06 +08:00
|
|
|
|
{
|
2023-07-11 17:46:32 +08:00
|
|
|
|
/// <summary>
|
2023-07-12 17:48:06 +08:00
|
|
|
|
/// 气体流量统计,根据输入名称和时间来查询
|
2023-07-11 17:46:32 +08:00
|
|
|
|
/// </summary>
|
2023-07-12 17:48:06 +08:00
|
|
|
|
public class GasFlowSqlHelp
|
2023-05-25 19:08:06 +08:00
|
|
|
|
{
|
2023-08-10 16:18:48 +08:00
|
|
|
|
|
|
|
|
|
|
2023-06-25 11:16:57 +08:00
|
|
|
|
/// <summary>
|
2023-08-10 16:18:48 +08:00
|
|
|
|
/// 查询当天的气体体积数据
|
2023-06-25 11:16:57 +08:00
|
|
|
|
/// </summary>
|
2023-08-10 16:18:48 +08:00
|
|
|
|
/// <param name="daySlices">拆分的天</param>
|
|
|
|
|
/// <param name="moduleName">PM名称</param>
|
|
|
|
|
/// <param name="gasFlowSumList">显示列表对象</param>
|
2023-06-25 11:16:57 +08:00
|
|
|
|
/// <returns></returns>
|
2023-08-10 16:18:48 +08:00
|
|
|
|
public static List<GasFlowSum> GetDayGasFlowSum(DateRangeHelper daySlices, string moduleName, List<GasFlowSum> gasFlowSumList)
|
2023-06-05 17:51:20 +08:00
|
|
|
|
{
|
2023-08-10 16:18:48 +08:00
|
|
|
|
List<DataTable> dataTableList = GetAllGasListDaySlices(daySlices, moduleName, gasFlowSumList);
|
|
|
|
|
|
|
|
|
|
string rowStr;
|
2023-06-05 17:51:20 +08:00
|
|
|
|
|
2023-08-10 16:18:48 +08:00
|
|
|
|
List<GasFlowSum> GasFlowrRunVentList = new List<GasFlowSum>();
|
|
|
|
|
|
|
|
|
|
foreach (var item in gasFlowSumList)
|
|
|
|
|
{
|
|
|
|
|
GasFlowrRunVentList.Add(new GasFlowSum() { Name = $"{item.Name}_Run" });
|
|
|
|
|
GasFlowrRunVentList.Add(new GasFlowSum() { Name = $"{item.Name}_Vent" });
|
|
|
|
|
}
|
|
|
|
|
foreach (DataTable ds in dataTableList)
|
|
|
|
|
{
|
|
|
|
|
double timeDifference = 0;
|
|
|
|
|
for (int i = 0; i < ds.Rows.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in GasFlowrRunVentList)
|
|
|
|
|
{
|
|
|
|
|
//$"{moduleName}.GasRealTimeFlow.{item.Name}_Run.FeedBack"
|
|
|
|
|
string property = $"{moduleName}.GasRealTimeFlow.{item.Name}.FeedBack";
|
|
|
|
|
rowStr = ds.Rows[i][property].ToString();
|
|
|
|
|
if (rowStr is not null && rowStr.Length == 0)
|
|
|
|
|
continue;
|
|
|
|
|
double bd = double.Parse(ds.Rows[i][property].ToString());
|
|
|
|
|
if (bd > 0)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (i != ds.Rows.Count - 1)//最后一个数据使用的时间差,是它前一个计算得到的
|
|
|
|
|
{
|
|
|
|
|
var startTime = new DateTime(long.Parse(ds.Rows[i]["time"].ToString()));
|
|
|
|
|
var endTime = new DateTime(long.Parse(ds.Rows[i + 1]["time"].ToString()));
|
|
|
|
|
timeDifference = (endTime - startTime).TotalSeconds;
|
|
|
|
|
}
|
|
|
|
|
item.Volume += bd * timeDifference;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return GasFlowrRunVentList;
|
2023-06-05 17:51:20 +08:00
|
|
|
|
}
|
2023-08-10 16:18:48 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据拆分的天循查询数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="daySlices">拆分的天</param>
|
|
|
|
|
/// <param name="moduleName">PM名称</param>
|
|
|
|
|
/// <param name="gasFlowSumList">显示集合列表</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static List<DataTable> GetAllGasListDaySlices(DateRangeHelper daySlices, string moduleName, List<GasFlowSum> gasFlowSumList)
|
2023-05-26 16:18:28 +08:00
|
|
|
|
{
|
2023-08-08 21:15:05 +08:00
|
|
|
|
List<DataTable> dataTableList = new List<DataTable>();
|
2023-08-10 16:18:48 +08:00
|
|
|
|
var ts = daySlices.Diff;
|
|
|
|
|
for (var day = 0; day <= ts.Days; day++)
|
2023-05-31 10:29:25 +08:00
|
|
|
|
{
|
2023-08-10 16:18:48 +08:00
|
|
|
|
var tblName = $"{daySlices.Start.AddDays(day):yyyyMMdd}.{moduleName}";
|
|
|
|
|
var sql = new StringBuilder();
|
|
|
|
|
// 检查表名是否存在,否则SQL执行出错。
|
|
|
|
|
if (CheckTableExists(tblName))
|
2023-05-31 10:29:25 +08:00
|
|
|
|
{
|
2023-08-10 16:18:48 +08:00
|
|
|
|
sql.Append("select time AS InternalTimeStamp");
|
|
|
|
|
// 添加待查询的列
|
|
|
|
|
|
|
|
|
|
// 添加待查询的列
|
|
|
|
|
foreach (var item in gasFlowSumList)
|
2023-05-31 10:29:25 +08:00
|
|
|
|
{
|
2023-08-10 16:18:48 +08:00
|
|
|
|
sql.Append("," + $"\"{moduleName}.GasRealTimeFlow.{item.Name}_Run.FeedBack\"");
|
2023-05-31 10:29:25 +08:00
|
|
|
|
|
2023-08-10 16:18:48 +08:00
|
|
|
|
sql.Append("," + $"\"{moduleName}.GasRealTimeFlow.{item.Name}_Vent.FeedBack\"");
|
|
|
|
|
}
|
2023-06-25 11:16:57 +08:00
|
|
|
|
|
2023-08-10 16:18:48 +08:00
|
|
|
|
sql.Append($", \"time\"");
|
|
|
|
|
sql.Append($" from \"{tblName}\"");
|
|
|
|
|
|
|
|
|
|
if (day < ts.Days)
|
|
|
|
|
sql.Append(" UNION ");
|
|
|
|
|
sql.Append(
|
|
|
|
|
$" where time between {daySlices.Start.Ticks} and {daySlices.End.Ticks} order by InternalTimeStamp asc");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var dataSet = QueryDataClient.Instance.Service.QueryData(sql.ToString());
|
|
|
|
|
if (dataSet is not null && dataSet.Rows.Count > 0)
|
|
|
|
|
dataTableList.Add(dataSet);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)//查询较早日期时,可能不存在属性会报错
|
|
|
|
|
{
|
2023-06-25 11:16:57 +08:00
|
|
|
|
|
2023-06-07 08:28:42 +08:00
|
|
|
|
}
|
2023-08-10 16:18:48 +08:00
|
|
|
|
|
2023-05-31 10:29:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-10 16:18:48 +08:00
|
|
|
|
|
2023-08-08 21:15:05 +08:00
|
|
|
|
return dataTableList;
|
2023-06-05 17:51:20 +08:00
|
|
|
|
}
|
2023-08-09 16:12:12 +08:00
|
|
|
|
|
2023-08-10 16:18:48 +08:00
|
|
|
|
private static bool CheckTableExists(string tableName)
|
|
|
|
|
{
|
|
|
|
|
var sql =
|
|
|
|
|
$"SELECT EXISTS ( SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = '{tableName}' )";
|
|
|
|
|
var table = QueryDataClient.Instance.Service.QueryData(sql);
|
|
|
|
|
if (table == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (table.Rows.Count <= 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var value = table.Rows[0]["exists"].ToString();
|
|
|
|
|
if (value.ToLower() == "true")
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static List<DataTable> GetDataSetDaySlices(DateRangeHelper daySlices, string tableName, string propertyCmd, string property)
|
2023-08-09 16:12:12 +08:00
|
|
|
|
{
|
|
|
|
|
List<DataTable> dataTableList = new List<DataTable>();
|
|
|
|
|
var ts = daySlices.Diff;
|
|
|
|
|
for (var day = 0; day <= ts.Days; day++)
|
|
|
|
|
{
|
|
|
|
|
var tblName = $"{daySlices.Start.AddDays(day):yyyyMMdd}.{tableName}";
|
|
|
|
|
var sql = new StringBuilder();
|
|
|
|
|
// 检查表名是否存在,否则SQL执行出错。
|
|
|
|
|
if (CheckTableExists(tblName))
|
|
|
|
|
{
|
|
|
|
|
sql.Append("select time AS InternalTimeStamp");
|
|
|
|
|
// 添加待查询的列
|
|
|
|
|
sql.Append(propertyCmd);
|
|
|
|
|
sql.Append($", \"time\"");
|
|
|
|
|
sql.Append($" from \"{tblName}\"");
|
|
|
|
|
|
|
|
|
|
if (day < ts.Days)
|
|
|
|
|
sql.Append(" UNION ");
|
|
|
|
|
sql.Append(
|
|
|
|
|
$" where time between {daySlices.Start.Ticks} and {daySlices.End.Ticks} and '{property}' is not NULL order by InternalTimeStamp asc");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var dataSet = QueryDataClient.Instance.Service.QueryData(sql.ToString());
|
|
|
|
|
if (dataSet is not null && dataSet.Rows.Count > 0)
|
|
|
|
|
dataTableList.Add(dataSet);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)//查询较早日期时,可能不存在属性会报错
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dataTableList;
|
|
|
|
|
}
|
2023-08-10 16:18:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-09 16:12:12 +08:00
|
|
|
|
public static double GetData(List<DataTable> dataTableList, string property)
|
2023-06-05 17:51:20 +08:00
|
|
|
|
{
|
2023-06-25 11:16:57 +08:00
|
|
|
|
double values = 0;
|
2023-06-07 08:28:42 +08:00
|
|
|
|
string rowStr;
|
2023-08-08 21:15:05 +08:00
|
|
|
|
foreach (DataTable ds in dataTableList)
|
2023-06-07 08:28:42 +08:00
|
|
|
|
{
|
2023-06-25 11:16:57 +08:00
|
|
|
|
double timeDifference = 0;
|
2023-08-08 21:15:05 +08:00
|
|
|
|
for (int i = 0; i < ds.Rows.Count; i++)
|
2023-06-05 17:51:20 +08:00
|
|
|
|
{
|
2023-08-08 21:15:05 +08:00
|
|
|
|
rowStr = ds.Rows[i][property].ToString();
|
2023-06-25 11:16:57 +08:00
|
|
|
|
if (rowStr is not null && rowStr.Length == 0)
|
|
|
|
|
continue;
|
2023-08-08 21:15:05 +08:00
|
|
|
|
double bd = double.Parse(ds.Rows[i][property].ToString());
|
2023-06-25 11:16:57 +08:00
|
|
|
|
if (bd > 0)
|
2023-06-05 17:51:20 +08:00
|
|
|
|
{
|
2023-06-07 08:28:42 +08:00
|
|
|
|
|
2023-08-08 21:15:05 +08:00
|
|
|
|
if (i != ds.Rows.Count - 1)//最后一个数据使用的时间差,是它前一个计算得到的
|
2023-06-25 11:16:57 +08:00
|
|
|
|
{
|
2023-08-08 21:15:05 +08:00
|
|
|
|
var startTime = new DateTime(long.Parse(ds.Rows[i]["time"].ToString()));
|
|
|
|
|
var endTime = new DateTime(long.Parse(ds.Rows[i + 1]["time"].ToString()));
|
2023-06-25 11:16:57 +08:00
|
|
|
|
timeDifference = (endTime - startTime).TotalSeconds;
|
|
|
|
|
}
|
|
|
|
|
values += bd * timeDifference;
|
|
|
|
|
}
|
2023-06-07 08:28:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-05 17:51:20 +08:00
|
|
|
|
return values;
|
2023-05-26 16:18:28 +08:00
|
|
|
|
}
|
2023-06-25 11:16:57 +08:00
|
|
|
|
|
2023-08-10 16:18:48 +08:00
|
|
|
|
|
2023-05-25 19:08:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|