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;
namespace QMAPP.MESReport.DAL.LineQTY
{
///
/// 生产统计
/// 于子清
/// 2017-10-18
///
public class QTYInfoCountDAL : BaseDAL
{
///
/// 生产统计
///
///
///
public List GetAllList(QTYInfoCountDModel condition)
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(QTYInfoCountDModel condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder groupBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
sqlBuilder.AppendLine("SELECT M.MATERIAL_SHORT as MATERIAL_NAME");
sqlBuilder.AppendLine(" ,C.[MATRIAL_CODE] AS [MATERIAL_CODE] ");
sqlBuilder.AppendLine(" ,SUM(C.[QTY]) AS [QTY] ");
groupBuilder.AppendLine(" GROUP BY M.MATERIAL_SHORT ");
groupBuilder.AppendLine(" ,C.[MATRIAL_CODE] ");
//构成查询语句
if (condition.TYPE_COUNT=="WEEK")
{
sqlBuilder.AppendLine(" ,DATENAME(WEEK,DATEADD(DAY,-1,C.[STATIS_DATE])) AS TDAY ");
groupBuilder.AppendLine(" ,DATENAME(WEEK,DATEADD(DAY,-1,C.[STATIS_DATE])) ");
}
else if (condition.TYPE_COUNT=="MONTH")
{
sqlBuilder.AppendLine(" ,MONTH(C.[STATIS_DATE]) AS TDAY ");
groupBuilder.AppendLine(" ,MONTH(C.[STATIS_DATE]) ");
}
else
{
sqlBuilder.AppendLine(" ,DAY(C.[STATIS_DATE]) AS TDAY ");
groupBuilder.AppendLine(" ,DAY(C.[STATIS_DATE]) ");
}
sqlBuilder.AppendLine(" FROM [T_SA_WORKCELLQTYCOUNT] AS C ");
sqlBuilder.AppendLine(" JOIN [T_MD_MATERIAL] AS M ");
sqlBuilder.AppendLine(" ON C.[MATRIAL_CODE] = M.[MATERIAL_CODE] ");
sqlBuilder.AppendLine(" JOIN [T_MD_MATERIAL_CLASS] AS MC ");
sqlBuilder.AppendLine(" ON M.[MATERIAL_TYPE_CODE] = MC.[MATERIAL_TYPE_CODE] ");
whereBuilder.Append(" AND MC.[MATERIAL_ATTRIBUTE] = '2'");
if (string.IsNullOrEmpty(condition.START_DATE) == false)
{
whereBuilder.Append(" AND C.[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 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 M.MATERIAL_CODE = @MATERIAL_CODE ");
parameters.Add(new DataParameter { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = condition.MATERIAL_CODE });
}
if (string.IsNullOrEmpty(condition.HBTYPE) == false)
{
whereBuilder.AppendFormat(" AND charindex(M.HBTYPE,'{0}')>0 ", condition.HBTYPE);
}
if (string.IsNullOrEmpty(condition.COLOR) == false)
{
whereBuilder.Append(" AND M.COLOR = @COLOR ");
parameters.Add(new DataParameter { ParameterName = "COLOR", DataType = DbType.String, Value = condition.COLOR });
}
if (string.IsNullOrEmpty(condition.PROJECTCODE) == false)
{
whereBuilder.Append(" AND M.PROJECTCODE = @PROJECTCODE ");
parameters.Add(new DataParameter { ParameterName = "PROJECTCODE", DataType = DbType.String, Value = condition.PROJECTCODE });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
sqlBuilder.Append(groupBuilder.ToString());
sqlBuilder.AppendLine(" ORDER BY TDAY ");
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}