2016-11-12 20:52:48 +08:00
using Longbow ;
2018-06-07 00:45:47 +08:00
using Longbow.Cache ;
2016-11-12 20:52:48 +08:00
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.Common ;
using System.Linq ;
namespace Bootstrap.DataAccess
{
/// <summary>
///
/// </summary>
2017-03-30 16:15:45 +08:00
public static class MessageHelper
2016-11-12 20:52:48 +08:00
{
2017-03-30 16:15:45 +08:00
private const string RetrieveMessageDataKey = "MessageHelper-RetrieveMessages" ;
2016-11-12 20:52:48 +08:00
/// <summary>
2016-11-14 09:42:59 +08:00
/// 所有有关userName所有消息列表
2016-11-12 20:52:48 +08:00
/// </summary>
2017-03-30 16:15:45 +08:00
/// <param name="userName"></param>
2016-11-12 20:52:48 +08:00
/// <returns></returns>
2017-03-30 16:15:45 +08:00
private static IEnumerable < Message > RetrieveMessages ( string userName )
2016-11-12 20:52:48 +08:00
{
2017-04-05 11:54:21 +08:00
var messageRet = CacheManager . GetOrAdd ( RetrieveMessageDataKey , key = >
2016-11-12 20:52:48 +08:00
{
2018-06-07 00:45:47 +08:00
string sql = "select m.*, d.Name, isnull(i.Code + u.Icon, '~/images/uploader/default.jpg'), u.DisplayName from [Messages] m left join Dicts d on m.Label = d.Code and d.Category = N'消息标签' and d.Define = 0 left join Dicts i on i.Category = N'头像地址' and i.Name = N'头像路径' and i.Define = 0 inner join Users u on m.[From] = u.UserName where [To] = @UserName or [From] = @UserName order by m.SendTime desc" ;
2016-11-12 20:52:48 +08:00
List < Message > messages = new List < Message > ( ) ;
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) ;
2018-09-08 19:57:42 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@UserName" , userName ) ) ;
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
2016-11-12 20:52:48 +08:00
{
2018-09-08 19:57:42 +08:00
while ( reader . Read ( ) )
2016-11-12 20:52:48 +08:00
{
2018-09-08 19:57:42 +08:00
messages . Add ( new Message ( )
2016-11-12 20:52:48 +08:00
{
2018-09-08 19:57:42 +08:00
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 ] ,
Mark = ( int ) reader [ 7 ] ,
IsDelete = ( int ) reader [ 8 ] ,
Label = ( string ) reader [ 9 ] ,
LabelName = LgbConvert . ReadValue ( reader [ 10 ] , string . Empty ) ,
FromIcon = ( string ) reader [ 11 ] ,
FromDisplayName = ( string ) reader [ 12 ]
} ) ;
2016-11-12 20:52:48 +08:00
}
}
return messages ;
2017-04-05 11:54:21 +08:00
} ) ;
2016-11-12 20:52:48 +08:00
return messageRet . OrderByDescending ( n = > n . SendTime ) ;
}
/// <summary>
2016-11-14 09:42:59 +08:00
/// 收件箱
/// </summary>
2017-03-30 16:15:45 +08:00
/// <param name="userName"></param>
2016-11-15 15:00:17 +08:00
2016-11-14 09:42:59 +08:00
public static IEnumerable < Message > Inbox ( string userName )
{
var messageRet = RetrieveMessages ( userName ) ;
2016-11-16 21:46:33 +08:00
return messageRet . Where ( n = > n . To . Equals ( userName , StringComparison . OrdinalIgnoreCase ) ) ;
2016-11-14 09:42:59 +08:00
}
/// <summary>
/// 发件箱
2016-11-12 20:52:48 +08:00
/// </summary>
2017-03-30 16:15:45 +08:00
/// <param name="userName"></param>
2016-11-12 20:52:48 +08:00
/// <returns></returns>
2016-11-14 09:42:59 +08:00
public static IEnumerable < Message > SendMail ( string userName )
2016-11-12 20:52:48 +08:00
{
2016-11-14 09:42:59 +08:00
var messageRet = RetrieveMessages ( userName ) ;
2016-11-16 21:46:33 +08:00
return messageRet . Where ( n = > n . From . Equals ( userName , StringComparison . OrdinalIgnoreCase ) ) ;
2016-11-14 09:42:59 +08:00
}
/// <summary>
/// 垃圾箱
/// </summary>
2017-03-30 16:15:45 +08:00
/// <param name="userName"></param>
2016-11-14 09:42:59 +08:00
/// <returns></returns>
public static IEnumerable < Message > Trash ( string userName )
{
var messageRet = RetrieveMessages ( userName ) ;
2016-11-16 21:46:33 +08:00
return messageRet . Where ( n = > n . IsDelete = = 1 ) ;
2016-11-14 09:42:59 +08:00
}
/// <summary>
/// 标旗
/// </summary>
2017-03-30 16:15:45 +08:00
/// <param name="userName"></param>
2016-11-14 09:42:59 +08:00
/// <returns></returns>
public static IEnumerable < Message > Mark ( string userName )
{
var messageRet = RetrieveMessages ( userName ) ;
2016-11-16 21:46:33 +08:00
return messageRet . Where ( n = > n . Mark = = 1 ) ;
2016-11-12 20:52:48 +08:00
}
/// <summary>
/// 获取Header处显示的消息列表
/// </summary>
2017-03-30 16:15:45 +08:00
/// <param name="userName"></param>
2016-11-12 20:52:48 +08:00
/// <returns></returns>
public static IEnumerable < Message > RetrieveMessagesHeader ( string userName )
{
2016-11-14 09:42:59 +08:00
var messageRet = Inbox ( userName ) ;
2016-11-12 20:52:48 +08:00
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 ;
}
}
}