public void InsertProductPriceHistory(ProductPriceHistoryContract[] d) { lock (MDB) { try { foreach (var x in d) { var productPrice = new DB.ProductPriceHistory(); x.ProductPriceHistory.CopyTo(productPrice, false); MDB.ProductPriceHistories.InsertOnSubmit(productPrice); } MDB.SubmitChanges(); } catch (Exception ex) { HandleMyException(ex); } } }
public void InsertProduct(Product_Location_Price_Contract[] d) { lock (MDB) { try { foreach (var x in d) { var productId = x.Product.ProductId; DB.Product product; if (productId != default(int)) { product = MDB.Products.FirstOrDefault(p => p.ProductId == productId); } else { var locationId = x.Location.LocationId; if (locationId == default(int)) { throw new Xxception("LocationId is not specified"); } //All owners of this location var owners = MDB.OwnerVsLocations.Where(o => o.LocationId == locationId).Select(o => o.OwnerId); //All locations that owners may own var locationIds = MDB.OwnerVsLocations.Where(o => owners.Contains(o.OwnerId)).Select(o => o.LocationId).ToArray(); //Only reuse products from other locations of those owners product = MDB.Products.FirstOrDefault(p => p.Description == x.Product.Description && ( (!p.ProductVsLocations.Any() || p.ProductVsLocations.Any(l => locationIds.Contains(l.LocationId))) && (!p.ProductPriceHistories.Any() || p.ProductPriceHistories.OrderByDescending(r => r.ChangeDate).First().Price == x.Price) ) ); if (product == null) { product = new DB.Product(); x.Product.CopyTo(product, false); MDB.Products.InsertOnSubmit(product); } } var prodVsLoc = new DB.ProductVsLocation() { Product = product, LocationId = x.Location.LocationId }; MDB.ProductVsLocations.InsertOnSubmit(prodVsLoc); var price = new DB.ProductPriceHistory() { Product = product, ChangeDate = DateTime.UtcNow, Price = x.Price }; MDB.ProductPriceHistories.InsertOnSubmit(price); } MDB.SubmitChanges(); } catch (Exception ex) { HandleMyException(ex); } } }