public ActionResult EditOnPost(GoodsViewModel postGoods) { var result = new DataJsonResult(); var category = _currencyService.GetSingleById <GoodsCategory>(postGoods.CategoryId); if (category.ParentId == Guid.Empty) { result.ErrorMessage = "一级分类下不能添加产品"; } else { postGoods.SingleGoods = postGoods.SingleGoodsJson.DeserializeJsonToList <SingleGoodsViewModel>(); var goods = _currencyService.GetSingleById <Goods>(postGoods.Id); var isNew = false; if (goods == null) { goods = new Goods { Id = postGoods.Id, CategoryId = postGoods.CategoryId, BrandId = postGoods.BrandId, Name = postGoods.Name, OriginalPrice = postGoods.OriginalPrice, GoodsType = postGoods.GoodsType, GoodsNo = postGoods.GoodsNo, Description = postGoods.Description, Status = GoodsStatus.NotInSale, CreateTime = DateTime.Now, SingleGoods = new List <SingleGoods>(), FreeShipping = postGoods.FreeShipping, IsHot = postGoods.IsHot, IsNew = postGoods.IsNew, IsBest = postGoods.IsBest, UsualWeight = postGoods.UsualWeight, IsPresell = postGoods.IsPresell, Brief = postGoods.Brief, Sort = postGoods.Sort }; goods.LastUpdateTime = goods.CreateTime; isNew = true; } else { goods.CategoryId = postGoods.CategoryId; goods.BrandId = postGoods.BrandId; goods.Name = postGoods.Name; goods.OriginalPrice = postGoods.OriginalPrice; goods.GoodsNo = postGoods.GoodsNo; goods.GoodsType = postGoods.GoodsType; goods.Description = postGoods.Description; goods.SingleGoods = new List <SingleGoods>(); goods.LastUpdateTime = DateTime.Now; goods.FreeShipping = postGoods.FreeShipping; goods.IsHot = postGoods.IsHot; goods.IsNew = postGoods.IsNew; goods.IsBest = postGoods.IsBest; goods.UsualWeight = postGoods.UsualWeight; goods.IsPresell = postGoods.IsPresell; goods.Brief = postGoods.Brief; goods.Sort = postGoods.Sort; } foreach (var postSingleGoods in postGoods.SingleGoods) { if (postSingleGoods.Stock > 0) { var singleGoods = new SingleGoods { Id = postSingleGoods.Id, CreateTime = goods.CreateTime, Stock = postSingleGoods.Stock, Unit = postSingleGoods.Unit, Price = postSingleGoods.Price, GoodsId = goods.Id, Weight = postSingleGoods.Weight, GrouponPrice = postSingleGoods.GrouponPrice, Attributes = new List <SingleGoodsAttribute>(), }; foreach (var attr in postSingleGoods.Attrs) { singleGoods.Attributes.Add(new SingleGoodsAttribute { Id = KeyGenerator.GetGuidKey(), AttributeId = attr.Id, AttributeValue = attr.Val.Trim(), SingleGoodsId = singleGoods.Id }); } singleGoods.Image = new StorageFile { Id = postSingleGoods.Image?.Id ?? Guid.Empty }.Simplified(); goods.SingleGoods.Add(singleGoods); } } goods.ShopPrice = goods.SingleGoods.Min(g => g.Price); goods.Stock = goods.SingleGoods.Sum(g => g.Stock); goods.GrouponPrice = goods.SingleGoods.Min(g => g.GrouponPrice); result.Success = _goodsService.SaveGoods(goods, isNew); if (result.Success) { //扩展分类 _goodsCategoryService.DeleteRelations(goods.Id); if (!string.IsNullOrWhiteSpace(postGoods.ExtendCategoryIds)) { foreach (var cId in postGoods.ExtendCategoryIds.Split(',')) { var relateModel = new GoodsCategoryRelation() { GoodsId = goods.Id, CategoryId = cId.ToGuid() }; _goodsCategoryService.InsetCategoryRelation(relateModel); } } //佣金 _currencyService.DeleteByConditon <GoodsCommission>(c => c.GoodsId.Equals(goods.Id)); for (int i = 0; i < postGoods.Commission.Length; i++) { _currencyService.Create(new GoodsCommission { Id = KeyGenerator.GetGuidKey(), GoodsId = goods.Id, Level = i + 1, Value = postGoods.Commission[i] }); } } //处理主图图片关联 _storageFileService.ReplaceFile(goods.Id, MallModule.Key, MallModule.DisplayName, postGoods.MainImage, MainImage); } return(Json(result)); }
public bool InsetCategoryRelation(GoodsCategoryRelation model) { model.Id = KeyGenerator.GetGuidKey(); return(_currencyService.Create(model)); }