public IActionResult EditPresent(EditViewModel model) { if (model.PresentId != 0) { _giftService.RemoveGift(model.PresentId); } else { try { _giftService.AddGift(model.Name, model.Description, model.Price, model.Image, model.Link, model.ProviderId); } catch (Exception) { ViewData["error"] = "Gaven kunne ikke tilføjes"; } } PrepPresView(model); return(View(model)); }
public JsonResult Edit(GiftViewModel model) { var result = new AjaxReturnData { success = false, msg = "未知错误" }; if (ModelState.IsValid) { GiftViewModel postdata = new GiftViewModel(); if (model.Id > 0) { GiftInfo dbdata = _iGiftService.GetByIdAsNoTracking(model.Id); //数据补充 if (dbdata == null) { result.success = false; result.msg = "编号有误"; return(Json(result)); } postdata = Mapper.Map <GiftInfo, GiftViewModel>(dbdata); } else { if (model.StockQuantity < 1) { result.success = false; result.msg = "库存必须大于0"; return(Json(result)); } } UpdateModel(postdata); GiftInfo data = new GiftInfo(); data = Mapper.Map <GiftViewModel, GiftInfo>(postdata); if (model.Id > 0) { _iGiftService.UpdateGift(data); } else { data.Sequence = 100; data.AddDate = DateTime.Now; data.SalesStatus = GiftInfo.GiftSalesStatus.Normal; _iGiftService.AddGift(data); } #region 转移图片 int index = 1; List <string> piclist = new List <string>(); piclist.Add(model.PicUrl1); piclist.Add(model.PicUrl2); piclist.Add(model.PicUrl3); piclist.Add(model.PicUrl4); piclist.Add(model.PicUrl5); string path = data.ImagePath; foreach (var item in piclist) { if (!string.IsNullOrWhiteSpace(item)) { string source = string.Empty; if (item.IndexOf("temp/") > 0) { source = item.Substring(item.LastIndexOf("/temp")); } else if (item.Contains(data.ImagePath)) { source = item.Substring(item.LastIndexOf(data.ImagePath)); } try { string dest = string.Format("{0}/{1}.png", path, index); if (source == dest) { index++; continue; } if (!string.IsNullOrWhiteSpace(source)) { Core.HimallIO.CopyFile(source, dest, true); } var imageSizes = EnumHelper.ToDictionary <ImageSize>().Select(t => t.Key); foreach (var imageSize in imageSizes) { string size = string.Format("{0}/{1}_{2}.png", path, index, imageSize); Core.HimallIO.CreateThumbnail(dest, size, imageSize, imageSize); } //using (Image image = Image.FromFile(source)) //{ // image.Save(dest, System.Drawing.Imaging.ImageFormat.Png); // var imageSizes = EnumHelper.ToDictionary<GiftInfo.ImageSize>().Select(t => t.Key); // foreach (var imageSize in imageSizes) // { // string size = string.Format("{0}/{1}_{2}.png", path, index, imageSize); // ImageHelper.CreateThumbnail(dest, size, imageSize, imageSize); // } //} index++; } catch (FileNotFoundException fex) { index++; Core.Log.Error("发布礼品时候,没有找到文件", fex); } catch (System.Runtime.InteropServices.ExternalException eex) { index++; Core.Log.Error("发布礼品时候,ExternalException异常", eex); } catch (Exception ex) { index++; Core.Log.Error("发布礼品时候,Exception异常", ex); } } else { string dest = string.Format("{0}/{1}.png", path, index); if (HimallIO.ExistFile(dest)) { HimallIO.DeleteFile(dest); } var imageSizes = EnumHelper.ToDictionary <ImageSize>().Select(t => t.Key); foreach (var imageSize in imageSizes) { string size = string.Format("{0}/{1}_{2}.png", path, index, imageSize); if (HimallIO.ExistFile(size)) { HimallIO.DeleteFile(size); } } index++; } } #endregion result.success = true; result.msg = "操作成功"; } else { result.success = false; result.msg = "数据有误"; } return(Json(result)); }
public JsonResult Edit(GiftViewModel model) { AjaxReturnData ajaxReturnDatum = new AjaxReturnData() { success = false, msg = "未知错误" }; AjaxReturnData ajaxReturnDatum1 = ajaxReturnDatum; if (!base.ModelState.IsValid) { ajaxReturnDatum1.success = false; ajaxReturnDatum1.msg = "数据有误"; } else { GiftViewModel giftViewModel = new GiftViewModel(); if (model.Id > 0) { GiftInfo byIdAsNoTracking = giftser.GetByIdAsNoTracking(model.Id); if (byIdAsNoTracking == null) { ajaxReturnDatum1.success = false; ajaxReturnDatum1.msg = "编号有误"; return(Json(ajaxReturnDatum1)); } giftViewModel = Mapper.Map <GiftInfo, GiftViewModel>(byIdAsNoTracking); } else if (model.StockQuantity < 1) { ajaxReturnDatum1.success = false; ajaxReturnDatum1.msg = "库存必须大于0"; return(Json(ajaxReturnDatum1)); } base.UpdateModel <GiftViewModel>(giftViewModel); GiftInfo giftInfo = new GiftInfo(); giftInfo = Mapper.Map <GiftViewModel, GiftInfo>(giftViewModel); if (model.Id <= 0) { giftInfo.Sequence = 100; giftInfo.AddDate = new DateTime?(DateTime.Now); giftInfo.SalesStatus = GiftInfo.GiftSalesStatus.Normal; giftser.AddGift(giftInfo); } else { giftser.UpdateGift(giftInfo); } int num = 1; List <string> strs = new List <string>() { model.PicUrl1, model.PicUrl2, model.PicUrl3, model.PicUrl4, model.PicUrl5 }; foreach (string str in strs) { string str1 = Server.MapPath(str); string str2 = Server.MapPath(giftInfo.ImagePath); if (string.IsNullOrWhiteSpace(str)) { string str3 = string.Format("{0}\\{1}.png", str2, num); if (System.IO.File.Exists(str3)) { System.IO.File.Delete(str3); } IEnumerable <int> dictionary = from t in EnumHelper.ToDictionary <ProductInfo.ImageSize>() select t.Key; foreach (int num1 in dictionary) { string str4 = string.Format("{0}/{1}_{2}.png", str2, num, num1); if (!System.IO.File.Exists(str4)) { continue; } System.IO.File.Delete(str4); } num++; } else { try { if (!Directory.Exists(str2)) { Directory.CreateDirectory(str2); } string str5 = string.Format("{0}\\{1}.png", str2, num); if (str1 != str5) { using (Image image = Image.FromFile(str1)) { image.Save(str5, ImageFormat.Png); IEnumerable <int> nums = from t in EnumHelper.ToDictionary <GiftInfo.ImageSize>() select t.Key; foreach (int num2 in nums) { string str6 = string.Format("{0}/{1}_{2}.png", str2, num, num2); ImageHelper.CreateThumbnail(str5, str6, num2, num2); } } num++; } else { num++; } } catch (FileNotFoundException fileNotFoundException1) { FileNotFoundException fileNotFoundException = fileNotFoundException1; num++; Log.Error("发布礼品时候,没有找到文件", fileNotFoundException); } catch (ExternalException externalException1) { ExternalException externalException = externalException1; num++; Log.Error("发布礼品时候,ExternalException异常", externalException); } catch (Exception exception1) { Exception exception = exception1; num++; Log.Error("发布礼品时候,Exception异常", exception); } } } ajaxReturnDatum1.success = true; ajaxReturnDatum1.msg = "操作成功"; } return(Json(ajaxReturnDatum1)); }