Exemplo n.º 1
0
        /// <summary>
        /// Загрузка справочника банков с Центрального банка
        /// </summary>
        private bool UpdateBanksFromCBR(IUnitOfWork uow)
        {
            if (!DownloadRegions() || !DownloadBIC())
            {
                return(false);
            }
            var UpdatedObject = new List <object>();

            UpdateRegions(uow);

            OutputMessage("Обновление банков");
            var bicList = bicDocument.BICDirectoryEntry.Where(x => x.Accounts != null);
            Dictionary <string, Dictionary <string, AccountsType> > loadedAccounts = bicList
                                                                                     .ToDictionary(x => x.BIC, x => x.Accounts.ToDictionary(y => y.Account));
            var storedBanksDict = allBanksList.ToDictionary(x => x.Bik);
            var index           = 0;

            foreach (var item in bicList)
            {
                Progress(index, bicList.Count());
                index++;
                Bank bank = null;
                if (storedBanksDict.ContainsKey(item.BIC))
                {
                    bank = storedBanksDict[item.BIC];
                }
                if (bank == null)
                {
                    bank = new Bank();
                    banksAdded++;
                }
                else
                {
                    if (!CompareBank(bank, item))
                    {
                        UpdatedObject.Add(bank);
                        banksFixed++;
                    }
                    else
                    {
                        continue;
                    }
                }
                bank.Bik    = item.BIC;
                bank.City   = item.ParticipantInfo.Nnp ?? "";
                bank.Name   = item.ParticipantInfo.NameP;
                bank.Region = regionsList.FirstOrDefault(x => x.RegionNum == int.Parse(item.ParticipantInfo.Rgn));
                var currentBankLoadedAccounts = loadedAccounts[item.BIC];
                foreach (var account in bank.CorAccounts.ToList())
                {
                    if (currentBankLoadedAccounts.ContainsKey(account.CorAccountNumber))
                    {
                        currentBankLoadedAccounts.Remove(account.CorAccountNumber);
                    }
                    else
                    {
                        bank.CorAccounts.Remove(account);
                    }
                }
                foreach (var acc in currentBankLoadedAccounts)
                {
                    bank.CorAccounts.Add(new CorAccount {
                        CorAccountNumber = acc.Key, InBank = bank
                    });
                }
                uow.Save(bank);
                if (bank.DefaultCorAccount == null)
                {
                    bank.DefaultCorAccount = bank.CorAccounts.First();
                    uow.Save(bank);
                }
            }

            OutputMessage("Обновление счетов, удаление не актуальных банков");
            var bl = activeBanksList.Where(bank => !bicList.Any(y => y.BIC == bank.Bik));

            index = 0;
            foreach (var item in bl)
            {
                Progress(index, bl.Count());
                index++;
                var account = accountsList.Where(x => !x.Inactive).FirstOrDefault(a => a.InBank == item);
                if (account != null)
                {
                    if (item.Deleted)
                    {
                        continue;
                    }
                    account.Inactive = true;
                    UpdatedObject.Add(account);
                    accountsDeactivated++;

                    item.Deleted = true;
                    UpdatedObject.Add(item);
                    banksDeactivated++;
                }
                else
                {
                    uow.Delete(item);
                    banksRemoved++;
                }
            }
            uow.Commit();
            OrmMain.NotifyObjectUpdated(UpdatedObject.ToArray());
            return(true);
        }