Ejemplo n.º 1
0
        public ActionResult Add(GoodsModel model)
        {
            // 判断传递的值是否为空
            if (model == null)
            {
                // 是空 则直接返回
                return(RedirectToAction("Add", new { msg = "提交的数据为空,请重新提交" }));
            }

            // 构造 Goods 对象
            #region 构造 Goods 对象

            Goods goods = new Goods();
            goods.Id           = Guid.NewGuid();
            goods.IsDeleted    = false;
            goods.Name         = model.Name;
            goods.Price        = model.Price;
            goods.Code         = "Goods_" + TimeManager.GetCurrentTimestamp();
            goods.CreatedTime  = DateTime.Now.Date;
            goods.Description  = model.Description;
            goods.ServiceCount = model.ServiceCount;
            goods.DeletedTime  = DateTime.MinValue.AddHours(8);

            // 商品新家字段  OriginalPrice  原价

            // 该字段不影响原来商品添加的逻辑
            goods.OriginalPrice = model.OriginalPrice;


            #endregion

            // 添加新的Goods 对象

            if (_goodsBLL.Add(goods))
            {
                #region 此处应该用事务来做 但是现在简易版就随意一点 将来这里必须用事务

                #endregion

                // 添加成功后 构造 goodsimage 对象
                // 并添加到数据库

                LogHelper.Log.Write("添加商品成功,现在正在构造goodsImage");

                GoodsImage gi = new GoodsImage();
                gi.Id          = Guid.NewGuid();
                gi.ImagePath   = model.PicturePath;
                gi.CreatedTime = DateTime.Now.Date;
                gi.GoodsId     = goods.Id;
                gi.IsDeleted   = false;
                gi.DeletedTime = DateTime.MinValue.AddHours(8);

                _goodsImagesBLL.Add(gi);

                LogHelper.Log.Write("添加商品图片成功");

                // 添加成功 则 遍历传递过来的分类列表
                // 并添加到数据库
                foreach (var item in model.CategoryList)
                {
                    if (item == null)
                    {
                        LogHelper.Log.Write("获取商品分类列表失败");
                    }
                    // 构造 GoodsCategory 对象
                    GoodsCategory gc = new GoodsCategory();
                    gc.Id          = Guid.NewGuid();
                    gc.CreatedTime = DateTime.Now.Date;
                    gc.CategoryId  = _categoryBLL.GetCategoryByCategoryNo(item).Id;
                    gc.IsDeleted   = false;
                    gc.GoodsId     = goods.Id;
                    gc.DeletedTime = DateTime.MinValue.AddHours(8);

                    _goodsCategoryBLL.Add(gc);
                }

                Thread.Sleep(2000);

                return(RedirectToAction("Add", new { msg = "添加成功" }));
            }
            else
            {
                return(RedirectToAction("Add", new { msg = "添加失败" }));
            }
        }