diff --git a/Bootstrap.Admin/Web.config b/Bootstrap.Admin/Web.config index 6a0a906a..40d6c547 100644 --- a/Bootstrap.Admin/Web.config +++ b/Bootstrap.Admin/Web.config @@ -45,6 +45,8 @@ + + diff --git a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index 012c46ac..ee1f8b8b 100644 --- a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -68,6 +68,8 @@ + + diff --git a/Bootstrap.DataAccess/Message.cs b/Bootstrap.DataAccess/Message.cs new file mode 100644 index 00000000..36e29994 --- /dev/null +++ b/Bootstrap.DataAccess/Message.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Bootstrap.DataAccess +{ + /// + /// 消息表实体 + /// author:liuchun + /// date:2016.11.12 + /// + public class Message + { + /// + /// 消息主键 数据库自增 + /// + public int ID { get; set; } + /// + /// 标题 + /// + public string Title { get; set; } + /// + /// 内容 + /// + public string Content { get; set; } + /// + /// 发消息人 + /// + public string From { get; set; } + /// + /// 收消息人 + /// + public string To { get; set; } + /// + /// 消息发送时间 + /// + public DateTime SendTime { get; set; } + /// + /// 消息状态:0-未读,1-已读,2-删除 + /// 和Dict表的通知消息关联 + /// + public string Status { get; set; } + + /// + /// 获得/设置 时间描述 2分钟内为刚刚 + /// + public string Period { get; set; } + } +} diff --git a/Bootstrap.DataAccess/MessageHelper.cs b/Bootstrap.DataAccess/MessageHelper.cs new file mode 100644 index 00000000..16b7f594 --- /dev/null +++ b/Bootstrap.DataAccess/MessageHelper.cs @@ -0,0 +1,124 @@ +using Longbow; +using Longbow.Caching; +using Longbow.Caching.Configuration; +using Longbow.ExceptionManagement; +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using System.Linq; + +namespace Bootstrap.DataAccess +{ + + + public class MessageHelper + { + internal const string RetrieveMessageFromDataKey = "MessageHelper-RetrieveMessagesFromOthers"; + internal const string RetrieveMessageToDataKey = "MessageHelper-RetrieveMessagesToOthers"; + + /// + /// 获取其他人发送给自己的消息列表 + /// + /// + /// + public static IEnumerable RetrieveMessagesFromOthers(string userName) + { + var messageRet = CacheManager.GetOrAdd(RetrieveMessageFromDataKey, CacheSection.RetrieveIntervalByKey(RetrieveMessageFromDataKey), key => + { + string sql = "select * from [Messages] where [To]=@UserName"; + List messages = new List(); + DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); + try + { + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", userName, ParameterDirection.Input)); + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + { + while (reader.Read()) + { + messages.Add(new Message() + { + ID = (int)reader[0], + Title = (string)reader[1], + Content = (string)reader[2], + From = (string)reader[3], + To = (string)reader[4], + SendTime = LgbConvert.ReadValue(reader[5], DateTime.MinValue), + Status = (string)reader[6] + + }); + } + } + } + catch (Exception ex) { ExceptionManager.Publish(ex); } + return messages; + + }, CacheSection.RetrieveDescByKey(RetrieveMessageFromDataKey)); + + return messageRet.OrderByDescending(n => n.SendTime); + } + + /// + /// 自己获取发送给其他人的消息列表 + /// + /// + /// + public static IEnumerable RetrieveMessagesToOthers(string userName) + { + var messageRet = CacheManager.GetOrAdd(RetrieveMessageToDataKey, CacheSection.RetrieveIntervalByKey(RetrieveMessageToDataKey), key => + { + string sql = "select * from [Messages] where [From]=@UserName"; + List messages = new List(); + DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); + try + { + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", userName, ParameterDirection.Input)); + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + { + while (reader.Read()) + { + messages.Add(new Message() + { + ID = (int)reader[0], + Title = (string)reader[1], + Content = (string)reader[2], + From = (string)reader[3], + To = (string)reader[4], + SendTime = LgbConvert.ReadValue(reader[5], DateTime.MinValue), + Status = (string)reader[6] + + }); + } + } + } + catch (Exception ex) { ExceptionManager.Publish(ex); } + return messages; + + }, CacheSection.RetrieveDescByKey(RetrieveMessageToDataKey)); + + return messageRet.OrderByDescending(n => n.SendTime); + } + + /// + /// 获取Header处显示的消息列表 + /// + /// + /// + public static IEnumerable RetrieveMessagesHeader(string userName) + { + + var messageRet = RetrieveMessagesFromOthers(userName); + + messageRet.AsParallel().ForAll(n => + { + var ts = DateTime.Now - n.SendTime; + if (ts.TotalMinutes < 5) n.Period = "刚刚"; + else if (ts.Days > 0) n.Period = string.Format("{0}天", ts.Days); + else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours); + else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes); + }); + return messageRet; + } + + } +}