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.
161 lines
7.1 KiB
161 lines
7.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using QMFrameWork.Data;
|
|
using QMAPP.MESReport.Entity.LineQTY;
|
|
using QMAPP.DAL;
|
|
using System.Data;
|
|
using QMAPP.MESReport.Entity.Tables;
|
|
|
|
namespace QMAPP.MESReport.DAL.LineQTY
|
|
{
|
|
/// <summary>
|
|
/// 设备用电分析 数据访问层
|
|
/// 于子清
|
|
/// 2017-11-6
|
|
/// </summary>
|
|
public class EquConsumeCountDAL : BaseDAL
|
|
{
|
|
public List<EquConsumeCountDModel> GetAllList(EquConsumeCountDModel condition)
|
|
{
|
|
List<EquConsumeCountDModel> list = new List<EquConsumeCountDModel>();
|
|
string sql = null;
|
|
List<DataParameter> parameters = new List<DataParameter>();
|
|
try
|
|
{
|
|
sql = this.GetQuerySql(condition, ref parameters);
|
|
//sql ="select tsa.EQPT_CODE as MACHINENAME,day(tsa.STATIS_DATE) tday,sum(tsa.CONSUME) CONSUME from T_SA_EQPTCONSUME tsa group by tsa.EQPT_CODE,day(tsa.STATIS_DATE)";
|
|
|
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
|
{
|
|
list = session.GetList<EquConsumeCountDModel>(sql, parameters.ToArray()).ToList();
|
|
}
|
|
return list;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
|
|
#region 获取查询语句
|
|
/// <summary>
|
|
/// 获取查询语句
|
|
/// </summary>
|
|
/// <param name="user">查询条件</param>
|
|
/// <param name="parameters">参数</param>
|
|
/// <returns>查询语句</returns>
|
|
private string GetQuerySql(EquConsumeCountDModel condition, ref List<DataParameter> parameters)
|
|
{
|
|
StringBuilder sqlBuilder = new StringBuilder();
|
|
StringBuilder whereBuilder = new StringBuilder();
|
|
try
|
|
{
|
|
#region 注销
|
|
|
|
//sqlBuilder.AppendLine(" select m.EQPT_CODE ,b.MACHINENAME , MAX(t.READING)-ISNULL(MIN(t.reading),0) as CONSUME from T_EM_METER_READINGS t LEFT JOIN T_EM_METER m ON m.PID=t.METER_PID LEFT JOIN T_BD_MACHINEINFO b ON b.MACHINECODDE=m.EQPT_CODE WHERE 1=1 ");
|
|
|
|
//if (string.IsNullOrEmpty(condition.START_DATE) == false)
|
|
//{
|
|
// whereBuilder.Append(" AND t.READ_TIME >= @START_DATE ");
|
|
// parameters.Add(new DataParameter { ParameterName = "START_DATE", DataType = DbType.DateTime, Value = condition.START_DATE });
|
|
//}
|
|
|
|
//if (string.IsNullOrEmpty(condition.END_DATE) == false)
|
|
//{
|
|
// whereBuilder.Append(" AND t.READ_TIME <= @END_DATE ");
|
|
// parameters.Add(new DataParameter { ParameterName = "END_DATE", DataType = DbType.DateTime, Value = condition.END_DATE });
|
|
//}
|
|
|
|
//whereBuilder.Append(" and t.READING>0 and t.READ_RESULT='1' and m.STATE='1' ");
|
|
|
|
|
|
//if (whereBuilder.Length > 0)
|
|
//{
|
|
// sqlBuilder.Append(" and " + whereBuilder.ToString().Substring(4));
|
|
//}
|
|
//sqlBuilder.Append(" group by b.MACHINENAME, m.EQPT_CODE ");
|
|
//sqlBuilder.Append(" order by m.EQPT_CODE ");
|
|
|
|
#endregion
|
|
|
|
sqlBuilder.AppendFormat(@" SELECT m.EQPT_CODE ,b.MACHINENAME ,TEMP1.MAXREADING-TEMP1.MINREADING as consume
|
|
FROM (
|
|
SELECT T.METER_PID ,MAX(T.READING) MAXREADING,MIN(T.READING) AS MINREADING FROM T_EM_METER_READINGS T
|
|
WHERE t.READ_TIME >= '{0}' AND t.READ_TIME <= '{1}' AND t.READ_RESULT='1' and t.READING>0
|
|
GROUP BY T.METER_PID
|
|
)TEMP1 INNER JOIN T_EM_METER M ON m.PID=TEMP1.METER_PID INNER JOIN T_BD_MACHINEINFO b
|
|
ON b.MACHINECODDE=M.EQPT_CODE", condition.START_DATE,condition.END_DATE);
|
|
|
|
return sqlBuilder.ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
private string GetQuerySqlOld(EquConsumeCountDModel condition, ref List<DataParameter> parameters)
|
|
{
|
|
StringBuilder sqlBuilder = new StringBuilder();
|
|
StringBuilder sqlBuilder2 = new StringBuilder();
|
|
StringBuilder whereBuilder = new StringBuilder();
|
|
try
|
|
{
|
|
string group = "";
|
|
//构成查询语句
|
|
|
|
if (condition.TYPE_COUNT == "YEAR")
|
|
{
|
|
//周统计
|
|
sqlBuilder.AppendLine("select tbd.MACHINENAME,year(tsa.STATIS_DATE) tday,sum(tsa.CONSUME) CONSUME from T_SA_EQPTCONSUME tsa inner join T_BD_MACHINEINFO tbd on tbd.MACHINECODDE=tsa.EQPT_CODE ");
|
|
group = " group by tbd.MACHINENAME,year(tsa.STATIS_DATE) ";
|
|
}
|
|
else if (condition.TYPE_COUNT == "MONTH")
|
|
{
|
|
//月统计
|
|
sqlBuilder.AppendLine("select tbd.MACHINENAME,month(tsa.STATIS_DATE) tday,sum(tsa.CONSUME) CONSUME from T_SA_EQPTCONSUME tsa inner join T_BD_MACHINEINFO tbd on tbd.MACHINECODDE=tsa.EQPT_CODE ");
|
|
group = " group by tbd.MACHINENAME,month(tsa.STATIS_DATE) ";
|
|
}
|
|
else
|
|
{
|
|
//日统计
|
|
sqlBuilder.AppendLine("select tbd.MACHINENAME,day(tsa.STATIS_DATE) tday,sum(tsa.CONSUME) CONSUME from T_SA_EQPTCONSUME tsa inner join T_BD_MACHINEINFO tbd on tbd.MACHINECODDE=tsa.EQPT_CODE ");
|
|
group = " group by tbd.MACHINENAME,day(tsa.STATIS_DATE) ";
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(condition.START_DATE) == false)
|
|
{
|
|
whereBuilder.Append(" AND tsa.STATIS_DATE >= @START_DATE ");
|
|
parameters.Add(new DataParameter { ParameterName = "START_DATE", DataType = DbType.DateTime, Value = condition.START_DATE });
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(condition.END_DATE) == false)
|
|
{
|
|
whereBuilder.Append(" AND tsa.STATIS_DATE <= @END_DATE ");
|
|
parameters.Add(new DataParameter { ParameterName = "END_DATE", DataType = DbType.DateTime, Value = condition.END_DATE });
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(condition.EQPT_CODE) == false)
|
|
{
|
|
whereBuilder.Append(" AND tsa.EQPT_CODE = @EQPT_CODE ");
|
|
parameters.Add(new DataParameter { ParameterName = "EQPT_CODE", DataType = DbType.String, Value = condition.EQPT_CODE });
|
|
}
|
|
|
|
if (whereBuilder.Length > 0)
|
|
{
|
|
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
|
|
}
|
|
sqlBuilder.Append(group);
|
|
|
|
return sqlBuilder.ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|