public ActionResult CreateProductCompetitorLedger(int?ProductCompetitorID, int ProductCompareSourceID, string InTaxPrice, string IncludeShippingCost, string Competitor, int ProductID, long Price, string Stock) { //create ProductCompetitorMapping first and use specified sourceID for creating ProductCompetitorPrice //use that ID for creating the Ledger using (var unit = GetUnitOfWork()) { try { var newMapping = new ProductCompetitorMapping() { ProductCompareSourceID = ProductCompareSourceID, InTaxPrice = InTaxPrice == "on" ? true : false, IncludeShippingCost = IncludeShippingCost == "on" ? true : false, Competitor = Competitor, ProductCompetitorID = ProductCompetitorID.HasValue ? ProductCompetitorID : null }; unit.Service <ProductCompetitorMapping>().Create(newMapping); var newCompetitorPrice = new ProductCompetitorPrice() { CreationTime = DateTime.Now.ToUniversalTime(), ProductCompetitorMapping = newMapping, ProductID = ProductID, Price = Price, Stock = Stock.ToString(), LastImport = DateTime.Now.ToUniversalTime() }; unit.Service <ProductCompetitorPrice>().Create(newCompetitorPrice); var newLedger = new ProductCompetitorLedger() { CreationTime = DateTime.Now.ToUniversalTime(), ProductCompetitorPrice = newCompetitorPrice, Stock = Stock.ToString(), Price = Price, CreatedBy = Concentrator.Objects.Web.Client.User.UserID }; unit.Service <ProductCompetitorLedger>().Create(newLedger); unit.Save(); return(Success("Sucessfully created ProductCompetitorLedger")); } catch (Exception ex) { return(Failure("Failed to create ProductCompetitorLedger", ex)); } } }
public SlurpResult Process() { log.InfoFormat("Start task for {0}", PluginType.Name); using (var unit = ServiceLocator.Current.GetInstance <IUnitOfWork>()) { var QueueItem = unit.Scope.Repository <SlurpQueue>().GetSingle(x => x.QueueID == QueueID); QueueItem.StartTime = DateTime.Now; ProductID = QueueItem.ProductID; SlurpResult result = new SlurpResult(QueueItem.Product.VendorItemNumber); try { var plugin = (ISlurpSite)Activator.CreateInstance(PluginType); result = plugin.Process(QueueItem.Product.VendorItemNumber); result.SiteName = PluginType.Name; #region Update Results if (result.ProductStatus == ProductStatus.Valid) { var mappingRepo = unit.Scope.Repository <ProductCompetitorMapping>(); var priceRepo = unit.Scope.Repository <ProductCompetitorPrice>(); var mappings = mappingRepo.GetAll(x => x.ProductCompareSourceID == QueueItem.ProductCompareSourceID).ToList(); var prices = priceRepo.GetAll(x => x.ProductCompetitorMapping.ProductCompareSourceID == QueueItem.ProductCompareSourceID).ToList(); foreach (var shop in result.Shops) { var mapping = mappings.SingleOrDefault(c => c.Competitor == shop.Name.Trim()); if (mapping == null) { //new mapping = new ProductCompetitorMapping() { Competitor = shop.Name.Trim(), ProductCompareSourceID = QueueItem.ProductCompareSourceID, ProductCompetitorID = null }; mappingRepo.Add(mapping); // context.SubmitChanges(); mappings.Add(mapping); } var newPrice = shop.TotalPrice ?? 0; var stockInfo = shop.Delivery.ToString(); //price insert ProductCompetitorPrice price = prices.SingleOrDefault(p => p.ProductCompetitorMappingID == mapping.ProductCompetitorMappingID && p.ProductID == QueueItem.ProductID); if (price == null) { price = new ProductCompetitorPrice() { ProductCompetitorMapping = mapping, ProductID = QueueItem.ProductID, Price = newPrice, Stock = stockInfo, CreationTime = DateTime.Now, LastImport = DateTime.Now.AddDays(-30) }; priceRepo.Add(price); } else { //ledger //check for diff if (price.Price != newPrice || price.Stock != stockInfo) { ProductCompetitorLedger ledger = new ProductCompetitorLedger() { Price = price.Price, Stock = price.Stock, ProductCompetitorPriceID = price.ProductCompetitorPriceID, CreationTime = DateTime.Now }; price.Price = newPrice; price.Stock = shop.Delivery.ToString(); unit.Scope.Repository <ProductCompetitorLedger>().Add(ledger); } } } CompleteItem(unit, QueueItem); //unit.Save(); } else { CompleteItem(unit, QueueItem); } #endregion log.DebugFormat("Sleeping for {0} seconds.", SleepTime); Thread.Sleep(SleepTime * 1000); } catch (Exception ex) { log.Error("Error during processing", ex); result = new SlurpResult(QueueItem.Product.VendorItemNumber); result.SiteName = PluginType.Name; CompleteItem(unit, QueueItem); } finally { WriteLog(result); } return(result); } }