private void startSupplementsCycle(MyTrainingOperationParam param, SupplementCycle dbCycle, Profile dbProfile, Guid supplementsCycleDefinitionId) { dbCycle.Profile = dbProfile; var dbCycleDefinition = dbCycle.SupplementsCycleDefinition = Session.QueryOver <SupplementCycleDefinition>().Where(x => x.GlobalId == supplementsCycleDefinitionId).Fetch(x => x.Weeks).Eager.Fetch(x => x.Weeks.First().Dosages).Eager.SingleOrDefault(); var definitionDTO = dbCycleDefinition.Map <SupplementCycleDefinitionDTO>(); SupplementsCycleRepetiter repetiter = new SupplementsCycleRepetiter(); var preparedCycleDefinition = repetiter.Preapre(definitionDTO, dbCycle.TotalWeeks); dbCycleDefinition = preparedCycleDefinition.Map <SupplementCycleDefinition>(); for (int i = 0; i < dbCycleDefinition.GetTotalDays(dbCycle.TotalWeeks); i++) { createEntryObject(param.MyTraining, i, dbProfile, dbCycle, () => { List <EntryObject> entries = new List <EntryObject>(); var dosages = getDayDosages(dbCycleDefinition, i, dbCycle); if (dosages.Count == 0) { return(entries); } if (dosages.OfType <SupplementCycleMeasurement>().Count() > 0) { SizeEntry sizeEntry = new SizeEntry(); entries.Add(sizeEntry); } var supplementEntries = dosages.OfType <SupplementCycleDosage>().ToList(); if (supplementEntries.Count == 0) { return(entries); } SuplementsEntry entry = new SuplementsEntry(); foreach (var dosage in supplementEntries) { SuplementItem item = new SuplementItem(); entry.AddItem(item); item.Name = dosage.Name; item.Time.TimeType = (TimeType)dosage.TimeType; item.Suplement = Session.Load <Suplement>(dosage.Supplement.GlobalId); item.Dosage = dosage.Dosage; if (dosage.DosageUnit == BodyArchitect.Model.DosageUnit.ON10KgWight) { item.Dosage = (dosage.Dosage * (dbCycle.Weight / 10M)).RoundToNearestHalf(); } item.DosageType = (DosageType)dosage.DosageType; } entries.Add(entry); return(entries); }); } }
protected T CreateTraining <T>(string name, DateTime startDate, TrainingEnd trainingEnd, int percentage, Profile profile, DateTime?endDate = null, Customer customer = null) where T : MyTraining, new() { T cycle = new T(); cycle.SetData(startDate, endDate, trainingEnd); cycle.Profile = profile; cycle.Name = name; cycle.Customer = customer; cycle.PercentageCompleted = percentage; SupplementCycle supplementCycle = cycle as SupplementCycle; if (supplementCycle != null) { supplementCycle.SupplementsCycleDefinition = definition; } insertToDatabase(cycle); return(cycle); }
private List <SupplementCycleEntry> getDayDosages(SupplementCycleDefinition definition, int currentDay, SupplementCycle cycle) { var trainingDays = !string.IsNullOrEmpty(cycle.TrainingDays) ? cycle.TrainingDays.Split(',').Select(x => TrainingDayItem.Create(x)).ToList() : new List <TrainingDayItem>(); var newDate = cycle.StartDate.AddDays(currentDay); int week = (int)((newDate - cycle.StartDate).TotalDays / 7) + 1; List <SupplementCycleEntry> dosages = new List <SupplementCycleEntry>(); foreach (var day in definition.Weeks) { if (week >= day.CycleWeekStart && week <= day.CycleWeekEnd) { foreach (var dosage in day.Dosages) { if (dosage.Repetitions == BodyArchitect.Model.SupplementCycleDayRepetitions.EveryDay) { dosages.Add(dosage); continue; } if (dosage.Repetitions == BodyArchitect.Model.SupplementCycleDayRepetitions.OnceAWeek && newDate.DayOfWeek == cycle.StartDate.DayOfWeek) { dosages.Add(dosage); continue; } if (dosage.Repetitions == BodyArchitect.Model.SupplementCycleDayRepetitions.StrengthTrainingDay && trainingDays.Where(x => x.Day == newDate.DayOfWeek && (x.TrainingType == SupplementsCycleTrainingType.Strength || x.TrainingType == SupplementsCycleTrainingType.Both)).Count() > 0) { dosages.Add(dosage); continue; } if (dosage.Repetitions == BodyArchitect.Model.SupplementCycleDayRepetitions.CardioTrainingDay && trainingDays.Where(x => x.Day == newDate.DayOfWeek && (x.TrainingType == SupplementsCycleTrainingType.Cardio || x.TrainingType == SupplementsCycleTrainingType.Both)).Count() > 0) { dosages.Add(dosage); continue; } if (dosage.Repetitions == BodyArchitect.Model.SupplementCycleDayRepetitions.NonTrainingDay && trainingDays.Where(x => x.Day == newDate.DayOfWeek).Count() == 0) { dosages.Add(dosage); continue; } } } } return(dosages); }