示例#1
0
        public void Add(IEnumerable <Scenario> scenarios)
        {
            if (scenarios == null || !scenarios.Any())
            {
                return;
            }

            var entities = _mapper.Map <ScenarioEntity[]>(scenarios);

            foreach (var entity in entities)
            {
                ApplyPassesOrder(entity.PassReferences);
            }

            using (var transaction = _dbContext.Specific.Database.BeginTransaction())
            {
                _dbContext.BulkInsertEngine.BulkInsert(entities, new BulkInsertOptions {
                    PreserveInsertOrder = true
                });

                BulkInsertNestedEntities(entities);

                transaction.Commit();

                var actionBuilder = new BulkInsertActionBuilder <ScenarioEntity>(entities, _mapper);
                actionBuilder.TryToUpdate(scenarios);
                actionBuilder.Build()?.Execute();
            }
        }
        public void Insert(List <RatingsPredictionSchedule> items, bool setIdentity = true)
        {
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            if (!items.Any())
            {
                return;
            }

            var entities = _mapper.Map <List <PredictionSchedule> >(items, opts => opts.UseEntityCache(_salesAreaByNameCache));

            using var transaction = _dbContext.Specific.Database.BeginTransaction();

            _dbContext.BulkInsertEngine.BulkInsert(entities,
                                                   new BulkInsertOptions {
                PreserveInsertOrder = true, SetOutputIdentity = true
            });

            var ratingEntities = entities.SelectMany(x => x.Ratings.Select(
                                                         r =>
            {
                r.PredictionScheduleId = x.Id;
                return(r);
            })).ToList();

            var ratingBulkInsertOptions = setIdentity
                ? new BulkInsertOptions {
                PreserveInsertOrder = true, SetOutputIdentity = true
            }
                : new BulkInsertOptions {
                BatchSize = 200000
            };

            _dbContext.BulkInsertEngine.BulkInsert(ratingEntities, ratingBulkInsertOptions);
            transaction.Commit();

            if (setIdentity)
            {
                var actionBuilder = new BulkInsertActionBuilder <PredictionSchedule>(entities, _mapper);
                actionBuilder.TryToUpdate(items, mappingOptions => mappingOptions.UseEntityCache(_salesAreaByIdCache));
                actionBuilder.Build()?.Execute();
            }
        }
示例#3
0
        public void AddRange(IEnumerable <SmoothFailure> items)
        {
            using (var transaction = _dbContext.Specific.Database.BeginTransaction())
            {
                var smoothFailureEntities = _mapper.Map <List <SmoothFailureEntity> >(items);
                var ids = smoothFailureEntities.Where(x => x.Id > 0).Select(x => x.Id).Distinct().ToList();

                if (ids.Any())
                {
                    var relatedSmoothFailureSmoothFailureMessagesIds = _dbContext
                                                                       .Query <SmoothFailureSmoothFailureMessage>()
                                                                       .Where(x => ids.Contains(x.SmoothFailureId))
                                                                       .Select(x => x.Id)
                                                                       .ToArray();

                    if (relatedSmoothFailureSmoothFailureMessagesIds.Any())
                    {
                        _dbContext.Specific.RemoveByIdentityIds <SmoothFailureSmoothFailureMessage>(
                            relatedSmoothFailureSmoothFailureMessagesIds);
                    }
                }

                //Here InsertOrUpdate is used depending on Raven bulk insert option OverwriteExisting = true
                _dbContext.BulkInsertEngine.BulkInsertOrUpdate(
                    smoothFailureEntities, new BulkInsertOptions {
                    PreserveInsertOrder = true, SetOutputIdentity = true
                });

                var newFailureMessagesList = smoothFailureEntities.SelectMany(x => x.FailureMessagesMap.Select(m =>
                {
                    m.SmoothFailureId = x.Id;
                    return(m);
                })).ToList();

                _dbContext.BulkInsertEngine.BulkInsert(newFailureMessagesList,
                                                       new BulkInsertOptions {
                    PreserveInsertOrder = true, SetOutputIdentity = true
                });

                transaction.Commit();

                var actionBuilder = new BulkInsertActionBuilder <SmoothFailureEntity>(smoothFailureEntities, _mapper);
                actionBuilder.TryToUpdate(items);
                actionBuilder.Build()?.Execute();
            }
        }
