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 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);
     }
 }