using Mesnac.Action.Base;
using Mesnac.Codd.Session;
using Mesnac.Controls.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mesnac.Action.ChemicalWeighing.Report.SolventReport
{
///
/// 生产报表中查询生产计划Action
///
class SelectPlanAction : ChemicalWeighingAction, IAction
{
private RuntimeParameter _runtime;
private IBaseControl _equipCode = null; //机台号
private IBaseControl _version = null; //版本
private IBaseControl _startdate = null; //开始日期
private IBaseControl _enddate = null; //结束日期
private IBaseControl _starttime = null; //开始时间
private IBaseControl _endtime = null; //结束时间
//private IBaseControl _recipeName = null; //配方名
string _recipeName = string.Empty;
//private IBaseControl _shiftName = null; //班次
string _shiftName = string.Empty;
private DbMCControl _dgvRtPlan = null; //生产计划
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime);
this._runtime = runtime;
ICSharpCode.Core.LoggingService.Debug("生产报表-生产计划-查询..");
//刷新生产计划DataGridView数据
this._dgvRtPlan = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "RT_plan").FirstOrDefault();
if (_dgvRtPlan == null || !(_dgvRtPlan.BaseControl is DataGridView))
{
ICSharpCode.Core.LoggingService.Warn("{生产报表-查询} 缺少生产计划DataGridView控件...");
runtime.IsReturn = false;
return;
}
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
dbHelper.ClearParameter();
StringBuilder sb = new StringBuilder();
//sb.AppendLine(@" select p.Plan_Id,p.Recipe_ID,p.Recipe_Name,p.Version,p.Plan_StateText,p.Plan_Num,p.Real_Num,p.Start_Date,p.End_Date,B.Shift_name as Shift_Name,p.Weight_Man,r.Total_Weight,Plan_TotalWeight,Total_Error,Plan_TotalError,
// case when IsRetransmission=0 then '是' when IsRetransmission=1 then '否' when IsRetransmission is null then '否' end as IsRetransmission
// from RT_plan p left join Pmt_recipe r on p.Recipe_ID=r.ID Left Join Pmt_ShifTime B On(p.Shift_Id = B.Shift_ID) WHERE 1=1");
sb.AppendLine(@" select p.Batch,p.ProductName,p.Plan_Id,p.Recipe_ID,p.Recipe_Name,p.Version,p.Plan_StateText,p.Plan_Num,p.Real_Num,p.Start_Date,p.End_Date,p.Weight_Man,r.Total_Weight,Plan_TotalWeight,Total_Error,Plan_TotalError,
case when IsRetransmission=0 then '是' when IsRetransmission=1 then '否' when IsRetransmission is null then '否' end as IsRetransmission
from RT_plan p left join Pmt_recipe r on p.Recipe_ID=r.ID WHERE 1=1");
List mcControllist = GetAllDbMCControlsByOption(DbOptionTypes.Query);//获取所有待初始化控件
//开始时间条件
this._startdate = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "startdate").FirstOrDefault().BaseControl;
this._starttime = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "starttime").FirstOrDefault().BaseControl;
if (_startdate != null && _starttime != null)
{
sb.AppendLine(@"And p.Start_Date >='" + Convert.ToDateTime(_startdate.MCValue).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(_starttime.MCValue).ToShortTimeString() + "' ");
}
else
{
ICSharpCode.Core.LoggingService.Debug("{生产报表} 缺少key值为startdate或者starttime的时间查询条件...");
return;
}
//结束时间条件
this._enddate = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "enddate").FirstOrDefault().BaseControl;
this._endtime = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "endtime").FirstOrDefault().BaseControl;
if (_enddate != null && _endtime != null)
{
sb.AppendLine(@" And p.End_Date <='" + Convert.ToDateTime(_enddate.MCValue).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(_endtime.MCValue).ToShortTimeString() + "' ");
}
else
{
ICSharpCode.Core.LoggingService.Debug("{生产报表} 缺少key值为enddate或者enddate的时间查询条件...");
return;
}
this._recipeName = (base.GetControlById("comRecipe") as System.Windows.Forms.ComboBox).Text;
//this._shiftName = (base.GetControlById("comShiftime") as System.Windows.Forms.ComboBox).Text;
if (!String.IsNullOrEmpty(this._recipeName))
{
sb.AppendLine(@" And p.Batch ='" + _recipeName + "' ");
}
//if (!String.IsNullOrEmpty(this._shiftName))
//{
// sb.AppendLine(@"And Shift_Name = '" + _shiftName + "' ");
//}
sb.Append(" ORDER BY p.Start_Date desc");
dbHelper.CommandText = sb.ToString();
dbHelper.CommandType = System.Data.CommandType.Text;
DataTable table = dbHelper.ToDataTable();
_dgvRtPlan.BaseControl.BindDataSource = null;
_dgvRtPlan.BaseControl.BindDataSource = table;
ExportAction.OnRefresh -= Process_Event;
ExportAction.OnRefresh += Process_Event;
}
#region 事件处理方法
private void Process_Event(object sender, EventArgs e)
{
if (sender is RuntimeParameter)
{
this.Run(sender as RuntimeParameter);
}
else
{
this.Run(this._runtime);
}
}
#endregion
//public DataTable QueryXlPlan(string sql)
//{
//StringBuilder sb = new StringBuilder();
//sb.AppendLine(@" select p.Plan_Id,p.Recipe_ID,p.Recipe_Name,p.Version,p.Plan_Num,p.Real_Num,p.Start_Date,p.End_Date,B.Shift_name as Shift_Name,p.Weight_Man,Total_Weight,Total_Error,
// case when IsRetransmission=0 then '是' when IsRetransmission=1 then '否' when IsRetransmission is null then '否' end as IsRetransmission
// from LR_plan p Left Join Pmt_ShifTime B On(p.Shift_Id = B.Shift_ID) WHERE 1=1");
//List mcControllist = GetAllDbMCControlsByOption(DbOptionTypes.Query);//获取所有待初始化控件
////开始时间条件
//this._startdate = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "startdate").FirstOrDefault().BaseControl;
//this._starttime = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "starttime").FirstOrDefault().BaseControl;
//if (_startdate != null && _starttime != null)
//{
// sb.AppendLine(@"And p.Start_Date >='" + Convert.ToDateTime(_startdate.MCValue).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(_starttime.MCValue).ToShortTimeString() + "' ");
//}
//else
//{
// ICSharpCode.Core.LoggingService.Debug("{生产报表} 缺少key值为startdate或者starttime的时间查询条件...");
// return;
//}
//结束时间条件
//this._enddate = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "enddate").FirstOrDefault().BaseControl;
//this._endtime = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "endtime").FirstOrDefault().BaseControl;
//if (_enddate != null && _endtime != null)
//{
// sb.AppendLine(@" And p.End_Date <='" + Convert.ToDateTime(_enddate.MCValue).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(_endtime.MCValue).ToShortTimeString() + "' ");
//}
//else
//{
// ICSharpCode.Core.LoggingService.Debug("{生产报表} 缺少key值为enddate或者enddate的时间查询条件...");
// return;
//}
//配方名和班次条件
//this._recipeName = (base.GetControlById("comRecipe") as System.Windows.Forms.ComboBox).Text;
//this._shiftName = (base.GetControlById("comShiftime") as System.Windows.Forms.ComboBox).Text;
//if (!String.IsNullOrEmpty(this._recipeName))
//{
// sb.AppendLine(@" And p.Recipe_Name ='" + _recipeName + "' ");
//}
//if (!String.IsNullOrEmpty(this._shiftName))
//{
// sb.AppendLine(@"And Shift_Name = '" + _shiftName + "' ");
//}
//sb.Append(" ORDER BY p.Start_Date desc");
//dbHelper.CommandText = sb.ToString();
//dbHelper.CommandType = System.Data.CommandType.Text;
//DataTable table = dbHelper.ToDataTable();
//}
}
}