示例#4
0
        public void Add(IEnumerable <Programme> item)
        {
            using (var operationDbContext = _dbContextFactory.Create())
            {
                var prgDictCache = new ProgrammeDictionaryCache(operationDbContext, trackingChanges: false);
                var prgCatCache  = new ProgrammeCategoryCache(operationDbContext, trackingChanges: false);

                var prgDictToProcess        = new HashSet <Entities.Tenant.ProgrammeDictionary>();
                var prgCatToProcess         = new List <ProgrammeCategory>();
                var prgToProcess            = new List <Entities.Tenant.Programmes.Programme>();
                var prgCatLinkToProcess     = new List <ProgrammeCategoryLink>();
                var prgDictEpisodeToProcess = new HashSet <ProgrammeEpisode>();

                foreach (var model in item)
                {
                    var programmeDictionary = prgDictCache.Get(model.ExternalReference);

                    if (programmeDictionary is null)
                    {
                        programmeDictionary = _mapper.Map <Entities.Tenant.ProgrammeDictionary>(model);
                        prgDictCache.Add(programmeDictionary);
                    }
                    else
                    {
                        programmeDictionary.Name           = model.ProgrammeName;
                        programmeDictionary.Description    = model.Description;
                        programmeDictionary.Classification = model.Classification;
                    }

                    _ = prgDictToProcess.Add(programmeDictionary);

                    var programme = _mapper.Map <Entities.Tenant.Programmes.Programme>(model,
                                                                                       opts => opts.UseEntityCache(_salesAreaByNameCache));

                    if (programme.Id == Guid.Empty)
                    {
                        programme.Id = Guid.NewGuid();
                    }

                    programme.ProgrammeDictionary = programmeDictionary;
                    prgToProcess.Add(programme);

                    foreach (var catName in model.ProgrammeCategories ?? Enumerable.Empty <string>())
                    {
                        var category = prgCatCache.GetOrAdd(catName, key =>
                        {
                            var newCat = new ProgrammeCategory {
                                Name = key
                            };
                            prgCatToProcess.Add(newCat);

                            return(newCat);
                        });

                        if (programme.ProgrammeCategoryLinks.All(x => !string.Equals(x.ProgrammeCategory.Name, category.Name)))
                        {
                            var link = new ProgrammeCategoryLink
                            {
                                ProgrammeCategory = category,
                                Programme         = programme,
                                ProgrammeId       = programme.Id
                            };

                            programme.ProgrammeCategoryLinks.Add(link);
                            prgCatLinkToProcess.Add(link);
                        }
                    }

                    if (!(model.Episode is null))
                    {
                        var episode = programmeDictionary.ProgrammeEpisodes.FirstOrDefault(x => x.Number == model.Episode.Number);

                        if (episode is null)
                        {
                            episode = new ProgrammeEpisode
                            {
                                Name   = model.Episode.Name,
                                Number = model.Episode.Number,
                                ProgrammeDictionary = programmeDictionary
                            };

                            programmeDictionary.ProgrammeEpisodes.Add(episode);
                        }
                        else
                        {
                            episode.Name = model.Episode.Name;
                        }

                        programme.Episode = episode;
                        _ = prgDictEpisodeToProcess.Add(episode);
                    }
                }

                using (var transaction = operationDbContext.Specific.Database.BeginTransaction())
                {
                    operationDbContext.BulkInsertEngine.BulkInsertOrUpdate(prgDictToProcess.ToList(),
                                                                           new BulkInsertOptions {
                        PreserveInsertOrder = true, SetOutputIdentity = true
                    });

                    foreach (var episode in prgDictEpisodeToProcess)
                    {
                        episode.ProgrammeDictionaryId = episode.ProgrammeDictionary.Id;
                    }

                    operationDbContext.BulkInsertEngine.BulkInsertOrUpdate(prgDictEpisodeToProcess.ToList(),
                                                                           new BulkInsertOptions {
                        PreserveInsertOrder = true, SetOutputIdentity = true
                    });

                    operationDbContext.BulkInsertEngine.BulkInsert(prgCatToProcess,
                                                                   new BulkInsertOptions {
                        PreserveInsertOrder = true, SetOutputIdentity = true
                    });

                    foreach (var prg in prgToProcess)
                    {
                        prg.ProgrammeDictionaryId = prg.ProgrammeDictionary.Id;
                        prg.EpisodeId             = prg.Episode?.Id;
                    }

                    operationDbContext.BulkInsertEngine.BulkInsert(prgToProcess,
                                                                   new BulkInsertOptions {
                        PreserveInsertOrder = true
                    });

                    foreach (var catLink in prgCatLinkToProcess)
                    {
                        catLink.ProgrammeCategoryId = catLink.ProgrammeCategory.Id;
                    }

                    operationDbContext.BulkInsertEngine.BulkInsert(prgCatLinkToProcess);

                    transaction.Commit();
                }

                var actionBuilder = new BulkInsertActionBuilder <Entities.Tenant.Programmes.Programme>(prgToProcess, _mapper);
                actionBuilder.TryToUpdate(item, opts => opts.UseEntityCache(_salesAreaByIdCache));
                actionBuilder.Build()?.Execute();
            }
        }
