using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.FJC.Entity.Operation;
using QMFrameWork.Data;
using QMFrameWork.Log;
using System.Data;
namespace QMAPP.FJC.DAL.Basic
{
///
/// 不合格品数量查询
///
public class UnQualityProductDAL
{
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(Product condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PRODUCTTYPE";
if (string.IsNullOrEmpty(page.SortExpression))
{
page.SortExpression = "PRODUCTTYPE DESC";
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage(sql, parameters.ToArray(), page);
List list = session.GetList(sql, parameters.ToArray()).ToList();
}
return page;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "不合格品信息获取列表错误!"
});
throw ex;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(Product condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.Append(@"SELECT
ROW_NUMBER() OVER(ORDER BY P.PRODUCTTYPE) AS PID,
P.PRODUCTTYPE,
M.MATERIAL_TYPE,
P.MATERIAL_CODE,
SUM(P.CAPACITY) AS CAPACITY
FROM T_AW_PRODUCT P
LEFT JOIN T_AW_MATERIALTYPE M ON M.MATERIAL_CODE=P.MATERIAL_CODE");
whereBuilder.Append(" AND P.STATUS ='1'");
#region 查询条件
if (string.IsNullOrEmpty(condition.PRODUCTTYPE) == false)
{
whereBuilder.Append(" AND P.PRODUCTTYPE = @PRODUCTTYPE ");
parameters.Add(new DataParameter { ParameterName = "PRODUCTTYPE", DataType = DbType.String, Value = condition.PRODUCTTYPE });
}
if (string.IsNullOrEmpty(condition.PRODUCTCODE) == false)
{
whereBuilder.Append(" AND P.MATERIAL_CODE LIKE @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) + "GROUP BY P.PRODUCTTYPE,P.MATERIAL_CODE,M.MATERIAL_TYPE");
}
#endregion
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取导出的数据
///
/// 获取导出的数据
///
/// 查询条件
/// 数据
public DataTable GetExportData(Product model)
{
DataTable dt = null;
string sql = null;
List parameters = new List();
try
{
//构成查询语句
sql = this.GetQuerySql(model, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
dt = session.GetTable(sql, parameters.ToArray());
dt.TableName = "UnQualityProductExp";
}
return dt;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "不合格品信息导出获取数据错误!"
});
throw ex;
}
}
#endregion
}
}