2023-05-26 16:18:28 +08:00
|
|
|
|
using Aitex.Core.RT.DBCore;
|
|
|
|
|
using MECF.Framework.Common.DataCenter;
|
|
|
|
|
using Sicentury.Core.EventArgs;
|
|
|
|
|
using Sicentury.Core.Tree;
|
|
|
|
|
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;
|
2023-05-26 16:18:28 +08:00
|
|
|
|
using System.Threading;
|
2023-05-25 19:08:06 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2023-05-26 16:18:28 +08:00
|
|
|
|
using MECF.Framework.Common.Equipment;
|
2023-06-05 17:51:20 +08:00
|
|
|
|
using System.Drawing.Text;
|
2023-06-07 08:28:42 +08:00
|
|
|
|
using Aitex.Core.RT.Log;
|
2023-05-25 19:08:06 +08:00
|
|
|
|
|
|
|
|
|
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.MfcCalculation
|
|
|
|
|
{
|
|
|
|
|
public class MfcSqlHelp
|
|
|
|
|
{
|
2023-06-25 11:16:57 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据名称和时间段查询结果
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="module">模块名称</param>
|
|
|
|
|
/// <param name="name"></param>
|
|
|
|
|
/// <param name="start"></param>
|
|
|
|
|
/// <param name="end"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static double Query(string module, string name, DateTime start, DateTime end)
|
2023-06-05 17:51:20 +08:00
|
|
|
|
{
|
2023-06-25 11:16:57 +08:00
|
|
|
|
var daySlices = DateRangeHelper.SplitInToHours(new DateRangeHelper(start, end), 12);
|
2023-06-05 17:51:20 +08:00
|
|
|
|
string tableName = module;
|
2023-06-25 11:16:57 +08:00
|
|
|
|
string propertyCmd = "," + $"\"{module}.{name}.FeedBack\"";
|
|
|
|
|
string property = $"{module}.{name}.FeedBack";
|
2023-06-05 17:51:20 +08:00
|
|
|
|
|
2023-06-25 11:16:57 +08:00
|
|
|
|
return GetData(GetDataSetList(daySlices, tableName, propertyCmd, property), property);
|
2023-06-05 17:51:20 +08:00
|
|
|
|
}
|
2023-06-25 11:16:57 +08:00
|
|
|
|
private static List<DataSet> GetDataSetList(IEnumerable<DateRangeHelper> daySlices, string tableName, string propertyCmd, string property)
|
2023-05-26 16:18:28 +08:00
|
|
|
|
{
|
2023-06-05 17:51:20 +08:00
|
|
|
|
List<DataSet> DataSetList = new List<DataSet>();
|
2023-05-31 10:29:25 +08:00
|
|
|
|
foreach (var range in daySlices)
|
|
|
|
|
{
|
|
|
|
|
var ts = range.Diff;
|
|
|
|
|
for (var day = 0; day <= ts.Days; day++)
|
|
|
|
|
{
|
|
|
|
|
var tblName = $"{range.Start.AddDays(day):yyyyMMdd}.{tableName}";
|
|
|
|
|
var sql = new StringBuilder();
|
|
|
|
|
// 检查表名是否存在,否则SQL执行出错。
|
2023-06-05 17:51:20 +08:00
|
|
|
|
if (CheckTableExists(tblName))
|
2023-05-31 10:29:25 +08:00
|
|
|
|
{
|
2023-06-05 17:51:20 +08:00
|
|
|
|
sql.Append("select time AS InternalTimeStamp");
|
2023-05-31 10:29:25 +08:00
|
|
|
|
// 添加待查询的列
|
2023-06-05 17:51:20 +08:00
|
|
|
|
sql.Append(propertyCmd);
|
2023-06-25 11:16:57 +08:00
|
|
|
|
sql.Append($", \"time\"");
|
2023-06-05 17:51:20 +08:00
|
|
|
|
sql.Append($" from \"{tblName}\"");
|
2023-05-31 10:29:25 +08:00
|
|
|
|
|
|
|
|
|
if (day < ts.Days)
|
|
|
|
|
sql.Append(" UNION ");
|
2023-06-07 08:28:42 +08:00
|
|
|
|
sql.Append(
|
2023-06-05 17:51:20 +08:00
|
|
|
|
$" where time between {range.Start.Ticks} and {range.End.Ticks} and '{property}' is not NULL order by InternalTimeStamp asc");
|
2023-06-07 08:28:42 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
DataSet dataSet = DB.ExecuteDataset(sql.ToString());
|
|
|
|
|
if (dataSet is not null && dataSet.Tables[0].Rows.Count > 0)
|
|
|
|
|
DataSetList.Add(dataSet);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)//查询较早日期时,可能不存在属性会报错
|
|
|
|
|
{
|
2023-06-25 11:16:57 +08:00
|
|
|
|
|
2023-06-07 08:28:42 +08:00
|
|
|
|
}
|
2023-06-25 11:16:57 +08:00
|
|
|
|
|
2023-06-07 08:28:42 +08:00
|
|
|
|
}
|
2023-05-31 10:29:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-05 17:51:20 +08:00
|
|
|
|
return DataSetList;
|
|
|
|
|
}
|
2023-06-25 11:16:57 +08:00
|
|
|
|
private static double GetData(List<DataSet> dataSetList, 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;
|
|
|
|
|
foreach (DataSet ds in dataSetList)
|
|
|
|
|
{
|
2023-06-25 11:16:57 +08:00
|
|
|
|
double timeDifference = 0;
|
|
|
|
|
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
|
2023-06-05 17:51:20 +08:00
|
|
|
|
{
|
2023-06-25 11:16:57 +08:00
|
|
|
|
rowStr = ds.Tables[0].Rows[i][property].ToString();
|
|
|
|
|
if (rowStr is not null && rowStr.Length == 0)
|
|
|
|
|
continue;
|
|
|
|
|
double bd = double.Parse(ds.Tables[0].Rows[i][property].ToString());
|
|
|
|
|
if (bd > 0)
|
2023-06-05 17:51:20 +08:00
|
|
|
|
{
|
2023-06-07 08:28:42 +08:00
|
|
|
|
|
2023-06-25 11:16:57 +08:00
|
|
|
|
if (i != ds.Tables[0].Rows.Count - 1)//最后一个数据使用的时间差,是它前一个计算得到的
|
|
|
|
|
{
|
|
|
|
|
var startTime = new DateTime(long.Parse(ds.Tables[0].Rows[i]["time"].ToString()));
|
|
|
|
|
var endTime = new DateTime(long.Parse(ds.Tables[0].Rows[i + 1]["time"].ToString()));
|
|
|
|
|
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-05-26 16:18:28 +08:00
|
|
|
|
private static bool CheckTableExists(string tableName)
|
|
|
|
|
{
|
|
|
|
|
var sql =
|
2023-06-05 17:51:20 +08:00
|
|
|
|
$"SELECT EXISTS ( SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = '{tableName}' )";
|
2023-05-26 16:18:28 +08:00
|
|
|
|
var table = DB.ExecuteDataset(sql);
|
|
|
|
|
if (table == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (table.Tables[0].Rows.Count <= 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-06-05 17:51:20 +08:00
|
|
|
|
var value = table.Tables[0].Rows[0]["exists"].ToString();
|
2023-05-26 16:18:28 +08:00
|
|
|
|
if (value.ToLower() == "true")
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-05-25 19:08:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|