示例#5
0
        public void Update(IEnumerable <Scenario> scenarios)
        {
            if (scenarios == null || !scenarios.Any())
            {
                return;
            }

            var entities = _mapper.Map <ScenarioEntity[]>(scenarios);

            foreach (var entity in entities)
            {
                ApplyPassesOrder(entity.PassReferences);
            }

            using (var transaction = _dbContext.Specific.Database.BeginTransaction())
            {
                _dbContext.BulkInsertEngine.BulkUpdate(entities, new BulkInsertOptions {
                    PreserveInsertOrder = true
                });

                // remove existing entities
                var scenarioIds    = entities.Select(x => x.Id).ToList();
                var roundsToDelete = _dbContext.Query <ScenarioCampaignPriorityRoundCollection>()
                                     .Where(x => scenarioIds.Contains(x.ScenarioId)).Select(x => x.Id).ToArray();
                var passReferencesToDelete = _dbContext.Query <ScenarioPassReference>()
                                             .Where(x => scenarioIds.Contains(x.ScenarioId)).Select(x => x.Id).ToArray();
                var passPrioritiesToDelete = _dbContext.Query <ScenarioCampaignPassPriority>()
                                             .Where(x => scenarioIds.Contains(x.ScenarioId)).Select(x => x.Id).ToArray();

                if (roundsToDelete.Any())
                {
                    for (int i = 0; i <= roundsToDelete.Length / DeleteBatchSize; i++)
                    {
                        var roundIds = roundsToDelete.Skip(i * DeleteBatchSize).Take(DeleteBatchSize).ToArray();
                        _dbContext.Specific.RemoveByIdentityIds <ScenarioCampaignPriorityRoundCollection>(roundIds);
                    }
                }

                if (passReferencesToDelete.Any())
                {
                    for (int i = 0; i <= passReferencesToDelete.Length / DeleteBatchSize; i++)
                    {
                        var passIds = passReferencesToDelete.Skip(i * DeleteBatchSize).Take(DeleteBatchSize).ToArray();
                        _dbContext.Specific.RemoveByIdentityIds <ScenarioPassReference>(passIds);
                    }
                }

                if (passPrioritiesToDelete.Any())
                {
                    for (int i = 0; i <= passPrioritiesToDelete.Length / DeleteBatchSize; i++)
                    {
                        var priorityIds = passPrioritiesToDelete.Skip(i * DeleteBatchSize).Take(DeleteBatchSize).ToArray();
                        _dbContext.Specific.RemoveByIdentityIds <ScenarioCampaignPassPriority>(priorityIds);
                    }
                }

                BulkInsertNestedEntities(entities);

                transaction.Commit();

                var actionBuilder = new BulkInsertActionBuilder <ScenarioEntity>(entities, _mapper);
                actionBuilder.TryToUpdate(scenarios);
                actionBuilder.Build()?.Execute();
            }
        }
