/// <summary>Loads the ag campaign sales areas.</summary> /// <param name="campaign">The campaign.</param> /// <param name="salesAreas">The sales areas.</param> /// <param name="agCampaignSalesArea">The ag campaign sales area.</param> /// <param name="dayPartNo">The day part no.</param> /// <param name="channelGroup">The channel group.</param> /// <returns></returns> protected List <AgCampaignSalesArea> LoadAgCampaignSalesAreas( Campaign campaign, IReadOnlyCollection <SalesArea> salesAreas, AgCampaignSalesArea agCampaignSalesArea, ref int dayPartNo, ref List <Tuple <int, int, SalesAreaGroup> > channelGroup) { if (campaign.SalesAreaCampaignTarget is null || campaign.SalesAreaCampaignTarget.Count == 0) { return(null); } var counter = 1; var salesAreaTargetMap = salesAreas .Where(x => x.TargetAreaName != null) .Select(x => x.TargetAreaName) .Distinct() .ToDictionary(x => x, x => counter++); var agCampaignSalesAreaList = new List <AgCampaignSalesArea>(); foreach (var salesAreaCampaignTarget in campaign.SalesAreaCampaignTarget) { int salesAreaNo; int channelGroupNo; if (_includeChannelGroup) { // In this case we use old channel group calculation logic // (assigning this value to both sales areas and channel group properties). GetCampaignChannelGroup(salesAreaCampaignTarget.SalesAreaGroup, campaign.CustomId, ref channelGroup, out salesAreaNo); channelGroupNo = salesAreaNo; } else { // In this case we use logic which is to calculate ChannelGroup "saoc_data.tgt_sare_no" value // using the SalesArea TargetAreaName property and use SalesArea CustomIds for everything else. var salesArea = salesAreas.FirstOrDefault(s => s.Name.Equals(salesAreaCampaignTarget.SalesArea)); salesAreaNo = salesArea?.CustomId ?? 0; channelGroupNo = salesArea is null || salesArea.TargetAreaName is null ? 0 : salesAreaTargetMap[salesArea.TargetAreaName]; } var totalDesiredSplit = salesAreaCampaignTarget.Multiparts?.Sum(l => l.DesiredPercentageSplit) ?? 0; var totalCurrentSplit = salesAreaCampaignTarget.Multiparts?.Sum(l => l.CurrentPercentageSplit) ?? 0; var agLengthList = salesAreaCampaignTarget.Multiparts != null ? _mapper.Map <List <AgLength> >(Tuple.Create(salesAreaCampaignTarget.Multiparts, campaign.CustomId, salesAreaNo)) : null; var agLengths = new AgLengths(); if (!(agLengthList is null)) { agLengths.AddRange(agLengthList); } var strikeWeights = salesAreaCampaignTarget.CampaignTargets ?.Where(t => t.StrikeWeights != null && t.StrikeWeights.Any()) .SelectMany(s => s.StrikeWeights).ToList(); var agStrikeWeightList = strikeWeights != null && strikeWeights.Any() ? _mapper.Map <List <AgStrikeWeight> >(Tuple.Create(strikeWeights, campaign.CustomId, salesAreaNo)) : null; var agStrikeWeights = new AgStrikeWeights(); if (!(agStrikeWeightList is null)) { agStrikeWeights.AddRange(agStrikeWeightList); } var agDayPartList = LoadAgDayParts( strikeWeights, campaign.CustomId, salesAreaNo, ref dayPartNo); var agPartList = !(agDayPartList is null) && agDayPartList.Any() ? _mapper.Map <List <AgPart> >(agDayPartList) : null; var agParts = new AgParts(); if (!(agPartList is null)) { agParts.AddRange(agPartList); } if (_mergeUniqueDayParts) { agDayPartList = MergeUniqueDayParts(agDayPartList, agParts); } var agDayParts = new AgDayParts(); if (!(agDayPartList is null)) { agDayParts.AddRange(agDayPartList); } var agDayPartLengths = agDayPartList?.Count > 0 ? agDayPartList.Select(x => x.AgDayPartLengths).ToList() : null; var agPartLengthList = _mapper.Map <List <AgPartLength> >(agDayPartLengths); var agPartLengths = new AgPartLengths(); if (!(agPartLengthList is null)) { agPartLengths.AddRange(agPartLengthList); } var agCampaignSalesAreaClone = agCampaignSalesArea.Clone(); agCampaignSalesAreaClone.SalesAreaNo = salesAreaNo; agCampaignSalesAreaClone.ChannelGroupNo = channelGroupNo; agCampaignSalesAreaClone.CampaignNo = campaign.CustomId; agCampaignSalesAreaClone.AgSalesAreaCampaignRequirement = _mapper.Map <AgRequirement>(Tuple.Create(totalDesiredSplit, totalCurrentSplit)); agCampaignSalesAreaClone.AgCampaignSalesAreaPtrRef = agCampaignSalesArea.AgCampaignSalesAreaPtrRef.Clone(); agCampaignSalesAreaClone.AgCampaignSalesAreaPtrRef.SalesAreaNo = salesAreaNo; agCampaignSalesAreaClone.AgLengths = agLengths; agCampaignSalesAreaClone.NbrAgLengths = agLengthList?.Count ?? 0; agCampaignSalesAreaClone.AgStrikeWeights = agStrikeWeights; agCampaignSalesAreaClone.NbrAgStrikeWeights = agStrikeWeightList?.Count ?? 0; agCampaignSalesAreaClone.AgDayParts = agDayParts; agCampaignSalesAreaClone.NbrAgDayParts = agDayPartList?.Count ?? 0; agCampaignSalesAreaClone.AgParts = agParts; agCampaignSalesAreaClone.NbrParts = agPartList?.Count ?? 0; agCampaignSalesAreaClone.NbrPartsLengths = agPartLengthList?.Count ?? 0; agCampaignSalesAreaClone.AgPartsLengths = agPartLengths; agCampaignSalesAreaClone.StopBooking = AgConversions.ToAgBooleanAsString(salesAreaCampaignTarget.StopBooking); agCampaignSalesAreaList.Add(agCampaignSalesAreaClone); } return(agCampaignSalesAreaList); }
private static AgBreak LoadAgBreak( Tuple <BreakWithProgramme, IReadOnlyCollection <SalesArea>, IReadOnlyCollection <Demographic>, List <int>, int, AgBreak> source, IMapper mapper) { var(breakModel, salesAreas, demographics, programmeCategories, uniqueId, agBreak) = source; var brk = breakModel.Break; var salesArea = salesAreas?.FirstOrDefault(s => s.Name == brk.SalesArea); var salesAreaId = salesArea?.CustomId ?? 0; var agProgCategories = new AgProgCategories(); if (programmeCategories != null) { agProgCategories.AddRange(programmeCategories); } var agBreakClone = agBreak.Clone(); agBreakClone.SalesAreaNo = salesAreaId; agBreakClone.SalesAreaId = salesAreaId; DecomposeScheduledDate(brk.ScheduledDate); void DecomposeScheduledDate(DateTime scheduledDate) { agBreakClone.ScheduledDate = scheduledDate.ToString("yyyyMMdd"); agBreakClone.NominalTime = scheduledDate.ToString("HHmmss"); agBreakClone.DayNumber = scheduledDate.DayOfWeek == 0 ? 7 : (int)scheduledDate.DayOfWeek; } agBreakClone.BroadcastDate = brk.BroadcastDate.HasValue ? brk.BroadcastDate.Value.ToString("yyyyMMdd") : "0"; agBreakClone.ClockHour = brk.ClockHour ?? 0; agBreakClone.BreakNo = brk.CustomId; agBreakClone.ExternalNo = brk.ExternalBreakRef; agBreakClone.Uid = uniqueId; agBreakClone.Duration = (int)brk.Duration.ToTimeSpan().TotalSeconds; agBreakClone.BreakPrice = brk.BreakPrice; agBreakClone.FloorRate = brk.FloorRate; agBreakClone.PositionInProg = breakModel.PositionInProgram.ToString(); agBreakClone.ProgNo = breakModel.ProgrammeNo; agBreakClone.ProgEventNo = breakModel.PrgtNo; agBreakClone.EpisNo = breakModel.EpisodeNumber.GetValueOrDefault(); agBreakClone.BreakTypeCode = brk.BreakType.Substring(0, 2).ToUpperInvariant(); agBreakClone.AgSalesAreaPtrRef = agBreak.AgSalesAreaPtrRef.Clone(); agBreakClone.AgSalesAreaPtrRef.SalesAreaNo = salesAreaId; var availInSec = (int)brk.OptimizerAvail.ToTimeSpan().TotalSeconds; agBreakClone.AgRegionalBreaks = new AgRegionalBreaks { new AgRegionalBreak { OpenAvail = availInSec, TregNo = salesAreaId } }; agBreakClone.AgAvals = new AgAvals { new AgAval { OpenAvail = availInSec, SalesAreaNo = salesAreaId, ReserveDuration = (int)brk.ReserveDuration.ToTimeSpan().TotalSeconds } }; agBreakClone.AgProgCategories = agProgCategories; agBreakClone.AgPredictions = mapper.Map <AgPredictions>( Tuple.Create(breakModel.Predictions, salesArea, demographics) ); agBreakClone.Solus = AgConversions.ToAgBooleanAsString(brk.Solus); agBreakClone.PremiumCategory = brk.PremiumCategory ?? string.Empty; agBreakClone.AllowSplit = AgConversions.ToAgBooleanAsString(brk.AllowSplit); agBreakClone.NationalRegionalSplit = AgConversions.ToAgBooleanAsString(brk.NationalRegionalSplit); agBreakClone.ExcludePackages = AgConversions.ToAgBooleanAsString(brk.ExcludePackages); agBreakClone.BonusAllowed = AgConversions.ToAgBooleanAsString(brk.BonusAllowed); agBreakClone.LongForm = AgConversions.ToAgBooleanAsString(brk.LongForm); return(agBreakClone); }
/// <summary>Serializes campaigns into the specified folder.</summary> /// <param name="folderName">Name of the folder.</param> /// <param name="run">The run.</param> /// <param name="campaigns">The campaigns.</param> /// <param name="demographics">The demographics.</param> /// <param name="salesAreas">The sales areas.</param> /// <param name="programmeDictionaries">The programme dictionaries.</param> /// <param name="programmeCategories">The programme categories.</param> /// <param name="autoBookDefaultParameters">The automatic book default parameters.</param> /// <param name="channelGroups">The channel groups.</param> public void Serialize( string folderName, Run run, IReadOnlyCollection <Campaign> campaigns, IReadOnlyCollection <BusinessType> businessTypes, IReadOnlyCollection <Demographic> demographics, IReadOnlyCollection <SalesArea> salesAreas, IReadOnlyCollection <ProgrammeDictionary> programmeDictionaries, IReadOnlyCollection <ProgrammeCategoryHierarchy> programmeCategories, IAutoBookDefaultParameters autoBookDefaultParameters, out IReadOnlyCollection <Tuple <int, int, SalesAreaGroup> > channelGroups, out List <AgCampaignInclusion> campaignIncludeFunctions) { channelGroups = null; campaignIncludeFunctions = null; if (campaigns is null || campaigns.Count == 0) { return; } RaiseInfo( $"Started populating {Filename}. Total campaigns - {campaigns.Count}, Current Time - {_clock.GetCurrentInstant().ToDateTimeUtc()}"); var channelGroup = new List <Tuple <int, int, SalesAreaGroup> >(); PrepareRelatedData(campaigns); var dayPartNo = 0; var agCampaigns = campaigns.Select(campaign => { var demographicNo = demographics .FirstOrDefault(d => d.ExternalRef.Equals(campaign.DemoGraphic))?.Id ?? 0; int.TryParse(campaign.Product, out var productCode); var product = productCode != 0 ? ResolveProduct(campaign) : null; var agCampaignSalesAreaList = LoadAgCampaignSalesAreas( campaign, salesAreas, autoBookDefaultParameters.AgCampaignSalesArea, ref dayPartNo, ref channelGroup); var agCampaignSalesAreas = new AgCampaignSalesAreas(); if (!(agCampaignSalesAreaList is null)) { agCampaignSalesAreas.AddRange(agCampaignSalesAreaList); } var agCampaignProgrammeList = LoadAgCampaignProgrammes( campaign.ProgrammesList, salesAreas, programmeDictionaries, programmeCategories); var agCampaignClone = autoBookDefaultParameters.AgCampaign.Clone(); agCampaignClone.CampaignNo = campaign.CustomId; agCampaignClone.ExternalNo = campaign.ExternalId; agCampaignClone.DemographicNo = demographicNo; agCampaignClone.DealNo = campaign.CustomId; agCampaignClone.ProductCode = productCode; agCampaignClone.ClearanceCode = campaign.ExpectedClearanceCode ?? ""; agCampaignClone.RevenueBudget = Convert.ToInt32(campaign.RevenueBudget); agCampaignClone.StartDate = campaign.StartDateTime.ToString(ShortDateFormat); agCampaignClone.EndDate = campaign.EndDateTime.ToString(ShortDateFormat); agCampaignClone.RootClashCode = string.IsNullOrWhiteSpace(product?.ClashCode) ? productCode.ToString() : ResolveRootClashCode(campaign); agCampaignClone.ClashCode = string.IsNullOrWhiteSpace(product?.ClashCode) ? productCode.ToString() : product.ClashCode; agCampaignClone.AdvertiserIdentifier = product?.AdvertiserIdentifier; agCampaignClone.ClashNo = 0; agCampaignClone.BusinessType = businessTypes.FirstOrDefault(x => x.Code == campaign.BusinessType && x.IncludeConversionIndex)?.Code ?? string.Empty; agCampaignClone.AgCampaignRequirement = _mapper.Map <AgRequirement>(Tuple.Create(campaign.TargetRatings, campaign.ActualRatings)); agCampaignClone.AgCampaignSalesAreas = agCampaignSalesAreas; agCampaignClone.NbrAgCampagignSalesArea = agCampaignSalesAreaList?.Count ?? 0; agCampaignClone.MaxAgCampagignSalesArea = agCampaignSalesAreaList?.Count ?? 0; agCampaignClone.IncludeFunctions = (int)GetIncludeFunctions(run, campaign); agCampaignClone.CampaignSpotMaxRatings = campaign.CampaignSpotMaxRatings; agCampaignClone.NbrAgCampaignProgrammeList = agCampaignProgrammeList.Count; agCampaignClone.AgProgrammeList = agCampaignProgrammeList; agCampaignClone.DeliveryCurrency = (int)campaign.DeliveryCurrency; agCampaignClone.StopBooking = AgConversions.ToAgBooleanAsString(campaign.StopBooking); return(agCampaignClone); }).ToList(); new AgCampaignsSerialization().MapFrom(agCampaigns).Serialize(Path.Combine(folderName, Filename)); channelGroups = channelGroup; campaignIncludeFunctions = agCampaigns.Select(campaign => new AgCampaignInclusion() { CampaignNo = campaign.CampaignNo, IncludeFunctions = campaign.IncludeFunctions }).ToList(); RaiseInfo( $"Finished populating {Filename}. Total campaigns - {campaigns.Count}, Current Time - {_clock.GetCurrentInstant().ToDateTimeUtc()}"); }