public string UpdateDishChefStar(dynamic requestData) { try { #region 修改菜品基本信息 DishChefStar model = JsonConvert.DeserializeObject <DishChefStar>(JsonConvert.SerializeObject(requestData.dishChefStar)); var IsExist = (from v in db.DishChefStar where v.ChefStarId == model.ChefStarId && v.DishType == model.DishType select v).FirstOrDefault(); var dishChefDishId = 0; //等于空就增加 if (IsExist == null) { if (model.DishStory.IndexOf("base64") > 0) { string d = model.DishStory; foreach (string a in imgHandel.GetHtmlImageUrlList(d)) { if (a.IndexOf(ImgHandle.DNS) < 0) { string[] asplit = a.Split(','); string imgtype = asplit[0].Substring(11, asplit[0].Length - 18); string filepath = imgHandel.Base64StringToImage(asplit[1], imgtype, "/pic/Dishtutor"); d = d.Replace(a, filepath); } } model.DishStory = d; } model.CreateTime = DateTime.Now; model.UpdateTime = DateTime.Now; model.CreatePerson = model.UpdatePerson; model.VisitCount = 0; model.PrasieCount = 0; model.ShareCount = 0; db.DishChefStar.Add(model); db.SaveChanges(); dishChefDishId = model.DishChefStarId; } else { IsExist.UpdateTime = DateTime.Now; IsExist.DishName = model.DishName; IsExist.DishStory = model.DishStory; string d = IsExist.DishStory; if (IsExist.DishStory.IndexOf("base64") > 0) { foreach (string a in imgHandel.GetHtmlImageUrlList(d)) { if (a.IndexOf(ImgHandle.DNS) < 0) { string[] asplit = a.Split(','); string imgtype = asplit[0].Substring(11, asplit[0].Length - 18); string filepath = imgHandel.Base64StringToImage(asplit[1], imgtype, "/pic/Dishtutor"); d = d.Replace(a, filepath); } } } IsExist.DishStory = d; IsExist.DishPicUrl = model.DishPicUrl; IsExist.UpdatePerson = model.UpdatePerson; model.DishChefStarId = 0; db.Entry <DishChefStar>(IsExist).State = EntityState.Modified; db.SaveChanges(); dishChefDishId = IsExist.DishChefStarId; } #endregion try { #region 修改菜品调料 var middledishMaterial = requestData.dishMaterial; //首先删除已存在菜品Id的调料 List <DishMaterial> qdishMaterial = new List <DishMaterial>(); for (int i = 0; i < middledishMaterial.Count; i++) { qdishMaterial.Add(JsonConvert.DeserializeObject <DishMaterial>(JsonConvert.SerializeObject(middledishMaterial[i]))); } var sql = string.Format(@"Delete DishMaterial where DishId={0}", dishChefDishId); dataContext.ExecuteNonQuery(CommandType.Text, sql); //保存调料信息 foreach (DishMaterial dm in qdishMaterial) { dm.DishId = dishChefDishId; db.DishMaterial.Add(dm); } db.SaveChanges(); #endregion #region 修改菜品步骤 var middleDishStep = requestData.dishStep; //先删除已有调料 List <DishStep> qDishStep = new List <DishStep>(); for (int i = 0; i < middleDishStep.Count; i++) { qDishStep.Add(JsonConvert.DeserializeObject <DishStep>(JsonConvert.SerializeObject(middleDishStep[i]))); } string sqlDishStep = string.Format(@"Delete DishStep where DishId={0}", dishChefDishId); dataContext.ExecuteNonQuery(CommandType.Text, sqlDishStep); //保存 int t = 1; foreach (DishStep i in qDishStep) { i.DishId = dishChefDishId; i.StepId = t; db.DishStep.Add(i); t++; } db.SaveChanges(); #endregion #region 修改推荐调料 var middleflavourRecRole = requestData.flavourRecRole; List <FlavourRecRole> modelflavourRecRole = new List <FlavourRecRole>(); for (int i = 0; i < middleflavourRecRole.Count; i++) { var test1 = JsonConvert.SerializeObject(middleflavourRecRole[i]); var test2 = JsonConvert.DeserializeObject <FlavourRecRole>(test1); modelflavourRecRole.Add(test2); } var sqlflavourRecRole = string.Format(@"Delete FlavourRecRole where RoleId={0} and DishId={1}", modelflavourRecRole[0].RoleId, dishChefDishId); dataContext.ExecuteNonQuery(CommandType.Text, sqlflavourRecRole); foreach (FlavourRecRole m in modelflavourRecRole) { m.DishId = dishChefDishId; m.CreateTime = DateTime.Now; m.UpdateTime = DateTime.Now; db.FlavourRecRole.Add(m); db.SaveChanges(); } #endregion } catch (Exception) { return("OK"); } return("OK"); } catch (Exception) { return("No"); } }
public string AddDishChefStar(dynamic requestData) { try { string addDishTutor = JsonConvert.SerializeObject(requestData); DishChefStar model = JsonConvert.DeserializeObject <DishChefStar>(addDishTutor); var IsExist = (from v in db.DishChefStar where v.ChefStarId == model.ChefStarId && v.DishType == model.DishType select v).FirstOrDefault(); //等于空就增加 if (IsExist == null) { if (model.DishStory.IndexOf("base64") > 0) { string d = model.DishStory; foreach (string a in imgHandel.GetHtmlImageUrlList(d)) { if (a.IndexOf(ImgHandle.DNS) < 0) { string[] asplit = a.Split(','); string imgtype = asplit[0].Substring(11, asplit[0].Length - 18); string filepath = imgHandel.Base64StringToImage(asplit[1], imgtype, "/pic/Dishtutor"); d = d.Replace(a, filepath); } } model.DishStory = d; } model.CreateTime = DateTime.Now; model.UpdateTime = DateTime.Now; model.VisitCount = 0; model.PrasieCount = 0; model.ShareCount = 0; db.DishChefStar.Add(model); db.SaveChanges(); return(model.DishChefStarId.ToString()); } //不为空就是修改 else { IsExist.UpdateTime = DateTime.Now; IsExist.DishName = model.DishName; IsExist.DishStory = model.DishStory; if (IsExist.DishStory.IndexOf("base64") > 0) { string d = IsExist.DishStory; foreach (string a in imgHandel.GetHtmlImageUrlList(d)) { if (a.IndexOf(ImgHandle.DNS) < 0) { string[] asplit = a.Split(','); string imgtype = asplit[0].Substring(11, asplit[0].Length - 18); string filepath = imgHandel.Base64StringToImage(asplit[1], imgtype, "/pic/Dishtutor"); d = d.Replace(a, filepath); } } IsExist.DishStory = d; } IsExist.DishPicUrl = model.DishPicUrl; IsExist.UpdatePerson = model.UpdatePerson; db.Entry <DishChefStar>(IsExist).State = EntityState.Modified; db.SaveChanges(); return(IsExist.DishChefStarId.ToString()); } } catch (Exception) { return("No"); } }