示例#6
0
        /// <summary>
        /// BulkInsert method, only for insert. It doesn't need SaveChanges for
        /// applying changes.
        /// </summary>
        /// <param name="items"></param>
        public void Add(IEnumerable <Product> items)
        {
            if (!(items?.Any() ?? false))
            {
                return;
            }

            PrepareDictionaryCache();
            var products           = new List <Entities.Tenant.Products.Product>();
            var advertisers        = new List <Advertiser>();
            var agencies           = new List <Agency>();
            var agencyGroups       = new List <Entities.Tenant.AgencyGroup>();
            var persons            = new List <Person>();
            var productAdvertisers = new List <ProductAdvertiser>();
            var productAgencies    = new List <ProductAgency>();
            var productPersons     = new List <ProductPerson>();

            foreach (var item in items)
            {
                var product = _mapper.Map <Entities.Tenant.Products.Product>(item);
                products.Add(product);
                if (!string.IsNullOrWhiteSpace(item.AdvertiserIdentifier))
                {
                    if (_dbAdvertisers.TryGetValue(item.AdvertiserIdentifier, out var dbAdv))
                    {
                        if (!string.Equals(dbAdv.Name, item.AdvertiserName))
                        {
                            dbAdv.Name = item.AdvertiserName;
                            advertisers.Add(dbAdv);
                        }
                    }
                    else
                    {
                        dbAdv = new Advertiser
                        {
                            ExternalIdentifier = item.AdvertiserIdentifier,
                            Name      = item.AdvertiserName,
                            ShortName = item.AdvertiserName
                        };
                        advertisers.Add(dbAdv);
                        _dbAdvertisers.Add(dbAdv.ExternalIdentifier, dbAdv);
                    }

                    var productAdvertiser = new ProductAdvertiser
                    {
                        Product    = product,
                        Advertiser = dbAdv,
                        StartDate  = item.AdvertiserLinkStartDate,
                        EndDate    = item.AdvertiserLinkEndDate
                    };
                    productAdvertisers.Add(productAdvertiser);
                    product.ProductAdvertisers.Add(productAdvertiser);
                }

                if (!string.IsNullOrWhiteSpace(item.AgencyIdentifier))
                {
                    Entities.Tenant.AgencyGroup dbAgencyGroup = null;
                    if (item.AgencyGroup != null)
                    {
                        var key = HashCode.Combine(item.AgencyGroup.ShortName, item.AgencyGroup.Code);
                        if (!_dbAgencyGroups.TryGetValue(key, out dbAgencyGroup))
                        {
                            dbAgencyGroup = _mapper.Map <Entities.Tenant.AgencyGroup>(item.AgencyGroup);
                            agencyGroups.Add(dbAgencyGroup);
                            _dbAgencyGroups.Add(key, dbAgencyGroup);
                        }
                    }

                    if (_dbAgencies.TryGetValue(item.AgencyIdentifier, out var dbAgency))
                    {
                        if (!string.Equals(dbAgency.Name, item.AgencyName))
                        {
                            dbAgency.Name = item.AgencyName;
                            agencies.Add(dbAgency);
                        }
                    }
                    else
                    {
                        dbAgency = new Agency
                        {
                            ExternalIdentifier = item.AgencyIdentifier,
                            Name      = item.AgencyName,
                            ShortName = item.AgencyName
                        };
                        agencies.Add(dbAgency);
                        _dbAgencies.Add(dbAgency.ExternalIdentifier, dbAgency);
                    }

                    var productAgency = new ProductAgency
                    {
                        Product     = product,
                        Agency      = dbAgency,
                        AgencyGroup = dbAgencyGroup,
                        StartDate   = item.AgencyStartDate,
                        EndDate     = item.AgencyLinkEndDate
                    };
                    productAgencies.Add(productAgency);
                    product.ProductAgencies.Add(productAgency);
                }

                if (!(item.SalesExecutive is null))
                {
                    if (_dbPersons.TryGetValue(item.SalesExecutive.Identifier, out var dbPerson))
                    {
                        if (!string.Equals(dbPerson.Name, item.SalesExecutive.Name))
                        {
                            dbPerson.Name = item.SalesExecutive.Name;
                            persons.Add(dbPerson);
                        }
                    }
                    else
                    {
                        dbPerson = _mapper.Map <Person>(item.SalesExecutive);
                        persons.Add(dbPerson);
                        _dbPersons.Add(dbPerson.ExternalIdentifier, dbPerson);
                    }

                    var productPerson = new ProductPerson
                    {
                        Product   = product,
                        Person    = dbPerson,
                        StartDate = item.SalesExecutive.StartDate,
                        EndDate   = item.SalesExecutive.EndDate
                    };
                    productPersons.Add(productPerson);
                    product.ProductPersons.Add(productPerson);
                }
            }

            using var transaction = _dbContext.Specific.Database.BeginTransaction();

            if (advertisers.Count > 0)
            {
                _dbContext.BulkInsertEngine.BulkInsertOrUpdate(advertisers,
                                                               new BulkInsertOptions {
                    SetOutputIdentity = true, PreserveInsertOrder = true
                });
            }

            if (agencies.Count > 0)
            {
                _dbContext.BulkInsertEngine.BulkInsertOrUpdate(agencies,
                                                               new BulkInsertOptions {
                    SetOutputIdentity = true, PreserveInsertOrder = true
                });
            }

            if (agencyGroups.Count > 0)
            {
                _dbContext.BulkInsertEngine.BulkInsertOrUpdate(agencyGroups,
                                                               new BulkInsertOptions {
                    SetOutputIdentity = true, PreserveInsertOrder = true
                });
            }

            if (persons.Count > 0)
            {
                _dbContext.BulkInsertEngine.BulkInsertOrUpdate(persons,
                                                               new BulkInsertOptions {
                    SetOutputIdentity = true, PreserveInsertOrder = true
                });
            }

            if (products.Count > 0)
            {
                _dbContext.BulkInsertEngine.BulkInsert(products);
            }

            if (productAdvertisers.Count > 0)
            {
                foreach (var item in productAdvertisers)
                {
                    item.ProductId    = item.Product.Uid;
                    item.AdvertiserId = item.Advertiser.Id;
                }

                _dbContext.BulkInsertEngine.BulkInsert(productAdvertisers);
            }

            if (productAgencies.Count > 0)
            {
                foreach (var item in productAgencies)
                {
                    item.ProductId     = item.Product.Uid;
                    item.AgencyId      = item.Agency.Id;
                    item.AgencyGroupId = item.AgencyGroup?.Id;
                }

                _dbContext.BulkInsertEngine.BulkInsert(productAgencies);
            }

            if (productPersons.Count > 0)
            {
                foreach (var item in productPersons)
                {
                    item.ProductId = item.Product.Uid;
                    item.PersonId  = item.Person.Id;
                }

                _dbContext.BulkInsertEngine.BulkInsert(productPersons);
            }

            transaction.Commit();

            var actionBuilder = new BulkInsertActionBuilder <Entities.Tenant.Products.Product>(products, _mapper);

            actionBuilder.TryToUpdate(items);
            actionBuilder.Build()?.Execute();
        }