using System; using System.Collections.Generic; using System.Linq; using System.Text; using Mesnac.Action.Base; using System.Windows.Forms; using Mesnac.Controls.Base; using Mesnac.Controls.Default; using Mesnac.Codd.Session; namespace Mesnac.Action.Feeding.Qingquan.Sys { /// /// 清除历史存盘数据界面初始化 /// public class InitClearHistoryDataUI : FeedingAction, IAction { public void Run(RuntimeParameter runtime) { base.RunIni(runtime); ICSharpCode.Core.LoggingService.Debug("清除历史存盘数据界面初始化..."); //1、查找日期控件、信息提示控件 IBaseControl datePickerbaseControl = this.GetMCControlByKey("EndDatePicker").FirstOrDefault(); IBaseControl lblInfo = this.GetMCControlByKey("lblInfo").FirstOrDefault(); if (datePickerbaseControl != null) { datePickerbaseControl.MCValue = DateTime.Now.AddMonths(-3); //默认保留三个月的历史数据 } if (lblInfo != null) { lblInfo.MCValue = String.Empty; } } } /// /// 清除历史存盘数据 /// public class ClearHistoryDataAction : FeedingAction , IAction { private IBaseControl _sender; //保存事件源 private IBaseControl _lblInfo; //消息标签 private IBaseControl _lblPercent; //百分比标签 private IBaseControl _progressBar; //进度条控件 private MCProgressBar _mcProgressBar; // private DateTime _endDate = DateTime.Now.AddMonths(-3); //保存截止日期 private DateTime _minDate = DateTime.Now.AddMonths(-3).AddDays(-1); //保存最小日期 private System.Timers.Timer timer = new System.Timers.Timer(); public void Run(RuntimeParameter runtime) { base.RunIni(runtime); if (MessageBox.Show(Language(40), Language(1), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { runtime.IsReturn = true; return; } this._sender = runtime.Sender as IBaseControl; this._lblInfo = this.GetMCControlByKey("lblInfo").FirstOrDefault(); this._lblPercent = this.GetMCControlByKey("lblPercent").FirstOrDefault(); this._progressBar = this.GetMCControlByKey("ProgressBar").FirstOrDefault(); IBaseControl datePickerbaseControl = this.GetMCControlByKey("EndDatePicker").FirstOrDefault(); if (this._progressBar != null) { this._mcProgressBar = this._progressBar as MCProgressBar; if (this._mcProgressBar != null) { if (datePickerbaseControl != null) { DateTime.TryParse(datePickerbaseControl.MCValue.ToString(), out this._endDate); if (this.GetMinDate(out this._minDate)) { TimeSpan ts = this._endDate - this._minDate; double days = ts.TotalDays + 1; if (days > 0) { string msg = String.Empty; msg = String.Format("清除{0:yyyy-MM-dd}之前的历史存盘数据!", this._endDate); base.DBLog("系统操作", "清除历史存盘数据", msg); this._mcProgressBar.Maximum = (int)days; //启动执行清除历史数据的业务线程 timer.Interval = 500; //时间间隔0.5秒 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); timer.Start(); //禁用事件源 if (this._sender != null) { this._sender.MCEnabled = false; } } } } } } } /// /// 删除历史数据业务处理 /// /// /// protected void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { ICSharpCode.Core.LoggingService.Debug("清除历史数据业务处理"); if (this._mcProgressBar != null) { if (this._mcProgressBar.Value < this._mcProgressBar.Maximum) { this._mcProgressBar.Value++; if (this._lblPercent != null) { this._lblPercent.MCValue = String.Format("{0}", this._mcProgressBar.Value * 100 / this._mcProgressBar.Maximum); } if (this._lblInfo != null) { this._lblInfo.MCValue = String.Format("正在删除{0:yyyy-MM-dd}的数据...", this._minDate); } this.DeleteByEndDate(this._minDate); if (this._minDate < this._endDate) { this._minDate = this._minDate.AddDays(1); } } if (this._mcProgressBar.Value >= this._mcProgressBar.Maximum) { //终止线程 this.timer.Stop(); if (this._lblInfo != null) { this._lblInfo.MCValue = "本次删除执行完毕..."; } //启用事件源 if (this._sender != null) { this._sender.MCEnabled = true; } } } } /// /// 删除指定日期之前的历史存盘数据 /// /// 截止日期 public void DeleteByEndDate(DateTime endDate) { string strEndDate = String.Format("{0:yyMMdd}", endDate); try { StringBuilder sbSql = new StringBuilder(); sbSql.Append("delete from PptMixData where left(Barcode,6) <= @endDate;"); //混炼数据 sbSql.Append("delete from PptWeigh where left(Barcode,6) <= @endDate;"); //称量数据 sbSql.Append("delete from PptLotData where left(Barcode,6) <= @endDate;"); //车报表 sbSql.Append("delete from PptGroupLot where FinishTag = 1 and convert(varchar,StartDatetime,12) <= @endDate;"); //批报表 sbSql.Append("delete from PptLotBatchCount where left(FirstPlanID,6) <= @endDate;"); //批次记录表 sbSql.Append("delete from PptShiftConfig where left(Barcode,6) <= @endDate;"); //架子信息表 sbSql.Append("delete from ppt_plan where LEFT(plan_id,6) <= @endDate;"); //计划表 DbHelper localHelper = base.NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); localHelper.CommandType = System.Data.CommandType.Text; localHelper.ClearParameter(); localHelper.CommandText = sbSql.ToString(); localHelper.AddParameter("@endDate", strEndDate); localHelper.ExecuteNonQuery(); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("清除历史数据-删除指定日期之前的历史存盘数据失败:" + ex.Message); } try { string strSql = "delete from Ppt_CurveData where left(Barcode,6) <= @endDate"; //密炼曲线表 DbHelper localCurveHelper = base.NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Curve); localCurveHelper.CommandType = System.Data.CommandType.Text; localCurveHelper.ClearParameter(); localCurveHelper.CommandText = strSql; localCurveHelper.AddParameter("@endDate", strEndDate); localCurveHelper.ExecuteNonQuery(); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("清除历史数据-删除指定日期之前的密炼曲线数据失败:" + ex.Message); } } /// /// 获取车报表中的最小日期 /// /// 输出参数:最小日期 /// 获取成功返回true,失败返回false public bool GetMinDate(out DateTime minDate) { bool blResult = false; //设置默认返回值 minDate = DateTime.Now.AddMonths(-3).AddDays(-1); //设置默认日期 try { string strSql = "select convert(datetime,left(MIN(barcode),6),12) from PptLotData"; DbHelper localHelper = base.NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); localHelper.CommandType = System.Data.CommandType.Text; localHelper.ClearParameter(); localHelper.CommandText = strSql; object result = localHelper.ToScalar(); if (result != null && result != System.DBNull.Value) { blResult = DateTime.TryParse(result.ToString(), out minDate); } } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("清除历史数据-获取最小日期失败:" + ex.Message); } return blResult; } } }