public async Task <ActionResult> Create( [Bind(Include = "Id,KeyWord,MatchType,KeyWordContentType,AllowEventKey,ContentId")] WeiChat_KeyWordAutoReply weiChat_KeyWordAutoReplay) { if (ModelState.IsValid) { if ( db.WeiChat_KeyWordAutoReplies.Any( p => (p.TenantId == TenantId) && (p.KeyWord == weiChat_KeyWordAutoReplay.KeyWord))) { ModelState.AddModelError("KeyWord", "关键字重复,请重新输入!"); return(View(weiChat_KeyWordAutoReplay)); } switch (weiChat_KeyWordAutoReplay.KeyWordContentType) { case KeyWordContentTypes.Text: break; case KeyWordContentTypes.Image: { var mediaId = Request.Form["mediaId"]; if (string.IsNullOrEmpty(mediaId)) { ModelState.AddModelError("", "您没有选择资源,请选择资源!"); return(View(weiChat_KeyWordAutoReplay)); } var image = new WeiChat_KeyWordImageContent { ImageMediaId = mediaId }; SetModelWithSaveChanges(image, default(Guid)); weiChat_KeyWordAutoReplay.ContentId = image.Id; } break; case KeyWordContentTypes.Voice: { var mediaId = Request.Form["mediaId"]; if (string.IsNullOrEmpty(mediaId)) { ModelState.AddModelError("", "您没有选择资源,请选择资源!"); return(View(weiChat_KeyWordAutoReplay)); } var model = new WeiChat_KeyWordVoiceContent { VoiceMediaId = mediaId }; SetModelWithSaveChanges(model, default(Guid)); weiChat_KeyWordAutoReplay.ContentId = model.Id; break; } case KeyWordContentTypes.Video: { var mediaId = Request.Form["mediaId"]; if (string.IsNullOrEmpty(mediaId)) { ModelState.AddModelError("", "您没有选择资源,请选择资源!"); return(View(weiChat_KeyWordAutoReplay)); } var model = new WeiChat_KeyWordVideoContent { MediaId = mediaId }; SetModelWithSaveChanges(model, default(Guid)); weiChat_KeyWordAutoReplay.ContentId = model.Id; break; } case KeyWordContentTypes.News: break; default: break; } SetModel(weiChat_KeyWordAutoReplay, default(Guid)); db.WeiChat_KeyWordAutoReplies.Add(weiChat_KeyWordAutoReplay); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(weiChat_KeyWordAutoReplay)); }
public ActionResult Index(WeiChat_NotAnswerReply model) { if (!ModelState.IsValid) { return(View(model)); } var subscribe = db.WeiChat_NotAnswerReplies.Find(model.Id); var isAdd = subscribe == null; Guid?oldContentId = null; if (!isAdd) { oldContentId = subscribe.ContentId; } if ((model.ContentId == null) && (model.KeyWordContentType != KeyWordContentTypes.CustomerService)) { ModelState.AddModelError("", "请选择或者填写内容!"); return(View(model)); } switch (model.KeyWordContentType) { case KeyWordContentTypes.Text: break; case KeyWordContentTypes.Image: { var mediaId = Request.Form["MediaId"]; if (string.IsNullOrEmpty(mediaId)) { ModelState.AddModelError("", "您没有选择资源,请选择资源!"); return(View(model)); } if (!isAdd) { var removeItem = db.WeiChat_KeyWordImageContents.Find(oldContentId); if (removeItem != null) { db.WeiChat_KeyWordImageContents.Remove(removeItem); } } var image = new WeiChat_KeyWordImageContent { ImageMediaId = mediaId }; SetModelWithChangeStates(image, default(Guid)); model.ContentId = image.Id; } break; case KeyWordContentTypes.Voice: { var mediaId = Request.Form["mediaId"]; if (string.IsNullOrEmpty(mediaId)) { ModelState.AddModelError("", "您没有选择资源,请选择资源!"); return(View(model)); } if (!isAdd) { var removeItem = db.WeiChat_KeyWordVoiceContents.Find(oldContentId); if (removeItem != null) { db.WeiChat_KeyWordVoiceContents.Remove(removeItem); } } var voice = new WeiChat_KeyWordVoiceContent { VoiceMediaId = mediaId }; SetModelWithChangeStates(voice, default(Guid)); model.ContentId = voice.Id; break; } case KeyWordContentTypes.Video: { var mediaId = Request.Form["mediaId"]; if (string.IsNullOrEmpty(mediaId)) { ModelState.AddModelError("", "您没有选择资源,请选择资源!"); return(View(model)); } if (!isAdd) { var removeItem = db.WeiChat_KeyWordVideoContents.Find(oldContentId); if (removeItem != null) { db.WeiChat_KeyWordVideoContents.Remove(removeItem); } } var video = new WeiChat_KeyWordVideoContent { MediaId = mediaId }; SetModelWithChangeStates(video, default(Guid)); model.ContentId = video.Id; break; } case KeyWordContentTypes.News: break; default: break; } if (subscribe != null) { subscribe.ContentId = model.ContentId; subscribe.KeyWordContentType = model.KeyWordContentType; subscribe.UpdateBy = UserId; subscribe.UpdateTime = DateTime.Now; db.SaveChanges(); } else { SetModelWithSaveChanges(model, isAdd ? default(Guid) : model.Id); } return(View(model)); }
public async Task <ActionResult> Edit( [Bind(Include = "Id,KeyWord,AllowEventKey,MatchType,KeyWordContentType,ContentId,AppId")] WeiChat_KeyWordAutoReply weiChat_KeyWordAutoReply) { if (ModelState.IsValid) { SetModel(weiChat_KeyWordAutoReply, weiChat_KeyWordAutoReply.Id); var oldValues = db.Entry(weiChat_KeyWordAutoReply).GetDatabaseValues(); if ((oldValues.GetValue <string>("KeyWord") != weiChat_KeyWordAutoReply.KeyWord) && db.WeiChat_KeyWordAutoReplies.Any( p => (p.TenantId == TenantId) && (p.KeyWord == weiChat_KeyWordAutoReply.KeyWord))) { ModelState.AddModelError("KeyWord", "关键字重复,请重新输入!"); return(View(weiChat_KeyWordAutoReply)); } var oldContentId = oldValues.GetValue <Guid?>("ContentId"); var oldType = oldValues.GetValue <KeyWordContentTypes>("KeyWordContentType"); #region 更新回复内容 switch (weiChat_KeyWordAutoReply.KeyWordContentType) { case KeyWordContentTypes.Text: break; case KeyWordContentTypes.Image: { var mediaId = Request.Form["mediaId"]; if (string.IsNullOrEmpty(mediaId)) { ModelState.AddModelError("", "您没有选择资源,请选择资源!"); return(View(weiChat_KeyWordAutoReply)); } var image = new WeiChat_KeyWordImageContent { ImageMediaId = mediaId }; SetModelWithSaveChanges(image, default(Guid)); weiChat_KeyWordAutoReply.ContentId = image.Id; } break; case KeyWordContentTypes.Voice: { var mediaId = Request.Form["mediaId"]; if (string.IsNullOrEmpty(mediaId)) { ModelState.AddModelError("", "您没有选择资源,请选择资源!"); return(View(weiChat_KeyWordAutoReply)); } var model = new WeiChat_KeyWordVoiceContent { VoiceMediaId = mediaId }; SetModelWithSaveChanges(model, default(Guid)); weiChat_KeyWordAutoReply.ContentId = model.Id; break; } case KeyWordContentTypes.Video: { var mediaId = Request.Form["mediaId"]; if (string.IsNullOrEmpty(mediaId)) { ModelState.AddModelError("", "您没有选择资源,请选择资源!"); return(View(weiChat_KeyWordAutoReply)); } var model = new WeiChat_KeyWordVideoContent { MediaId = mediaId }; SetModelWithSaveChanges(model, default(Guid)); weiChat_KeyWordAutoReply.ContentId = model.Id; break; } case KeyWordContentTypes.News: break; default: break; } #endregion #region 除之前的资源 if (oldContentId != null) { switch (oldType) { case KeyWordContentTypes.Text: break; case KeyWordContentTypes.Image: { var oldContent = db.WeiChat_KeyWordImageContents.Find(oldContentId); if (oldContent != null) { db.WeiChat_KeyWordImageContents.Remove(oldContent); } } break; case KeyWordContentTypes.Voice: { var oldContent = db.WeiChat_KeyWordVoiceContents.Find(oldContentId); if (oldContent != null) { db.WeiChat_KeyWordVoiceContents.Remove(oldContent); } } break; case KeyWordContentTypes.Video: { var oldContent = db.WeiChat_KeyWordVideoContents.Find(oldContentId); if (oldContent != null) { db.WeiChat_KeyWordVideoContents.Remove(oldContent); } } break; case KeyWordContentTypes.News: break; case KeyWordContentTypes.CustomerService: break; default: break; } } #endregion db.Entry(weiChat_KeyWordAutoReply).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(weiChat_KeyWordAutoReply)); }