Exemplo n.º 1
0
        /// <summary>
        /// 检验商品库存
        /// </summary>
        /// <param name="cartList"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private bool CheckGoods(List <QiyeGoodsCart> cartList, ref string msg)
        {
            //判定是否存在失效商品
            QiyeGoods        good      = null;
            string           goodsId   = string.Join(",", cartList.Select(s => s.GoodsId).Distinct());
            List <QiyeGoods> goodslist = QiyeGoodsBLL.SingleModel.GetListByIds(goodsId);

            if (goodslist == null || goodslist.Count <= 0)
            {
                msg = "商品:找不到商品";
                return(false);
            }
            foreach (QiyeGoodsCart c in cartList)
            {
                good = goodslist.FirstOrDefault(f => f.Id == c.GoodsId);
                if (good == null)
                {
                    msg = "商品:购物车商品失效";
                    return(false);
                }

                if (c.State != 0)
                {
                    msg = "商品:购物车商品已失效";
                    return(false);
                }
                //检查当前商品库存是否足够
                if (good.StockLimit)
                {
                    int curGoodQty = 0;
                    if (string.IsNullOrWhiteSpace(c.SpecIds))
                    {
                        curGoodQty = good.Stock;
                    }
                    else
                    {
                        List <GoodsSpecDetail> goodList = good.GASDetailList.Where(x => x.Id.Equals(c.SpecIds)).ToList();
                        if (goodList == null || goodList.Count <= 0)
                        {
                            msg = $"商品: {good.Name} 库存不足!";
                            return(false);
                        }
                        curGoodQty = goodList[0].Stock;
                    }
                    if (curGoodQty < c.Count)
                    {
                        msg = $"商品: {good.Name} 库存不足,请重新选购!";
                        return(false);
                    }
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 修改商品库存
        /// </summary>
        /// <param name="cartList">购物车</param>
        /// <param name="userInfo"></param>
        /// <param name="tran"></param>
        /// <param name="msg"></param>
        /// <param name="getSql">是否获取sql</param>
        /// <param name="type">是否加库存</param>
        public void UpdateGoodsStock(List <QiyeGoodsCart> cartList, ref TransactionModel tran, ref string msg, bool getSql = false, bool type = false)
        {
            if (cartList == null || cartList.Count <= 0)
            {
                msg = "库存:购物车数据为空";
                return;
            }

            string goodsId = string.Join(",", cartList.Select(s => s.GoodsId));
            //根据订单内记录数量减库存,加销量
            List <QiyeGoods> goodsList = QiyeGoodsBLL.SingleModel.GetListByIds(goodsId);

            if (goodsList == null || goodsList.Count <= 0)
            {
                msg = "库存:商品已过期";
                return;
            }

            Utility.Easyui.EasyuiHelper <GoodsSpecDetail> goodDtlJsonHelper = new Utility.Easyui.EasyuiHelper <GoodsSpecDetail>();
            foreach (QiyeGoodsCart cartitem in cartList)
            {
                QiyeGoods goods = goodsList.FirstOrDefault(f => f.Id == cartitem.GoodsId);
                goods.SalesCount = type ? goods.SalesCount - cartitem.Count : goods.SalesCount + cartitem.Count;
                goods.SalesCount = goods.SalesCount < 0 ? 0 : goods.SalesCount;
                if (goods.StockLimit) //限制库存时才去操作库存
                {
                    if (string.IsNullOrWhiteSpace(cartitem.SpecIds))
                    {
                        goods.Stock = type ? goods.Stock + cartitem.Count : goods.Stock - cartitem.Count;
                    }
                    else
                    {
                        goods.Stock = type ? goods.Stock + cartitem.Count : goods.Stock - cartitem.Count;
                        List <GoodsSpecDetail> entGoodsAttrDtls = new List <GoodsSpecDetail>();
                        goods.GASDetailList.ForEach(y =>
                        {
                            if (y.Id.Equals(cartitem.SpecIds))
                            {
                                y.Stock = type ? y.Stock + cartitem.Count : y.Stock - cartitem.Count;
                            }
                            entGoodsAttrDtls.Add(y);
                        });
                        //规格库存详情重新赋值
                        goods.SpecDetail = goodDtlJsonHelper.SToJsonArray(entGoodsAttrDtls);
                    }
                }
                //更新商品库存
                tran.Add(QiyeGoodsBLL.SingleModel.BuildUpdateSql(goods, $"Stock,SpecDetail,SalesCount"));
            }

            //是否执行
            if (!getSql)
            {
                if (!ExecuteTransactionDataCorect(tran.sqlArray, tran.ParameterArray))
                {
                    msg = "库存:修改库存失败";
                }
            }

            return;
        }