Sic.Framework-Nanjing-Baishi/MECF.Framework.RT.Equipment.../HardwareUnits/MfcCalculation/MfcSqlHelp.cs

110 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Aitex.Core.RT.DBCore;
using MECF.Framework.Common.DataCenter;
using Sicentury.Core.EventArgs;
using Sicentury.Core.Tree;
using Sicentury.Core;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MECF.Framework.Common.Equipment;
using System.Drawing.Text;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.MfcCalculation
{
public class MfcSqlHelp
{
public static List<double> Query(string module,string tabName, string name,DateTime start,DateTime end)
{
var daySlices =DateRangeHelper.SplitInToHours(new DateRangeHelper(start, end), 12);
string tableName = module;
string propertyCmd = "," + $"\"{module}.{tabName}.{name}.FeedBack\"";
string property = $"{module}.{tabName}.{name}.FeedBack";
return GetData(GetDataSetList(daySlices, tableName, propertyCmd, property), property);
}
private static List<DataSet> GetDataSetList(IEnumerable<DateRangeHelper> daySlices, string tableName, string propertyCmd,string property)
{
List<string> cmdList = new List<string>();
List<DataSet> DataSetList = new List<DataSet>();
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执行出错。
if (CheckTableExists(tblName))
{
sql.Append("select time AS InternalTimeStamp");
// 添加待查询的列
sql.Append(propertyCmd);
sql.Append($" from \"{tblName}\"");
if (day < ts.Days)
sql.Append(" UNION ");
}
string st = sql.ToString();
sql.Append(
$" where time between {range.Start.Ticks} and {range.End.Ticks} and '{property}' is not NULL order by InternalTimeStamp asc");
//DataSet table = DB.ExecuteDataset(sql.ToString());
DataSetList.Add(DB.ExecuteDataset(sql.ToString()));
cmdList.Add(sql.ToString());
}
}
return DataSetList;
}
private static List<double> GetData(List<DataSet> dataSetList, string property)
{
string str;
List<double> values = new List<double>();
foreach (DataSet ds in dataSetList)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
if (ds.Tables[0].Rows[i][property].ToString().Length == 0)
continue;
double bd = double.Parse(ds.Tables[0].Rows[i][property].ToString());
if (bd > 0)
{
values.Add(bd);
}
}
}
}
return values;
}
private static bool CheckTableExists(string tableName)
{
var sql =
$"SELECT EXISTS ( SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = '{tableName}' )";
var table = DB.ExecuteDataset(sql);
if (table == null)
return false;
if (table.Tables[0].Rows.Count <= 0)
return false;
var value = table.Tables[0].Rows[0]["exists"].ToString();
if (value.ToLower() == "true")
return true;
return false;
}
}
}