天津投入产出系统后端
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.
 
 
 
 
 
 

156 lines
6.1 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.FJC.Entity.EnergyManage;
using System.Data;
using QMFrameWork.Data;
using QMFrameWork.Log;
namespace QMAPP.FJC.DAL.EnergyManage
{
public class MeterReadingsDAL
{
#region 获取列表
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <param name="page">数据页</param>
/// <returns>数据页</returns>
public DataPage GetList(Meter condition, DataPage page)
{
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PID";
page.SortExpression = "EQPT_CODE";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage<Meter>(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "FIS 计划信息数据层-获取列表"
});
throw;
}
}
#endregion
#region 获取查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySql(Meter condition, ref List<DataParameter> parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句E.PID,
sqlBuilder.AppendLine(" SELECT ");
sqlBuilder.AppendLine(" E.EQUIPMENT_NAME AS EQPT_NAME , ");
sqlBuilder.AppendLine(" E.EQUIPMENT_CODE AS EQPT_CODE , ");
sqlBuilder.AppendLine(" SUM(R.DIFF_WITH_LAST) AS CONSUME ");
sqlBuilder.AppendLine(" FROM T_EM_METER_READINGS AS R ");
sqlBuilder.AppendLine(" LEFT JOIN T_EM_METER AS M ON R.METER_PID=M.PID ");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_EQUIPMENT AS E ON E.EQUIPMENT_CODE=M.EQPT_CODE ");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_FACTORY AS F ON F.FACTORY_CODE=M.FACTORY_CODE ");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_WORKCENTER AS W ON W.WORKCENTER_CODE=E.WORKCENTER_CODE ");
//查询条件
//工厂号
if (string.IsNullOrEmpty(condition.FACTORY_CODE) == false)
{
whereBuilder.Append(" AND M.FACTORY_CODE = @FACTORY_CODE");
parameters.Add(new DataParameter { ParameterName = "FACTORY_CODE", DataType = DbType.String, Value = condition.FACTORY_CODE });
}
//设备号
if (string.IsNullOrEmpty(condition.EQPT_CODE) == false)
{
whereBuilder.Append(" AND EQPT_CODE= @EQPT_CODE");
parameters.Add(new DataParameter { ParameterName = "EQPT_CODE", DataType = DbType.String, Value = condition.EQPT_CODE });
}
//工作中心编号
//if (string.IsNullOrEmpty(condition.WORKCENTER_CODE) == false)
//{
// whereBuilder.Append(" AND E.WORKCENTER_CODE=@WORKCENTER_CODE");
// parameters.Add(new DataParameter { ParameterName = "WORKCENTER_CODE", DataType = DbType.String, Value = condition.WORKCENTER_CODE });
//}
////开始时间
//if (condition.ONLINETIMESTART != DateTime.MinValue)
//{
// whereBuilder.Append(" AND R.READ_TIME >= @ONLINETIMESTART");
// parameters.Add(new DataParameter { ParameterName = "ONLINETIMESTART", DataType = DbType.String, Value = condition.ONLINETIMESTART });
//}
//if (condition.ONLINETIMEEND != DateTime.MinValue)
//{
// whereBuilder.Append(" AND R.READ_TIME <= @ONLINETIMEEND");
// parameters.Add(new DataParameter { ParameterName = "ONLINETIMEEND", DataType = DbType.String, Value = condition.ONLINETIMEEND });
//}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
sqlBuilder.AppendLine(" GROUP BY E.EQUIPMENT_NAME,E.EQUIPMENT_CODE ");
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取导出的数据
/// <summary>
/// 获取导出的数据
/// </summary>
/// <param name="user">查询条件</param>
/// <returns>数据</returns>
public DataTable GetExportData(Meter model)
{
DataTable dt = null;
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
//构成查询语句
sql = this.GetQuerySql(model, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
dt = session.GetTable(sql, parameters.ToArray());
dt.TableName = "EqptConsumeExp";
}
return dt;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "设备耗能信息导出获取数据错误!"
});
throw ex;
}
}
#endregion
}
}