重构站内消息代码

This commit is contained in:
Argo-Lenovo 2016-11-12 20:52:48 +08:00
parent e81b21fd16
commit b870d525fc
4 changed files with 160 additions and 161 deletions

View File

@ -21,7 +21,9 @@ namespace Bootstrap.Admin.Models
var notis = NotificationHelper.RetrieveNotifications();
NotifiCount = notis.Count();
Notifications = notis.Take(6);
Messages = MessageHelper.RetrieveMessagesHeader(HttpContext.Current.User.Identity.Name);
var msgs = MessageHelper.RetrieveMessagesHeader(UserName);
MessageCount = msgs.Count();
Messages = msgs.Take(6);
}
public string UserName { get; protected set; }
/// <summary>
@ -53,9 +55,12 @@ namespace Bootstrap.Admin.Models
/// </summary>
public int NotifiCount { get; set; }
/// <summary>
/// 或者/设置消息列表
/// 获得/设置 消息列表
/// </summary>
public IEnumerable<Message> Messages { get; set; }
/// <summary>
/// 获得/设置 消息数量
/// </summary>
public int MessageCount { get; set; }
}
}

View File

@ -108,42 +108,42 @@
</li>
<!-- settings end -->
<!-- message dropdown start-->
@if (Model.Messages.Count() > 0)
{
<li id="header_inbox_bar" class="dropdown">
<a data-toggle="dropdown" class="dropdown-toggle" href="#">
<i class="fa fa-envelope-o"></i>
<span class="badge bg-important">@Model.Messages.Count()</span>
</a>
<ul class="dropdown-menu inbox">
<div class="notify-arrow notify-arrow-red"></div>
<li>
<p>您有 @Model.Messages.Count() 个未读的消息</p>
</li>
@foreach(var mess in Model.Messages.Take(6))
{
<li>
<a href="~/Admin/Messages/@mess.ID">
<span class="photo"><img alt="avatar" src="../content/images/logo6.jpg"></span>
<span class="subject">
<span class="from">@mess.From</span>
<span class="time">@mess.Period</span>
</span>
<span class="message">@mess.Title</span>
</a>
</li>
}
<li>
<a href="~/Admin/Messages">查看所有消息</a>
</li>
</ul>
</li>
}
@if (Model.MessageCount > 0)
{
<li class="dropdown">
<a data-toggle="dropdown" class="dropdown-toggle" href="#">
<i class="fa fa-envelope-o"></i>
<span class="badge bg-important">@Model.MessageCount</span>
</a>
<ul class="dropdown-menu inbox">
<div class="notify-arrow notify-arrow-red"></div>
<li>
<p>您有 @Model.MessageCount 个未读的消息</p>
</li>
@foreach (var mess in Model.Messages)
{
<li>
<a href="~/Admin/Messages?id=@mess.ID">
<span class="photo"><img alt="avatar" src="../content/images/logo6.jpg"></span>
<span class="subject">
<span class="from">@mess.From</span>
<span class="time">@mess.Period</span>
</span>
<span class="message">@mess.Title</span>
</a>
</li>
}
<li>
<a href="~/Admin/Messages">查看所有消息</a>
</li>
</ul>
</li>
}
<!-- message dropdown end -->
<!-- notification dropdown start-->
@if (Model.NotifiCount > 0)
{
<li id="header_notification_bar" class="dropdown">
<li class="dropdown">
<a data-toggle="dropdown" class="dropdown-toggle" href="#">
<i class="fa fa-bell-o"></i>
<span class="badge bg-warning">@Model.NotifiCount</span>

View File

@ -1,124 +1,118 @@
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";
/// <summary>
/// 获取其他人发送给自己的消息列表
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static IEnumerable<Message> RetrieveMessagesFromOthers(string userName)
{
var messageRet = CacheManager.GetOrAdd(RetrieveMessageFromDataKey, CacheSection.RetrieveIntervalByKey(RetrieveMessageFromDataKey), key =>
{
string sql = "select * from [Messages] where [To]=@UserName";
List<Message> messages = new List<Message>();
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);
}
/// <summary>
/// 自己获取发送给其他人的消息列表
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static IEnumerable<Message> RetrieveMessagesToOthers(string userName)
{
var messageRet = CacheManager.GetOrAdd(RetrieveMessageToDataKey, CacheSection.RetrieveIntervalByKey(RetrieveMessageToDataKey), key =>
{
string sql = "select * from [Messages] where [From]=@UserName";
List<Message> messages = new List<Message>();
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);
}
/// <summary>
/// 获取Header处显示的消息列表
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static IEnumerable<Message> 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;
}
}
}
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
{
/// <summary>
///
/// </summary>
public class MessageHelper
{
internal const string RetrieveMessageFromDataKey = "MessageHelper-RetrieveMessagesFromOthers";
internal const string RetrieveMessageToDataKey = "MessageHelper-RetrieveMessagesToOthers";
/// <summary>
/// 获取其他人发送给自己的消息列表
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static IEnumerable<Message> RetrieveMessagesFromOthers(string userName)
{
var messageRet = CacheManager.GetOrAdd(RetrieveMessageFromDataKey, CacheSection.RetrieveIntervalByKey(RetrieveMessageFromDataKey), key =>
{
string sql = "select * from [Messages] where [To]=@UserName";
List<Message> messages = new List<Message>();
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);
}
/// <summary>
/// 自己获取发送给其他人的消息列表
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static IEnumerable<Message> RetrieveMessagesToOthers(string userName)
{
var messageRet = CacheManager.GetOrAdd(RetrieveMessageToDataKey, CacheSection.RetrieveIntervalByKey(RetrieveMessageToDataKey), key =>
{
string sql = "select * from [Messages] where [From]=@UserName";
List<Message> messages = new List<Message>();
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);
}
/// <summary>
/// 获取Header处显示的消息列表
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static IEnumerable<Message> 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;
}
}
}

View File

@ -324,8 +324,8 @@ CREATE TABLE [dbo].[Messages](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Title] [nvarchar](50) NOT NULL,
[Content] [nvarchar](500) NOT NULL,
[From] [nvarchar](50) NOT NULL,
[To] [nvarchar](50) NOT NULL,
[From] [varchar](50) NOT NULL,
[To] [varchar](50) NOT NULL,
[SendTime] [datetime] NOT NULL,
[Status] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Messages] PRIMARY KEY CLUSTERED