/// <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 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 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 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(); } } } } } }
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 void AfterChangeEvaluateStatus(Order order, EvaluateStatus oldEvaluateStatus) { using (var dbContext = new MallDbContext()) { if (oldEvaluateStatus == EvaluateStatus.NotEvaluated && order.EvaluateStatus == EvaluateStatus.Evaluated) { var query = _currencyService.GetList <OrderGoods>(x => x.OrderId == order.Id).GroupBy(x => x.GoodsId); foreach (IGrouping <Guid, OrderGoods> group in query) { var num = 0; foreach (OrderGoods orderGoods in group) { //判断商品是否已退款 var evaluates = _currencyService.Count <Evaluate.Models.Evaluate>(x => x.SourceId == orderGoods.Id); if (evaluates > 0) { //商品评价数+1 num++; } } if (num > 0) { var goods = dbContext.Goods.FirstOrDefault(x => x.Id == group.Key); if (goods != null) { var entry = dbContext.Entry(goods); entry.State = System.Data.Entity.EntityState.Unchanged; entry.Property(o => o.EvaluateCount).IsModified = true; goods.EvaluateCount = goods.EvaluateCount + num; } } } dbContext.SaveChanges(); } else if (oldEvaluateStatus == EvaluateStatus.Evaluated && order.EvaluateStatus == EvaluateStatus.Replied) { } } }
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 DeleteGoods(Guid goodsId) { var goods = LoadFullGoods(goodsId); if (goods == null) { return(true); } using (var dbContext = new MallDbContext()) { 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(goodsId)).ToList(); categories.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted); //购物车 var carts = dbContext.Carts.Where(me => me.GoodsId.Equals(goodsId)).ToList(); carts.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted); //商品相关标记 var mrkupsList = dbContext.Markups.Where(me => me.SourceId.Equals(goodsId) && me.ModuleKey == MallModule.Key).ToList(); mrkupsList.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted); //商品主信息 dbContext.Entry(goods).State = EntityState.Deleted; dbContext.SaveChanges(); //商品图片 _storageFileService.DisassociateFile(goodsId, MallModule.Key); return(true); } }