/// <summary> /// This Function MUST be executed AFTER having populated the Transport Table. /// /// TO DO : Secure the function with checking on opening the connexion if Transport Table already exists, /// if not, execute the Initialize_TransportInfo Method. /// </summary> static void Initialize_PaysData() { var data = GetFactoryDataJSON().pays; using (var context = new PalaisDuBonbonEntities()) { Console.WriteLine("---- Initialize Pays Table : Data To Procces : " + data.Count); int c = 1; List <string> transports_avion = new List <string>(); transports_avion.Add("USA"); transports_avion.Add("Canada"); transports_avion.Add("Mexique"); transports_avion.Add("Chine"); List <string> transports_bateau = new List <string>(); transports_bateau.Add("Japon"); transports_bateau.Add("Afrique du Sud"); foreach (var d in data) { PAYS p = new PAYS(); p.PAYS_NOM = d.Value; if (transports_bateau.Contains(d.Value)) { p.PAYS_TRANSPORT = "bateau"; } else if (transports_avion.Contains(d.Value)) { p.PAYS_TRANSPORT = "avion"; } else { p.PAYS_TRANSPORT = "camion"; } context.PAYS.Add(p); Console.WriteLine("-------- Data Added : (" + c + "/" + data.Count + ")"); c++; } context.SaveChanges(); Console.WriteLine("---- Initialize Pays 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"); }