/// <summary> /// 获取周聚集数据 /// </summary> /// <param name="sensorId">传感器ID</param> /// <param name="factorId"></param> /// <param name="timeRange">周中的天,周一:1-周日:7</param> /// <param name="beginTime">开始时间范围</param> /// <param name="endTime">结束时间范围</param> /// <returns></returns> public RawData GetWeekAggRawData(int sensorId, int factorId, AggTimeRange timeRange, DateTime beginTime, DateTime endTime) { TableInfo info; if (!this.GetTableInfo(factorId, out info)) { return(null); } string colums = info.ColumnNames; string tableName = info.TableName; string[] colName = colums.Split(','); string sql = String.Format( @"select {0} from {1} where ACQUISITION_DATETIME between '{2}' and '{3}' and DATEPART(dw,[ACQUISITION_DATETIME]) in ({4}) and DATEPART(hh,[ACQUISITION_DATETIME])>= {5} and DATEPART(hh,[ACQUISITION_DATETIME]) < {6} and SENSOR_ID = {7} {8}", colums, tableName, beginTime, endTime, GetDayOfWeekString(timeRange.DateBegin, timeRange.DateEnd), timeRange.DataBeginHour, timeRange.DataEndHour, sensorId, this.GetDataNotNullCondition(colName) ); RawData rawData = new RawData(); rawData.SensorId = sensorId; try { DataSet ds = this.helper.Query(sql); Log.DebugFormat("{0}传感器获取周聚集数据Sql:{1}", sensorId, sql); rawData.Values = ConvertDataSetToValue(ds, colName); } catch (Exception e) { Log.ErrorFormat("{0}传感器周聚集数据获取失败,sql:{1}, error:{2},trace{3}", sensorId, sql, e.Message, e.StackTrace); } return(rawData); }
public static DateTime GetAggDate(AggType type, AggTimeRange timeRange, DateTime nowTime) { DateTime date; int day; switch (type) { case AggType.Day: date = timeRange.DataEndHour < nowTime.Hour ? nowTime : nowTime.AddDays(-1); break; case AggType.Week: day = DateTimeHelper.GetDayOfWeekByWeekFirstDayMon(nowTime); if ((day > timeRange.DateEnd) || (day == timeRange.DateEnd && timeRange.DataEndHour < nowTime.Hour)) { date = nowTime; } else { date = nowTime.AddDays(-7); } break; case AggType.Month: day = nowTime.Day; if ((day > timeRange.DateEnd) || (day == timeRange.DateEnd && timeRange.DataEndHour < nowTime.Hour)) { date = nowTime; } else { date = nowTime.AddMonths(-1); } break; default: date = nowTime; break; } return(date); }