using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using QMFrameWork.Data; using QMAPP.MESReport.Entity.LineQTY; using QMAPP.DAL; using System.Data; using QMAPP.MD.DAL; using QMAPP.MD.Entity; using QMFrameWork.Log; namespace QMAPP.MESReport.DAL.LineQTY { /// /// 返修率统计 /// 于子清 /// 2017-10-25 /// public class RepairRateCountDAL : BaseDAL { /// /// 生产统计 /// /// /// public List GetAllListOld(string workcenter) { List list = new List(); string sql = null; List parameters = new List(); try { sql = "select sumValue as QTY,countValue as COMPLETE_QTY, maintemp.DEFECTVALUE,maintemp.MATERIAL_CODE from "; sql += "(select count(1) as sumValue ,convert(varchar(10),m.CREATEDATE,120) as DEFECTVALUE ,MATERIAL_CODE from t_aw_main m "; sql += "where m.WORKCENTER_CODE='" + workcenter + "' and m.CREATEDATE between DATEADD(day,-7,getdate()) and getdate() "; sql += "group by convert(varchar(10),m.CREATEDATE,120),MATERIAL_CODE)maintemp, "; sql += "(select count(1) as countValue ,convert(varchar(10),t.CREATEDATE,120) as DEFECTVALUE ,MATERIAL_CODE from T_AW_MENDRECORDER t "; sql += "where t.MENDRESULT='0' and t.WORKCENTER_CODE='" + workcenter + "' and t.CREATEDATE between DATEADD(day,-7,getdate()) and getdate() "; sql += "group by convert(varchar(10),t.CREATEDATE,120),MATERIAL_CODE)mendtemp "; //sql += "where maintemp.DEFECTVALUE=mendtemp.DEFECTVALUE and maintemp.MATERIAL_CODE=mendtemp.MATERIAL_CODE "; sql += "where maintemp.MATERIAL_CODE=mendtemp.MATERIAL_CODE "; using (IDataSession session = AppDataFactory.CreateMainSession()) { list = session.GetList(sql, parameters.ToArray()).ToList(); } return list; } catch (Exception ex) { throw ex; } } public List GetAllListNew(string workcenter,int tday,string sqlbll) { List list = new List(); string sql = null; List parameters = new List(); try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //int tday = -7; //完成数量 //sql = " select count(1) as QTY ,MATERIAL_CODE from t_aw_main where CREATEDATE between DATEADD(day," + tday + ",getdate()) and getdate() and WORKCENTER_CODE='" + workcenter + "' group by MATERIAL_CODE "; //修改-闫&张 20180227 sql = sqlbll; var pmList = session.GetList(sql, parameters.ToArray()); LogManager.LogHelper.Info(new LogInfo() { Tag = workcenter+"完成数量", Info = sql }); //返修数量 sql = " select count(1) as QTY ,MATERIAL_CODE from T_AW_MENDRECORDER where MENDRESULT='0' and CREATEDATE between DATEADD(day," + tday + ",getdate()) and getdate() and WORKCENTER_CODE='" + workcenter + "' group by MATERIAL_CODE "; var opList = session.GetList(sql, parameters.ToArray()); LogManager.LogHelper.Info(new LogInfo() { Tag = workcenter+"返修数量", Info = sql }); List materialList = new MaterialDAL().GetMaterialList(new Material()); //添加完成数量 if (pmList != null && pmList.Count > 0) { foreach (var item in pmList) { //零件名称 if (!string.IsNullOrEmpty(item.MATERIAL_CODE)) { var material = materialList.FirstOrDefault(x => x.MATERIAL_CODE == item.MATERIAL_CODE); if (material != null) { item.MATERIAL_NAME = material.MATERIAL_NAME; item.MATERIAL_SHORT = material.MATERIAL_SHORT; if (string.IsNullOrEmpty(material.MATERIAL_SHORT)) { item.MATERIAL_SHORT = material.MATERIAL_CODE; } } } if (opList != null && opList.Count > 0) { var item_ORDERPLAN = opList.FirstOrDefault(x => x.MATERIAL_CODE == item.MATERIAL_CODE); //计划数量 if (item_ORDERPLAN != null) { //返修数量赋值给完成数集合里的返修数量 item.QTY = item_ORDERPLAN.QTY; //移除重复的MATERIAL_CODE opList.Remove(item_ORDERPLAN); } } list.Add(item); } } //添加完成数量list里没有的MATERIAL_CODE if (opList != null && opList.Count > 0) { foreach (var item in opList) { //零件名称 if (!string.IsNullOrEmpty(item.MATERIAL_CODE)) { var material = materialList.FirstOrDefault(x => x.MATERIAL_CODE == item.MATERIAL_CODE); if (material != null) { item.MATERIAL_NAME = material.MATERIAL_NAME; item.MATERIAL_SHORT = material.MATERIAL_SHORT; if (string.IsNullOrEmpty(material.MATERIAL_SHORT)) { item.MATERIAL_SHORT = material.MATERIAL_CODE; } } } list.Add(item); } } } return list; } catch (Exception ex) { throw ex; } } public List GetAllList(QTYCompletionRateDModel condition) { List list = new List(); string sql = null; List parameters = new List(); try { var workcenter = ""; var sqltemp = ""; if (string.IsNullOrEmpty(condition.WORKCENTER_CODE)) { return list; } if (condition.WORKCENTER_CODE == "INJECTION_AREA") { workcenter = "INJECTION_AREA"; sqltemp = "SELECT count(1) as COMPLETE_QTY ,MATERIAL_CODE FROM T_AW_MAINOPERATION m where m.WORKCENTER_CODE='INJECTION_AREA' "; } else if (condition.WORKCENTER_CODE == "1") { workcenter = "BORADPASSY"; sqltemp = "SELECT count(1) as COMPLETE_QTY ,M.MATERIAL_CODE FROM T_AW_PRODUCT M left join T_MD_MATERIAL TM ON M.MATERIAL_CODE=TM.MATERIAL_CODE where TM.PROJECTCODE='VW331 DP' and exists(select 1 from T_MD_MATERIAL_CLASS mc where mc.MATERIAL_TYPE_CODE=MATERIAL_TYPE and mc.MATERIAL_ATTRIBUTE='2') "; } else if (condition.WORKCENTER_CODE == "2") { workcenter = "BORAIPASSY"; sqltemp = "SELECT count(1) as COMPLETE_QTY ,M.MATERIAL_CODE FROM T_AW_PRODUCT M left join T_MD_MATERIAL TM ON M.MATERIAL_CODE=TM.MATERIAL_CODE where TM.PROJECTCODE='VW331/5CN IP' and exists(select 1 from T_MD_MATERIAL_CLASS mc where mc.MATERIAL_TYPE_CODE=MATERIAL_TYPE and mc.MATERIAL_ATTRIBUTE='2') "; } else { workcenter = condition.WORKCENTER_CODE; sqltemp = "SELECT count(1) as COMPLETE_QTY ,M.MATERIAL_CODE FROM T_AW_PRODUCT M left join T_MD_MATERIAL TM ON M.MATERIAL_CODE=TM.MATERIAL_CODE where M.WORKCENTER_CODE='" + condition.WORKCENTER_CODE + "' and exists(select 1 from T_MD_MATERIAL_CLASS mc where mc.MATERIAL_TYPE_CODE=MATERIAL_TYPE and mc.MATERIAL_ATTRIBUTE='2') "; } if (string.IsNullOrEmpty(condition.START_DATE) == false && string.IsNullOrEmpty(condition.END_DATE) == false) { var startDate = condition.START_DATE + " 00:00:00"; var endDate = condition.END_DATE + " 23:59:59"; sqltemp += " and m.CREATEDATE >= '" + startDate + "' and m.CREATEDATE <= '" + endDate + "' group by M.MATERIAL_CODE "; } using (IDataSession session = AppDataFactory.CreateMainSession()) { sql = sqltemp; var pmList = session.GetList(sql, parameters.ToArray()); LogManager.LogHelper.Info(new LogInfo() { Tag = workcenter + "完成数量", Info = sql }); //返修数量 sql = " select sum(MENDNUMBER) as QTY ,MATERIAL_CODE from T_AW_MENDRECORDER where MENDRESULT='" + condition.MENDRESULT + "' and BATCH_NO >= '" + condition.START_DATE.Replace("-", "") + "' and BATCH_NO <= '" + condition.END_DATE.Replace("-", "") + "' and WORKCENTER_CODE='" + workcenter + "' group by MATERIAL_CODE "; var opList = session.GetList(sql, parameters.ToArray()); LogManager.LogHelper.Info(new LogInfo() { Tag = workcenter + "返修数量", Info = sql }); List materialList = new MaterialDAL().GetMaterialList(new Material()); //添加完成数量 if (pmList != null && pmList.Count > 0) { foreach (var item in pmList) { //零件名称 if (!string.IsNullOrEmpty(item.MATERIAL_CODE)) { var material = materialList.FirstOrDefault(x => x.MATERIAL_CODE == item.MATERIAL_CODE); if (material != null) { item.MATERIAL_NAME = material.MATERIAL_NAME; item.MATERIAL_SHORT = material.MATERIAL_SHORT; if (string.IsNullOrEmpty(item.MATERIAL_SHORT)) { item.MATERIAL_SHORT = material.MATERIAL_CODE; } } } if (opList != null && opList.Count > 0) { var item_ORDERPLAN = opList.FirstOrDefault(x => x.MATERIAL_CODE == item.MATERIAL_CODE); //计划数量 if (item_ORDERPLAN != null) { //返修数量赋值给完成数集合里的返修数量 item.QTY = item_ORDERPLAN.QTY; //移除重复的MATERIAL_CODE opList.Remove(item_ORDERPLAN); } } list.Add(item); } } //添加完成数量list里没有的MATERIAL_CODE if (opList != null && opList.Count > 0) { foreach (var item in opList) { //零件名称 if (!string.IsNullOrEmpty(item.MATERIAL_CODE)) { var material = materialList.FirstOrDefault(x => x.MATERIAL_CODE == item.MATERIAL_CODE); if (material != null) { item.MATERIAL_NAME = material.MATERIAL_NAME; item.MATERIAL_SHORT = material.MATERIAL_SHORT; if (string.IsNullOrEmpty(item.MATERIAL_SHORT)) { item.MATERIAL_SHORT = material.MATERIAL_CODE; } } } list.Add(item); } } } return list; } catch (Exception ex) { throw ex; } } /// /// 查询注塑件返修率 /// /// 查询当前日期前X天 X=-7 /// public List GetInjectReworkRate(int dayrange=-7) { List list = new List(); StringBuilder sql = new StringBuilder(); ; List parameters = new List(); try { sql.AppendLine("SELECT S.MATERIAL_CODE, MT.MATERIAL_NAME, MT.MATERIAL_SHORT, COUNT(S.PRODUCTCODE) AS COMPLETE_QTY, "); sql.AppendLine(" COUNT(S.MPID) AS REWORK_QTY, ROUND(COUNT(S.MPID) * 1.0 / COUNT(S.PRODUCTCODE) * 100, 1) "); sql.AppendLine(" AS REWORK_RATE "); sql.AppendLine("FROM (SELECT O.PRODUCTCODE, O.MATERIAL_CODE, M.PID AS MPID "); sql.AppendLine(" FROM T_AW_MAINOPERATION AS O LEFT OUTER JOIN "); sql.AppendLine(" T_AW_MENDRECORDER AS M ON O.PDID = M.PPID AND MENDRESULT='0' "); sql.AppendLine(" WHERE (O.CREATEDATE BETWEEN DATEADD(DAY, " + dayrange.ToString() + ", GETDATE()) AND GETDATE()) AND O.WORKCENTER_CODE='INJECTION_AREA') AS S LEFT OUTER JOIN "); sql.AppendLine(" T_MD_MATERIAL AS MT ON MT.MATERIAL_CODE = S.MATERIAL_CODE "); sql.AppendLine("GROUP BY S.MATERIAL_CODE, MT.MATERIAL_NAME, MT.MATERIAL_SHORT "); sql.AppendLine("ORDER BY S.MATERIAL_CODE "); using (IDataSession session = AppDataFactory.CreateMainSession()) { list = session.GetList(sql.ToString(), parameters.ToArray()).ToList(); } return list; } catch (Exception ex) { throw ex; } } /// /// 查询门板、仪表板返修率 /// /// /// public List GetReworkRate(string project,int dayrange = -7) { List list = new List(); StringBuilder sql = new StringBuilder(); ; List parameters = new List(); try { sql.AppendLine(" SELECT S.MATERIAL_CODE "); sql.AppendLine(" , S.MATERIAL_NAME "); sql.AppendLine(" , S.MATERIAL_SHORT "); sql.AppendLine(" , COUNT(S.PRODUCTCODE) AS COMPLETE_QTY "); sql.AppendLine(" , COUNT(S.MPID) AS REWORK_QTY "); sql.AppendLine(" , ROUND(COUNT(S.MPID) * 1.0 / COUNT(S.PRODUCTCODE) * 100, 1) AS REWORK_RATE "); sql.AppendLine(" FROM (SELECT P.PRODUCTCODE "); sql.AppendLine(" , P.MATERIAL_CODE "); sql.AppendLine(" , M.PID AS MPID "); sql.AppendLine(" , MT.MATERIAL_NAME "); sql.AppendLine(" , MT.MATERIAL_SHORT "); sql.AppendLine(" FROM T_AW_PRODUCT AS P "); sql.AppendLine(" LEFT JOIN T_AW_MENDRECORDER AS M "); sql.AppendLine(" ON M.PPID = P.PID "); sql.AppendLine(" AND M.MENDRESULT='0' "); sql.AppendLine(" LEFT JOIN T_MD_MATERIAL AS MT "); sql.AppendLine(" ON P.MATERIAL_CODE = MT.MATERIAL_CODE "); sql.AppendLine(" WHERE (P.CREATEDATE BETWEEN DATEADD(DAY, " + dayrange.ToString() + ", GETDATE()) AND GETDATE()) "); sql.AppendLine(" AND MT.PROJECTCODE='" + project + "' "); sql.AppendLine(" AND EXISTS(SELECT 1 AS Expr1 "); sql.AppendLine(" FROM T_MD_MATERIAL_CLASS AS MC "); sql.AppendLine(" WHERE (MATERIAL_TYPE_CODE = P.MATERIAL_TYPE) "); sql.AppendLine(" AND (MATERIAL_ATTRIBUTE = '2'))) "); sql.AppendLine(" AS S "); sql.AppendLine(" GROUP BY S.MATERIAL_CODE, S.MATERIAL_NAME, S.MATERIAL_SHORT "); sql.AppendLine(" ORDER BY S.MATERIAL_CODE"); using (IDataSession session = AppDataFactory.CreateMainSession()) { list = session.GetList(sql.ToString(), parameters.ToArray()).ToList(); } return list; } catch (Exception ex) { throw ex; } } #region 获取查询语句 /// /// 获取查询语句 /// /// 查询条件 /// 参数 /// 查询语句 private string GetQuerySql(RepairRateCountDModel condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder sqlBuilder2 = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { string group = " group by tmd.MATERIAL_NAME,tsa.MATERIAL_CODE,day(tsa.STATIS_DATE),tsay.val"; //构成查询语句 sqlBuilder.AppendLine("select tmd.MATERIAL_NAME,tsa.MATERIAL_CODE,tsay.val as MFlV ,SUM(REWORK_QTY) REWORK_QTY,SUM(tsa.QTY) QTY,day(tsa.STATIS_DATE) tday from T_SA_LINEQTYCOUNT tsa inner join T_MD_MATERIAL tmd on tsa.MATERIAL_CODE =tmd.MATERIAL_CODE inner join T_SA_YZQCOUNT tsay on tsay.pid =5 "); if (string.IsNullOrEmpty(condition.START_DATE) == false) { whereBuilder.Append(" AND tsa.STATIS_DATE >= @START_DATE "); parameters.Add(new DataParameter { ParameterName = "START_DATE", DataType = DbType.DateTime, Value = condition.START_DATE }); } if (string.IsNullOrEmpty(condition.END_DATE) == false) { whereBuilder.Append(" AND tsa.STATIS_DATE <= @END_DATE "); parameters.Add(new DataParameter { ParameterName = "END_DATE", DataType = DbType.DateTime, Value = condition.END_DATE }); } if (string.IsNullOrEmpty(condition.MATERIAL_CODE) == false) { whereBuilder.Append(" AND tsa.MATERIAL_CODE = @MATERIAL_CODE "); parameters.Add(new DataParameter { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = condition.MATERIAL_CODE }); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } sqlBuilder.Append(group); return sqlBuilder.ToString(); } catch (Exception ex) { throw ex; } } #endregion #region 获取导出的数据 /// /// 获取导出的数据 /// /// 查询条件 /// 数据 public DataTable GetExportData(QTYCompletionRateDModel model) { var workcenter = "注塑"; if (model.WORKCENTER_CODE == "1") { workcenter = "门板"; } else if (model.WORKCENTER_CODE == "2") { workcenter = "仪表板"; } var list = GetAllList(model); foreach (var lq in list) { lq.WORKCENTER_NAME = workcenter; } return CopyToDataTable(list); } public DataTable GetExportDataReason(RepairRateCountDModel model) { var list = GetMenderReason(model); //return CopyToDataTable(list); DateTime start,end; if (DateTime.TryParse(model.START_DATE, out start) && DateTime.TryParse(model.END_DATE, out end)) { DataTable dt = new DataTable(); dt.Columns.Add("DEFECT"); for(int i=0;i<=30;i++) { dt.Columns.Add(i.ToString()); } var title = dt.NewRow(); title["0"] = "日期:"; title["2"] = start.Date.ToString("yyyy年MM月dd日") + " - " + end.Date.ToString("yyyy年MM月dd日"); title["9"] = "工作中心:"; title["11"] = model.WORKCENTER_CODE.PadRight(20,' '); if (!string.IsNullOrWhiteSpace(model.MaterialCode)) { title["18"] = "物料号:"; title["20"] = model.MaterialCode + " " + model.MaterialName; } dt.Rows.Add(title); var headerrow = dt.NewRow(); headerrow["DEFECT"] = "缺陷类型"; for (int i = 0; i <= (end - start).Days; i++) { headerrow[i.ToString()] = start.AddDays(i).Date.Day.ToString() + "th"; } dt.Rows.Add(headerrow); foreach (var defectgroup in list.GroupBy(p => p.DEFECTVALUE).OrderBy(p => p.Key)) { var datarow = dt.NewRow(); datarow["DEFECT"] = defectgroup.Key; for (int i = 0; i <= (end - start).Days; i++) { string value=" "; var day=defectgroup.FirstOrDefault(p=>p.CREATEDATE==start.AddDays(i).Date.ToString("yyyy-MM-dd")); if(day!=null) { value=day.MENDNUMBER; } datarow[i.ToString()] = value; } dt.Rows.Add(datarow); } return dt; } else { return null; } } //把list转换成DataTable public static DataTable CopyToDataTable(IEnumerable array) { var ret = new DataTable(); foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T))) ret.Columns.Add(dp.Name); foreach (T item in array) { var Row = ret.NewRow(); foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T))) Row[dp.Name] = dp.GetValue(item); ret.Rows.Add(Row); } return ret; } #endregion /// /// 报废原因统计 /// public List GetMenderReason(RepairRateCountDModel condition) { List list = new List(); string sql = null; ; List parameters = new List(); try { sql = GetSqlMenderReason(condition, ref parameters); using (IDataSession session = AppDataFactory.CreateMainSession()) { list = session.GetList(sql.ToString(), parameters.ToArray()).ToList(); } foreach (var lq in list) { if (!string.IsNullOrEmpty(lq.DEFECTVALUE)) { lq.DEFECTVALUE=lq.DEFECTVALUE.Replace("\r", ""); lq.DEFECTVALUE=lq.DEFECTVALUE.Replace("\n", ""); } //lq.MaterialName = model.MaterialName; } return list; } catch (Exception ex) { throw ex; } } private string GetSqlMenderReason(RepairRateCountDModel condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder sqlBuilder2 = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { //构成查询语句 if (condition.SelectMender=="0") { sqlBuilder.AppendLine("SELECT R.DEFECTVALUE, SUM(M.MENDNUMBER) AS MENDNUMBER FROM T_AW_MENDERREASON R LEFT JOIN T_AW_MENDRECORDER M ON R.PPID=M.PID "); } else if (condition.SelectMender == "1") { sqlBuilder.AppendLine(" SELECT CONVERT(varchar(100), M.CREATEDATE, 23) as CREATEDATE, R.DEFECTVALUE, SUM(M.MENDNUMBER) AS MENDNUMBER FROM T_AW_MENDERREASON R LEFT JOIN T_AW_MENDRECORDER M ON R.PPID=M.PID "); } sqlBuilder.AppendLine(" WHERE M.MENDNUMBER IS NOT NULL AND MENDNUMBER > 0 "); if (string.IsNullOrEmpty(condition.START_DATE) == false) { whereBuilder.Append(" AND M.CREATEDATE >= @START_DATE "); parameters.Add(new DataParameter { ParameterName = "START_DATE", DataType = DbType.DateTime, Value = condition.START_DATE }); } if (string.IsNullOrEmpty(condition.END_DATE) == false) { whereBuilder.Append(" AND M.CREATEDATE <= @END_DATE "); parameters.Add(new DataParameter { ParameterName = "END_DATE", DataType = DbType.DateTime, Value = condition.END_DATE }); } if (string.IsNullOrEmpty(condition.MaterialCode) == false) { whereBuilder.Append(" AND M.MATERIAL_CODE = @MATERIAL_CODE "); parameters.Add(new DataParameter { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = condition.MaterialCode }); } if (string.IsNullOrEmpty(condition.WORKCENTER_CODE) == false) { whereBuilder.Append(" AND M.WORKCENTER_CODE = @WORKCENTER_CODE "); parameters.Add(new DataParameter { ParameterName = "WORKCENTER_CODE", DataType = DbType.String, Value = condition.WORKCENTER_CODE }); } if (whereBuilder.Length > 0) { sqlBuilder.Append(whereBuilder); } string group = ""; if (condition.SelectMender == "0") { group = " GROUP BY R.DEFECTVALUE ORDER BY R.DEFECTVALUE ASC"; } else if (condition.SelectMender == "1") { group = " GROUP BY R.DEFECTVALUE,CONVERT(varchar(100), M.CREATEDATE, 23) ORDER BY R.DEFECTVALUE "; } sqlBuilder.Append(group); return sqlBuilder.ToString(); } catch (Exception ex) { throw ex; } } } }