public static Dictionary <Entity, List <Incubation> > CorrectFundIncubations() { var fundIncubations = IncubationDataAccess.GetAllFundIncubations(); var toto = fundIncubations.GroupBy(f => f.Fund.Id); int count = toto.Count(); if (fundIncubations.Count != count) { Log.Error("IncubationStream.GetHistory", "Duplicated fund"); } Dictionary <Entity, List <Incubation> > gp = new Dictionary <Entity, List <Incubation> >(); List <FundIncubation> update = new List <FundIncubation>(); foreach (var fund in toto) { gp.Add(fund.First().Fund, fund.SelectMany(e => e.Incubations).ToList()); var all = fund.SelectMany(e => e.Incubations).ToList(); List <Incubation> distincIncubations = fund.SelectMany(e => e.Incubations).Distinct(new IncubationComparer()).ToList(); if (all.Count() != distincIncubations.Count) { Console.WriteLine("distinct"); } update.Add(new FundIncubation() { Fund = fund.First().Fund, Incubations = distincIncubations, LastUpdate = DateTime.UtcNow }); } IncubationDataAccess.DeleteFundIncubationCollection(); IncubationDataAccess.SaveDailyActivity(update); return(gp); }
private static void SaveDailyActivity(Dictionary <Entity, List <Incubation> > dic) { if (dic.Count == 0) { return; } Dictionary <Entity, List <Incubation> > toSave = new Dictionary <Entity, List <Incubation> >(); foreach (var item in dic) { toSave.Add(item.Key, item.Value); } #region Update FundIncubations existant try { List <FundIncubation> existingFundIncubations = IncubationDataAccess.FindExistingIncubations(toSave.Select(i => i.Key.Id).ToList()); if (existingFundIncubations.Count > toSave.Count) { Log.Error("IncubationStream.SaveDailyActivity", "Too much FundIncubation found"); return; } foreach (var fundIncubation in existingFundIncubations) { var current = toSave.FirstOrDefault(i => i.Key.Id == fundIncubation.Fund.Id); Entity fund = current.Key; List <Incubation> updatedIncubations = fundIncubation.Incubations; bool newIncubation = false; foreach (var invest in current.Value) { // vérifie que l'investissement courant n'existe pas dans l'investissement existant Incubation investement = fundIncubation.Incubations.FirstOrDefault(i => i.Id == invest.Id); if (investement == null) { updatedIncubations.Add(invest); newIncubation = true; } else { toSave.Remove(fund); } } if (newIncubation) { var updatedFundIncubation = new FundIncubation(fundIncubation, fund, updatedIncubations); IncubationDataAccess.Update(updatedFundIncubation); Log.Info("IncubationStream.SaveDailyActivity", "Update fund incubation", string.Format("{0} in {1}", updatedFundIncubation.Fund.Name, string.Join(", ", updatedFundIncubation.Incubations.Select(incubation => incubation.Startup.Name)))); } } } catch (Exception ex) { Log.Error("IncubationStream.SaveDailyActivity", "Update", ex); throw; } #endregion #region Insert nouveau FundIncubations try { if (toSave.Any()) { List <FundIncubation> incubations = new List <FundIncubation>(); foreach (var item in toSave) { incubations.Add(new FundIncubation() { Fund = item.Key, Incubations = item.Value, LastUpdate = DateTime.UtcNow }); } IncubationDataAccess.SaveDailyActivity(incubations); Log.Info("IncubationStream.SaveDailyActivity", "New funds incubations", string.Join(", ", incubations.ToString())); } else { Log.Info("IncubationStream.SaveDailyActivity", "No new fund incubation"); } } catch (Exception ex) { Log.Error("IncubationStream.SaveDailyActivity", "Insert", ex); throw; } #endregion }