using System; using System.Collections.Generic; using System.Linq; using System.Text; using Mesnac.Action.Base; using Mesnac.Controls.Base; using Mesnac.Codd.Session; using Mesnac.Controls.Default; using System.Data; using System.Windows.Forms; namespace Mesnac.Action.Feeding.Qingquan.Report { #region 批报表初始化 public class PlanLotReportLoad : FeedingAction, IAction { public void Run(RuntimeParameter runtime) { //if (!LotClass.IsFirstLoad) // return; //LotClass.IsFirstLoad = false; base.RunIni(runtime); List mcControllist = GetAllDbMCControlsByOption(DbOptionTypes.Query);//获取所有待初始化控件 IBaseControl startdate = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "startdate").FirstOrDefault().BaseControl; IBaseControl enddate = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "enddate").FirstOrDefault().BaseControl; IBaseControl starttime = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "starttime").FirstOrDefault().BaseControl; IBaseControl endtime = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "endtime").FirstOrDefault().BaseControl; if (startdate != null && enddate != null) { startdate.MCValue = DateTime.Now.ToShortDateString(); starttime.MCValue = DateTime.Now.ToString("yyyy-MM-dd") + " 00:00"; enddate.MCValue = DateTime.Now.AddDays(1); endtime.MCValue = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + " 00:00"; } else { base.LogError("{批报表} 缺少key值为startdate或者enddate的时间查询条件..."); return; } DbHelper dbHelper; foreach (DbMCControl c in mcControllist) { if (c.BaseControl.MCKey == null) continue; if (c.DbMCSource != null && c.BaseControl is MCCombobox) { DbMCControl pptlotcontrol = this.GetDbMCControlByBaseControlKey(c.BaseControl.MCKey).FirstOrDefault(); if (pptlotcontrol != null) { dbHelper = NewDbHelper(pptlotcontrol.DesignSource); if (dbHelper == null) { return; } dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; string strsql = "select * from [" + pptlotcontrol.BaseControl.InitDataSource.ToLower().Trim() + "]"; if (pptlotcontrol.BaseControl.InitDataSource.ToLower().Trim().StartsWith("select")) { strsql = pptlotcontrol.BaseControl.InitDataSource.ToLower().Trim(); } string sqlstr = strsql.ToString(); dbHelper.CommandText = sqlstr; DataTable table = dbHelper.ToDataTable(); pptlotcontrol.BaseControl.BindDataSource = table; } } } } } #endregion #region 查询 public class PlanLotReportSelect : FeedingAction, IAction { public void Run(RuntimeParameter runtime) { base.RunIni(runtime); DbMCControl pptlotButton = this.GetDbMCControlByBaseControlKey("Planlot").FirstOrDefault(); IBaseControl lblInfo = this.GetMCControlByKey("ShowMsgControl").FirstOrDefault(); //信息提示控件 if (pptlotButton == null) { base.LogError("{批报表} 缺少key值为Planlot查询按钮..."); return; } StringBuilder sqlstr = new StringBuilder(); sqlstr.AppendLine(@"SELECT * FROM (SELECT Ppt_Plan.mater_code as RecipeMaterialCode,Ppt_Plan.recipe_code AS RecipeMaterialName,SUM(Ppt_Plan.Plan_Num) AS PlanNum,SUM(Ppt_Plan.Real_Num) AS RealNum FROM dbo.Ppt_Plan"); // sqlstr.AppendLine(@"LEFT JOIN dbo.Pmt_Recipe ON Ppt_Plan.equip_code=Pmt_Recipe.equip_code AND Ppt_Plan.mater_code=Pmt_Recipe.mater_code AND Ppt_Plan.edt_code=Pmt_Recipe.edt_code // WHERE 1=1"); sqlstr.AppendLine(@"INNER JOIN dbo.Pmt_Recipe ON substring(Ppt_Plan.equip_code,4,2)=Pmt_Recipe.equip_code AND Ppt_Plan.mater_code=Pmt_Recipe.mater_code AND Ppt_Plan.edt_code=Pmt_Recipe.edt_code "); List mcControllist = GetAllDbMCControlsByOption(DbOptionTypes.Query);//获取所有待初始化控件 CheckBox checkDate = this.GetAllFormControlById("MCCheckBox1").FirstOrDefault() as CheckBox;//条件选择框 CheckBox checkClass = this.GetAllFormControlById("MCCheckBox2").FirstOrDefault() as CheckBox; CheckBox checkShift = this.GetAllFormControlById("MCCheckBox3").FirstOrDefault() as CheckBox; CheckBox checkRecipt = this.GetAllFormControlById("MCCheckBox4").FirstOrDefault() as CheckBox; if (checkDate.Checked) { IBaseControl startdate = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "startdate").FirstOrDefault().BaseControl; IBaseControl enddate = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "enddate").FirstOrDefault().BaseControl; if (startdate != null && enddate != null) { sqlstr.AppendLine(@" AND Ppt_Plan.plan_datetime>= cast('" + Convert.ToDateTime(startdate.MCValue).ToString("yyyy-MM-dd") + "' as datetime) AND Ppt_Plan.plan_datetime <= cast('" + Convert.ToDateTime(enddate.MCValue).ToString("yyyy-MM-dd") + "' as datetime) "); } } foreach (DbMCControl c in mcControllist) { if (c.BaseControl.MCKey == null) continue; if (c.BaseControl.MCKey.ToLower() != "startdate" && c.BaseControl.MCKey.ToLower() != "starttime" && c.BaseControl.MCKey.ToLower() != "enddate" && c.BaseControl.MCKey.ToLower() != "endtime") { if (c.BaseControl.MCKey.ToLower() == "ppt_plan.shift_class" && checkClass.Checked) sqlstr.AppendLine(@" AND " + c.BaseControl.MCKey + "='" + c.BaseControl.MCValue.ToString() + "' "); if (c.BaseControl.MCKey.ToLower() == "ppt_plan.shift" && checkShift.Checked) sqlstr.AppendLine(@" AND " + c.BaseControl.MCKey + "='" + c.BaseControl.MCValue.ToString() + "' "); if (c.BaseControl.MCKey.ToLower() == "pmt_recipe.objid" && checkRecipt.Checked) sqlstr.AppendLine(@" AND " + c.BaseControl.MCKey + "='" + c.BaseControl.MCValue.ToString() + "' "); } } sqlstr.AppendLine(" GROUP BY Ppt_Plan.mater_code,Ppt_Plan.recipe_code) PptPlan"); DbHelper dbHelper = NewDbHelper(pptlotButton.DesignSource); if (dbHelper == null) { return; } dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlstr.ToString(); DataTable table = dbHelper.ToDataTable(); DbMCControl planlotgrid = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "PptPlan").LastOrDefault(); planlotgrid.BaseControl.BindDataSource = table; string msg1 = Language(301); //查询完成 if (lblInfo == null) { Mesnac.Basic.MessageBoxTimeOut.Show(msg1, Language(1), 2000); } else { lblInfo.MCValue = msg1; } } } #endregion #region 批报表明细,左边的表格 public class PlanLotReportDetail : FeedingAction, IAction { public void Run(RuntimeParameter runtime) { base.RunIni(runtime); MCDataGridView mcdgv = runtime.Sender as MCDataGridView; DbMCControl planlotgrid = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "PptPlan").LastOrDefault(); //物料编码 string materialCode = mcdgv.SelectedRows[0].Cells["RecipeMaterialCode"].Value.ToString(); //拼接SQL语句BEGIN StringBuilder sqlstr = new StringBuilder(); sqlstr.AppendLine(@"SELECT Ppt_Plan.*,tp1.ItemName AS PlanStateName,tp2.ClassName,tp3.ShiftName FROM dbo.Ppt_Plan LEFT JOIN dbo.SysCode tp1 ON Ppt_Plan.Plan_State=tp1.ItemCode AND tp1.TypeID='PlanState' LEFT JOIN dbo.PptClass tp2 ON Ppt_Plan.shift_class=tp2.ObjID LEFT JOIN dbo.PptShift tp3 ON Ppt_Plan.shift=tp3.ObjID"); sqlstr.AppendLine(@"WHERE Ppt_Plan.mater_code= '" + materialCode + "'"); List mcControllist = GetAllDbMCControlsByOption(DbOptionTypes.Query);//获取所有待初始化控件 CheckBox checkDate = this.GetAllFormControlById("MCCheckBox1").FirstOrDefault() as CheckBox;//条件选择框 CheckBox checkClass = this.GetAllFormControlById("MCCheckBox2").FirstOrDefault() as CheckBox; CheckBox checkShift = this.GetAllFormControlById("MCCheckBox3").FirstOrDefault() as CheckBox; CheckBox checkRecipt = this.GetAllFormControlById("MCCheckBox4").FirstOrDefault() as CheckBox; if (checkDate.Checked)//如果日期选中,添加日期选择条件 { IBaseControl startdate = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "startdate").FirstOrDefault().BaseControl; IBaseControl starttime = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "starttime").FirstOrDefault().BaseControl; IBaseControl enddate = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "enddate").FirstOrDefault().BaseControl; IBaseControl endtime = mcControllist.Where(t => t.BaseControl.MCKey != null && t.BaseControl.MCKey.ToLower() == "endtime").FirstOrDefault().BaseControl; if (starttime == null || endtime == null) { base.LogError("{批报表} 缺少key值为starttime或者endtime的时间查询条件..."); return; } if (startdate != null && enddate != null) { sqlstr.AppendLine(@"AND Ppt_Plan.Plan_Datetime>= cast('" + Convert.ToDateTime(startdate.MCValue).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(starttime.MCValue).ToShortTimeString() + "' as datetime) AND Ppt_Plan.Plan_Datetime <= cast('" + Convert.ToDateTime(enddate.MCValue).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(endtime.MCValue).ToShortTimeString() + "' as datetime) "); } else { base.LogError("{批报表} 缺少key值为startdate或者enddate的时间查询条件..."); return; } } foreach (DbMCControl c in mcControllist) { if (c.BaseControl.MCKey == null) continue; if (c.BaseControl.MCKey.ToLower() != "startdate" && c.BaseControl.MCKey.ToLower() != "starttime" && c.BaseControl.MCKey.ToLower() != "enddate" && c.BaseControl.MCKey.ToLower() != "endtime" && c.BaseControl.MCKey.ToLower() != "pmt_recipe.objid") { if (c.BaseControl.MCKey.ToLower() == "ppt_plan.shift_class" && checkClass.Checked) sqlstr.AppendLine(@"AND " + c.BaseControl.MCKey + "='" + c.BaseControl.MCValue.ToString() + "' "); if (c.BaseControl.MCKey.ToLower() == "ppt_plan.shift" && checkShift.Checked) sqlstr.AppendLine(@"AND " + c.BaseControl.MCKey + "='" + c.BaseControl.MCValue.ToString() + "' "); } } sqlstr.AppendLine("ORDER BY Ppt_Plan.mater_code"); //拼接SQL语句END DbHelper dbHelper = NewDbHelper(planlotgrid.DesignSource); if (dbHelper == null) { return; } dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlstr.ToString(); DataTable table = dbHelper.ToDataTable(); DbMCControl planlotdetailgrid = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "PptPlan").FirstOrDefault(); planlotdetailgrid.BaseControl.BindDataSource = table; } } #endregion #region 批报表明细信息 public class PlanLotFastReport : FeedingAction, IAction { public void Run(RuntimeParameter runtime) { base.RunIni(runtime); DbMCControl planlotdetailgrid = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "PptPlan").FirstOrDefault(); if (planlotdetailgrid == null) { base.LogError("{批报表} 缺少明细表格控件..."); return; } MCDataGridView dgv = planlotdetailgrid.BaseControl as MCDataGridView; if (dgv.SelectedRows.Count != 1) { MessageBox.Show(base.Language(304)); return; } if (dgv.SelectedRows[0].Cells["Real_Num"].Value.ToString().Trim() == "0") { MessageBox.Show("该批次没有完成车数。"); return; } string PlanId = dgv.SelectedRows[0].Cells["Plan_id"].Value.ToString(); PlanLotDetail pd = new PlanLotDetail(); pd.PlanId = PlanId; pd.ReprotStart(); //pd.Show(); } } #endregion #region 报表明细数据获取 public class PlanLotCommon { //判断是否第一次加载 public static bool IsFirstLoad = true; /// /// 根据计划号获取配方物料 /// /// /// /// public static DataTable GetRecipeMaterialData(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string planID) { DatabaseAction action = new DatabaseAction(); DbHelper dbHelper; dbHelper = action.NewDbHelper(dbType); if (dbHelper == null) { action.LogError("获取数据连接失败..."); return new DataTable(); } dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; string sqlstr = @" SELECT p.recipe_code as RecipeName , p.equip_code as equipCode , p.oper_datetime as RealStartTime , p.real_datetime as RealEndtime , c.ItemName , shift.ShiftName + '—' + class.ClassName as ShiftClassName , p.Real_Num as RealNum, p.Plan_Num as PlanNum , re.mater_name as RecipeMaterialName FROM PptLotData lot LEFT JOIN Ppt_Plan p ON P.Plan_ID = lot.PlanID LEFT JOIN Pmt_Recipe re ON re.mater_code = p.mater_code AND re.edt_code = p.edt_code AND re.equip_code = substring(p.equip_code,4,2) LEFT JOIN SysCode c ON c.ItemCode = re.RecipeType AND c.TypeID = 'PmtType' LEFT JOIN PptShift shift ON shift.ObjID = lot.ShiftID LEFT JOIN PptClass class ON class.ObjID = lot.ClassID WHERE p.Plan_ID = '" + planID + "'"; dbHelper.CommandText = sqlstr; DataTable table = dbHelper.ToDataTable(); return table; } /// /// 根据计划获取混炼信息的总时间、总重量等信息 /// /// /// /// public static DataTable GetRptPlanLotMainAvgAndSum(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string planID) { DatabaseAction action = new DatabaseAction(); DbHelper dbHelper; dbHelper = action.NewDbHelper(dbType); if (dbHelper == null) { action.LogError("获取数据连接失败..."); return new DataTable(); } dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; string sqlstr = @" SELECT sum(DoneAllRtime) as sumDoneAllRtime,avg(DoneAllRtime) as avgDoneAllRtime, sum(DoneRtime) as sumDoneRtime,avg(DoneRtime) as avgDoneRtime, sum(BwbTime) as sumBwbTime ,avg(BwbTime) as avgBwbTime, sum(PolyDisTime) as sumPolyDistime,avg(PolyDisTime) as avgPolydistime, sum(CBDisTime) as sumCBDistime,avg(CBDisTime) as avgCBdistime, sum(OilDisTime) as sumOilDistime,avg(OilDisTime) as avgOilDistime, sum(Oil2DisTime) as sumOil2Distime,avg(Oil2DisTime) as avgOil2Distime, sum(PowderDisTime) as sumPowderDisTime,avg(PowderDisTime) as avgPowderDisTime, sum(PjTemp) as sumPjTemp,round(avg(PjTemp),2) as avgPjTemp, sum(PjPower) as sumPjPower,round(avg(PjPower),2) as avgPjPower, sum(LotEnergy) as sumLotEnergy,round(avg(LotEnergy),2) as avgLotEnergy, sum(RealWeight) as sumRealweight,avg(RealWeight) as avgRealweight, max(DoneAllRtime) as maxDoneAllRtime,min(DoneAllRtime) as minDoneAllRtime, max(DoneRtime) as maxDoneRtime,min(DoneRtime) as minDoneRtime, max(BwbTime) as maxBwbTime ,min(BwbTime) as minBwbTime, max(PjTemp) as maxPjTemp,min(PjTemp) as minPjTemp, max(PjPower) as maxPjPower,min(PjPower) as minPjPower, max(LotEnergy) as maxLotEnergy,min(LotEnergy) as minLotEnergy, STDEV(DoneAllRtime) as STDDoneAllRtime,STDEV(DoneRtime) as STDDoneRtime, STDEV(BwbTime) as STDBwbTime,STDEV(PjTemp) as STDPjTemp, STDEV(PjPower) as STDPjPower,STDEV(LotEnergy) as STDLotEnergy FROM PptLotData WHERE PlanID = '" + planID + "'"; dbHelper.CommandText = sqlstr; DataTable table = dbHelper.ToDataTable(); return table; } /// /// 从表总计和信息 /// /// /// public static DataTable GetRptPlanLotSumDetailInfo(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string planID) { DatabaseAction action = new DatabaseAction(); DbHelper dbHelper; dbHelper = action.NewDbHelper(dbType); if (dbHelper == null) { action.LogError("获取数据连接失败..."); return new DataTable(); } dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; string sqlstr = @" SELECT c.sumweight, a.barcode,serialid ,convert(nvarchar,startdatetime,108) as startdatetime,startdatetime as startdatetime1, doneallrtime, donertime,bwbtime,PolyDisTime,CBDisTime,OilDisTime,Oil2DisTime,PowderDistime,[PjTemp] as PjTemp,pjpower , PjEner as PjEner FROM PptLotData a LEFT JOIN (select barcode, sum(real_weight) as sumweight from PptWeigh where barcode in (select distinct barcode from PptLotData where planid ='" + planID + "') group by barcode) c ON a.barcode = c.barcode "; sqlstr += " WHERE planid ='" + planID + "'"; sqlstr += " ORDER BY a.barcode "; dbHelper.CommandText = sqlstr; DataTable table = dbHelper.ToDataTable(); return table; } /// /// 从表对应称量信息 /// /// /// public static DataTable GetRptPlanLotMaterialDetailInfo(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string planID) { DatabaseAction action = new DatabaseAction(); DbHelper dbHelper; dbHelper = action.NewDbHelper(dbType); if (dbHelper == null) { action.LogError("获取数据连接失败..."); return new DataTable(); } dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; string sqlstr = @" SELECT a.Barcode,a.Set_Weight as SetWeight,a.Real_Weight as Real_weight, CASE a.Weigh_Type WHEN '0' THEN '炭黑' WHEN '1' THEN '油1' WHEN '3' THEN '粉料' WHEN '2' THEN '胶料' WHEN '4' THEN '小料' WHEN '5' THEN '油2' ELSE a.Weigh_Type END AS Weigh_Type ,a.Weight_ID as WeightID,a.Mater_Code as MaterCode,a.Mater_Name as Mater_name,b.MaterName as RecipeName, b.SerialID AS serial_id,b.StartDatetime AS StartDatetime,b.DoneRtime AS DoneRtime, b.DoneAllRtime AS DoneAllRtime,BwbTime AS BwbTime,PjTemp AS PjTemp,PolyDisTime AS PolyDisTime, CBDisTime AS CBDisTime,OilDisTime AS OilDisTime,PjPower AS PjPower,LotEnergy AS LotEnergy FROM dbo.PptWeigh a LEFT JOIN PptLotData b ON a.Barcode=b.Barcode WHERE a.Plan_ID = '" + planID + "'"; sqlstr += " ORDER BY a.Barcode,Weigh_Type,Weight_ID,a.Mater_code "; dbHelper.CommandText = sqlstr; DataTable table = dbHelper.ToDataTable(); return table; } } #endregion }