using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess
{
///
///
///
[TableName("Messages")]
public class Message
{
///
/// 消息主键 数据库自增
///
public string 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-已读 和Dict表的通知消息关联
///
public string Status { get; set; }
///
/// 标旗状态:0-未标旗,1-已标旗
///
public int Flag { get; set; }
///
/// 删除状态:0-未删除,1-已删除
///
public int IsDelete { get; set; }
///
/// 消息标签:0-一般,1-紧要 和Dict表的消息标签关联
///
public string Label { get; set; }
///
/// 获得/设置 标签名称
///
[ResultColumn]
public string LabelName { get; set; }
///
/// 获得/设置 时间描述 2分钟内为刚刚
///
[ResultColumn]
public string Period { get; set; }
///
/// 获得/设置 发件人头像
///
[ResultColumn]
public string FromIcon { get; set; }
///
/// 获得/设置 发件人昵称
///
[ResultColumn]
public string FromDisplayName { get; set; }
//TODO: SQL语句不兼容
///
/// 所有有关userName所有消息列表
///
///
///
protected virtual IEnumerable Retrieves(string userName)
{
var db = DbManager.Create();
var t = db.Provider.EscapeSqlIdentifier("To");
var f = db.Provider.EscapeSqlIdentifier("From");
return db.Fetch($"select m.*, d.Name, u.DisplayName from Messages m left join Dicts d on m.Label = d.Code and d.Category = @0 and d.Define = 0 inner join Users u on m.{f} = u.UserName where {t} = @1 or {f} = @1 order by SendTime desc", "消息标签", userName);
}
///
/// 收件箱
///
///
public virtual IEnumerable Inbox(string userName)
{
var messageRet = Retrieves(userName);
return messageRet.Where(n => n.To.Equals(userName, StringComparison.OrdinalIgnoreCase));
}
///
/// 发件箱
///
///
///
public virtual IEnumerable SendMail(string userName)
{
var messageRet = Retrieves(userName);
return messageRet.Where(n => n.From.Equals(userName, StringComparison.OrdinalIgnoreCase));
}
///
/// 垃圾箱
///
///
///
public virtual IEnumerable Trash(string userName)
{
var messageRet = Retrieves(userName);
return messageRet.Where(n => n.IsDelete == 1);
}
///
/// 标旗
///
///
///
public virtual IEnumerable Mark(string userName)
{
var messageRet = Retrieves(userName);
return messageRet.Where(n => n.Flag == 1);
}
///
/// 获取Header处显示的消息列表
///
///
///
public virtual IEnumerable RetrieveHeaders(string userName)
{
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;
}
///
///
///
///
///
public virtual bool Save(Message msg)
{
var db = DbManager.Create();
db.Save(msg);
return true;
}
}
}