Ejemplo n.º 1
0
        /// <summary>
        /// 获取产品图片地址
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string GetProductImg(ShopProduct model, bool isBig = false)
        {
            var shopimg = DB.ShopProductImage.Where(a => a.ProductID == model.ID).ToList();

            if (shopimg.Count <= 0)
            {
                return("/assets/shop/images/position.jpg");
            }
            ShopProductImage first = shopimg.FirstOrDefault();

            if (isBig)
            {
                return(GetImg(first.URL));
            }
            return(GetImg(first.ThumURL));
        }
Ejemplo n.º 2
0
        public ActionResult Save(ShopProduct entity)
        {
            var json = new JsonHelp();
            //var shangpin = DB.ShopProductCategory.FindEntity(p => p.ID == entity.CategoryID1);
            var shop = DB.Shop.FindEntity(p => p.ID == 1);

            entity.UpdateTime = DateTime.Now;
            var imgs1 = Request["imgpath1"];
            var imgs2 = Request["imgpath2"];

            try
            {
                using (var db = new DbMallEntities())
                {
                    if (entity.ID == 0)
                    {
                        // entity.PriceCongXiao = 0;
                        //if (entity.CategoryID1 == ShopEnum.ShopType.订单商品.GetHashCode())
                        //{
                        //    entity.PriceScore = 0;
                        //    entity.PriceVip = 0;
                        //}
                        if (shop == null)
                        {
                            json.IsSuccess = false;
                            json.Msg       = "添加失败,您暂无商家权限";
                            return(Json(json));
                        }
                        else if (shop.IsCheck != true)
                        {
                            json.IsSuccess = false;
                            json.Msg       = "添加失败,您暂无商家权限";
                            return(Json(json));
                        }
                        //if (shangpin.Name == "优选区")
                        //{
                        //    if (entity.PriceShopping < 1)
                        //    {
                        //        json.IsSuccess = false;
                        //        json.Msg = "添加失败,现金价不准为0";
                        //        return Json(json);
                        //    }
                        //    if (entity.PV < 1)
                        //    {
                        //        json.IsSuccess = false;
                        //        json.Msg = "添加失败,积分价不准为0";
                        //        return Json(json);
                        //    }
                        //}
                        //else if (shangpin.Name == "消费区")
                        //{
                        //    if (entity.PriceShopping < 1)
                        //    {
                        //        json.IsSuccess = false;
                        //        json.Msg = "添加失败,现金价不准为0";
                        //        return Json(json);
                        //    }
                        //    if (entity.PV > 1)
                        //    {
                        //        json.IsSuccess = false;
                        //        json.Msg = "添加失败,积分不需要填写";
                        //        return Json(json);
                        //    }
                        //}
                        db.ShopProducts.Add(entity);
                        db.SaveChanges();
                        #region 添加商品相册图片
                        if (!string.IsNullOrEmpty(imgs1) && imgs1.EndsWith("&"))
                        {
                            var list1 = imgs1.Substring(0, imgs1.Length - 1).Split('&');
                            var list2 = imgs2.Substring(0, imgs2.Length - 1).Split('&');
                            if (list1.Length == list2.Length)
                            {
                                var xlist = new List <ShopProductImage>();
                                for (int i = 0; i < list1.Length; i++)
                                {
                                    var img = new ShopProductImage()
                                    {
                                        CreateTime = DateTime.Now,
                                        ProductID  = entity.ID,
                                        Sort       = i + 1,
                                        URL        = list1[i],
                                        ThumURL    = list2[i]
                                    };
                                    db.ShopProductImages.Add(img);
                                }
                                var first = db.ShopProducts.FirstOrDefault(a => a.ID == entity.ID);
                                first.Image = list1[0];
                            }
                            else
                            {
                                json.IsSuccess = false;
                                json.Msg       = "添加失败,请重试商品图片";
                                return(Json(json));
                            }
                        }
                        #endregion
                        json.Msg = "添加";
                    }
                    else
                    {
                        var first = db.ShopProducts.FirstOrDefault(a => a.ID == entity.ID);
                        WebTools.CopyToObject(entity, first);
                        //json.IsSuccess = DB.ShopProduct.Update(first);
                        #region 修改商品相册图片
                        //if (json.IsSuccess)
                        {
                            if (!string.IsNullOrEmpty(imgs1) && imgs1.EndsWith("&"))
                            {
                                var list1 = imgs1.Substring(0, imgs1.Length - 1).Split('&');
                                var list2 = imgs2.Substring(0, imgs2.Length - 1).Split('&');
                                if (list1.Length == list2.Length)
                                {
                                    var olds = db.ShopProductImages.Where(a => a.ProductID == entity.ID);
                                    #region 先删除多余的图片
                                    foreach (var item in olds)
                                    {
                                        var exsit = list1.Any(a => a == item.URL);
                                        if (exsit == false)
                                        {
                                            db.ShopProductImages.Remove(item);
                                        }
                                    }
                                    #endregion
                                    #region 再添加原来没有的
                                    for (int i = 0; i < list1.Length; i++)
                                    {
                                        var cururl = list1[i];
                                        var exsit  = olds.FirstOrDefault(a => a.URL == cururl);
                                        if (exsit == null)
                                        {
                                            var img = new ShopProductImage()
                                            {
                                                CreateTime = DateTime.Now,
                                                ProductID  = entity.ID,
                                                Sort       = i + 1,
                                                URL        = list1[i],
                                                ThumURL    = list2[i]
                                            };
                                            db.ShopProductImages.Add(img);
                                        }
                                    }
                                    #endregion
                                    if (list1.Length > 0)
                                    {
                                        first.Image = list1[0];
                                    }
                                }
                                else
                                {
                                    json.IsSuccess = false;
                                }
                            }
                        }
                        #endregion
                        json.Msg = "修改";
                    }
                    db.SaveChanges();
                    json.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                json.IsSuccess = false;
                LogHelper.Error("商品添加失败:" + WebTools.getFinalException(e));
            }
            if (json.IsSuccess)
            {
                json.ReUrl = ControllerPath + "/Index";   //注册成功就跳转到 激活页
                json.Msg  += "成功";
            }
            else
            {
                json.Msg += "失败";
            }
            return(Json(json));
        }