public void AddProductsToHomePage(long shopId, PlatformType platformType, IEnumerable <long> productIds) { var productService = ServiceProvider.Instance <IProductService> .Create; var existHomepageProductIds = Context.MobileHomeProductsInfo.Where(item => item.ShopId == shopId && item.PlatFormType == platformType).Select(item => item.ProductId); //获取当前店铺已添加的首页商品Id var notExistProductIds = productIds.Where(item => !existHomepageProductIds.Contains(item)); //从待添加的商品中去除已添加的商品 foreach (var productId in notExistProductIds) { var product = productService.GetProduct(productId); if (!product.IsDeleted) { if (shopId != 0 && product.ShopId != shopId)//店铺添加首页商品时,判断该商品是否为该店铺的商品 { throw new Himall.Core.HimallException("待添加至首页的商品不得包含非本店铺商品"); } var mobileHomepageProduct = new MobileHomeProductsInfo() { PlatFormType = platformType, Sequence = 1, ProductId = productId, ShopId = shopId }; Context.MobileHomeProductsInfo.Add(mobileHomepageProduct); } } var delProductIds = existHomepageProductIds.Where(e => !productIds.Contains(e)); if (delProductIds.Count() > 0) { var delProduct = Context.MobileHomeProductsInfo.Where(item => item.ShopId == shopId && item.PlatFormType == platformType && delProductIds.Contains(item.ProductId)); Context.MobileHomeProductsInfo.RemoveRange(delProduct); } Context.SaveChanges(); }
public void UpdateSequence(long shopId, long id, short sequence) { MobileHomeProductsInfo mobileHomeProductsInfo = context.MobileHomeProductsInfo.FirstOrDefault((MobileHomeProductsInfo item) => item.Id == id && item.ShopId == shopId); if (mobileHomeProductsInfo == null) { throw new HimallException(string.Format("不存在Id为{0}的首页产品设置", id)); } mobileHomeProductsInfo.Sequence = sequence; context.SaveChanges(); }
public void AddProductsToHomePage(long shopId, PlatformType platformType, IEnumerable <long> productIds) { IProductService create = Instance <IProductService> .Create; IQueryable <long> mobileHomeProductsInfo = from item in context.MobileHomeProductsInfo where item.ShopId == shopId && (int)item.PlatFormType == (int)platformType select item.ProductId; IEnumerable <long> nums = from item in productIds where !mobileHomeProductsInfo.Contains(item) select item; foreach (long num in nums) { ProductInfo product = create.GetProduct(num); if (shopId != 0 && product.ShopId != shopId) { throw new HimallException("待添加至首页的产品不得包含非本供应商产品"); } MobileHomeProductsInfo mobileHomeProductsInfo1 = new MobileHomeProductsInfo() { PlatFormType = platformType, Sequence = 1, ProductId = num, ShopId = shopId }; context.MobileHomeProductsInfo.Add(mobileHomeProductsInfo1); } IQueryable <long> nums1 = from e in mobileHomeProductsInfo where !productIds.Contains(e) select e; if (nums1.Count() > 0) { IQueryable <MobileHomeProductsInfo> mobileHomeProductsInfos = from item in context.MobileHomeProductsInfo where item.ShopId == shopId && (int)item.PlatFormType == (int)platformType && nums1.Contains(item.ProductId) select item; context.MobileHomeProductsInfo.RemoveRange(mobileHomeProductsInfos); } context.SaveChanges(); }