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 获取信息 /// /// 获取信息 /// /// 条件 /// 信息 public EmMeterReadings Get(EmMeterReadings model) { //页面为空 return null; } #endregion #region 获取列表 /// /// 获取列表 /// /// 条件 /// 数据页 /// 数据页 public DataPage GetList(EmMeterReadings condition, DataPage page) { string sql = null; List parameters = new List(); try { sql = this.GetQuerySql(condition, ref parameters); //分页关键字段及排序 page.KeyName = "METER_CODE"; page.SortExpression = "METER_CODE"; 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(EmMeterReadings 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 List GetList(EmMeterReadings 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(EmMeterReadings 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 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 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 } }