using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using QMAPP.DAL;
using QMAPP.FJC.Entity.EM;
using QMAPP.FJC.Entity.Equipment;
using QMFrameWork.Data;
using QMFrameWork.Log;
namespace QMAPP.FJC.DAL.EM
{
public class UsageLogDAL : BaseDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// 信息
public UsageLogEntity Get(UsageLogEntity model)
{
//页面为空
return null;
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(UsageLogEntity condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PID";
page.SortExpression = "PID";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "数据层-获取列表"
});
throw;
}
}
public DataPage GetCountList(UsageLogEntity condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetCountSql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "RTIME";
page.SortExpression = "RTIME";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "数据层-获取列表"
});
throw;
}
}
public UsageLogEntity GetCounts(UsageLogEntity condition)
{
List parameters = new List();
try
{
var sql = this.GetQuerySql1(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
return session.Get(sql, parameters.ToArray());
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取列表
///
/// 条件
/// 全部集合
public List GetList(UsageLogEntity 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)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "数据层-获取区域列表"
});
throw;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(UsageLogEntity condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
var pidStr = condition.PID;
var ftimeStr = condition.FLAG_TIME;
try
{
//构成查询语句
sqlBuilder.AppendLine(" select MOULD_PID as PID ");
sqlBuilder.AppendLine(" ,MOULD_PID ");
sqlBuilder.AppendLine(" ,MACHINE_PID ");
sqlBuilder.AppendLine(" ,count(MOULD_PID)as USAGECOUNT ");
sqlBuilder.AppendLine(" from T_EQP_USAGELOG ");
//sqlBuilder.AppendLine(" LEFT JOIN T_EM_METER m ON m.PID=t.METER_PID ");
//sqlBuilder.AppendLine(" LEFT JOIN T_BD_MACHINEINFO b ON b.MACHINECODDE=m.EQPT_CODE ");
//whereBuilder.Append(" and t.READ_RESULT='1' and m.STATE='1'");
//查询条件
//if (string.IsNullOrEmpty(condition.MOULD_PID) == false)
//{
// whereBuilder.Append(@" AND MOULD_PID LIKE '%'+" + "@MOULD_PID" + "+'%'");
// //whereBuilderSet.Append(" AND C.MACHINECODDE LIKE '%'+" + "@EQPT_CODE" + "+'%'");
// parameters.Add(new DataParameter { ParameterName = "MOULD_PID", DataType = DbType.String, Value = condition.MOULD_PID });
//}
//if (string.IsNullOrEmpty(condition.MACHINE_PID) == false)
//{
// whereBuilder.Append(@" AND MACHINE_PID LIKE '%'+" + "@MACHINE_PID" + "+'%'");
// //whereBuilderSet.Append(" AND C.MACHINECODDE LIKE '%'+" + "@EQPT_CODE" + "+'%'");
// parameters.Add(new DataParameter { ParameterName = "MACHINE_PID", DataType = DbType.String, Value = condition.MACHINE_PID });
//}
if (string.IsNullOrEmpty(condition.START_OPERATION_DATE) == false)
{
whereBuilder.Append(" AND OPERATION_DATE >= @START_OPERATION_DATE");
parameters.Add(new DataParameter { ParameterName = "START_OPERATION_DATE", DataType = DbType.String, Value = condition.START_OPERATION_DATE });
}
if (string.IsNullOrEmpty(condition.END_OPERATION_DATE) == false)
{
whereBuilder.Append(" AND OPERATION_DATE <= @END_OPERATION_DATE");
parameters.Add(new DataParameter { ParameterName = "END_OPERATION_DATE", DataType = DbType.String, Value = condition.END_OPERATION_DATE });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
sqlBuilder.AppendLine(" group by MOULD_PID, MACHINE_PID ");
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
private string GetQuerySql1(UsageLogEntity condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
var pidStr = condition.PID;
var ftimeStr = condition.FLAG_TIME;
try
{
//构成查询语句
sqlBuilder.AppendLine(" select count(1)as USAGECOUNT ");
sqlBuilder.AppendLine(" from T_EQP_USAGELOG ");
//sqlBuilder.AppendLine(" LEFT JOIN T_EM_METER m ON m.PID=t.METER_PID ");
//sqlBuilder.AppendLine(" LEFT JOIN T_BD_MACHINEINFO b ON b.MACHINECODDE=m.EQPT_CODE ");
//whereBuilder.Append(" and t.READ_RESULT='1' and m.STATE='1'");
//查询条件
if (string.IsNullOrEmpty(condition.START_OPERATION_DATE) == false)
{
whereBuilder.Append(" AND OPERATION_DATE >= @START_OPERATION_DATE");
parameters.Add(new DataParameter { ParameterName = "START_OPERATION_DATE", DataType = DbType.String, Value = condition.START_OPERATION_DATE });
}
if (string.IsNullOrEmpty(condition.END_OPERATION_DATE) == false)
{
whereBuilder.Append(" AND OPERATION_DATE <= @END_OPERATION_DATE");
parameters.Add(new DataParameter { ParameterName = "END_OPERATION_DATE", DataType = DbType.String, Value = condition.END_OPERATION_DATE });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
//sqlBuilder.AppendLine(" group by MOULD_PID, MACHINE_PID ");
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
private string GetCountSql(UsageLogEntity condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
int dateCount = 4;
if (!string.IsNullOrEmpty(condition.FLAG_TIME))
{
int tempCount = Convert.ToInt32(condition.FLAG_TIME);
if (tempCount==1)
{
dateCount = 7;
}
else if (tempCount == 2)
{
dateCount = 10;
}
}
//构成查询语句
sqlBuilder.AppendLine(" select RTIME,count(MOULD_PID) as USAGECOUNT from ");
sqlBuilder.AppendLine(" (select ");
sqlBuilder.AppendLine(" convert(varchar(" + dateCount + "),OPERATION_DATE,120) RTIME , ");
sqlBuilder.AppendLine(" MOULD_PID ");
sqlBuilder.AppendLine(" from T_EQP_USAGELOG ");
sqlBuilder.Append(" WHERE MOULD_PID = '" + condition.PID + "' ");
if (string.IsNullOrEmpty(condition.START_OPERATION_DATE) == false)
{
sqlBuilder.Append(" AND OPERATION_DATE >= '" + condition.START_OPERATION_DATE + "'");
}
if (string.IsNullOrEmpty(condition.END_OPERATION_DATE) == false)
{
sqlBuilder.Append(" AND OPERATION_DATE <= '" + condition.END_OPERATION_DATE + "'");
}
sqlBuilder.Append(" ) t ");
//whereBuilder.Append(" and t.READ_RESULT='1' and m.STATE='1'");
//whereBuilder.Append(" AND m.EQPT_CODE = '" + condition.PID + "' ");
//查询条件
//if (string.IsNullOrEmpty(condition.STARTCREATEDATE) == false)
//{
// whereBuilder.Append(" AND t.READ_TIME >= '" + condition.STARTCREATEDATE + "'");
//}
//if (string.IsNullOrEmpty(condition.ENDCREATEDATE) == false)
//{
// whereBuilder.Append(" AND t.READ_TIME <= '" + condition.ENDCREATEDATE + "'");
//}
//if (whereBuilder.Length > 0)
//{
// sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
//}
sqlBuilder.AppendLine(" group by RTIME ");
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
//task程序
public List GetMonitorCode(IDataSession Bsession)
{
List parameters = new List();
string sql = "SELECT * FROM T_TM_MonitorCode WHERE ReadFlag=0 and CurrentMode = '3'";
if (Bsession != null)
{
//插入基本信息
return Bsession.GetList(sql, parameters.ToArray()).ToList();
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
return session.GetList(sql, parameters.ToArray()).ToList();
}
}
public List GetMonitorData(IDataSession Bsession)
{
List parameters = new List();
string sql = "SELECT * FROM T_TM_MonitorData WHERE ReadFlag=0 ";
if (Bsession != null)
{
//插入基本信息
return Bsession.GetList(sql, parameters.ToArray()).ToList();
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
return session.GetList(sql, parameters.ToArray()).ToList();
}
}
public int Insert(UsageLogEntity model)
{
try
{
if (BaseSession != null)
{
//插入基本信息
return BaseSession.Insert(model);
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
return session.Insert(model);
}
}
catch (Exception ex)
{
throw ex;
}
}
public int UpdateMonitorCode(MonitorCodeEntity model, IDataSession Bsession)
{
return Bsession.Update(model);
}
public int UpdateMonitorData(MonitorDataEntity model, IDataSession Bsession)
{
return Bsession.Update(model);
}
}
}