天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

266 lines
10 KiB

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using QMAPP.DAL;
using QMAPP.FJC.Entity.EM;
using QMFrameWork.Data;
using QMFrameWork.Log;
namespace QMAPP.FJC.DAL.EM
{
public class EmMeterReadingsDAL : BaseDAL
{
#region 获取信息
/// <summary>
/// 获取信息
/// </summary>
/// <param name="">条件</param>
/// <returns>信息</returns>
public EmMeterReadings Get(EmMeterReadings model)
{
//页面为空
return null;
}
#endregion
#region 获取列表
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <param name="page">数据页</param>
/// <returns>数据页</returns>
public DataPage GetList(EmMeterReadings condition, DataPage page)
{
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "METER_CODE";
page.SortExpression = "METER_CODE";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage<EmMeterReadings>(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "能源管理数据层-获取列表"
});
throw;
}
}
public DataPage GetCountList(EmMeterReadings condition, DataPage page)
{
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetCountSql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "RTIME";
page.SortExpression = "RTIME";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage<EmMeterReadings>(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "能源管理数据层-获取列表"
});
throw;
}
}
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <returns>全部集合</returns>
public List<EmMeterReadings> GetList(EmMeterReadings condition)
{
List<EmMeterReadings> list = new List<EmMeterReadings>();
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySql(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList<EmMeterReadings>(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "能源管理数据层-获取区域列表"
});
throw;
}
}
#endregion
#region 获取查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySql(EmMeterReadings condition, ref List<DataParameter> parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
var pidStr = condition.PID;
var ftimeStr = condition.FLAG_TIME;
try
{
//构成查询语句
sqlBuilder.AppendLine(" select m.PID ");
sqlBuilder.AppendLine(" ,m.EQPT_CODE ");
sqlBuilder.AppendLine(" ,m.METER_CODE ");
sqlBuilder.AppendLine(" ,m.METER_NAME ");
sqlBuilder.AppendLine(" ,b.MACHINENAME ");
sqlBuilder.AppendLine(" ,MAX(t.READING)as READING ");
sqlBuilder.AppendLine(" from T_EM_METER_READINGS t ");
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.METER_CODE) == false)
{
whereBuilder.Append(@" AND m.METER_CODE LIKE '%'+" + "@METER_CODE" + "+'%'");
parameters.Add(new DataParameter { ParameterName = "METER_CODE", DataType = DbType.String, Value = condition.METER_CODE });
}
if (string.IsNullOrEmpty(condition.EQPT_CODE) == false)
{
whereBuilder.Append(@" AND m.EQPT_CODE LIKE '%'+" + "@EQPT_CODE" + "+'%'");
//whereBuilderSet.Append(" AND C.MACHINECODDE LIKE '%'+" + "@EQPT_CODE" + "+'%'");
parameters.Add(new DataParameter { ParameterName = "EQPT_CODE", DataType = DbType.String, Value = condition.EQPT_CODE });
}
if (string.IsNullOrEmpty(condition.STARTCREATEDATE) == false)
{
whereBuilder.Append(" AND t.READ_TIME >= @STARTCREATEDATE");
parameters.Add(new DataParameter { ParameterName = "STARTCREATEDATE", DataType = DbType.String, Value = condition.STARTCREATEDATE });
}
if (string.IsNullOrEmpty(condition.ENDCREATEDATE) == false)
{
whereBuilder.Append(" AND t.READ_TIME <= @ENDCREATEDATE");
parameters.Add(new DataParameter { ParameterName = "ENDCREATEDATE", DataType = DbType.String, Value = condition.ENDCREATEDATE });
}
whereBuilder.Append(@" and m.pid is not null ");
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
sqlBuilder.AppendLine(" group by b.MACHINENAME, m.EQPT_CODE ,m.PID,m.METER_NAME, m.METER_CODE ");
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
private string GetCountSql(EmMeterReadings condition, ref List<DataParameter> 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,sum(DIFF_WITH_LAST) as READING from ");
sqlBuilder.AppendLine(" (select READ_TIME, ");
sqlBuilder.AppendLine(" convert(varchar(" + dateCount + "),READ_TIME,120) RTIME , ");
sqlBuilder.AppendLine(" t.DIFF_WITH_LAST , ");
sqlBuilder.AppendLine(" Row_Number() OVER (partition by convert(varchar(" + dateCount + "),READ_TIME,120) ORDER BY READ_TIME ) rank ");
sqlBuilder.AppendLine(" from T_EM_METER_READINGS t ");
//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 ");
//sqlBuilder.Append(" WHERE t.READ_RESULT='1' and m.STATE='1' ");
sqlBuilder.Append(" where METER_PID = '" + condition.PID + "' ");
if (string.IsNullOrEmpty(condition.STARTCREATEDATE) == false)
{
sqlBuilder.Append(" AND t.READ_TIME >= '" + condition.STARTCREATEDATE + "'");
}
if (string.IsNullOrEmpty(condition.ENDCREATEDATE) == false)
{
sqlBuilder.Append(" AND t.READ_TIME <= '" + condition.ENDCREATEDATE + "'");
}
sqlBuilder.Append(" ) t1 where rank !=1 ");
//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
}
}