using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mesnac.Action.Base;
using System.IO;
using System.Windows.Forms;
using Mesnac.Action.Feeding.BasicInfo;
using Mesnac.Codd.Session;
namespace Mesnac.Action.Feeding.FinishBatch
{
///
/// 停机记录处理类
///
public class StartStopRecord //: FeedingAction, IAction
{
private static bool _isFirstRun = true; //是否首次执行
private static DateTime? _lastSaveEndTime = null;
private static bool _isInsert = false; //存盘后,是否进行过停机记录的追加
private static DateTime _lastUpdateDateTime = DateTime.Now; //保存最后一次更新停机记录的时间
private static DateTime _lastMixRunTime = DateTime.Now; //保存密炼运行的最后时间
///
/// 最后一次存盘结束时间
///
public static DateTime? LastSaveEndTime
{
get
{
if (StartStopRecord._lastSaveEndTime == null)
{
FeedingAction action = new FeedingAction();
string lastSaveEndTime = action.GetSysValue("StopRecord_LastSaveEndTime");
if (!String.IsNullOrEmpty(lastSaveEndTime))
{
StartStopRecord._lastSaveEndTime = Convert.ToDateTime(lastSaveEndTime);
}
}
return StartStopRecord._lastSaveEndTime;
}
set
{
StartStopRecord._lastSaveEndTime = value;
FeedingAction action = new FeedingAction();
action.UpdateSysValue("StopRecord_LastSaveEndTime", String.Format("{0:yyyy-MM-dd HH:mm:ss}", value));
}
}
public static DateTime LastMixRunTime
{
get { return StartStopRecord._lastMixRunTime; }
set { StartStopRecord._lastMixRunTime = value; }
}
///
/// 存盘后,是否进行过停机记录的追加
///
public static bool IsInsert
{
get { return StartStopRecord._isInsert; }
set { StartStopRecord._isInsert = value; }
}
///
/// 停机记录业务入口
///
public static void Run()
{
FeedingAction action = new FeedingAction();
if (StartStopRecord._isFirstRun)
{
//首次执行订阅存盘信号完成事件
FinishBatch.SaveFinishBatch.OnFinishBatchSave += delegate(object sender, EventArgs e)
{
StartStopRecord.LastSaveEndTime = DateTime.Now; //在存盘信号完成时,更新最后存盘结束时间
StartStopRecord._isInsert = false;
action.UpdateSysValue("StopRecord_IsInsert", "false"); //保存是否进行过停机记录
};
DateTime dt = DateTime.Now;
DateTime.TryParse(StartStopRecord.LastSaveEndTime.ToString(),out dt); //首次运行把最后存盘时间设为密炼最后运行时间
StartStopRecord.LastMixRunTime = dt;
StartStopRecord._isFirstRun = false;
string stopRecord_IsInsert = action.GetSysValue("StopRecord_IsInsert");
if (!String.IsNullOrEmpty(stopRecord_IsInsert))
{
bool.TryParse(stopRecord_IsInsert, out StartStopRecord._isInsert);
}
}
if (StartStopRecord.LastSaveEndTime == null)
{
//未接收到过存盘信号
return;
}
if (action.NetType == Mesnac.Action.Base.BaseAction.NetTypes.Net)
{
try
{
//网络版
//bool isConnect = PlanCommon.PingIpOrDomainName(action.GetConfigValue("ServerIP", "127.0.0.1"));
//bool isConnect = PlanCommon.IsCanConnectServer();
//if (!isConnect)
//{
// //连接服务器数据库失败...
// ICSharpCode.Core.LoggingService.Error(action.Language(33));
// return;
//}
DbHelper serverHelper = action.NewDbHelper(Basic.DataSourceFactory.MCDbType.Server);
if (serverHelper == null)
{
ICSharpCode.Core.LoggingService.Error("获取网络数据连接失败,无法记录停机记录...");
return;
}
//1、获取最后一次存盘时间(保存至配置文件),最后存盘时间>=3分钟 && 密炼时间(PLC获取)== 0 时插入停机记录,并且每分钟更新一次停机结束时间
//System.TimeSpan ts = DateTime.Now - Convert.ToDateTime(StartStopRecord.LastSaveEndTime);
System.TimeSpan ts = DateTime.Now - StartStopRecord.LastMixRunTime;
//if (SaveDataPerLotHandler.RecipeTime == 0 && StartStopRecord._isInsert == false && ts.TotalSeconds >= 180)
if (PlcData.Instance.CurrentMixingTime.LastValue.ToInt() == 0 && StartStopRecord._isInsert == false && ts.TotalSeconds >= 180)
{
if (PlanCommon.PlanLog == null)
{
ICSharpCode.Core.LoggingService.Error("获取不到计划日志,无法记录停机记录...");
return;
}
//向网络库Eqm_EquipStop表中添加停机记录
try
{
//获取当前班组名称
string className = PlanCommon.GetClassName(Basic.DataSourceFactory.MCDbType.Server, 1, PlanCommon.PlanLog.LastSelectDate, PlanCommon.PlanLog.LastSelectShiftID);
serverHelper.ClearParameter();
serverHelper.CommandType = System.Data.CommandType.Text;
string strSql = "if not exists(select * from Ppt_pmdownrecord where Equip_code = @EquipCode and convert(varchar(19),Mp_startdate,120) = @StartTime) ";
strSql += " insert into Ppt_pmdownrecord (Equip_code,shift_id,shift_Class,Mp_startdate,Mp_enddate) values(@EquipCode,@ShiftID,@ClassName,@StartTime,getdate())";//,ShiftID,ClassName
serverHelper.CommandText = strSql;
serverHelper.AddParameter("@EquipCode", action.CurrEquipCode);
serverHelper.AddParameter("@ShiftID", PlanCommon.PlanLog.LastSelectShiftID);
serverHelper.AddParameter("@ClassName", className);
serverHelper.AddParameter("@StartTime", String.Format("{0:yyyy-MM-dd HH:mm:ss}", Convert.ToDateTime(StartStopRecord.LastSaveEndTime)));
serverHelper.ExecuteNonQuery();
StartStopRecord._isInsert = true;
action.UpdateSysValue("StopRecord_IsInsert", "true"); //保存是否进行过停机记录
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("停机记录追加失败!", ex);
}
}
//2、每分钟更新一次停机结束时间
ts = DateTime.Now - StartStopRecord._lastUpdateDateTime;
//if (SaveDataPerLotHandler.RecipeTime == 0 && StartStopRecord._isInsert == true && ts.TotalSeconds >= 60)
if (PlcData.Instance.CurrentMixingTime.LastValue.ToInt() == 0 && StartStopRecord._isInsert == true && ts.TotalSeconds >= 60)
{
//获取当前班组名称
string className = PlanCommon.GetClassName(Basic.DataSourceFactory.MCDbType.Server, 1, PlanCommon.PlanLog.LastSelectDate, PlanCommon.PlanLog.LastSelectShiftID);
serverHelper.ClearParameter();
string strSql2 = "if not exists(select * from Ppt_pmdownrecord where Equip_code = @EquipCode and convert(varchar(19),Mp_startdate,120) = @StartTime) ";
strSql2 += " insert into Ppt_pmdownrecord (Equip_code,shift_id,shift_Class,Mp_startdate,Mp_enddate) values(@EquipCode,@ShiftID,@ClassName,@StartTime,dateadd(ss,30,getdate()))";//ShiftID,ClassName,@ShiftID,@ClassName,
strSql2 += " else ";
strSql2 += " Update Ppt_pmdownrecord set Mp_enddate = getDate() Where Equip_code = @EquipCode and Mp_startdate = @StartTime";
serverHelper.CommandText = strSql2;
serverHelper.AddParameter("@EquipCode", action.CurrEquipCode);
serverHelper.AddParameter("@ShiftID", PlanCommon.PlanLog.LastSelectShiftID);
serverHelper.AddParameter("@ClassName", className);
serverHelper.AddParameter("@StartTime", String.Format("{0:yyyy-MM-dd HH:mm:ss}", Convert.ToDateTime(StartStopRecord.LastSaveEndTime)));
serverHelper.ExecuteNonQuery();
StartStopRecord._lastUpdateDateTime = DateTime.Now;
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("停机记录更新失败:" + ex.Message);
}
}
}
}
}