コード例 #1
0
        public async Task <List <PharmacyMedicineSupply> > GetSupplies(string medicineName, int demand)
        {
            _log4net.Info("GetSupplies request Initiated in MedicineSupplyRepository for " + medicineName + " with demand count " + demand);
            //List of Supply to be sent
            List <PharmacyMedicineSupply> supplies = new List <PharmacyMedicineSupply>();
            var stock = new List <MedicineStock>();

            try
            {
                using (var httpclient = new HttpClient())
                {
                    httpclient.BaseAddress = new Uri("http://40.76.174.193/");
                    HttpResponseMessage res = await httpclient.GetAsync("MedicineStockInformation");

                    _log4net.Info("GetSupplies request Initiated for the Medicine Api");
                    if (res.IsSuccessStatusCode)
                    {
                        _log4net.Info("GetSupplies data recieved from the Stock Api");
                        var result = res.Content.ReadAsStringAsync().Result;
                        stock = JsonConvert.DeserializeObject <List <MedicineStock> >(result);
                    }
                    else
                    {
                        _log4net.Error("GetSupplies no data recieved from the Stock Api");
                        return(null);
                    }
                }



                //Dictionary to store the Name of med and stock value as key value pair
                Dictionary <string, int> meds = new Dictionary <string, int>();
                foreach (var medicine in stock)
                {
                    meds.Add(medicine.Name, medicine.NumberOfTabletsInStock);
                }

                //List of Pharmacy the company does business with
                List <string> Pharmacies = new List <string>()
                {
                    "Max Pharmacy", "Appolo Pharmacy", "Synergy Pharmacy", "GoodWill Pharmacy"
                };
                int totalPharmacies = Pharmacies.Count;
                PharmacyMedicineSupply medSupply;

                //Equal distribution of Medicine in Demand
                int inStock     = meds[medicineName];
                int demandStock = demand;
                if (inStock > demandStock)
                {
                    int medCount = demandStock / totalPharmacies;
                    for (int i = 0; i < totalPharmacies - 1; i++)
                    {
                        medSupply = new PharmacyMedicineSupply()
                        {
                            MedicineName = medicineName, PharmacyName = Pharmacies[i], SupplyCount = medCount
                        };
                        supplies.Add(medSupply);
                        demandStock = demandStock - medCount;
                    }
                    medSupply = new PharmacyMedicineSupply()
                    {
                        MedicineName = medicineName, PharmacyName = Pharmacies[totalPharmacies - 1], SupplyCount = demandStock
                    };
                    supplies.Add(medSupply);
                }
                else
                {
                    int medCount = inStock / totalPharmacies;
                    for (int i = 0; i < totalPharmacies - 1; i++)
                    {
                        medSupply = new PharmacyMedicineSupply()
                        {
                            MedicineName = medicineName, PharmacyName = Pharmacies[i], SupplyCount = medCount
                        };
                        supplies.Add(medSupply);
                        inStock = inStock - medCount;
                    }
                    medSupply = new PharmacyMedicineSupply()
                    {
                        MedicineName = medicineName, PharmacyName = Pharmacies[totalPharmacies - 1], SupplyCount = inStock
                    };
                    supplies.Add(medSupply);
                }

                return(supplies);
            }
            catch (Exception ex)
            {
                _log4net.Error("GetSupplies Could not Connect to the Medicine Stock Api" + ex.Message);
            }
            return(supplies);
        }
