コード例 #1
0
        public async Task DoWorkAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                //Default countries
                var result = await _dbCountry.GetAllAsync();

                if (!result.Any())
                {
                    var countriesInfo = await _client.GetCountryInfoByRegion(CountryRegions.Europe);

                    var countries = countriesInfo
                                    .Where(x => !string.IsNullOrEmpty(x.Code) && string.IsNullOrEmpty(x.Name))
                                    .Select(x => new Country()
                    {
                        Code = x.Code,
                        Name = x.Name
                    }).ToList();
                    countriesInfo = await _client.GetCountryInfoByRegion(CountryRegions.Americas);

                    countries.AddRange(countriesInfo
                                       .Select(x => new Country()
                    {
                        Code = x.Code,
                        Name = x.Name
                    }));

                    await _dbCountry.AddRangeAsync(countries);
                }

                //Dealers
                var dealers = await _dbDealer.GetAllAsync();

                if (!dealers.Any())
                {
                    var dealersInfo = await _client.GetAllCarDealersAsync();
                    await SetDealerAsync(dealersInfo.ToList());

                    int rowCount      = dealers.Count;
                    var dealersAmount = Convert.ToInt32(_keeper.GetVariableByKey("DealerContainerCounter"));

                    var circlesValues = Convert.ToInt32(Math.Round(dealersAmount / (decimal)rowCount, MidpointRounding.AwayFromZero));
                    //for (int i = 1; i < circlesValues; i++)
                    for (int i = 1; i < 2; i++)
                    {
                        dealersInfo = await _client.GetAllCarDealersAsync(rowCount *i + 1);

                        await SetDealerAsync(dealersInfo.ToList());
                    }
                }

                //Cars
                var cars = await _dbCar.GetAllAsync();

                if (!cars.Any())
                {
                    dealers = await _dbDealer.GetAllAsync();

                    foreach (var dealer in dealers)
                    {
                        var carsInfoList = await _client.GetActiveCarAsync(dealer.NativeId);
                        await SetCarAsync(dealer, carsInfoList.ToList());
                    }
                }
                //VinHistory
                var histories = await _dbCarHistory.GetAllAsync();

                if (!histories.Any())
                {
                    cars = await _dbCar.GetAllAsync();

                    dealers = await _dbDealer.GetAllAsync();

                    foreach (var car in cars)
                    {
                        var carsInfoList = await _client.GetCarHistoryByVinCodeAsync(car.VinCode);
                        await SetCarHistoryAsync(car, dealers, carsInfoList.ToList());
                    }
                }

                await Task.Delay(TimeSpan.FromDays(1));
            }
        }