static void Initialize_PrixLots() { var data = GetFactoryDataJSON().prix_lots_info; using (var context = new PalaisDuBonbonEntities()) { Console.WriteLine("---- Initialize Prix Lots Table : Data To Procces : " + data.Count * 3); int c = 1; foreach (var d in data) { string lot_type = d.type; PRIX_LOT pl_sachet = new PRIX_LOT(); pl_sachet.BONBON = context.BONBONs.FirstOrDefault(b => b.BONBON_TYPE == lot_type); pl_sachet.CONDITIONNEMENT = context.CONDITIONNEMENTs.FirstOrDefault(cond => cond.CONDITIONNEMENT_NOM == "sachet"); pl_sachet.BONBON_ID = pl_sachet.BONBON.BONBON_ID; pl_sachet.CONDITIONNEMENT_ID = pl_sachet.CONDITIONNEMENT.CONDITIONNEMENT_ID; pl_sachet.PRIX_LOT_PRIX = d.sachet; context.PRIX_LOT.Add(pl_sachet); c++; Console.WriteLine("-------- Data Added : (" + c + "/" + data.Count + ")"); PRIX_LOT pl_boite = new PRIX_LOT(); pl_boite.BONBON = context.BONBONs.FirstOrDefault(b => b.BONBON_TYPE == lot_type); pl_boite.CONDITIONNEMENT = context.CONDITIONNEMENTs.FirstOrDefault(cond => cond.CONDITIONNEMENT_NOM == "boite"); pl_boite.BONBON_ID = pl_boite.BONBON.BONBON_ID; pl_boite.CONDITIONNEMENT_ID = pl_boite.CONDITIONNEMENT.CONDITIONNEMENT_ID; pl_boite.PRIX_LOT_PRIX = d.boite; context.PRIX_LOT.Add(pl_boite); c++; Console.WriteLine("-------- Data Added : (" + c + "/" + data.Count + ")"); PRIX_LOT pl_echantillon = new PRIX_LOT(); pl_echantillon.BONBON = context.BONBONs.FirstOrDefault(b => b.BONBON_TYPE == lot_type); pl_echantillon.CONDITIONNEMENT = context.CONDITIONNEMENTs.FirstOrDefault(cond => cond.CONDITIONNEMENT_NOM == "echantillon"); pl_echantillon.BONBON_ID = pl_echantillon.BONBON.BONBON_ID; pl_echantillon.CONDITIONNEMENT_ID = pl_echantillon.CONDITIONNEMENT.CONDITIONNEMENT_ID; pl_echantillon.PRIX_LOT_PRIX = d.echantillon; context.PRIX_LOT.Add(pl_echantillon); c++; Console.WriteLine("-------- Data Added : (" + c + "/" + (data.Count * 3) + ")"); if ((c % 100) == 0) { context.SaveChanges(); } } context.SaveChanges(); Console.WriteLine("---- Initialize Prix Lots Table : DONE ! ----"); } }
public static void GenerateOrders() { var configData = GetConfigOrderGenerationDataJSON(); int total_orders = (int)configData.nb_commandes.Value; bool useRandom_countries = configData.useRandom_countries.Value; bool useRandom_candies = configData.useRandom_candies.Value; List <COMMANDE> _generated_orders = new List <COMMANDE>(); Dictionary <PAYS, int> Countries_OrdersCount = new Dictionary <PAYS, int>(); Console.WriteLine("---- Starting ORDERS GENERATION"); using (var context = new PalaisDuBonbonEntities()) { foreach (PAYS p in context.PAYS) { PAYS pays = p; int ordersCount = (int)(p.RATIO_COMMANDE * total_orders) / 100; Countries_OrdersCount.Add(p, ordersCount); } if (!useRandom_countries) { foreach (KeyValuePair <PAYS, int> pays in Countries_OrdersCount) { for (int i = 0; i < pays.Value; i++) // pays.value = nb de cmds d'un pays { int num_commande = rnd.Next(100000, 999999); int data_generation_year = (int)configData.generation_fabrication_data_date; DateTime date_commande = DateTime.Now; int nb_lots = rnd.Next((int)configData.lots_min.Value, (int)configData.lots_max.Value); // nb de lots a generer for (int n = 0; n < nb_lots; n++) { COMMANDE commande = new COMMANDE(); commande.COMMANDE_NUM_COMMANDE = num_commande; commande.COMMANDE_ETAT = false; commande.COMMANDE_DATE_COMMANDE = date_commande; commande.COMMANDE_DATE_FABRICATION = null; commande.COMMANDE_DATE_CONDITIONNEMENT = null; commande.COMMANDE_DATE_EXPEDITION = null; commande.PAYS_ID = pays.Key.PAYS_ID; commande.PAY = pays.Key; commande.COMMANDE_QUANTITE = nb_lots; int cond_count = context.CONDITIONNEMENTs.Count(); int rndConditionnement = rnd.Next(1, cond_count + 1); commande.CONDITIONNEMENT = context.CONDITIONNEMENTs.Where(cd => cd.CONDITIONNEMENT_ID == rndConditionnement).First(); commande.CONDITIONNEMENT_ID = commande.CONDITIONNEMENT.CONDITIONNEMENT_ID; int randomBonbon = rnd.Next(0, 100); var bonbon_probas = GetFactoryDataJSON().taux_ventes_2016; // int selected_bonbon = 0; // int selected_bonbon_id = 0; float myValue = 0f; string choosed_candy_name = ""; foreach (var prob in bonbon_probas) { float valeur = prob.valeur; myValue += valeur; if (myValue >= randomBonbon) { choosed_candy_name = prob.type_bonbon; break; } /*selected_bonbon_id++; * int val = prob.valeur; * selected_bonbon += val; * if (selected_bonbon >= randomBonbon) * { * break; * }*/ } List <BONBON> bonbons_available = context.BONBONs.Where(b => b.BONBON_TYPE == choosed_candy_name).ToList(); commande.BONBON = bonbons_available.ElementAt(rnd.Next(bonbons_available.Count)); commande.BONBON_ID = commande.BONBON.BONBON_ID; PRIX_LOT test = context.PRIX_LOT.Where(pl => pl.CONDITIONNEMENT_ID == commande.CONDITIONNEMENT.CONDITIONNEMENT_ID).Single(pl => pl.BONBON.BONBON_TYPE == commande.BONBON.BONBON_TYPE); commande.COMMANDE_PRIX_LOT = commande.COMMANDE_QUANTITE * test.PRIX_LOT_PRIX; _generated_orders.Add(commande); Console.WriteLine("-------- Order Generated"); } } } } int counter = 0; foreach (COMMANDE com in _generated_orders) { Console.WriteLine("-------- Pushing Order No (" + counter + "/" + _generated_orders.Count + ")"); context.COMMANDEs.Add(com); if ((counter % 100) == 0) { Console.WriteLine("-------- Saving changes...."); context.SaveChanges(); } counter++; } Console.WriteLine("-------- Saving changes...."); context.SaveChanges(); Console.WriteLine("---- ORDERS GENERATION DONE"); Console.WriteLine("---- WRITING GENERATED DATA TO generated_orders.json"); // string jsonCommandes = JsonConvert.SerializeObject(_generated_orders); // GenerateOrdersJSONFile(jsonCommandes); Console.WriteLine("---- CLOSING CONNEXION..."); } Console.WriteLine("---- WRITING DONE ! END OF FUNC"); }