private static bool EnsureProductContext(SUPPLY_MIN_PRICE_MX product) { bool shouldSyncProduct = false; using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var colorEntity = db.Set<ProductPropertyValueEntity>().Where(ppv => ppv.ChannelValueId == product.PRO_COLOR_SID) .Join(db.Set<ProductPropertyEntity>() .Join(db.Set<ProductMapEntity>().Where(pm => pm.ChannelPId == product.PRODUCT_SID), o => o.ProductId, i => i.ProductId, (o, i) => o) ,o => o.PropertyId, i => i.Id, (o, i) => o).FirstOrDefault(); var sizeEntity = db.Set<ProductPropertyValueEntity>().Where(ppv => ppv.ChannelValueId == product.PRO_STAN_SID) .Join(db.Set<ProductPropertyEntity>() .Join(db.Set<ProductMapEntity>().Where(pm => pm.ChannelPId == product.PRODUCT_SID) , o => o.ProductId, i => i.ProductId, (o, i) => o), o => o.PropertyId, i => i.Id, (o, i) => o).FirstOrDefault(); if (colorEntity == null || sizeEntity == null) shouldSyncProduct = true; if (shouldSyncProduct) return ProductPropertySyncJob.SyncOne(product.PRODUCT_SID, product.PRO_STAN_SID ?? 0, product.PRO_STAN_NAME, product.PRO_COLOR_SID ?? 0, product.PRO_COLOR); } return true; }
public static void SyncOne(SUPPLY_MIN_PRICE_MX product) { if (!EnsureProductContext(product)) return; using (var ts = new TransactionScope()) { using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var existProduct = db.Set<ProductEntity>().Join(db.Set<ProductMapEntity>().Where(ep => ep.ChannelPId == product.PRODUCT_SID), o => o.Id, i => i.ProductId, (o, i) => o).FirstOrDefault(); var color = db.Set<ProductPropertyValueEntity>().Where(b => b.ChannelValueId == product.PRO_COLOR_SID) .Join(db.Set<ProductPropertyEntity>().Where(pp => pp.ProductId == existProduct.Id), o => o.PropertyId, i => i.Id, (o, i) => o).FirstOrDefault(); var size = db.Set<ProductPropertyValueEntity>().Where(b => b.ChannelValueId == product.PRO_STAN_SID) .Join(db.Set<ProductPropertyEntity>().Where(pp => pp.ProductId == existProduct.Id), o => o.PropertyId, i => i.Id, (o, i) => o).FirstOrDefault(); var inventory = db.Set<InventoryEntity>().Where(i => i.ChannelInventoryId == product.PRO_DETAIL_SID).FirstOrDefault(); int amount = (int)product.PRO_STOCK_SUM; bool no4sale = product.PRO_ACTIVE_BIT.GetValueOrDefault()==0; if (no4sale) amount = 0; if (inventory == null) { db.Inventories.Add(new InventoryEntity() { ProductId = existProduct.Id, PColorId = color.Id, PSizeId = size.Id, UpdateDate = DateTime.Now, UpdateUser = 0, Amount = amount, ChannelInventoryId = (int)product.PRO_DETAIL_SID }); } else { inventory.Amount = amount; } //update product.is4sale if ((existProduct.Is4Sale ?? false) == false && !no4sale) { existProduct.Is4Sale = true; existProduct.UpdatedDate = DateTime.Now; } db.SaveChanges(); } ts.Complete(); } }