using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMFrameWork.Data;
using QMAPP.MESReport.Entity.LineQTY;
using QMAPP.DAL;
using System.Data;
using QMAPP.MESReport.Entity.Tables;
using System.ComponentModel;
namespace QMAPP.MESReport.DAL.LineQTY
{
public class ShipmentCountDAL : BaseDAL
{
#region 获取门板信息
///
/// 生产发运统计
///
///
///
public List GetDPList(ShipmentCountConditionModel condition)
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = this.GetDPQuerySql(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetDPQuerySql(ShipmentCountConditionModel condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder sqlBuilder2 = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
string groupexp = "CONVERT(VARCHAR(20),SCANDATE,23)"; ;
switch (condition.TYPE_COUNT)
{
case "DAY":
{
break;
}
case "MONTH":
{
groupexp = "CAST(YEAR(SCANDATE) AS VARCHAR)+'年'+CAST(DATEPART(M,SCANDATE) AS VARCHAR)+'月'";
break;
}
case "WEEK":
{
groupexp = "CAST(YEAR(SCANDATE) AS VARCHAR)+'年'+CAST(DATEPART(WK,SCANDATE) AS VARCHAR)+'th周'";
break;
}
}
sqlBuilder.AppendLine(" SELECT MIN(QTY) AS QTY,HBTYPE as MATERIAL_NAME,COLOR as MATERIAL_CODE,TDAY");
sqlBuilder.AppendLine("FROM (");
sqlBuilder.AppendLine("select count(1) as QTY");
sqlBuilder.AppendFormat(" ,{0} AS TDAY ", groupexp);
sqlBuilder.AppendLine(",M.HBTYPE,M.COLOR,M.MATERIAL_CODE");
sqlBuilder.AppendLine("from T_PP_FISORDERSENDDETAIL F ");
sqlBuilder.AppendLine("LEFT JOIN T_PP_PASCARDDETAILS T ");
sqlBuilder.AppendLine("ON T.FISDETAILID=F.FIS_PID ");
sqlBuilder.AppendLine("left join T_MD_MATERIAL m ");
sqlBuilder.AppendLine("on m.MATERIAL_CODE=f.MATERIAL_CODE ");
sqlBuilder.AppendLine("WHERE 1=1 ");
sqlBuilder.AppendLine("AND HBTYPE IS NOT NULL");
sqlBuilder.AppendLine("AND SCANDATE IS NOT NULL");
if (string.IsNullOrEmpty(condition.WORKCENTER_CODE) == false)
{
List strlist = condition.WORKCENTER_CODE.Split('/').ToList();
sqlBuilder.AppendLine("and f.WORKCENTER_CODE IN (");
for (int i = 0; i < strlist.Count; i++)
{
sqlBuilder.AppendFormat("'{0}'", strlist[i]);
if ((i + 1) != strlist.Count)
{
sqlBuilder.AppendLine(",");
}
}
sqlBuilder.AppendLine(")");
}
if (string.IsNullOrEmpty(condition.START_DATE) == false)
{
sqlBuilder.AppendFormat("and t.SCANDATE >'{0}' ", condition.START_DATE);
parameters.Add(new DataParameter { ParameterName = "SCANDATE", DataType = DbType.DateTime, Value = condition.START_DATE });
}
if (string.IsNullOrEmpty(condition.END_DATE) == false)
{
sqlBuilder.AppendFormat("and t.SCANDATE<'{0}'", Convert.ToDateTime(condition.END_DATE).AddDays(1).ToShortDateString());
}
sqlBuilder.AppendFormat(" GROUP BY {0},M.HBTYPE,M.COLOR,M.MATERIAL_CODE)TEMP1 ", groupexp);
sqlBuilder.AppendLine(" GROUP BY HBTYPE,COLOR,TDAY ");
sqlBuilder.AppendLine(" ORDER BY TDAY ASC ");
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取仪表板信息
///
/// 生产发运统计
///
///
///
public List GetIPList(ShipmentCountConditionModel condition)
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = this.GetIPQuerySql(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetIPQuerySql(ShipmentCountConditionModel condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder sqlBuilder2 = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
string groupexp = "CONVERT(VARCHAR(20),SCANDATE,23)"; ;
switch (condition.TYPE_COUNT)
{
case "DAY":
{
break;
}
case "MONTH":
{
groupexp = "CAST(YEAR(SCANDATE) AS VARCHAR)+'年'+CAST(DATEPART(M,SCANDATE) AS VARCHAR)+'月'";
break;
}
case "WEEK":
{
groupexp = "CAST(YEAR(SCANDATE) AS VARCHAR)+'年'+CAST(DATEPART(WK,SCANDATE) AS VARCHAR)+'th周'";
break;
}
}
sqlBuilder.AppendLine("select count(1) as QTY");
sqlBuilder.AppendFormat(" ,{0} AS TDAY ", groupexp);
sqlBuilder.AppendLine(",M.MATERIAL_SHORT as MATERIAL_NAME,'' AS MATERIAL_CODE");
sqlBuilder.AppendLine("from T_PP_FISORDERSENDDETAIL F ");
sqlBuilder.AppendLine("LEFT JOIN T_PP_PASCARDDETAILS T ");
sqlBuilder.AppendLine("ON T.FISDETAILID=F.FIS_PID ");
sqlBuilder.AppendLine("left join T_MD_MATERIAL m ");
sqlBuilder.AppendLine("on m.MATERIAL_CODE=f.MATERIAL_CODE ");
sqlBuilder.AppendLine("WHERE 1=1 ");
sqlBuilder.AppendLine("AND HBTYPE IS NOT NULL");
sqlBuilder.AppendLine("AND SCANDATE IS NOT NULL");
if (string.IsNullOrEmpty(condition.WORKCENTER_CODE) == false)
{
List strlist = condition.WORKCENTER_CODE.Split('/').ToList();
sqlBuilder.AppendLine("and f.WORKCENTER_CODE IN (");
for (int i = 0; i < strlist.Count; i++)
{
sqlBuilder.AppendFormat("'{0}'", strlist[i]);
if ((i + 1) != strlist.Count)
{
sqlBuilder.AppendLine(",");
}
}
sqlBuilder.AppendLine(")");
}
if (string.IsNullOrEmpty(condition.START_DATE) == false)
{
sqlBuilder.AppendFormat("and t.SCANDATE >'{0}' ", condition.START_DATE);
parameters.Add(new DataParameter { ParameterName = "SCANDATE", DataType = DbType.DateTime, Value = condition.START_DATE });
}
if (string.IsNullOrEmpty(condition.END_DATE) == false)
{
sqlBuilder.AppendFormat("and t.SCANDATE<'{0}'", Convert.ToDateTime(condition.END_DATE).AddDays(1).ToShortDateString());
}
sqlBuilder.AppendFormat(" GROUP BY {0},M.MATERIAL_SHORT,M.MATERIAL_CODE ", groupexp);
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取物料产品列表
///
/// 获取物料产品列表
///
///
///
///
public List GetWList()
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = @"SELECT MATERIAL_CODE,MATERIAL_NAME
FROM T_MD_MATERIAL M
LEFT JOIN T_MD_MATERIAL_CLASS MC
ON M.MATERIAL_TYPE_CODE=MC.MATERIAL_TYPE_CODE
WHERE MC.MATERIAL_ATTRIBUTE='2' and PROJECTCODE='VW331 DP'
ORDER BY M.MATERIAL_NAME";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取导出的数据
///
/// 获取导出的数据
///
/// 查询条件
/// 数据
public DataTable GetExportData(ShipmentCountConditionModel model)
{
List list = new List();
//门板
if (model.WORKCENTER_CODE == "DPLFY/DPRFY")
{
list = GetDPList(model);
}
//仪表板
else if (model.WORKCENTER_CODE.StartsWith("IPFY"))
{
list = GetIPList(model);
foreach (ShipmentCountConditionModel item in list)
{
item.MaterialValue = item.MATERIAL_NAME;
}
}
return CopyToDataTable(list);
}
///
/// 把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
}
}