using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using QMAPP.FJC.Entity.Injection;
using QMAPP.FJC.Entity.ProduceManage;
using QMFrameWork.Data;
using QMFrameWork.Log;
using QMAPP.FJC.Entity.FeedInManage;
namespace QMAPP.FJC.DAL.FeedInManage
{
///
/// 模块编号:M4-3
/// 作 用:不合格品信息查询
/// 作 者:张敬贺
/// 编写日期:2015年06月15日
///
public class UnQualityDAL
{
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(Epidermis condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PID";
page.SortExpression = "UPDATEDATE 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(Epidermis condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.AppendLine(" SELECT ");
sqlBuilder.AppendLine(" SUM((T.CAPACITY-T.USINGCOUNT)) AS UNUSED , ");
sqlBuilder.AppendLine(" T.MATERIAL_CODE, ");
sqlBuilder.AppendLine(" M.MATERIAL_NAME, ");
sqlBuilder.AppendLine(" M.MATERIAL_TYPE_CODE, ");
sqlBuilder.AppendLine(" c.MATERIAL_TYPE_NAME ");
sqlBuilder.AppendLine(" FROM T_AW_PRODUCT AS T ");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_MATERIAL M on M.MATERIAL_CODE=T.MATERIAL_CODE ");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_MATERIAL_CLASS c on c.MATERIAL_TYPE_CODE=M.MATERIAL_TYPE_CODE ");
whereBuilder.Append(" AND T.USINGSTATE='0' AND T.OUTFLAG='0' AND T.STATUS='1'");
#region 查询条件
if (string.IsNullOrEmpty(condition.PRODUCTTYPE) == false)
{
whereBuilder.Append(" AND UN.PRODUCTTYPE = @PRODUCTTYPE");
parameters.Add(new DataParameter { ParameterName = "PRODUCTTYPE", DataType = DbType.String, Value = condition.PRODUCTTYPE });
}
if (string.IsNullOrEmpty(condition.PRODUCTCODE) == false)
{
whereBuilder.Append(" AND UN.PRODUCTCODE LIKE @PRODUCTCODE");
parameters.Add(new DataParameter { ParameterName = "PRODUCTCODE", DataType = DbType.String, Value = condition.PRODUCTCODE });
}
if (condition.STARTCREATEDATE != DateTime.MinValue)
{
whereBuilder.Append(" AND UN.CREATEDATE >= @STARTCREATEDATE");
parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "STARTCREATEDATE", Value = condition.STARTCREATEDATE.Date.ToString("yyyy-MM-dd HH:mm:ss") });
}
if (condition.ENDCREATEDATE != DateTime.MinValue)
{
whereBuilder.Append(" AND UN.CREATEDATE < @ENDCREATEDATE");
parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "ENDCREATEDATE", Value = condition.ENDCREATEDATE.Date.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss") });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
#endregion
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取导出的数据
///
/// 获取导出的数据
///
/// 查询条件
/// 数据
public DataTable GetExportData(Epidermis 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 = "UnQualityExp";
}
return dt;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "不合格品信息导出获取数据错误!"
});
throw ex;
}
}
#endregion
}
}