| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 | using iBemsDataService.Model;using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Web;namespace iBemsDataService.Controllers.Bems.Common{    // TIME_INTERVAL    public enum TimeInterval    {        QuarterMin = 1,        Hour,        Day,        Month,        Year    };    public class PointHistoryValueManager    {        iBemsEntities db = null;        public PointHistoryValueManager(iBemsEntities db)        {            this.db = db;        }        public CalculationValue[] GetPointValues(int siteId,            int facilityCode, int propertyId, TimeInterval timeIntervalType,            DateTime startDate, DateTime endDate)        {            switch (timeIntervalType)            {                case TimeInterval.QuarterMin:                    return GetMonitoringPointHistory(                        db.BemsMonitoringPointHistory15min,                        siteId,                        facilityCode,                        propertyId,                        startDate,                        endDate);                case TimeInterval.Hour:                    return GetMonitoringPointHistory(                        db.BemsMonitoringPointHistoryHourly,                        siteId,                        facilityCode,                        propertyId,                        startDate,                        endDate);                case TimeInterval.Day:                    return GetMonitoringPointHistory(                        db.BemsMonitoringPointHistoryDaily,                        siteId,                        facilityCode,                        propertyId,                        startDate,                        endDate);                case TimeInterval.Month:                    return GetMonitoringPointHistoryMonthly(                        db.BemsMonitoringPointHistoryDaily,                        siteId,                        facilityCode,                        propertyId,                        startDate,                        endDate);                case TimeInterval.Year:                    return GetMonitoringPointHistoryYearly(                        db.BemsMonitoringPointHistoryDaily,                        siteId,                        facilityCode,                        propertyId,                        startDate,                        endDate);                default:                    throw new Exception("IntervalType Value Error!");            }        }        public CalculationValue[] GetMonitoringPointHistory(            DbSet<BemsMonitoringPointHistory15min> dbSet,            int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)        {            var query = dbSet.Where(x =>                    x.SiteId == siteId &&                    x.FacilityCode == facilityCode &&                    x.PropertyId == propertyId &&                    startDate <= x.CreatedDateTime && x.CreatedDateTime <= endDate);            if (query.Any() == false)            {                return null;            }            return query.Select(x => new CalculationValue            {                DateTime = x.CreatedDateTime,                Value = x.CurrentValue            }).ToArray();        }        public CalculationValue[] GetMonitoringPointHistory(            DbSet<BemsMonitoringPointHistoryHourly> dbSet,            int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)        {            var query = dbSet.Where(x =>                    x.SiteId == siteId &&                    x.FacilityCode == facilityCode &&                    x.PropertyId == propertyId &&                    startDate <= x.CreatedDateTime && x.CreatedDateTime <= endDate);            if (query.Any() == false)            {                return null;            }            return query.Select(x => new CalculationValue            {                DateTime = x.CreatedDateTime,                Value = x.CurrentValue            }).ToArray();        }        public CalculationValue[] GetMonitoringPointHistory(            DbSet<BemsMonitoringPointHistoryDaily> dbSet,            int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)        {            var query = dbSet.Where(x =>                    x.SiteId == siteId &&                    x.FacilityCode == facilityCode &&                    x.PropertyId == propertyId &&                    startDate <= x.CreatedDateTime && x.CreatedDateTime <= endDate);            if (query.Any() == false)            {                return null;            }            return query.Select(x => new CalculationValue            {                DateTime = x.CreatedDateTime,                //Value = x.CurrentValue hcLee 20150428                Value = x.DailyValue            }).ToArray();        }        public CalculationValue[] GetMonitoringPointHistoryMonthly(            DbSet<BemsMonitoringPointHistoryDaily> dbSet,            int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)        {            var groupQuery = from data in dbSet                             where data.SiteId == siteId &&                                     data.FacilityCode == facilityCode &&                                     data.PropertyId == propertyId &&                                     startDate <= data.CreatedDateTime && data.CreatedDateTime <= endDate                             group data by new { Year = data.CreatedDateTime.Year, Month = data.CreatedDateTime.Month } into g                             select g;            //select new CalculationValue            //{             //   DateTime = new DateTime( g.Key.Year , g.Key.Month , 1 ) ,            //   Value = g.Sum( x => x.CurrentValue )            //};            if (groupQuery == null || groupQuery.Any() == false)            {                return null;            }            CalculationValue[] values = new CalculationValue[groupQuery.Count()];            var i = 0;            foreach (var g in groupQuery)            {                double dValue = 0;                if (CheckPointValueType(siteId, facilityCode, propertyId) == 0) dValue = g.Sum(x => x.DailyValue);                else if (CheckPointValueType(siteId, facilityCode, propertyId) == 1) dValue = g.Average(x => x.DailyValue);                else if (CheckPointValueType(siteId, facilityCode, propertyId) == 2)                {                    dValue = g.Sum(x => x.DailyValue);                    dValue = (dValue > 0) ? 1 : 0;                }                values[i] = new CalculationValue                {                    DateTime = new DateTime(g.Key.Year, g.Key.Month, 1),                    //Value = g.Sum(x => x.CurrentValue)                    Value = dValue                };                i++;            }            return values;        }        public int CheckPointValueType(int siteId, int facilityCode, int propertyId)        {            // return 0 -> 누적, 1 -> 평균,  2 -> On,Off            var Query = from data in db.BemsMonitoringPoint                             where data.SiteId == siteId &&                                     data.FacilityCode == facilityCode &&                                     data.PropertyId == propertyId select data;            if (Query.FirstOrDefault().ValueType == 2) return 2;            if (Query.FirstOrDefault().ValueType == 0 && Query.FirstOrDefault().FuelTypeId > 0) return 0;            if (Query.FirstOrDefault().ValueType == 0 && Query.FirstOrDefault().FuelTypeId == 0 ) return 1;            return 0;        }        public CalculationValue[] GetMonitoringPointHistoryYearly(            DbSet<BemsMonitoringPointHistoryDaily> dbSet,            int siteId, int facilityCode, int propertyId, DateTime startDate, DateTime endDate)        {            var groupQuery = from data in dbSet                             where data.SiteId == siteId &&                                     data.FacilityCode == facilityCode &&                                     data.PropertyId == propertyId &&                                     startDate <= data.CreatedDateTime && data.CreatedDateTime <= endDate                             group data by new { Year = data.CreatedDateTime.Year } into g                             select g;            //select new CalculationValue            //{            //    DateTime = new DateTime( g.Key..Year , 1 , 1 ) ,            //    Value = g.Sum( x => x.CurrentValue )            //};            if (groupQuery.Any() == false)            {                return null;            }            CalculationValue[] values = new CalculationValue[groupQuery.Count()];            var i = 0;            foreach (var g in groupQuery)            {                values[i] = new CalculationValue                {                    DateTime = new DateTime(g.Key.Year, 1, 1),                    //Value = g.Sum(x => x.CurrentValue)                    Value = g.Sum(x => x.DailyValue)                };                i++;            }            return values;        }    }}
 |