2019-01-11 23:20:28 +08:00
using System ;
2016-11-15 15:00:17 +08:00
using System.Collections.Generic ;
2018-10-21 10:02:59 +08:00
using System.Linq ;
2016-11-15 15:00:17 +08:00
namespace Bootstrap.DataAccess
{
/// <summary>
2017-03-30 16:15:45 +08:00
///
2016-11-15 15:00:17 +08:00
/// </summary>
public class Message
{
/// <summary>
/// 消息主键 数据库自增
/// </summary>
2018-10-30 13:07:29 +08:00
public string Id { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-15 15:00:17 +08:00
/// <summary>
/// 标题
/// </summary>
public string Title { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-15 15:00:17 +08:00
/// <summary>
/// 内容
/// </summary>
public string Content { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-15 15:00:17 +08:00
/// <summary>
/// 发消息人
/// </summary>
public string From { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-15 15:00:17 +08:00
/// <summary>
/// 收消息人
/// </summary>
public string To { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-15 15:00:17 +08:00
/// <summary>
/// 消息发送时间
/// </summary>
public DateTime SendTime { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-15 15:00:17 +08:00
/// <summary>
/// 消息状态: 0-未读, 1-已读 和Dict表的通知消息关联
/// </summary>
public string Status { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-15 15:00:17 +08:00
/// <summary>
/// 标旗状态: 0-未标旗, 1-已标旗
/// </summary>
public int Mark { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-15 15:00:17 +08:00
/// <summary>
/// 删除状态: 0-未删除, 1-已删除
/// </summary>
public int IsDelete { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-15 15:00:17 +08:00
/// <summary>
/// 消息标签: 0-一般, 1-紧要 和Dict表的消息标签关联
/// </summary>
public string Label { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-15 15:00:17 +08:00
/// <summary>
/// 获得/设置 标签名称
/// </summary>
public string LabelName { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-15 15:00:17 +08:00
/// <summary>
/// 获得/设置 时间描述 2分钟内为刚刚
/// </summary>
public string Period { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-18 14:42:51 +08:00
/// <summary>
/// 获得/设置 发件人头像
/// </summary>
public string FromIcon { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-18 14:42:51 +08:00
/// <summary>
/// 获得/设置 发件人昵称
/// </summary>
public string FromDisplayName { get ; set ; }
2019-01-11 23:20:28 +08:00
//TODO: SQL语句不兼容
2018-10-19 23:09:52 +08:00
/// <summary>
2018-10-21 10:02:59 +08:00
/// 所有有关userName所有消息列表
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
protected virtual IEnumerable < Message > Retrieves ( string userName )
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
var db = DbManager . Db ;
var t = db . Provider . EscapeSqlIdentifier ( "To" ) ;
var f = db . Provider . EscapeSqlIdentifier ( "From" ) ;
return db . Fetch < Message > ( $"select m.*, d.Name, u.DisplayName from [Messages] m left join Dicts d on m.Label = d.Code and d.Category = @Category and d.Define = 0 inner join Users u on m.{f} = u.UserName where {t} = @UserName or {f} = @UserName order by SendTime desc" , new { UserName = userName , Category = "消息标签" } ) ;
2018-10-21 10:02:59 +08:00
}
2019-01-11 23:20:28 +08:00
2018-10-21 10:02:59 +08:00
/// <summary>
2018-10-19 23:09:52 +08:00
/// 收件箱
/// </summary>
/// <param name="userName"></param>
2018-10-21 10:02:59 +08:00
public virtual IEnumerable < Message > Inbox ( string userName )
{
2019-01-11 23:20:28 +08:00
var messageRet = Retrieves ( userName ) ;
2018-10-21 10:02:59 +08:00
return messageRet . Where ( n = > n . To . Equals ( userName , StringComparison . OrdinalIgnoreCase ) ) ;
}
2019-01-11 23:20:28 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 发件箱
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
2018-10-21 10:02:59 +08:00
public virtual IEnumerable < Message > SendMail ( string userName )
{
2019-01-11 23:20:28 +08:00
var messageRet = Retrieves ( userName ) ;
2018-10-21 10:02:59 +08:00
return messageRet . Where ( n = > n . From . Equals ( userName , StringComparison . OrdinalIgnoreCase ) ) ;
}
2019-01-11 23:20:28 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 垃圾箱
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
2018-10-21 10:02:59 +08:00
public virtual IEnumerable < Message > Trash ( string userName )
{
2019-01-11 23:20:28 +08:00
var messageRet = Retrieves ( userName ) ;
2018-10-21 10:02:59 +08:00
return messageRet . Where ( n = > n . IsDelete = = 1 ) ;
}
2019-01-11 23:20:28 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 标旗
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
2018-10-21 10:02:59 +08:00
public virtual IEnumerable < Message > Flag ( string userName )
{
2019-01-11 23:20:28 +08:00
var messageRet = Retrieves ( userName ) ;
2018-10-21 10:02:59 +08:00
return messageRet . Where ( n = > n . Mark = = 1 ) ;
}
2019-01-11 23:20:28 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 获取Header处显示的消息列表
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual IEnumerable < Message > RetrieveHeaders ( string userName )
2018-10-21 10:02:59 +08:00
{
var messageRet = Inbox ( 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 ;
}
2016-11-15 15:00:17 +08:00
}
}