/// <summary> /// 设备曲线数据读取器。 /// </summary> /// <param name="device">设备集合中的第一次访问的设备对象。</param> /// <param name="startDate">访问数据的起始日期,如果未设置过 DateRange 日期范围则以该日期作为数据筛选的起始日期。</param> /// <param name="endDate">访问数据的结束日期,如果未设置过 DateRange 日期范围则以该日期作为数据筛选的结束日期。</param> /// <param name="curvePoint">是否将曲线数据强制转换为指定的点数。如果该值为 0 则表示不转换,设备曲线数据点数使用各设备的默认值。</param> /// <param name="mergeOption">当转换后的点数小于目前曲线点数时,如何合并目前的数据。</param> /// <param name="splitOption">当转换后的点数大于目前曲线点数时,如何拆分目前的数据。</param> /// <param name="tableNameF">96及以下点数数据表名。</param> /// <param name="tableNameS">96以上点数数据表名。</param> public DeviceCurveDataReader(Device device, DateTime startDate, DateTime endDate, int curvePoint, CurveMergeOptions mergeOption, CurveSplitOptions splitOption, string tableNameF, string tableNameS) { this.m_CurvePoint = curvePoint; this.m_FirstDevice = device; if (device.DateRange != null && device.DateRange.ContainsDay(startDate, endDate)) { this.m_DateRange = device.DateRange; } else { this.m_DateRange = new DateRange(startDate, endDate); } string strDeviceIdWhereF = ""; string strDeviceIdWhereS = ""; if (device.Source != null && device.Source.Contains(device)) { foreach (Device device1 in device.Source) { CurvePointOptions _CurvePoint = this.GetDeviceCurvePoint(device1); if (_CurvePoint <= CurvePointOptions.Point96 || (curvePoint > 0 && curvePoint < (int)CurvePointOptions.Point96)) { strDeviceIdWhereF += "," + device1.DeviceId; } else { strDeviceIdWhereS += "," + device1.DeviceId; } if (device1.DateRange == null || device1.DateRange.UniqueId != this.m_DateRange.UniqueId) { device1.DateRange = this.m_DateRange; } this.SetReader(device1); } if (strDeviceIdWhereF.Length > 0) { strDeviceIdWhereF = " IN (" + strDeviceIdWhereF.Substring(1) + ")"; } if (strDeviceIdWhereS.Length > 0) { strDeviceIdWhereS = " IN (" + strDeviceIdWhereS.Substring(1) + ")"; } } else { CurvePointOptions _CurvePoint = this.GetDeviceCurvePoint(device); if (_CurvePoint <= CurvePointOptions.Point96 || (curvePoint > 0 && curvePoint < (int)CurvePointOptions.Point96)) { strDeviceIdWhereF = "=" + device.DeviceId; } else { strDeviceIdWhereS = "=" + device.DeviceId; } if (device.DateRange == null || device.DateRange.UniqueId != this.m_DateRange.UniqueId) { device.DateRange = this.m_DateRange; } this.SetReader(device); } AC.Base.Database.DbConnection dbConn = device.Application.GetDbConnection(); if (dbConn != null) { try { foreach (DateRange.MonthlyForDayRange range in this.DateRange.GetMonthRanges()) { if (strDeviceIdWhereF.Length > 0) { string strTableName = Database.DbConnection.GetMonthlyName(tableNameF, range.Date.Year, range.Date.Month); if (dbConn.TableIsExist(strTableName)) { string strSqlDate = range.GetSqlWhere("DateNum"); if (strSqlDate.Length > 0) { strSqlDate = " AND " + strSqlDate; } string strSql = "SELECT * FROM " + strTableName + " WHERE DeviceId" + strDeviceIdWhereF + strSqlDate; System.Data.IDataReader dr = dbConn.ExecuteReader(strSql); while (dr.Read()) { Device deviceF = this.GetDevice(Function.ToInt(dr["DeviceId"])); CurvePointOptions _DeviceCurvePointF = this.GetDeviceCurvePoint(deviceF); if (_DeviceCurvePointF > CurvePointOptions.Point96) { _DeviceCurvePointF = CurvePointOptions.Point96; //如果设备默认的曲线数据点数大于96点,则使用96点数据。 } decimal?[] decValues = new decimal?[_DeviceCurvePointF.GetPointCount()]; for (int intPointIndex = 0; intPointIndex < _DeviceCurvePointF.GetPointCount(); intPointIndex++) { string strColumnName = "Value" + (intPointIndex * (96 / _DeviceCurvePointF.GetPointCount()) + 1); decValues[intPointIndex] = Function.ToDecimalNull(dr[strColumnName]); } if (curvePoint != 0 && curvePoint != (int)_DeviceCurvePointF) { //需要对曲线数据进行转换 decValues = CurveValue.ConvertTo(decValues, (CurvePointOptions)curvePoint, mergeOption, splitOption); } this.DoDataReaderF(deviceF, dr, decValues); } dr.Close(); } } if (strDeviceIdWhereS.Length > 0) { string strTableName = Database.DbConnection.GetMonthlyName(tableNameS, range.Date.Year, range.Date.Month); if (dbConn.TableIsExist(strTableName)) { string strSqlDate = range.GetSqlWhere("DateNum"); if (strSqlDate.Length > 0) { strSqlDate = " AND " + strSqlDate; } string strSql = "SELECT * FROM " + strTableName + " WHERE DeviceId" + strDeviceIdWhereS + strSqlDate; System.Data.IDataReader dr = dbConn.ExecuteReader(strSql); while (dr.Read()) { this.DoDataReaderS(this.GetDevice(Function.ToInt(dr["DeviceId"])), dr); } dr.Close(); } } } } catch (Exception ex) { throw ex; } finally { dbConn.Close(); } if (curvePoint > 0 && strDeviceIdWhereS.Length > 0) { //通知继承的曲线读取器转换分秒数据点数。 this.DoConvertS(); } } }
/// <summary> /// 搜索指定页数的曲线数据。 /// </summary> /// <param name="pageNum">搜索第几页的数据。</param> /// <returns></returns> public IList <V> Search(int pageNum) { List <V> lstValues = new List <V>(); this.PageNum = pageNum; this.RecordsetCount = 0; this.PageCount = 0; Database.DbConnection dbConn = this.Application.GetDbConnection(); if (dbConn == null) { throw new Exception("当前系统未配置数据库连接,无法使用搜索功能。"); } try { if (dbConn.TableIsExist(this.GetDataTableName())) { string strWhere = Tables.Device.TableName + "." + Tables.Device.DeviceId + "=" + this.GetDataTableName() + ".DeviceId"; if (this.StartDate == this.EndDate) { strWhere += " AND " + this.GetDataTableName() + ".DateNum=" + Function.ToIntDate(this.StartDate); } else { strWhere += " AND " + this.GetDataTableName() + ".DateNum>=" + Function.ToIntDate(this.StartDate) + " AND " + this.GetDataTableName() + ".DateNum<=" + Function.ToIntDate(this.EndDate); } strWhere += this.GetSqlWhere(); if (this.Filters.Count > 0) { string strFilterWhere = this.Filters.GetFilterSql(true); if (strFilterWhere != null && strFilterWhere.Length > 0) { strWhere += " AND (" + strFilterWhere + ")"; } } if (strWhere.Length > 0) { strWhere = " WHERE " + strWhere; } string strTableNames = ""; if (this.m_TableNames != null && this.m_TableNames.Length > 0) { foreach (string name in this.m_TableNames) { strTableNames += "," + name; } } string strSql; // 查询结果记录集总数。 strSql = "SELECT COUNT(" + this.GetDataTableName() + ".DeviceId) FROM " + Tables.Device.TableName + strTableNames + "," + this.GetDataTableName() + strWhere; this.RecordsetCount = Function.ToInt(dbConn.ExecuteScalar(strSql)); // 计算总页数并检查当前页是否在页数范围内。 if (this.RecordsetCount == 0) { this.PageCount = 0; this.PageNum = 0; } else { if (this.PageSize > 0) { this.PageCount = this.RecordsetCount / this.PageSize; if ((this.RecordsetCount % this.PageSize) > 0) { this.PageCount++; } if (this.PageNum < 1) { this.PageNum = 1; } if (this.PageNum > this.PageCount) { this.PageNum = this.PageCount; } } else { this.PageNum = 1; this.PageCount = 1; } System.Data.IDataReader dr = null; if (this.PageCount == 1) { strSql = "SELECT " + this.GetSelectColumn() + " FROM " + Tables.Device.TableName + strTableNames + "," + this.GetDataTableName() + strWhere + this.GetOrder(true, this.IsAscend ? false : true); dr = dbConn.ExecuteReader(strSql); } else if (this.PageCount > 1) { if (this.PageNum == 1) { dr = dbConn.GetDataReader(this.PageSize, this.GetSelectColumn(), Tables.Device.TableName + strTableNames + "," + this.GetDataTableName(), strWhere, this.GetOrder(true, this.IsAscend ? false : true)); } else if (this.PageNum == this.PageCount) { dr = dbConn.GetDataReader(this.PageSize, this.RecordsetCount, this.GetSelectColumn(), Tables.Device.TableName + strTableNames + "," + this.GetDataTableName(), strWhere, this.GetOrder(true, this.IsAscend ? true : false), this.GetOrder(false, this.IsAscend ? false : true)); } else { dr = dbConn.GetDataReader(this.PageSize, this.PageNum, this.GetSelectColumn(), Tables.Device.TableName + strTableNames + "," + this.GetDataTableName(), strWhere, this.GetOrder(true, this.IsAscend ? false : true), this.GetOrder(false, this.IsAscend ? true : false), this.GetOrder(false, this.IsAscend ? false : true)); } } if (dr != null) { DeviceCollection devices = new DeviceCollection(true); while (dr.Read()) { int intDeviceId = Function.ToInt(dr[Tables.Device.DeviceId]); Device device = this.Application.GetDeviceInstance(intDeviceId); if (device != null) { device.SetDataReader(dr); } else { DeviceType deviceType = this.Application.DeviceTypeSort.GetDeviceType(Function.ToString(dr[Tables.Device.DeviceType])); if (deviceType != null) { device = deviceType.CreateDevice(); device.SetApplication(this.Application); device.SetDataReader(dr); this.Application.SetDeviceInstance(device); } } if (device != null) { devices.Add(device); decimal?[] decValues = new decimal?[CurvePointOptions.Point96.GetPointCount()]; for (int intPointIndex = 0; intPointIndex < CurvePointOptions.Point96.GetPointCount(); intPointIndex++) { string strColumnName = "Value" + (intPointIndex + 1); decValues[intPointIndex] = Function.ToDecimalNull(dr[strColumnName]); } if (this.CurvePoint != CurvePointOptions.Point96) { //需要对曲线数据进行转换 decValues = CurveValue.ConvertTo(decValues, (CurvePointOptions)this.CurvePoint, this.CurveMerge, CurveSplitOptions.Copy); } lstValues.Add(this.DoDataReader(device, dr, decValues)); } } dr.Close(); } } } else { this.RecordsetCount = 0; this.PageCount = 0; this.PageNum = 0; } } catch (Exception ex) { throw ex; } finally { dbConn.Close(); } return(lstValues); }