public ActionResult AllMessage() { string id = CurrentUser.Id; List <Message> model = new List <Message>(); using (var db = new NoticeDataContext()) { var notices = db.Notice.Where(n => ((n.RecipientID == id || n.SponsorID == id) && (n.NoticeObject == "留言" || n.NoticeObject == "咨询"))); foreach (var no in notices) { var user = UserManager.FindById(no.SponsorID); model.Add(new Message { BasicUserInfo = new BasicUserInfo { UserID = user.Id, HeadImg = user.HeadPortrait, UserName = user.UserName }, Notice = no }); } return(View(new AllMessageViewModel { Cnt = notices.Count(), Messages = model.OrderBy(m => m.Notice.NoticeTime).AsEnumerable() })); } }
/// <summary> /// 添加留言通知 /// </summary> /// <param name="RecipientID">接收人ID</param> /// <param name="SponsorID">发起人ID</param> /// <param name="NoticeObject">主题(类别)</param> /// <param name="NoticeContent">内容</param> /// <param name="TreasureUID">物品UID(可选)</param> /// <returns></returns> public void AddNotice(string RecipientID, string SponsorID, string NoticeObject, string NoticeContent, string TreasureUID = null, string RelpyNoticeUID = null) { using (var db = new NoticeDataContext()) { db.Notice.InsertOnSubmit(new Domain.Db.Notice { IsRead = false, NoticeContent = NoticeContent, NoticeObject = NoticeObject, NoticeTime = DateTime.Now, NoticeUID = Guid.NewGuid(), RecipientID = RecipientID, SponsorID = SponsorID, TreasureUID = TreasureUID, RelpyNoticeUID = RelpyNoticeUID }); db.SubmitChanges(); } }
/// <summary> /// 我的通知 /// </summary> /// <returns></returns> public ActionResult Message(string NoticeUID = "") { if (!string.IsNullOrEmpty(NoticeUID)) { //获取通知对象 using (var db = new NoticeDataContext()) { var notice = db.Notice.Where(n => n.NoticeUID == Guid.Parse(NoticeUID)).FirstOrDefault(); //将当前通知设为已读 notice.IsRead = true; db.SubmitChanges(); if (notice != null) { //新建视图模型 MessageViewModel model = new MessageViewModel { BasicUserInfoMy = new BasicUserInfo { UserID = CurrentUser.Id, UserName = CurrentUser.UserName }, BasicUserInfoTa = new BasicUserInfo { UserID = UserManager.FindById(notice.SponsorID).Id, UserName = UserManager.FindById(notice.SponsorID).UserName }, CurrMessUID = notice.NoticeUID.ToString(), }; //新建历史消息集合 List <Message> messageLog = new List <Message>(); bool isR = true; //不为空, if (notice.RelpyNoticeUID != null) { do { //获取发起人 var Spon = UserManager.FindById(notice.SponsorID); if (Spon != null) { messageLog.Add(new Message { BasicUserInfo = new BasicUserInfo { UserID = Spon.Id, HeadImg = Spon.HeadPortrait, Email = Spon.Email, UserName = Spon.UserName }, Notice = notice }); } if (notice.RelpyNoticeUID == null) { break; } else { notice = db.Notice.Where(n => n.NoticeUID == Guid.Parse(notice.RelpyNoticeUID)).FirstOrDefault(); } } while (true); } else { do { //获取发起人 var Spon = UserManager.FindById(notice.SponsorID); if (Spon != null) { messageLog.Add(new Message { BasicUserInfo = new BasicUserInfo { UserID = Spon.Id, HeadImg = Spon.HeadPortrait, Email = Spon.Email, UserName = Spon.UserName }, Notice = notice }); } //检查当前有无回复目标 notice = db.Notice.Where(t => t.RelpyNoticeUID == notice.NoticeUID.ToString()).FirstOrDefault(); if (notice == null) { break; } } while (true); isR = false; } if (isR) { var notice2 = db.Notice.Where(n => n.NoticeUID == Guid.Parse(NoticeUID)).FirstOrDefault(); int cnt = 0; do { //获取发起人 var Spon = UserManager.FindById(notice2.SponsorID); if (Spon != null) { if (cnt != 0) { messageLog.Add(new Message { BasicUserInfo = new BasicUserInfo { UserID = Spon.Id, HeadImg = Spon.HeadPortrait, Email = Spon.Email, UserName = Spon.UserName }, Notice = notice2 }); } } //检查当前有无回复目标 notice2 = db.Notice.Where(t => t.RelpyNoticeUID == notice2.NoticeUID.ToString()).FirstOrDefault(); if (notice2 == null) { break; } cnt = 1; } while (true); } //添加数据 model.Messages = messageLog.OrderBy(m => m.Notice.NoticeTime).Distinct().AsEnumerable(); return(View(model)); } } } return(View("Error")); }
public PartialViewResult UserInfo() { //获取当前用户id string userid = CurrentUser.Id; //新建通知留言List List <NoticeTongZhi> tongZhis = new List <NoticeTongZhi>(); List <NoticeLiuYan> liuYans = new List <NoticeLiuYan>(); //获取信息 using (var db = new NoticeDataContext()) { var notices = db.Notice.Where(n => (n.RecipientID == userid && n.IsRead == false)).OrderByDescending(n => n.NoticeTime); //遍历所有的Notice foreach (var notice in notices) { //获取当前发起人的信息 var user = UserManager.FindById(notice.SponsorID); if (user != null) { //留言类 if (notice.NoticeObject == "留言" || notice.NoticeObject == "警告") { liuYans.Add(new NoticeLiuYan { BasicUserInfo = new BasicUserInfo { UserID = user.Id, HeadImg = user.HeadPortrait, UserName = user.UserName }, Content = notice.NoticeContent, isRead = notice.IsRead, Time = notice.NoticeTime, Id = notice.NoticeUID.ToString() }); } //其他通知类 else { //获取物品对象 var trea = repository.Treasures.Where(t => t.TreasureUID == Guid.Parse(notice.TreasureUID)).FirstOrDefault(); if (trea != null) { tongZhis.Add(new NoticeTongZhi { BasicUserInfo = new BasicUserInfo { UserID = user.Id, UserName = user.UserName, HeadImg = user.HeadPortrait }, Action = notice.NoticeObject, isRead = notice.IsRead, Time = notice.NoticeTime, Treasure = trea, Id = notice.NoticeUID.ToString() }); } } } } } return(PartialView(new UserHeadInfo { BasicUserInfo = new BasicUserInfo { HeadImg = CurrentUser.HeadPortrait, UserID = CurrentUser.Id, RealName = CurrentUser.RealName }, noticeLiuYans = liuYans, noticeTongZhis = tongZhis })); }