コード例 #2
0
        /*   public IEnumerable<PharmacyMedicineSupply> GetSupplies(List<MedicineDemand> demand)
         * {
         *     //List of Supply to be sent
         *     List<PharmacyMedicineSupply> supplies = new List<PharmacyMedicineSupply>();
         *
         *     //List of All the meds in stock
         *     var stock = new List<MedicineStock>() {
         *     new MedicineStock{Name="A",ChemicalComposition="a,b,c",DateOfExpiry="02-11-2022",NumberOfTabletsInStock=200,TargetAilment="General"},
         *     new MedicineStock{Name="B",ChemicalComposition="a,b,c",DateOfExpiry="02-11-2022",NumberOfTabletsInStock=100,TargetAilment="General"},
         *     new MedicineStock{Name="C",ChemicalComposition="a,b,c",DateOfExpiry="02-11-2022",NumberOfTabletsInStock=200,TargetAilment="General"},
         *     new MedicineStock{Name="D",ChemicalComposition="a,b,c",DateOfExpiry="02-11-2022",NumberOfTabletsInStock=200,TargetAilment="General"},
         *     new MedicineStock{Name="E",ChemicalComposition="a,b,c",DateOfExpiry="02-11-2022",NumberOfTabletsInStock=400,TargetAilment="General"},
         *     };
         *
         *     //Dictionary to store the Name of med and stock value as key value pair
         *     Dictionary<string, int> meds = new Dictionary<string, int>();
         *     foreach(var med in stock)
         *     {
         *         meds.Add(med.Name, med.NumberOfTabletsInStock);
         *     }
         *
         *     //List of Pharmacy the company does business with
         *     List<string> Pharmacies = new List<string>() {"Pharmacy1","Pharmacy2","Pharmacy3","Pharmacy4"};
         *     int totalPharmacies = Pharmacies.Count;
         *     PharmacyMedicineSupply medSupply;
         *     foreach(var med in demand)
         *     {
         *         int inStock = meds[med.Medicine];
         *         int demandStock = med.Demand;
         *         if(inStock>med.Demand)
         *         {
         *             int medcount = demandStock / totalPharmacies;
         *             for(int i=0;i<totalPharmacies-1;i++)
         *             {
         *                 medSupply = new PharmacyMedicineSupply() { MedicineName = med.Medicine, PharmacyName = Pharmacies[i], SupplyCount = medcount };
         *                 supplies.Add(medSupply);
         *                 demandStock = demandStock - medcount;
         *             }
         *             medSupply = new PharmacyMedicineSupply() { MedicineName = med.Medicine, PharmacyName = Pharmacies[totalPharmacies-1], SupplyCount = demandStock };
         *             supplies.Add(medSupply);
         *         }
         *         else
         *         {
         *             int medcount = inStock / totalPharmacies;
         *             for (int i = 0; i < totalPharmacies - 1; i++)
         *             {
         *                 medSupply = new PharmacyMedicineSupply() { MedicineName = med.Medicine, PharmacyName = Pharmacies[i], SupplyCount = medcount };
         *                 supplies.Add(medSupply);
         *                 inStock = inStock - medcount;
         *             }
         *             medSupply = new PharmacyMedicineSupply() { MedicineName = med.Medicine, PharmacyName = Pharmacies[totalPharmacies - 1], SupplyCount = inStock };
         *             supplies.Add(medSupply);
         *         }
         *
         *     }
         *
         *
         *
         *     return supplies;
         * }*/

        public async Task <IEnumerable <PharmacyMedicineSupply> > GetSupplies(string med, int demand)
        {
            //List of Supply to be sent
            List <PharmacyMedicineSupply> supplies = new List <PharmacyMedicineSupply>();

            //List of All the meds in stock

            /* var stock = new List<MedicineStock>() {
             * new MedicineStock{Name="A",ChemicalComposition="a,b,c",DateOfExpiry="02-11-2022",NumberOfTabletsInStock=200,TargetAilment="General"},
             * new MedicineStock{Name="B",ChemicalComposition="a,b,c",DateOfExpiry="02-11-2022",NumberOfTabletsInStock=100,TargetAilment="General"},
             * new MedicineStock{Name="C",ChemicalComposition="a,b,c",DateOfExpiry="02-11-2022",NumberOfTabletsInStock=200,TargetAilment="General"},
             * new MedicineStock{Name="D",ChemicalComposition="a,b,c",DateOfExpiry="02-11-2022",NumberOfTabletsInStock=200,TargetAilment="General"},
             * new MedicineStock{Name="E",ChemicalComposition="a,b,c",DateOfExpiry="02-11-2022",NumberOfTabletsInStock=400,TargetAilment="General"},
             * };*/

            var stock = new List <MedicineStock>();

            using (var httpclient = new HttpClient())
            {
                httpclient.BaseAddress = new Uri("https://localhost:44366/");
                HttpResponseMessage res = await httpclient.GetAsync("MedicineStockInformation");

                if (res.IsSuccessStatusCode)
                {
                    var result = res.Content.ReadAsStringAsync().Result;
                    stock = JsonConvert.DeserializeObject <List <MedicineStock> >(result);
                }
            }

            //Dictionary to store the Name of med and stock value as key value pair
            Dictionary <string, int> meds = new Dictionary <string, int>();

            foreach (var medicine in stock)
            {
                meds.Add(medicine.Name, medicine.NumberOfTabletsInStock);
            }

            //List of Pharmacy the company does business with
            List <string> Pharmacies = new List <string>()
            {
                "Pharmacy1", "Pharmacy2", "Pharmacy3", "Pharmacy4"
            };
            int totalPharmacies = Pharmacies.Count;
            PharmacyMedicineSupply medSupply;


            int inStock     = meds[med];
            int demandStock = demand;

            if (inStock > demandStock)
            {
                int medcount = demandStock / totalPharmacies;
                for (int i = 0; i < totalPharmacies - 1; i++)
                {
                    medSupply = new PharmacyMedicineSupply()
                    {
                        MedicineName = med, PharmacyName = Pharmacies[i], SupplyCount = medcount
                    };
                    supplies.Add(medSupply);
                    demandStock = demandStock - medcount;
                }
                medSupply = new PharmacyMedicineSupply()
                {
                    MedicineName = med, PharmacyName = Pharmacies[totalPharmacies - 1], SupplyCount = demandStock
                };
                supplies.Add(medSupply);
            }
            else
            {
                int medcount = inStock / totalPharmacies;
                for (int i = 0; i < totalPharmacies - 1; i++)
                {
                    medSupply = new PharmacyMedicineSupply()
                    {
                        MedicineName = med, PharmacyName = Pharmacies[i], SupplyCount = medcount
                    };
                    supplies.Add(medSupply);
                    inStock = inStock - medcount;
                }
                medSupply = new PharmacyMedicineSupply()
                {
                    MedicineName = med, PharmacyName = Pharmacies[totalPharmacies - 1], SupplyCount = inStock
                };
                supplies.Add(medSupply);
            }

            return(supplies);
        }