public List <GoodsCategory> GetShowIndexCategories() { using (var dbContext = new MallDbContext()) { return(dbContext.GoodsCategories.Where(c => c.ShowIndex).OrderByDescending(c => c.Sort).ToList()); } }
public List <GoodsCategoryRelation> GetCategoryRelations(Guid goodsId) { using (var dbContext = new MallDbContext()) { return(dbContext.GoodsCategoryRelations.Include(m => m.GoodsCategory).Where(me => me.GoodsId == goodsId).ToList()); } }
public List <BrowseViewModel> LoadBrowseGoodsByPage(string memberId, int pageIndex, int pageSize, out int totalCount) { using (var dbContext = new MallDbContext()) { var query = (from m in dbContext.Markups join goods in dbContext.Goods on m.SourceId equals goods.Id where m.MemberId == memberId && m.ModuleKey.Equals(MallModule.Key, StringComparison.OrdinalIgnoreCase) && m.MarkupType == MarkupType.Browse && goods.Status != GoodsStatus.Delete select new { m.CreateTime, goods.Id, goods.Name, goods.ShopPrice, goods.Status }).OrderByDescending(x => x.CreateTime); totalCount = query.Count(); var data = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); var resData = data.Select(item => new BrowseViewModel() { Id = item.Id, Name = item.Name, ShopPrice = item.ShopPrice, Status = item.Status, BrowseTime = item.CreateTime }).ToList(); return(resData); } }
/// <summary> /// 分类下商品数量 /// </summary> /// <param name="categoryId"></param> /// <returns></returns> public int HasGoodsCount(Guid categoryId) { using (var dbContext = new MallDbContext()) { return(dbContext.Goods.Count(me => me.CategoryId == categoryId)); } }
/// <summary> /// 添加测试商品(仅测试使用) /// </summary> /// <param name="goods"></param> /// <returns></returns> public bool AddGoodsForTest(Goods goods) { using (var dbContext = new MallDbContext()) { goods.Id = KeyGenerator.GetGuidKey(); dbContext.Goods.Add(goods); //dbContext.SingleGoods.AddRange(goods.SingleGoods); foreach (var singleGoods in goods.SingleGoods) { singleGoods.Id = KeyGenerator.GetGuidKey(); dbContext.SingleGoods.Add(singleGoods); foreach (var attr in singleGoods.Attributes) { attr.Id = KeyGenerator.GetGuidKey(); dbContext.SingleGoodsAttributes.Add(attr); } //dbContext.SingleGoodsAttributes.AddRange(singleGoods.Attributes); } if (dbContext.SaveChanges() > 0) { return(true); } else { return(false); } } }
public Guid CreateCategory(GoodsCategory model) { bool result; using (var dbContext = new MallDbContext()) { model.Id = KeyGenerator.GetGuidKey(); if (model.ParentId == Guid.Empty) { model.MergerId = model.Id.ToString(); model.Level = 1; } else { var parentCategory = _currencyService.GetSingleById <GoodsCategory>(model.ParentId); model.MergerId = parentCategory.MergerId + "," + model.Id; model.Level = parentCategory.Level + 1; } dbContext.GoodsCategories.Add(model); result = dbContext.SaveChanges() > 0; } if (!result) { return(Guid.Empty); } Logger.Operation($"创建商品分类-{model.Name}:{model.Id}", MallModule.Instance, SecurityLevel.Normal); return(model.Id); }
/// <summary> /// 还原库存 /// </summary> /// <param name="order"></param> private void RestoreStock(Order order) { //还原库存 using (var dbContext = new MallDbContext()) { foreach (var orderGoods in order.OrderGoods) { var goods = dbContext.Goods.FirstOrDefault(g => g.Id.Equals(orderGoods.GoodsId)); if (goods != null) { goods.Stock += orderGoods.Quantity; dbContext.Entry(goods).State = System.Data.Entity.EntityState.Modified; } var singleGoods = dbContext.SingleGoods.FirstOrDefault(g => g.Id.Equals(orderGoods.SingleGoodsId)); if (singleGoods != null) { singleGoods.Stock += orderGoods.Quantity; dbContext.Entry(singleGoods).State = System.Data.Entity.EntityState.Modified; } } var changeCount = dbContext.SaveChanges(); Logger.Operation($"订单{order.OrderNo}取消发货,修改了{changeCount}个产品的库存", MallModule.Instance); } }
public List <GoodsCategory> GetCategories() { using (var dbContext = new MallDbContext()) { return(dbContext.GoodsCategories.OrderByDescending(me => me.Sort).ToList()); } }
/// <summary> /// 获取商品所包含的属性和属性值 /// </summary> /// <param name="goodsId"></param> /// <returns></returns> public List <GoodsAttiuteValues> GetGoodsAttributeValues(Guid goodsId) { using (var dbContext = new MallDbContext()) { var query = from gar in dbContext.SingleGoodsAttributes join gs in dbContext.SingleGoods on gar.SingleGoodsId equals gs.Id join ga in dbContext.GoodsAttributes on gar.AttributeId equals ga.Id where gs.GoodsId == goodsId group new { gar, ga } by new { gar.AttributeId, ga.Name } into g select new { AttributeId = g.Key.AttributeId, AttributeName = g.Key.Name, AttributeValues = g.Select(gar => gar.gar.AttributeValue) }; var data = query.OrderBy(a => a.AttributeId).ToList(); var resData = data.Select(item => new GoodsAttiuteValues() { AttributeId = item.AttributeId, AttributeName = item.AttributeName, AttributeValues = string.Join(",", item.AttributeValues.Distinct().ToList()) }).ToList(); return(resData); }; }
/// <summary> /// 根据条件获取所有商品信息 /// </summary> /// <param name="expression"></param> /// <returns></returns> public List <Goods> GetList(Expression <Func <Goods, bool> > expression) { using (var dbContex = new MallDbContext()) { var query = dbContex.Goods.Where(expression).ToList(); return(query); } }
public int InventoryWarning() { var systemConfig = _configService.Get <SystemConfig>(); using (var dbContext = new MallDbContext()) { return(dbContext.Goods.Count(o => o.Stock <= systemConfig.StockWarning && o.Status != Mall.Models.GoodsStatus.Delete)); } }
public void AfterChangePayStatus(Order order, PayStatus oldStatus) { var systemConfig = _configService.Get <SystemConfig>(); if (oldStatus != PayStatus.Paid && order.PayStatus == PayStatus.Paid) { //订单付款减库存 if (systemConfig.ReduceStock == Config.Models.ReduceStock.AfterPay) { ReduceStock(order); } //增加已付款计数 using (var dbContext = new MallDbContext()) { foreach (var orderGoods in order.OrderGoods) { var goods = dbContext.Goods.FirstOrDefault(g => g.Id.Equals(orderGoods.GoodsId)); if (goods != null) { goods.PaymentAmount += 1; dbContext.Entry(goods).State = System.Data.Entity.EntityState.Modified; } } var changeCount = dbContext.SaveChanges(); Logger.Operation($"订单{order.OrderNo}付款,修改了{changeCount}个产品的付款计数", MallModule.Instance); } } if (oldStatus == PayStatus.Paid && order.PayStatus == PayStatus.Unpaid) { //如果存在余额支付,退货余额,并设置订单余额支付为0,累计回未支付总额 if (order.BalancePay > 0) { using (TransactionScope scope = new TransactionScope()) { string error; _walletService.Deposit(order.MemberId, Wallet.Models.WalletType.Cash, order.BalancePay, $"订单{order.OrderNo}退款", out error); if (string.IsNullOrWhiteSpace(error)) { Logger.Operation($"订单{order.OrderNo}退款,用户{order.MemberId}获得{order.BalancePay}元退款", WalletModule.Instance); order.UnpayFee = order.PayFee; order.BalancePay = 0; if (_currencyService.Update(order)) { //提交 scope.Complete(); } } } } } }
/// <summary> /// 团购列表 /// </summary> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <param name="totalCount"></param> /// <returns></returns> public List <Goods> GetGroupGoods(int pageIndex, int pageSize, out int totalCount) { using (var dbContext = new MallDbContext()) { var query = dbContext.Goods.Where(me => me.IsGroupon && me.Status == GoodsStatus.InSale) .OrderByDescending(me => me.Sort).ThenByDescending(me => me.CreateTime); totalCount = query.Count(); return(query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()); } }
public List <Cart> GetList(string memberId) { using (var dbContext = new MallDbContext()) { var query = dbContext.Carts.Where(me => me.MemberId == memberId && me.Status > 0) .OrderByDescending(me => me.CreateTime); return(query.ToList()); } }
public Guid UpdateCategory(GoodsCategory model) { bool result; using (var dbContext = new MallDbContext()) { if (model.ParentId == Guid.Empty) { model.MergerId = model.Id.ToString(); model.Level = 1; } else { var parentCategory = _currencyService.GetSingleById <GoodsCategory>(model.ParentId); model.MergerId = parentCategory.MergerId + "," + model.Id; model.Level = parentCategory.Level + 1; } var oldCategory = _currencyService.GetSingleById <GoodsCategory>(model.Id); bool parentChange = oldCategory.ParentId != model.ParentId; bool levelChange = oldCategory.Level != model.Level; dbContext.Entry(model).State = EntityState.Modified; //父级变更 if (parentChange) { var idStr = model.Id.ToString(); var childCategories = dbContext.GoodsCategories.Where(x => x.MergerId.Contains(idStr) && x.Id != model.Id); foreach (var child in childCategories) { var mergerArr = child.MergerId.Split(new string[] { idStr }, StringSplitOptions.None); child.MergerId = model.MergerId + mergerArr[1]; //等级发生变更 if (levelChange) { child.Level = child.MergerId.Split(',').Length; } dbContext.Entry(child).State = EntityState.Modified; } } result = dbContext.SaveChanges() > 0; } if (!result) { return(Guid.Empty); } Logger.Operation($"更新商品分类-{model.Name}:{model.Id}", MallModule.Instance, SecurityLevel.Normal); return(model.Id); }
public void SetGoodsInvalidBySingleGoodsId(Guid singleGoodsId) { using (var dbContext = new MallDbContext()) { var cartList = dbContext.Carts.Where(me => me.SingleGoodsId.Equals(singleGoodsId) && me.Status > 0).ToList(); foreach (Cart info in cartList) { info.Status = CartStatus.Invalid; } dbContext.SaveChanges(); } }
public bool RestoreGoods(Guid goodsId) { using (var dbContext = new MallDbContext()) { var goods = dbContext.Goods.FirstOrDefault(g => g.Id.Equals(goodsId)); if (goods != null) { goods.Status = GoodsStatus.NotInSale; dbContext.SaveChanges(); return(true); } return(false); } }
public SingleGoods LoadFullSingleGoods(Guid singleGoodsId) { using (var dbContext = new MallDbContext()) { var singleGoods = dbContext.SingleGoods.Include(sg => sg.Goods).FirstOrDefault(g => g.Id.Equals(singleGoodsId)); if (singleGoods == null) { return(null); } singleGoods.Attributes = dbContext.SingleGoodsAttributes.Where(sga => sga.SingleGoodsId.Equals(singleGoods.Id)).ToList(); return(singleGoods); } }
public List <Evaluate.Models.Evaluate> GetNew9Evaluates(Guid goodsId) { using (var dbContext = new MallDbContext()) { var query = (from e in dbContext.Evaluates join s in dbContext.SingleGoods on e.ExtentsionId equals s.Id where e.SourceType.Equals("Order") && e.ModuleKey.Equals(OrderProcessModule.Key, StringComparison.OrdinalIgnoreCase) && s.GoodsId == goodsId select e).DistinctBy(x => x.MemberId).OrderByDescending(x => x.CreateTime); return(query.Take(9).ToList()); } }
public List <Models.Goods> LoadCollectGoodsByPage(string memberId, int pageIndex, int pageSize, out int totalCount) { using (var dbContext = new MallDbContext()) { var query = (from m in dbContext.Markups join goods in dbContext.Goods on m.SourceId equals goods.Id where m.MemberId == memberId && m.ModuleKey.Equals(MallModule.Key, StringComparison.OrdinalIgnoreCase) && m.MarkupType == MarkupType.Collect && goods.Status != GoodsStatus.Delete select goods).OrderByDescending(x => x.CreateTime); totalCount = query.Count(); return(query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()); } }
/// <summary> /// /// </summary> /// <param name="goodId"></param> /// <param name="status"></param> /// <returns></returns> public bool SetGoodsStatus(Guid goodId, GoodsStatus status) { using (var dbContext = new MallDbContext()) { var goods = dbContext.Goods.FirstOrDefault(me => me.Id == goodId); if (goods != null) { goods.Status = status; dbContext.SaveChanges(); Logger.Operation($"[{goods.Name}]设置状态为:{goods.Status.Description()}", MallModule.Instance, SecurityLevel.Normal); return(true); } return(false); } }
public List <Evaluate.Models.Evaluate> GetGoodsEvaluatesListByPage(Guid goodsId, int pageIndex, int pageSize, out int totalCount) { using (var dbContext = new MallDbContext()) { var query = (from e in dbContext.Evaluates join s in dbContext.SingleGoods on e.ExtentsionId equals s.Id where e.SourceType.Equals("Order") && e.ModuleKey.Equals(OrderProcessModule.Key, StringComparison.OrdinalIgnoreCase) && s.GoodsId == goodsId select e).OrderByDescending(x => x.CreateTime); totalCount = query.Count(); return(query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()); } }
public bool SaveGoods(Goods goods, bool insert = true) { using (var dbContext = new MallDbContext()) { if (insert) { dbContext.Goods.Add(goods); } else { dbContext.Goods.Attach(goods); dbContext.Entry(goods).State = EntityState.Modified; } var singleGoodsList = dbContext.SingleGoods.Where(sg => sg.GoodsId.Equals(goods.Id)).ToList(); foreach (var singleGoods in singleGoodsList) { var attributes = dbContext.SingleGoodsAttributes.Where(sga => sga.SingleGoodsId.Equals(singleGoods.Id)).ToList(); attributes.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted); } singleGoodsList.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted); dbContext.SingleGoods.AddRange(goods.SingleGoods); foreach (var singleGoods in goods.SingleGoods) { dbContext.SingleGoodsAttributes.AddRange(singleGoods.Attributes); } if (dbContext.SaveChanges() > 0) { //删除历史单品图片 _storageFileService.DeleteBySourceType(goods.Id.ToString()); //关联单品的图片 foreach (var singleGoods in goods.SingleGoods) { _storageFileService.AssociateFile(singleGoods.Id, MallModule.Instance.InnerKey, MallModule.Instance.InnerDisplayName, singleGoods.Image.Id, goods.Id.ToString()); } return(true); } else { return(false); } } }
public void AfterSubmitOrder(Order order) { var systemConfig = _configService.Get <SystemConfig>(); //下单减库存 if (systemConfig.ReduceStock == BntWeb.Config.Models.ReduceStock.AfterOrder) { ReduceStock(order); } //积分完全付款,减库存 if (systemConfig.ReduceStock == Config.Models.ReduceStock.AfterPay && order.PayStatus == PayStatus.Paid) { ReduceStock(order); } //订单付款减库存 已付款减库存 if (systemConfig.ReduceStock == Config.Models.ReduceStock.AfterDeliver && order.PayStatus == PayStatus.Paid) { ReduceStock(order); } //增加商品的销量 //如果订单已经付款,增加已付款计数 using (var dbContext = new MallDbContext()) { foreach (var orderGoods in order.OrderGoods) { var goods = dbContext.Goods.FirstOrDefault(g => g.Id.Equals(orderGoods.GoodsId)); if (goods != null) { goods.SalesVolume += orderGoods.Quantity; if (order.PayStatus == PayStatus.Paid) { goods.PaymentAmount += 1; } dbContext.Entry(goods).State = System.Data.Entity.EntityState.Modified; } } var changeCount = dbContext.SaveChanges(); Logger.Operation($"生成新订单{order.OrderNo},修改了{changeCount}个产品的销量统计", MallModule.Instance); } }
public List <GoodsCategory> LoadByPage(out int totalCount, int pageIndex = 1, int pageSize = 10) { Expression <Func <GoodsCategory, bool> > expression = c => (c.ParentId == null || c.ParentId == Guid.Empty); List <GoodsCategory> list; using (var dbContext = new MallDbContext()) { totalCount = dbContext.GoodsCategories.AsExpandable().Where(expression).Count(); list = dbContext.GoodsCategories.AsExpandable().Where(expression).OrderByDescending(c => c.Sort).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); } foreach (var item in list) { item.ChildCategories = GetAllChilds(item.Id); } return(list); }
public bool BatchRecoveryGoods(List <Guid> goodsIds) { using (var dbContext = new MallDbContext()) { var arrGoodsIds = goodsIds.ToArray(); var goodsList = dbContext.Goods.Where(g => arrGoodsIds.Contains(g.Id)).ToList(); if (goodsList.Any()) { foreach (var goods in goodsList) { goods.Status = GoodsStatus.Delete; } dbContext.SaveChanges(); return(true); } return(false); } }
public bool BatchDeleteGoods(List <Guid> goodsIds) { using (var dbContext = new MallDbContext()) { var arrGoodsIds = goodsIds.ToArray(); var goodsList = dbContext.Goods.Where(g => arrGoodsIds.Contains(g.Id)).ToList(); if (goodsList.Any()) { foreach (var goods in goodsList) { goods.SingleGoods = dbContext.SingleGoods.Where(sg => sg.GoodsId.Equals(goods.Id)).ToList(); foreach (var singleGoods in goods.SingleGoods) { //单品属性 var attributes = dbContext.SingleGoodsAttributes.Where(sga => sga.SingleGoodsId.Equals(singleGoods.Id)).ToList(); attributes.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted); } //单品信息 goods.SingleGoods.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted); //商品分类关联关系 var categories = dbContext.GoodsCategoryRelations.Where(sga => sga.GoodsId.Equals(goods.Id)).ToList(); categories.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted); //购物车 var carts = dbContext.Carts.Where(me => me.GoodsId.Equals(goods.Id)).ToList(); carts.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted); //商品相关标记 var mrkupsList = dbContext.Markups.Where(me => me.SourceId.Equals(goods.Id) && me.ModuleKey == MallModule.Key).ToList(); mrkupsList.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted); //商品主信息 dbContext.Entry(goods).State = EntityState.Deleted; //商品图片 _storageFileService.DisassociateFile(goods.Id, MallModule.Key); } dbContext.SaveChanges(); return(true); } return(false); } }
public bool CheckSingleGoodsIsInvalid(Guid singleGoodsId, decimal originalPrice) { using (var dbContext = new MallDbContext()) { var singleGoods = dbContext.SingleGoods.Include(sg => sg.Goods).FirstOrDefault(g => g.Id.Equals(singleGoodsId)); if (singleGoods != null && singleGoods.Goods.Status == GoodsStatus.InSale && singleGoods.Stock > 0 ) { if (singleGoods.Goods.IsGroupon && singleGoods.Goods.GrouponStartTime <= DateTime.Now && singleGoods.Goods.GrouponEndTime >= DateTime.Now && singleGoods.GrouponPrice == originalPrice) { return(false); } else if (singleGoods.Price == originalPrice) { return(false); } } return(true); } }
public Goods LoadFullGoods(Guid goodsId) { using (var dbContext = new MallDbContext()) { var goods = dbContext.Goods.FirstOrDefault(g => g.Id.Equals(goodsId)); if (goods == null) { return(null); } goods.SingleGoods = dbContext.SingleGoods.Where(sg => sg.GoodsId.Equals(goodsId)).ToList(); foreach (var singleGoods in goods.SingleGoods) { //商品Id作为SourceType,在编辑保存的时候,根据这个值批量删除历史文件关联 singleGoods.Image = _storageFileService.GetFiles(singleGoods.Id, MallModule.Instance.InnerKey, goods.Id.ToString()).FirstOrDefault()?.Simplified() ?? _storageFileService.GetFiles(goodsId, MallModule.Key, "MainImage").FirstOrDefault()?.Simplified(); singleGoods.Attributes = dbContext.SingleGoodsAttributes.Where(sga => sga.SingleGoodsId.Equals(singleGoods.Id)).OrderBy(sga => sga.AttributeId).ToList(); } return(goods); } }
/// <summary> /// 取分类商品 /// </summary> /// <param name="categoryId"></param> /// <param name="keyword"></param> /// <param name="sortType"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <param name="totalCount"></param> /// <returns></returns> public List <Goods> GetGoodsByCategory(Guid?categoryId, string keyword, GoodsSortType sortType, int pageIndex, int pageSize, out int totalCount) { using (var dbContext = new MallDbContext()) { var query = dbContext.Goods.Where(me => me.Status > 0); if (categoryId != null && categoryId != Guid.Empty) { query = query.Where(me => me.CategoryId == categoryId); } if (!string.IsNullOrWhiteSpace(keyword)) { query = query.Where(me => me.Name.Contains(keyword)); } switch (sortType) { case GoodsSortType.Degault: query = query.OrderByDescending(me => me.Sort).ThenByDescending(me => me.CreateTime); break; case GoodsSortType.PriceLow: query = query.OrderBy(me => me.ShopPrice).ThenByDescending(me => me.Sort).ThenByDescending(me => me.CreateTime); break; case GoodsSortType.PriceHigh: query = query.OrderByDescending(me => me.ShopPrice).ThenByDescending(me => me.Sort).ThenByDescending(me => me.CreateTime); break; case GoodsSortType.SalesVolumeHigh: query = query.OrderByDescending(me => me.SalesVolume).ThenByDescending(me => me.Sort).ThenByDescending(me => me.CreateTime); break; default: query = query.OrderByDescending(me => me.Sort).ThenByDescending(me => me.CreateTime); break; } totalCount = query.Count(); return(query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()); } }