消息管理-后台实体类和方法

This commit is contained in:
liuchun_0206@163.com 2016-11-12 14:58:26 +08:00
parent 605dcb90bb
commit ea3056a53a
4 changed files with 178 additions and 0 deletions

View File

@ -45,6 +45,8 @@
<add key="NotificationHelper-RetrieveNotifications" interval="600" desc="通知管理数据缓存"/>
<add key="UserHelper-RetrieveNewUsers" interval="300" desc="新用户数据缓存" />
<add key="ExceptionHelper-RetrieveExceptions" interval="600" desc="程序异常数据缓存"/>
<add key="MessageHelper-RetrieveMessagesFromOthers" interval="600" desc="接收消息数据缓存" />
<add key="MessageHelper-RetrieveMessagesToOthers" interval="600" desc="发送消息数据缓存"/>
</cacheManager>

View File

@ -68,6 +68,8 @@
<Compile Include="LogHelper.cs" />
<Compile Include="Menu.cs" />
<Compile Include="MenuHelper.cs" />
<Compile Include="Message.cs" />
<Compile Include="MessageHelper.cs" />
<Compile Include="NotificationHelper.cs" />
<Compile Include="Notification.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bootstrap.DataAccess
{
/// <summary>
/// 消息表实体
/// author:liuchun
/// date:2016.11.12
/// </summary>
public class Message
{
/// <summary>
/// 消息主键 数据库自增
/// </summary>
public int ID { get; set; }
/// <summary>
/// 标题
/// </summary>
public string Title { get; set; }
/// <summary>
/// 内容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 发消息人
/// </summary>
public string From { get; set; }
/// <summary>
/// 收消息人
/// </summary>
public string To { get; set; }
/// <summary>
/// 消息发送时间
/// </summary>
public DateTime SendTime { get; set; }
/// <summary>
/// 消息状态0-未读1-已读2-删除
/// 和Dict表的通知消息关联
/// </summary>
public string Status { get; set; }
/// <summary>
/// 获得/设置 时间描述 2分钟内为刚刚
/// </summary>
public string Period { get; set; }
}
}

View File

@ -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";
/// <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;
}
}
}