Exemplo n.º 1
0
        /// <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);
            }
        }
Exemplo n.º 2
0
        /// <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);
                }
            }
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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();
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
 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();
     }
 }
Exemplo n.º 7
0
 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);
     }
 }
Exemplo n.º 8
0
        /// <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);
            }
        }
Exemplo n.º 9
0
        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);
                }
            }
        }
Exemplo n.º 10
0
        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);
            }
        }
Exemplo n.º 11
0
        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);
            }
        }
Exemplo n.º 12
0
        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);
            }
        }
Exemplo n.º 13
0
        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)
                {
                }
            }
        }
Exemplo n.º 14
0
        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);
            }
        }
Exemplo n.º 15
0
 public async Task <bool> SaveChanges()
 {
     return(_context.SaveChanges() >= 0);
 }