2023-12-06 13:52:01 +08:00
using Aitex.Core.RT.Log ;
using DocumentFormat.OpenXml.InkML ;
using MECF.Framework.Common.DataCenter ;
using MECF.Framework.Common.Equipment ;
2023-12-05 08:51:05 +08:00
using Sicentury.Core ;
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Linq ;
using System.Text ;
namespace MECF.Framework.UI.Client.CenterViews.DataLogs.ProcessHistory
{
public class ProcessHistorySqlHelp
{
public static List < RecipeProcessHistoryData > QueryRecipeProcessHistoryData ( string starTime , string endTime , string pm )
{
string cmd = $"select * from process_data where process_in='{pm}' and process_begin_time between '{starTime}' and '{endTime}' order by process_begin_time desc" ;
return GetPMCoatingList ( cmd ) ;
}
2023-12-05 13:59:28 +08:00
//**********************************************时间区间内Recipe运行记录查询*************************************************************
2023-12-05 08:51:05 +08:00
private static List < RecipeProcessHistoryData > GetPMCoatingList ( string cmd )
{
List < RecipeProcessHistoryData > recipeProcessHistoryDataList = new List < RecipeProcessHistoryData > ( ) ;
try
{
var dt = QueryDataClient . Instance . Service . QueryData ( cmd ) ; //只能界面用
if ( dt = = null | | dt . Rows . Count = = 0 )
return null ;
for ( int i = 0 ; i < dt . Rows . Count ; i + + )
{
RecipeProcessHistoryData recipeProcessHistoryData = new RecipeProcessHistoryData ( )
{
PmModel = dt . Rows [ i ] [ "process_in" ] . ToString ( ) ,
XPath = dt . Rows [ i ] [ "recipe_name" ] . ToString ( ) ,
Status = dt . Rows [ i ] [ "process_status" ] . ToString ( ) ,
ProcessGuid = dt . Rows [ i ] [ "guid" ] . ToString ( ) ,
BeginTime = dt . Rows [ i ] [ "process_begin_time" ] . ToString ( ) ,
EndTime = dt . Rows [ i ] [ "process_end_time" ] . ToString ( )
} ;
recipeProcessHistoryDataList . Add ( recipeProcessHistoryData ) ;
}
}
catch ( Exception )
{
return null ;
}
return recipeProcessHistoryDataList ;
}
2023-12-05 13:59:28 +08:00
//**********************************************选中Recipe时, 查询当时记录的总的步骤流程*************************************************
2023-12-05 08:51:05 +08:00
public static List < ProcessHistoryData > QueryProcessHistoryData ( string processGuid , string recipeName )
{
string cmd = $"select * from recipe_step_data where process_data_guid='{processGuid}'" ;
return GetProcessHistoryDataList ( cmd , recipeName ) ;
}
private static List < ProcessHistoryData > GetProcessHistoryDataList ( string cmd , string recipeName )
{
List < ProcessHistoryData > processHistoryDataList = new List < ProcessHistoryData > ( ) ;
try
{
var dt = QueryDataClient . Instance . Service . QueryData ( cmd ) ;
if ( dt = = null | | dt . Rows . Count = = 0 )
return null ;
for ( int i = 0 ; i < dt . Rows . Count ; i + + )
{
ProcessHistoryData recipeProcessHistoryData = new ProcessHistoryData ( )
{
StepBeginTime = dt . Rows [ i ] [ "step_begin_time" ] . ToString ( ) ,
StepEndTime = dt . Rows [ i ] [ "step_end_time" ] . ToString ( ) ,
RecipeName = recipeName ,
StepName = dt . Rows [ i ] [ "step_name" ] . ToString ( ) ,
StepTime = dt . Rows [ i ] [ "step_time" ] . ToString ( ) ,
StepNumber = dt . Rows [ i ] [ "step_number" ] . ToString ( )
} ;
processHistoryDataList . Add ( recipeProcessHistoryData ) ;
}
}
catch ( Exception )
{
return null ;
}
return processHistoryDataList ;
}
2023-12-05 13:59:28 +08:00
//************************************************单步时硬件数据结果查询*******************************************************************
2023-12-05 08:51:05 +08:00
/// <summary>
/// 查询气体体积数据
/// </summary>
/// <param name="daySlices">拆分的天</param>
/// <param name="moduleName">PM名称</param>
/// <param name="gasFlowSumList">显示列表对象</param>
/// <returns></returns>
2023-12-05 13:59:28 +08:00
public static List < ProcessHistoryDeviceData > GetProcessHistoryDeviceData ( IEnumerable < DateRangeHelper > daySlices , string moduleName , List < ProcessHistoryItem > processHistoryItemList )
2023-12-05 08:51:05 +08:00
{
2023-12-05 13:59:28 +08:00
List < DataTable > dataTableList = new List < DataTable > ( ) ;
foreach ( var daySlice in daySlices ) //拆分天
{
dataTableList . Add ( GetProcessHistoryDeviceDataListDaySlices ( daySlice , moduleName , processHistoryItemList ) ) ; //拆分后,每次查询结果放入集合中
}
2023-12-05 08:51:05 +08:00
List < ProcessHistoryDeviceData > processHistoryDeviceDataList = new List < ProcessHistoryDeviceData > ( ) ;
foreach ( var item in processHistoryItemList )
{
var phd = new ProcessHistoryDeviceData ( ) ;
phd . DeviceName = item . Display ;
phd . Unit = item . Unit ;
foreach ( DataTable ds in dataTableList )
{
for ( int i = 0 ; i < ds . Rows . Count ; i + + )
{
2023-12-05 13:59:28 +08:00
if ( ! item . IsPropertyExists )
continue ;
2023-12-05 08:51:05 +08:00
string value = ds . Rows [ i ] [ $"{moduleName}.{item.Property}" ] . ToString ( ) ;
if ( value is not null & & value . Length = = 0 )
continue ;
2023-12-06 13:52:01 +08:00
phd . DeviceValue . Add ( float . Parse ( value ) ) ; //历史数据点
phd . TimeStamp . Add ( ds . Rows [ i ] [ "time" ] . ToString ( ) ) ; //时间戳
2023-12-05 08:51:05 +08:00
}
}
phd . CalculateData ( ) ;
processHistoryDeviceDataList . Add ( phd ) ;
}
return processHistoryDeviceDataList ;
}
/// <summary>
/// 根据拆分的天循查询数据
/// </summary>
/// <param name="daySlices">拆分的天</param>
/// <param name="moduleName">PM名称</param>
/// <param name="gasFlowSumList">显示集合列表</param>
/// <returns></returns>
2023-12-05 13:59:28 +08:00
private static DataTable GetProcessHistoryDeviceDataListDaySlices ( DateRangeHelper daySlices , string moduleName , List < ProcessHistoryItem > processHistoryItemList )
2023-12-05 08:51:05 +08:00
{
2023-12-05 13:59:28 +08:00
DataTable dataTable = new ( ) ;
2023-12-05 08:51:05 +08:00
var ts = daySlices . Diff ;
for ( var day = 0 ; day < = ts . Days ; day + + )
{
var tblName = $"{daySlices.Start.AddDays(day):yyyyMMdd}.{moduleName}" ;
var sql = new StringBuilder ( ) ;
// 检查表名是否存在, 否则SQL执行出错。
if ( CheckTableExists ( tblName ) )
{
sql . Append ( "select time AS InternalTimeStamp" ) ;
// 添加待查询的列
foreach ( var item in processHistoryItemList )
{
2023-12-05 13:59:28 +08:00
// 检查表中是否存在对应属性, 否则SQL执行出错。
if ( CheckPropertyExists ( tblName , $"{moduleName}.{item.Property}" ) )
{
item . IsPropertyExists = true ;
sql . Append ( "," + $"\" { moduleName } . { item . Property } \ "" ) ;
}
else //不查询状态标记
item . IsPropertyExists = false ;
2023-12-05 08:51:05 +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
{
2023-12-05 13:59:28 +08:00
var _dataTable = QueryDataClient . Instance . Service . QueryData ( sql . ToString ( ) ) ;
if ( _dataTable is not null & & _dataTable . Rows . Count > 0 )
dataTable = _dataTable ;
2023-12-05 08:51:05 +08:00
}
catch ( Exception ex ) //查询较早日期时,可能不存在属性会报错
{
}
}
}
2023-12-05 13:59:28 +08:00
return dataTable ;
2023-12-05 08:51:05 +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 ;
}
2023-12-05 13:59:28 +08:00
private static bool CheckPropertyExists ( string tableName , string property )
2023-12-05 08:51:05 +08:00
{
var sql =
2023-12-05 13:59:28 +08:00
$"select count(1) from information_schema.columns WHERE table_name = '{tableName}' and column_name = '{property}'" ;
2023-12-05 08:51:05 +08:00
var table = QueryDataClient . Instance . Service . QueryData ( sql ) ;
if ( table = = null )
return false ;
if ( table . Rows . Count < = 0 )
return false ;
2023-12-05 13:59:28 +08:00
var value = int . Parse ( table . Rows [ 0 ] [ "count" ] . ToString ( ) ) ;
if ( value = = 0 )
return false ;
2023-12-05 08:51:05 +08:00
return true ;
}
}
}