using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.FJC.Entity.Operation;
using QMFrameWork.Data;
using QMAPP.DAL;
using System.Data;
namespace QMAPP.FJC.DAL.Operation
{
public class InjectionCheckDAL : BaseDAL
{
#region 插入信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public int Insert(InjectionCheck model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Insert(model);
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(InjectionCheck condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PID";
page.SortExpression = "CREATEDATE DESC";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(InjectionCheck condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.Append("SELECT A.PID,A.PRODUCTCODE,A.PRODUCTTYPE,A.MACHINENAME,A.MACHINECODDE,A.PRODUCEDATE,A.CREATEUSER,A.CREATEDATE,A.UPDATEUSER,A.UPDATEDATE,");
//if (condition.INTERNALNUMBER > 0)
//{
//}
//else
//{
// sqlBuilder.Append(" 0 as HAVEINJECTION");
//}
sqlBuilder.AppendFormat(" (SELECT COUNT(1) FROM T_AW_INJECTIONRECORDER R WHERE R.MACHINECODDE=A.MACHINECODDE AND DATEDIFF(mi,R.CREATEDATE,A.PRODUCEDATE)<(SELECT CHECKMAXVALUE FROM T_AW_INJECTIONCHECKSET) AND DATEDIFF(mi,R.CREATEDATE,A.PRODUCEDATE)>(SELECT CHECKMINVALUE FROM T_AW_INJECTIONCHECKSET) ) as HAVEINJECTION", condition.INTERNALNUMBER.ToString());
sqlBuilder.Append(" FROM T_AW_INJECTIONCHECK A ");
//whereBuilder.Append(" AND FLGDEL<>1");
//查询条件
whereBuilder.Append(" AND A.CREATEDATE>@CREATEDATESTART ");
parameters.Add(new DataParameter { ParameterName = "CREATEDATESTART", DataType = DbType.String, Value = condition.CREATEDATESTART.ToString("yyyy-MM-dd HH:mm:ss") });
whereBuilder.Append(" AND A.CREATEDATE<@CREATEDATEEND ");
parameters.Add(new DataParameter { ParameterName = "CREATEDATEEND", DataType = DbType.String, Value = condition.CREATEDATEEND.ToString("yyyy-MM-dd HH:mm:ss") });
if (string.IsNullOrEmpty(condition.PRODUCTTYPE) == false)
{
whereBuilder.Append(" AND A.PRODUCTTYPE=@PRODUCTTYPE ");
parameters.Add(new DataParameter { ParameterName = "PRODUCTTYPE", DataType = DbType.String, Value = condition.PRODUCTTYPE });
}
if (string.IsNullOrEmpty(condition.MACHINECODDE) == false)
{
whereBuilder.Append(" AND A.MACHINECODDE=@MACHINECODDE ");
parameters.Add(new DataParameter { ParameterName = "MACHINECODDE", DataType = DbType.String, Value = condition.MACHINECODDE });
}
if (string.IsNullOrEmpty(condition.PRODUCTCODE) == false)
{
whereBuilder.Append(" AND A.PRODUCTCODE LIKE '%'+" + "@PRODUCTCODE" + "+'%' ");
parameters.Add(new DataParameter { ParameterName = "PRODUCTCODE", DataType = DbType.String, Value = condition.PRODUCTCODE });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
public int Delete(string pid)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Delete(new InjectionCheck() { PID = pid });
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
}
}