///// <summary> ///// Gets the best supplier's name, part number, and price breaks, ///// from the JSON result string, based on supplier affinity. ///// </summary> ///// <remarks> ///// For our current purposes of costing the board in US dollars, only suppliers ///// who have price lists in US dollars are considered qualified. ///// ///// To find the best supplier for this part, first we create two dictionaries: ///// one that maps all qualified suppliers to their quantity in stock, and one ///// that maps all qualified suppliers to their US-dollar price list. ///// ///// Then, the best supplier is the first supplier we find on the affinity list ///// who is also in our dictionary of qualified suppliers. ///// ///// If none of the affinity suppliers make the cut, then the best is considered ///// to be the qualified supplier with the maximum number of these parts in stock. ///// </remarks> ///// <param name="supplierName">The best supplier's name</param> ///// <param name="supplierPartNumber">The best supplier's part number</param> ///// <param name="supplierPriceBreaks">The best supplier's price breaks, in US dollars</param> ///// <param name="OctopartResult">The input JSON results string</param> ///// <param name="SourceAffinity">A list of preferred suppliers, with the most favored first.</param> //public static void GetSupplierNameAndPartNumberAndPriceBreaks( // out string supplierName, // out string supplierPartNumber, // out List<KeyValuePair<int, float>> supplierPriceBreaks, // String OctopartResult, // List<String> SourceAffinity = null) //{ // supplierName = ""; // supplierPartNumber = ""; // supplierPriceBreaks = new List<KeyValuePair<int, float>>(); // dynamic dynJson = JsonConvert.DeserializeObject(OctopartResult); // bool done = false; // Dictionary<string, string> mapSupplierToPartNumber = new Dictionary<string, string>(); // Dictionary<string, int> mapSupplierToQtyInStock = new Dictionary<string, int>(); // // Create a mapping of suppliers to part numbers and qualified in-stock quantities. // // For now, only suppliers that have a price list in US dollars are considered qualified. // if ((dynJson != null) && // (dynJson.offers != null)) // { // foreach (var item in dynJson.offers) // { // string partNumber = item.sku; // string supplier = item.seller.name; // int qtyInStock = item.in_stock_quantity; // if ((item.prices != null) && (item.prices.USD != null)) // { // // Only choose suppliers who have price breaks in US dollars. // mapSupplierToPartNumber.Add(supplier, partNumber); // mapSupplierToQtyInStock.Add(supplier, qtyInStock); // } // } // } // // Check if any of our favorite sources are listed // if (null != SourceAffinity) // { // foreach (string favSource in SourceAffinity) // { // if (mapSupplierToPartNumber.ContainsKey(favSource)) // { // // We found a favorite source... // supplierName = favSource; // supplierPartNumber = mapSupplierToPartNumber[favSource]; // done = true; // so, we are done. // break; // } // } // } // if (!done) // { // // No favorite source was found, so find a supplier with maximum stock available. // int maxAvailable = -1; // foreach (KeyValuePair<string, int> entry in mapSupplierToQtyInStock) // { // int qtyInStock = entry.Value; // if (qtyInStock > maxAvailable) // { // maxAvailable = qtyInStock; // supplierName = entry.Key; // supplierPartNumber = mapSupplierToPartNumber[supplierName]; // } // } // } // // Find the price breaks for this supplier. // if( supplierName.Length > 0 ) // { // // Find our chosen seller's offer. // foreach (var item in dynJson.offers) // { // if( item.seller.name == supplierName ) // { // // We found the seller's offer // if ((item.prices != null) && (item.prices.USD != null)) // { // // only take price lists in US dollars for now. // foreach (var pair in item.prices.USD) // { // int qty = pair[0]; // float price = float.Parse((string)pair[1]); // KeyValuePair<int, float> pricePoint = new KeyValuePair<int, float>(qty, price); // supplierPriceBreaks.Add(pricePoint); // } // break; // } // } // } // } //} /// <summary> /// Fill in the sellermapStructure from the Octopart results string. /// </summary> /// <param name="OctopartResult">The Octopart results string to parse.</param> /// <returns>the filled-in sellermapStructure</returns> public static SellerMapStruct GetSellerMapStructure( String OctopartResult) { var sellermapStructure = new SellerMapStruct(); sellermapStructure.sellerMap = new Dictionary<string,SkuMapStruct>(); dynamic dynJson = JsonConvert.DeserializeObject(OctopartResult); if ((dynJson != null) && (dynJson.offers != null)) { foreach (var offer in dynJson.offers) { // get the sku string sku = ""; if (offer.sku != null) { sku = offer.sku; } // get the seller name string sellerName = ""; if ((offer.seller != null) && (offer.seller.name != null)) { sellerName = offer.seller.name; } // Check if the seller isn't already in the seller map if (!sellermapStructure.sellerMap.ContainsKey(sellerName)) { // We need to add it with an empty SKU map structure. SkuMapStruct emptySkuMapStruct = new SkuMapStruct(); sellermapStructure.sellerMap.Add(sellerName, emptySkuMapStruct); } // Check if the SKU is in the SKU map structure. if (!sellermapStructure.sellerMap[sellerName].skuMap.ContainsKey(sku)) { CurrencyMapStruct emptyCurrencyMapStruct = new CurrencyMapStruct(); sellermapStructure.sellerMap[sellerName].skuMap.Add(sku, emptyCurrencyMapStruct); } // Cerate an alias to avoid a few levels of indirection CurrencyMapStruct thisCurrencyMapStruct = sellermapStructure.sellerMap[sellerName].skuMap[sku]; // get the prices if (offer.prices != null) { foreach (var currency in offer.prices) { string currencyCode = ""; // Get the currency code currencyCode = currency.Name; List<PricePoint> priceBreaks = new List<PricePoint>(); foreach (var pricePair in currency.Value) { int qty = pricePair[0]; float price = float.Parse((string)pricePair[1]); PricePoint pricePoint = new PricePoint(qty,price); priceBreaks.Add(pricePoint); } // Add the price breaks to the currency map thisCurrencyMapStruct.currencyMap[currencyCode] = priceBreaks; } } } } return sellermapStructure; }
///// <summary> ///// Gets the best supplier's name, part number, and price breaks, ///// from the JSON result string, based on supplier affinity. ///// </summary> ///// <remarks> ///// For our current purposes of costing the board in US dollars, only suppliers ///// who have price lists in US dollars are considered qualified. ///// ///// To find the best supplier for this part, first we create two dictionaries: ///// one that maps all qualified suppliers to their quantity in stock, and one ///// that maps all qualified suppliers to their US-dollar price list. ///// ///// Then, the best supplier is the first supplier we find on the affinity list ///// who is also in our dictionary of qualified suppliers. ///// ///// If none of the affinity suppliers make the cut, then the best is considered ///// to be the qualified supplier with the maximum number of these parts in stock. ///// </remarks> ///// <param name="supplierName">The best supplier's name</param> ///// <param name="supplierPartNumber">The best supplier's part number</param> ///// <param name="supplierPriceBreaks">The best supplier's price breaks, in US dollars</param> ///// <param name="OctopartResult">The input JSON results string</param> ///// <param name="SourceAffinity">A list of preferred suppliers, with the most favored first.</param> //public static void GetSupplierNameAndPartNumberAndPriceBreaks( // out string supplierName, // out string supplierPartNumber, // out List<KeyValuePair<int, float>> supplierPriceBreaks, // String OctopartResult, // List<String> SourceAffinity = null) //{ // supplierName = ""; // supplierPartNumber = ""; // supplierPriceBreaks = new List<KeyValuePair<int, float>>(); // dynamic dynJson = JsonConvert.DeserializeObject(OctopartResult); // bool done = false; // Dictionary<string, string> mapSupplierToPartNumber = new Dictionary<string, string>(); // Dictionary<string, int> mapSupplierToQtyInStock = new Dictionary<string, int>(); // // Create a mapping of suppliers to part numbers and qualified in-stock quantities. // // For now, only suppliers that have a price list in US dollars are considered qualified. // if ((dynJson != null) && // (dynJson.offers != null)) // { // foreach (var item in dynJson.offers) // { // string partNumber = item.sku; // string supplier = item.seller.name; // int qtyInStock = item.in_stock_quantity; // if ((item.prices != null) && (item.prices.USD != null)) // { // // Only choose suppliers who have price breaks in US dollars. // mapSupplierToPartNumber.Add(supplier, partNumber); // mapSupplierToQtyInStock.Add(supplier, qtyInStock); // } // } // } // // Check if any of our favorite sources are listed // if (null != SourceAffinity) // { // foreach (string favSource in SourceAffinity) // { // if (mapSupplierToPartNumber.ContainsKey(favSource)) // { // // We found a favorite source... // supplierName = favSource; // supplierPartNumber = mapSupplierToPartNumber[favSource]; // done = true; // so, we are done. // break; // } // } // } // if (!done) // { // // No favorite source was found, so find a supplier with maximum stock available. // int maxAvailable = -1; // foreach (KeyValuePair<string, int> entry in mapSupplierToQtyInStock) // { // int qtyInStock = entry.Value; // if (qtyInStock > maxAvailable) // { // maxAvailable = qtyInStock; // supplierName = entry.Key; // supplierPartNumber = mapSupplierToPartNumber[supplierName]; // } // } // } // // Find the price breaks for this supplier. // if( supplierName.Length > 0 ) // { // // Find our chosen seller's offer. // foreach (var item in dynJson.offers) // { // if( item.seller.name == supplierName ) // { // // We found the seller's offer // if ((item.prices != null) && (item.prices.USD != null)) // { // // only take price lists in US dollars for now. // foreach (var pair in item.prices.USD) // { // int qty = pair[0]; // float price = float.Parse((string)pair[1]); // KeyValuePair<int, float> pricePoint = new KeyValuePair<int, float>(qty, price); // supplierPriceBreaks.Add(pricePoint); // } // break; // } // } // } // } //} /// <summary> /// Fill in the sellermapStructure from the Octopart results string. /// </summary> /// <param name="OctopartResult">The Octopart results string to parse.</param> /// <returns>the filled-in sellermapStructure</returns> public static SellerMapStruct GetSellerMapStructure(String OctopartResult) { var sellermapStructure = new SellerMapStruct(); sellermapStructure.sellerMap = new Dictionary <string, SkuMapStruct>(); dynamic dynJson = JsonConvert.DeserializeObject(OctopartResult); if ((dynJson != null) && (dynJson.offers != null)) { foreach (var offer in dynJson.offers) { // get the sku string sku = ""; if (offer.sku != null) { sku = offer.sku; } // get the seller name string sellerName = ""; if ((offer.seller != null) && (offer.seller.name != null)) { sellerName = offer.seller.name; } // Check if the seller isn't already in the seller map if (!sellermapStructure.sellerMap.ContainsKey(sellerName)) { // We need to add it with an empty SKU map structure. SkuMapStruct emptySkuMapStruct = new SkuMapStruct(); sellermapStructure.sellerMap.Add(sellerName, emptySkuMapStruct); } // Check if the SKU is in the SKU map structure. if (!sellermapStructure.sellerMap[sellerName].skuMap.ContainsKey(sku)) { CurrencyMapStruct emptyCurrencyMapStruct = new CurrencyMapStruct(); sellermapStructure.sellerMap[sellerName].skuMap.Add(sku, emptyCurrencyMapStruct); } // Cerate an alias to avoid a few levels of indirection CurrencyMapStruct thisCurrencyMapStruct = sellermapStructure.sellerMap[sellerName].skuMap[sku]; // get the prices if (offer.prices != null) { foreach (var currency in offer.prices) { string currencyCode = ""; // Get the currency code currencyCode = currency.Name; List <PricePoint> priceBreaks = new List <PricePoint>(); foreach (var pricePair in currency.Value) { int qty = pricePair[0]; float price = float.Parse((string)pricePair[1], CultureInfo.InvariantCulture); PricePoint pricePoint = new PricePoint(qty, price); priceBreaks.Add(pricePoint); } // Add the price breaks to the currency map thisCurrencyMapStruct.currencyMap[currencyCode] = priceBreaks; } } } } return(sellermapStructure); }