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-24
///
public class WasteRateCountDAL : BaseDAL
{
public List GetAllList(WasteRateCountDModel 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(WasteRateCountDModel condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder sqlBuilder2 = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
string group = " group by day(STATIS_DATE) ";
//构成查询语句
if (condition.TYPE_COUNT == "MONTH")
{
//月统计
sqlBuilder.AppendLine("select sum(QTY) QTY,sum(NOK_QTY) NOK_QTY,month(STATIS_DATE) tday from T_SA_LINEQTYCOUNT ");
group = " group by month(STATIS_DATE)";
}
else if (condition.TYPE_COUNT == "WEEK")
{
sqlBuilder.AppendLine("select sum(QTY) QTY,sum(NOK_QTY) NOK_QTY,DateName(week,DATEADD(DAY,-1,STATIS_DATE)) tday from T_SA_LINEQTYCOUNT ");
group = " group by DateName(week,DATEADD(DAY,-1,STATIS_DATE))";
}
else
{
//日统计
sqlBuilder.AppendLine("select sum(QTY) QTY,sum(NOK_QTY) NOK_QTY,day(STATIS_DATE) tday from T_SA_LINEQTYCOUNT ");
}
if (string.IsNullOrEmpty(condition.START_DATE) == false)
{
whereBuilder.Append(" AND tppo.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 tppo.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 tppo.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
}
}