コード例 #1
0
        public void Handle(IEnumerator <IProgrammeCreated> eventEnumerator)
        {
            InitializeCaches();

            var prgDictToProcess        = new HashSet <ProgrammeDictionary>();
            var prgCatToProcess         = new List <ProgrammeCategoryEntity>();
            var prgToProcess            = new List <ProgrammeEntity>();
            var prgCatLinkToProcess     = new List <ProgrammeCategoryLink>();
            var prgDictEpisodeToProcess = new HashSet <ProgrammeEpisode>();
            var scheduleToProcess       = new List <ScheduleEntity>();
            var schPrgToProcess         = new List <ScheduleProgrammeEntity>();

            while (eventEnumerator.MoveNext())
            {
                var eventModel = eventEnumerator.Current;

                if (eventModel is null)
                {
                    continue;
                }

                var salesArea = _salesAreaCache.Get(eventModel.SalesArea);
                if (salesArea is null)
                {
                    throw new DataSyncException(DataSyncErrorCode.SalesAreaNotFound,
                                                $"Invalid Sales Area: {eventModel.SalesArea}");
                }

                if (!(eventModel.ProgrammeCategories is null) && eventModel.ProgrammeCategories.Any())
                {
                    var invalidCategories = eventModel.ProgrammeCategories
                                            .Except(_categoryHierarchyNames, StringComparer.InvariantCultureIgnoreCase).ToArray();
                    if (invalidCategories.Length != 0)
                    {
                        throw new DataSyncException(DataSyncErrorCode.ProgrammeCategoryNotFound,
                                                    "Invalid programme categories: " + string.Join(",", invalidCategories));
                    }
                }

                var programmeDictionary = _programmeDictionaryCache.Get(eventModel.ExternalReference);

                if (programmeDictionary is null)
                {
                    programmeDictionary = _mapper.Map <ProgrammeDictionary>(eventModel);
                    _programmeDictionaryCache.Add(programmeDictionary);
                }
                else
                {
                    programmeDictionary.Name           = eventModel.ProgrammeName;
                    programmeDictionary.Description    = eventModel.Description;
                    programmeDictionary.Classification = eventModel.Classification;
                }

                _ = prgDictToProcess.Add(programmeDictionary);

                var programme = _mapper.Map <ProgrammeEntity>(eventModel);
                programme.Id = Guid.NewGuid();
                programme.ProgrammeDictionary = programmeDictionary;
                programme.SalesAreaId         = salesArea.Id;
                _prgtNoCounter.Process(programme);
                prgToProcess.Add(programme);

                foreach (var catName in eventModel.ProgrammeCategories ?? Enumerable.Empty <string>())
                {
                    var category = _programmeCategoryCache.GetOrAdd(catName, key =>
                    {
                        var newCat = new ProgrammeCategoryEntity {
                            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 (!(eventModel.Episode is null))
                {
                    var episode = programmeDictionary.ProgrammeEpisodes.FirstOrDefault(x => x.Number == eventModel.Episode.Number);

                    if (episode is null)
                    {
                        episode = _mapper.Map <ProgrammeEpisodeEntity>(eventModel.Episode);
                        episode.ProgrammeDictionary = programmeDictionary;

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

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

                var schedule = _scheduleCache.GetOrAdd(programme.ScheduleUniqueKey,
                                                       key =>
                {
                    var sch = new ScheduleEntity
                    {
                        SalesAreaId = programme.SalesAreaId,
                        Date        = programme.StartDateTime.Date
                    };
                    scheduleToProcess.Add(sch);

                    return(sch);
                });

                schPrgToProcess.Add(new ScheduleProgrammeEntity {
                    Programme = programme, Schedule = schedule
                });
            }

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

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

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

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

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

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

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

                    dbContext.BulkInsertEngine.BulkInsert(prgCatLinkToProcess);

                    dbContext.BulkInsertEngine.BulkInsert(scheduleToProcess,
                                                          new BulkInsertOptions {
                        PreserveInsertOrder = true, SetOutputIdentity = true
                    });

                    foreach (var scheduleProgramme in schPrgToProcess)
                    {
                        scheduleProgramme.ProgrammeId = scheduleProgramme.Programme.Id;
                        scheduleProgramme.ScheduleId  = scheduleProgramme.Schedule.Id;
                    }

                    dbContext.BulkInsertEngine.BulkInsert(schPrgToProcess);

                    transaction.Commit();
                }
            }
        }