protected override void Sync(SyncDataQueue queue) { DateTime startTime = new DateTime(queue.Time.Year, queue.Time.Month, 1); DateTime endTime = startTime.AddMonths(1).AddDays(-1); List <CityDailyAirQuality> source = Model.CityDailyAirQuality.Where(o => o.Time >= startTime && o.Time <= endTime).ToList(); if (source.Any()) { List <AirQualityShortTerm> airQualityShortTermList = source.Select(o => o.ToAirQualityShortTerm()).ToList(); List <CityMonthlyAirQuality> list = new List <CityMonthlyAirQuality>(); AirQualityLongTermCalculator calculator = new AirQualityLongTermCalculator(); foreach (var cityGroup in airQualityShortTermList.GroupBy(o => o.Code)) { AirQualityShortTerm first = cityGroup.First(); AirQualityLongTerm item = new AirQualityLongTerm() { Code = first.Code, Time = startTime, Name = first.Name }; calculator.Calculate(cityGroup, item); list.Add(item.ToCityMonthlyAirQuality()); } IQueryable <CityMonthlyAirQuality> oldList = Model.CityMonthlyAirQuality.Where(o => o.Time == startTime); Model.CityMonthlyAirQuality.RemoveRange(oldList); Model.CityMonthlyAirQuality.AddRange(list); queue.Status = true; } queue.LastTime = DateTime.Now; }
public static AirQualityLongTermYearOnYear ToAirQualityLongTermYearOnYear(this AirQualityLongTerm source) { return(new AirQualityLongTermYearOnYear() { Code = source.Code, Time = source.Time, Name = source.Name, SO2 = source.SO2, NO2 = source.NO2, PM10 = source.PM10, CO = source.CO, O3 = source.O3, PM25 = source.PM25, AQCI = source.AQCI, StandardDays = source.StandardDays, PrimaryPollutant = source.PrimaryPollutant, SO2R = source.SO2R, NO2R = source.NO2R, PM10R = source.PM10R, COR = source.COR, O3R = source.O3R, PM25R = source.PM25R, AQCIR = source.AQCIR, StandardDaysR = source.StandardDaysR }); }
public static CityYearlyAirQuality ToCityYearlyAirQuality(this AirQualityLongTerm source) { return(new CityYearlyAirQuality() { Code = source.Code, Time = source.Time, Name = source.Name, SO2 = source.SO2, NO2 = source.NO2, PM10 = source.PM10, CO = source.CO, O3 = source.O3, PM25 = source.PM25, AQCI = source.AQCI, StandardDays = source.StandardDays, PrimaryPollutant = source.PrimaryPollutant }); }
public List <AirQualityLongTerm> GetCityAnyTimeRangeAirQuality(string token, DateTime startTime, DateTime endTime, string areaCode) { if (validator.Validate(token, "CityAirQuality", "GetCityAnyTimeRangeAirQuality")) { IQueryable <CityDailyAirQuality> query = model.CityDailyAirQuality.Where(o => o.Time >= startTime && o.Time <= endTime); if (!string.IsNullOrEmpty(areaCode)) { Area area = model.Area.FirstOrDefault(o => o.AreaCode == areaCode); if (area == null) { query = query.Where(o => false); } else { string[] cityCodes = area.City.Select(o => o.CityCode).ToArray(); query = query.Where(o => cityCodes.Contains(o.Code)); } } List <AirQualityShortTerm> source = query.ToList().Select(o => o.ToAirQualityShortTerm()).ToList(); AirQualityLongTermCalculator calculator = new AirQualityLongTermCalculator(); List <AirQualityLongTerm> list = new List <AirQualityLongTerm>(); foreach (var cityGroup in source.GroupBy(o => o.Code)) { AirQualityShortTerm first = cityGroup.First(); AirQualityLongTerm item = new AirQualityLongTerm() { Code = first.Code, Time = startTime, Name = first.Name }; calculator.Calculate(cityGroup, item); list.Add(item); } calculator.CalculateRank(list); return(list); } else { return(null); } }
public List <AirQualityLongTermYearOnYear> Calculate(List <AirQualityLongTerm> list, List <AirQualityLongTerm> baseList) { List <AirQualityLongTermYearOnYear> newList = new List <AirQualityLongTermYearOnYear>(); foreach (AirQualityLongTerm item in list) { AirQualityLongTermYearOnYear newItem = item.ToAirQualityLongTermYearOnYear(); AirQualityLongTerm baseItem = baseList.FirstOrDefault(o => o.Code == item.Code); if (baseItem != null) { if (item.AQCI.HasValue && baseItem.AQCI.HasValue) { newItem.AQCIYOY = Math.Round((item.AQCI.Value - baseItem.AQCI.Value) / baseItem.AQCI.Value * 100, 2); } } newList.Add(newItem); } RankCalculator rankCalculator = new RankCalculator(); rankCalculator.Calculate(newList, o => o.AQCIYOY, (o, rank) => { o.AQCIYOYR = rank; }); return(newList); }