重构代码:移动缓存代码到Helper类中

This commit is contained in:
Argo-MacBookPro 2018-10-28 15:03:59 +08:00
parent 018e1d1851
commit 32bd9d2aa3
24 changed files with 492 additions and 547 deletions

View File

@ -56,7 +56,7 @@ namespace Bootstrap.DataAccess.SQLite
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Message", ex.Message)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Message", ex.Message));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@StackTrace", DbAdapterManager.ToDBValue(ex.StackTrace))); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@StackTrace", DbAdapterManager.ToDBValue(ex.StackTrace)));
DbAccessManager.DBAccess.ExecuteNonQuery(cmd); DbAccessManager.DBAccess.ExecuteNonQuery(cmd);
CacheManager.Clear(RetrieveExceptionsDataKey); CacheManager.Clear(ExceptionsHelper.RetrieveExceptionsDataKey);
ClearExceptions(); ClearExceptions();
} }
} }
@ -66,31 +66,28 @@ namespace Bootstrap.DataAccess.SQLite
/// <returns></returns> /// <returns></returns>
public override IEnumerable<DataAccess.Exceptions> RetrieveExceptions() public override IEnumerable<DataAccess.Exceptions> RetrieveExceptions()
{ {
return CacheManager.GetOrAdd(RetrieveExceptionsDataKey, key => string sql = "select * from Exceptions where LogTime > datetime('now', 'localtime', '-7 day') order by LogTime desc";
List<Exceptions> exceptions = new List<Exceptions>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
string sql = "select * from Exceptions where LogTime > datetime('now', 'localtime', '-7 day') order by LogTime desc"; while (reader.Read())
List<Exceptions> exceptions = new List<Exceptions>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) exceptions.Add(new Exceptions()
{ {
exceptions.Add(new Exceptions() Id = LgbConvert.ReadValue(reader[0], 0),
{ AppDomainName = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), ErrorPage = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
AppDomainName = (string)reader[1], UserId = reader.IsDBNull(3) ? string.Empty : (string)reader[3],
ErrorPage = reader.IsDBNull(2) ? string.Empty : (string)reader[2], UserIp = reader.IsDBNull(4) ? string.Empty : (string)reader[4],
UserId = reader.IsDBNull(3) ? string.Empty : (string)reader[3], ExceptionType = (string)reader[5],
UserIp = reader.IsDBNull(4) ? string.Empty : (string)reader[4], Message = (string)reader[6],
ExceptionType = (string)reader[5], StackTrace = (string)reader[7],
Message = (string)reader[6], LogTime = LgbConvert.ReadValue(reader[8], DateTime.MinValue)
StackTrace = (string)reader[7], });
LogTime = LgbConvert.ReadValue(reader[8], DateTime.MinValue)
});
}
} }
return exceptions; }
}); return exceptions;
} }
} }
} }

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
using System.Linq;
namespace Bootstrap.DataAccess.SQLite namespace Bootstrap.DataAccess.SQLite
{ {
@ -21,30 +20,26 @@ namespace Bootstrap.DataAccess.SQLite
/// <returns></returns> /// <returns></returns>
public override IEnumerable<DataAccess.Log> RetrieveLogs(string tId = null) public override IEnumerable<DataAccess.Log> RetrieveLogs(string tId = null)
{ {
var ret = CacheManager.GetOrAdd(RetrieveLogsDataKey, key => string sql = "select * from Logs where LogTime > datetime('now', 'localtime', '-7 day')";
List<Log> logs = new List<Log>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
string sql = "select * from Logs where LogTime > datetime('now', 'localtime', '-7 day')"; while (reader.Read())
List<Log> logs = new List<Log>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) logs.Add(new Log()
{ {
logs.Add(new Log() Id = LgbConvert.ReadValue(reader[0], 0),
{ CRUD = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), UserName = (string)reader[2],
CRUD = (string)reader[1], LogTime = LgbConvert.ReadValue(reader[3], DateTime.MinValue),
UserName = (string)reader[2], ClientIp = (string)reader[4],
LogTime = LgbConvert.ReadValue(reader[3], DateTime.MinValue), ClientAgent = (string)reader[5],
ClientIp = (string)reader[4], RequestUrl = (string)reader[6]
ClientAgent = (string)reader[5], });
RequestUrl = (string)reader[6]
});
}
} }
return logs; }
}); return logs;
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase));
} }
/// <summary> /// <summary>
/// 删除日志信息 /// 删除日志信息
@ -79,7 +74,7 @@ namespace Bootstrap.DataAccess.SQLite
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RequestUrl", p.RequestUrl)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RequestUrl", p.RequestUrl));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1; ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
} }
CacheManager.Clear(RetrieveLogsDataKey); CacheManager.Clear(LogHelper.RetrieveLogsDataKey);
DeleteLogAsync(); DeleteLogAsync();
return ret; return ret;
} }

View File

@ -1,10 +1,8 @@
using Longbow; using Longbow;
using Longbow.Cache;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
using System.Linq;
namespace Bootstrap.DataAccess.SQLite namespace Bootstrap.DataAccess.SQLite
{ {
@ -20,38 +18,33 @@ namespace Bootstrap.DataAccess.SQLite
/// <returns></returns> /// <returns></returns>
protected override IEnumerable<DataAccess.Message> RetrieveMessages(string userName) protected override IEnumerable<DataAccess.Message> RetrieveMessages(string userName)
{ {
var messageRet = CacheManager.GetOrAdd(RetrieveMessageDataKey, key => string sql = "select m.*, d.Name, ifnull(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 = '消息标签' and d.Define = 0 left join Dicts i on i.Category = '头像地址' and i.Name = '头像路径' and i.Define = 0 inner join Users u on m.[From] = u.UserName where [To] = @UserName or [From] = @UserName order by m.SendTime desc";
List<Message> messages = new List<Message>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserName", userName));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
string sql = "select m.*, d.Name, ifnull(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 = '消息标签' and d.Define = 0 left join Dicts i on i.Category = '头像地址' and i.Name = '头像路径' and i.Define = 0 inner join Users u on m.[From] = u.UserName where [To] = @UserName or [From] = @UserName order by m.SendTime desc"; while (reader.Read())
List<Message> messages = new List<Message>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserName", userName));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) messages.Add(new Message()
{ {
messages.Add(new Message() Id = LgbConvert.ReadValue(reader[0], 0),
{ Title = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), Content = (string)reader[2],
Title = (string)reader[1], From = (string)reader[3],
Content = (string)reader[2], To = (string)reader[4],
From = (string)reader[3], SendTime = LgbConvert.ReadValue(reader[5], DateTime.MinValue),
To = (string)reader[4], Status = (string)reader[6],
SendTime = LgbConvert.ReadValue(reader[5], DateTime.MinValue), Mark = LgbConvert.ReadValue(reader[7], 0),
Status = (string)reader[6], IsDelete = LgbConvert.ReadValue(reader[8], 0),
Mark = LgbConvert.ReadValue(reader[7], 0), Label = (string)reader[9],
IsDelete = LgbConvert.ReadValue(reader[8], 0), LabelName = LgbConvert.ReadValue(reader[10], string.Empty),
Label = (string)reader[9], FromIcon = (string)reader[11],
LabelName = LgbConvert.ReadValue(reader[10], string.Empty), FromDisplayName = (string)reader[12]
FromIcon = (string)reader[11], });
FromDisplayName = (string)reader[12]
});
}
} }
return messages; }
return messages;
});
return messageRet.OrderByDescending(n => n.SendTime);
} }
} }
} }

View File

@ -1,5 +1,4 @@
using Longbow; using Longbow;
using Longbow.Cache;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -15,30 +14,27 @@ namespace Bootstrap.DataAccess.SQLite
/// <returns></returns> /// <returns></returns>
public override IEnumerable<DataAccess.Task> RetrieveTasks() public override IEnumerable<DataAccess.Task> RetrieveTasks()
{ {
return CacheManager.GetOrAdd(RetrieveTasksDataKey, key => string sql = "select t.*, u.DisplayName from Tasks t inner join Users u on t.UserName = u.UserName order by AssignTime desc limit 1000";
List<Task> tasks = new List<Task>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
string sql = "select t.*, u.DisplayName from Tasks t inner join Users u on t.UserName = u.UserName order by AssignTime desc limit 1000"; while (reader.Read())
List<Task> tasks = new List<Task>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) tasks.Add(new Task()
{ {
tasks.Add(new Task() Id = LgbConvert.ReadValue(reader[0], 0),
{ TaskName = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), AssignName = (string)reader[2],
TaskName = (string)reader[1], UserName = (string)reader[3],
AssignName = (string)reader[2], TaskTime = LgbConvert.ReadValue(reader[4], 0),
UserName = (string)reader[3], TaskProgress = (double)reader[5],
TaskTime = LgbConvert.ReadValue(reader[4], 0), AssignTime = LgbConvert.ReadValue(reader[6], DateTime.MinValue),
TaskProgress = (double)reader[5], AssignDisplayName = (string)reader[7]
AssignTime = LgbConvert.ReadValue(reader[6], DateTime.MinValue), });
AssignDisplayName = (string)reader[7]
});
}
} }
return tasks; }
}); return tasks;
} }
} }
} }

View File

@ -1,5 +1,4 @@
using Bootstrap.Security; using Bootstrap.Security;
using Longbow.Cache;
using Longbow.Data; using Longbow.Data;
using Longbow.Security.Cryptography; using Longbow.Security.Cryptography;
using System; using System;
@ -8,7 +7,6 @@ using System.Data;
using System.Data.Common; using System.Data.Common;
using System.Linq; using System.Linq;
namespace Bootstrap.DataAccess.SQLite namespace Bootstrap.DataAccess.SQLite
{ {
/// <summary> /// <summary>
@ -245,29 +243,25 @@ namespace Bootstrap.DataAccess.SQLite
/// <returns></returns> /// <returns></returns>
public override BootstrapUser RetrieveUserByUserName(string userName) public override BootstrapUser RetrieveUserByUserName(string userName)
{ {
var key = string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName); BootstrapUser user = null;
return CacheManager.GetOrAdd(key, k => var sql = "select UserName, DisplayName, case ifnull(d.Code, '') when '' then '~/images/uploader/' else d.Code end || ifnull(Icon, 'default.jpg') Icon, u.Css from Users u left join Dicts d on d.Define = '0' and d.Category = '头像地址' and Name = '头像路径' where ApprovedTime is not null and UserName = @UserName";
var db = DbAccessManager.DBAccess;
var cmd = db.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(db.CreateParameter("@UserName", userName));
using (DbDataReader reader = db.ExecuteReader(cmd))
{ {
BootstrapUser user = null; if (reader.Read())
var sql = "select UserName, DisplayName, case ifnull(d.Code, '') when '' then '~/images/uploader/' else d.Code end || ifnull(Icon, 'default.jpg') Icon, u.Css from Users u left join Dicts d on d.Define = '0' and d.Category = '头像地址' and Name = '头像路径' where ApprovedTime is not null and UserName = @UserName";
var db = DbAccessManager.DBAccess;
var cmd = db.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(db.CreateParameter("@UserName", userName));
using (DbDataReader reader = db.ExecuteReader(cmd))
{ {
if (reader.Read()) user = new BootstrapUser
{ {
user = new BootstrapUser UserName = (string)reader[0],
{ DisplayName = (string)reader[1],
UserName = (string)reader[0], Icon = (string)reader[2],
DisplayName = (string)reader[1], Css = reader.IsDBNull(3) ? string.Empty : (string)reader[3]
Icon = (string)reader[2], };
Css = reader.IsDBNull(3) ? string.Empty : (string)reader[3]
};
}
} }
return user; }
}, RetrieveUsersByNameDataKey); return user;
} }
} }
} }

View File

@ -27,54 +27,54 @@ namespace Bootstrap.DataAccess
{ {
roleIds.ToList().ForEach(id => roleIds.ToList().ForEach(id =>
{ {
cacheKeys.Add(string.Format("{0}-{1}", User.RetrieveUsersByRoleIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", UserHelper.RetrieveUsersByRoleIdDataKey, id));
cacheKeys.Add(string.Format("{0}-{1}", Group.RetrieveGroupsByRoleIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", GroupHelper.RetrieveGroupsByRoleIdDataKey, id));
cacheKeys.Add(string.Format("{0}-{1}", Menu.RetrieveMenusByRoleIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", MenuHelper.RetrieveMenusByRoleIdDataKey, id));
}); });
cacheKeys.Add(Role.RetrieveRolesDataKey + "*"); cacheKeys.Add(RoleHelper.RetrieveRolesDataKey + "*");
cacheKeys.Add(Menu.RetrieveMenusDataKey + "*"); cacheKeys.Add(MenuHelper.RetrieveMenusDataKey + "*");
cacheKeys.Add(RetrieveAllRolesDataKey + "*"); cacheKeys.Add(RetrieveAllRolesDataKey + "*");
corsKeys.Add(Menu.RetrieveMenusDataKey + "*"); corsKeys.Add(MenuHelper.RetrieveMenusDataKey + "*");
} }
if (userIds != null) if (userIds != null)
{ {
userIds.ToList().ForEach(id => userIds.ToList().ForEach(id =>
{ {
cacheKeys.Add(string.Format("{0}-{1}", Role.RetrieveRolesByUserIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByUserIdDataKey, id));
cacheKeys.Add(string.Format("{0}-{1}", Group.RetrieveGroupsByUserIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", GroupHelper.RetrieveGroupsByUserIdDataKey, id));
cacheKeys.Add(Menu.RetrieveMenusDataKey + "*"); cacheKeys.Add(MenuHelper.RetrieveMenusDataKey + "*");
corsKeys.Add(Menu.RetrieveMenusDataKey + "*"); corsKeys.Add(MenuHelper.RetrieveMenusDataKey + "*");
}); });
cacheKeys.Add(User.RetrieveNewUsersDataKey + "*"); cacheKeys.Add(UserHelper.RetrieveNewUsersDataKey + "*");
cacheKeys.Add(User.RetrieveUsersDataKey + "*"); cacheKeys.Add(UserHelper.RetrieveUsersDataKey + "*");
corsKeys.Add(User.RetrieveUsersDataKey + "*"); corsKeys.Add(UserHelper.RetrieveUsersDataKey + "*");
} }
if (groupIds != null) if (groupIds != null)
{ {
groupIds.ToList().ForEach(id => groupIds.ToList().ForEach(id =>
{ {
cacheKeys.Add(string.Format("{0}-{1}", Role.RetrieveRolesByGroupIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByGroupIdDataKey, id));
cacheKeys.Add(string.Format("{0}-{1}", User.RetrieveUsersByGroupIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", UserHelper.RetrieveUsersByGroupIdDataKey, id));
}); });
cacheKeys.Add(Group.RetrieveGroupsDataKey + "*"); cacheKeys.Add(GroupHelper.RetrieveGroupsDataKey + "*");
cacheKeys.Add(Menu.RetrieveMenusDataKey + "*"); cacheKeys.Add(MenuHelper.RetrieveMenusDataKey + "*");
corsKeys.Add(Menu.RetrieveMenusDataKey + "*"); corsKeys.Add(MenuHelper.RetrieveMenusDataKey + "*");
cacheKeys.Add(RetrieveAllRolesDataKey + "*"); cacheKeys.Add(RetrieveAllRolesDataKey + "*");
} }
if (menuIds != null) if (menuIds != null)
{ {
menuIds.ToList().ForEach(id => menuIds.ToList().ForEach(id =>
{ {
cacheKeys.Add(string.Format("{0}-{1}", Role.RetrieveRolesByMenuIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByMenuIdDataKey, id));
}); });
cacheKeys.Add(Menu.RetrieveMenusByRoleIdDataKey + "*"); cacheKeys.Add(MenuHelper.RetrieveMenusByRoleIdDataKey + "*");
cacheKeys.Add(Menu.RetrieveMenusDataKey + "*"); cacheKeys.Add(MenuHelper.RetrieveMenusDataKey + "*");
corsKeys.Add(Menu.RetrieveMenusDataKey + "*"); corsKeys.Add(MenuHelper.RetrieveMenusDataKey + "*");
} }
if (dictIds != null) if (dictIds != null)
{ {
cacheKeys.Add(Dict.RetrieveDictsDataKey + "*"); cacheKeys.Add(Dict.RetrieveDictsDataKey + "*");
cacheKeys.Add(Dict.RetrieveCategoryDataKey); cacheKeys.Add(DictHelper.RetrieveCategoryDataKey);
corsKeys.Add(Dict.RetrieveDictsDataKey + "*"); corsKeys.Add(Dict.RetrieveDictsDataKey + "*");
} }
if (cacheKey != null) if (cacheKey != null)

View File

@ -1,6 +1,5 @@
using Bootstrap.Security; using Bootstrap.Security;
using Bootstrap.Security.SQLServer; using Bootstrap.Security.SQLServer;
using Longbow.Cache;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -17,7 +16,6 @@ namespace Bootstrap.DataAccess
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
public const string RetrieveCategoryDataKey = "DictHelper-RetrieveDictsCategory";
/// <summary> /// <summary>
/// 缓存索引BootstrapAdmin后台清理缓存时使用 /// 缓存索引BootstrapAdmin后台清理缓存时使用
/// </summary> /// </summary>
@ -92,20 +90,17 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<string> RetrieveCategories() public virtual IEnumerable<string> RetrieveCategories()
{ {
return CacheManager.GetOrAdd(RetrieveCategoryDataKey, key => var ret = new List<string>();
string sql = "select distinct Category from Dicts";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
var ret = new List<string>(); while (reader.Read())
string sql = "select distinct Category from Dicts";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) ret.Add((string)reader[0]);
{
ret.Add((string)reader[0]);
}
} }
return ret; }
}); return ret;
} }
/// <summary> /// <summary>
/// ///

View File

@ -14,10 +14,6 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public class Exceptions public class Exceptions
{ {
/// <summary>
///
/// </summary>
protected const string RetrieveExceptionsDataKey = "ExceptionHelper-RetrieveExceptions";
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -97,7 +93,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Message", ex.Message)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Message", ex.Message));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@StackTrace", DbAdapterManager.ToDBValue(ex.StackTrace))); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@StackTrace", DbAdapterManager.ToDBValue(ex.StackTrace)));
DbAccessManager.DBAccess.ExecuteNonQuery(cmd); DbAccessManager.DBAccess.ExecuteNonQuery(cmd);
CacheManager.Clear(RetrieveExceptionsDataKey); CacheManager.Clear(ExceptionsHelper.RetrieveExceptionsDataKey);
ClearExceptions(); ClearExceptions();
} }
} }
@ -107,31 +103,28 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Exceptions> RetrieveExceptions() public virtual IEnumerable<Exceptions> RetrieveExceptions()
{ {
return CacheManager.GetOrAdd(RetrieveExceptionsDataKey, key => string sql = "select * from Exceptions where DATEDIFF(Week, LogTime, GETDATE()) = 0 order by LogTime desc";
List<Exceptions> exceptions = new List<Exceptions>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
string sql = "select * from Exceptions where DATEDIFF(Week, LogTime, GETDATE()) = 0 order by LogTime desc"; while (reader.Read())
List<Exceptions> exceptions = new List<Exceptions>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) exceptions.Add(new Exceptions()
{ {
exceptions.Add(new Exceptions() Id = (int)reader[0],
{ AppDomainName = (string)reader[1],
Id = (int)reader[0], ErrorPage = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
AppDomainName = (string)reader[1], UserId = reader.IsDBNull(3) ? string.Empty : (string)reader[3],
ErrorPage = reader.IsDBNull(2) ? string.Empty : (string)reader[2], UserIp = reader.IsDBNull(4) ? string.Empty : (string)reader[4],
UserId = reader.IsDBNull(3) ? string.Empty : (string)reader[3], ExceptionType = (string)reader[5],
UserIp = reader.IsDBNull(4) ? string.Empty : (string)reader[4], Message = (string)reader[6],
ExceptionType = (string)reader[5], StackTrace = (string)reader[7],
Message = (string)reader[6], LogTime = (DateTime)reader[8],
StackTrace = (string)reader[7], });
LogTime = (DateTime)reader[8],
});
}
} }
return exceptions; }
}); return exceptions;
} }
} }
} }

View File

@ -1,5 +1,4 @@
using Longbow; using Longbow;
using Longbow.Cache;
using Longbow.Data; using Longbow.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -15,10 +14,6 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public class Group public class Group
{ {
public const string RetrieveGroupsDataKey = "GroupHelper-RetrieveGroups";
public const string RetrieveGroupsByUserIdDataKey = "GroupHelper-RetrieveGroupsByUserId";
public const string RetrieveGroupsByRoleIdDataKey = "GroupHelper-RetrieveGroupsByRoleId";
public const string RetrieveGroupsByUserNameDataKey = "BootstrapAdminGroupMiddleware-RetrieveGroupsByUserName";
/// <summary> /// <summary>
/// 获得/设置 群组主键ID /// 获得/设置 群组主键ID
/// </summary> /// </summary>
@ -45,26 +40,22 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Group> RetrieveGroups(int id = 0) public virtual IEnumerable<Group> RetrieveGroups(int id = 0)
{ {
var ret = CacheManager.GetOrAdd(RetrieveGroupsDataKey, key => string sql = "select * from Groups";
List<Group> groups = new List<Group>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
string sql = "select * from Groups"; while (reader.Read())
List<Group> groups = new List<Group>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) groups.Add(new Group()
{ {
groups.Add(new Group() Id = LgbConvert.ReadValue(reader[0], 0),
{ GroupName = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2]
GroupName = (string)reader[1], });
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2]
});
}
} }
return groups; }
}); return groups;
return id == 0 ? ret : ret.Where(t => id == t.Id);
} }
/// <summary> /// <summary>
/// 删除群组信息 /// 删除群组信息
@ -112,29 +103,24 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Group> RetrieveGroupsByUserId(int userId) public virtual IEnumerable<Group> RetrieveGroupsByUserId(int userId)
{ {
string key = string.Format("{0}-{1}", RetrieveGroupsByUserIdDataKey, userId); string sql = "select g.ID,g.GroupName,g.[Description],case ug.GroupID when g.ID then 'checked' else '' end [status] from Groups g left join UserGroup ug on g.ID=ug.GroupID and UserID=@UserID";
var ret = CacheManager.GetOrAdd(key, k => List<Group> groups = new List<Group>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserID", userId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
string sql = "select g.ID,g.GroupName,g.[Description],case ug.GroupID when g.ID then 'checked' else '' end [status] from Groups g left join UserGroup ug on g.ID=ug.GroupID and UserID=@UserID"; while (reader.Read())
List<Group> groups = new List<Group>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserID", userId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) groups.Add(new Group()
{ {
groups.Add(new Group() Id = LgbConvert.ReadValue(reader[0], 0),
{ GroupName = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
GroupName = (string)reader[1], Checked = (string)reader[3]
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], });
Checked = (string)reader[3]
});
}
} }
return groups; }
}, RetrieveGroupsByUserIdDataKey); return groups;
return ret;
} }
/// <summary> /// <summary>
/// 保存用户部门关系 /// 保存用户部门关系
@ -189,28 +175,24 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Group> RetrieveGroupsByRoleId(int roleId) public virtual IEnumerable<Group> RetrieveGroupsByRoleId(int roleId)
{ {
string k = string.Format("{0}-{1}", RetrieveGroupsByRoleIdDataKey, roleId); List<Group> groups = new List<Group>();
return CacheManager.GetOrAdd(k, key => string sql = "select g.ID,g.GroupName,g.[Description],case rg.GroupID when g.ID then 'checked' else '' end [status] from Groups g left join RoleGroup rg on g.ID=rg.GroupID and RoleID=@RoleID";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RoleID", roleId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
List<Group> groups = new List<Group>(); while (reader.Read())
string sql = "select g.ID,g.GroupName,g.[Description],case rg.GroupID when g.ID then 'checked' else '' end [status] from Groups g left join RoleGroup rg on g.ID=rg.GroupID and RoleID=@RoleID";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RoleID", roleId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) groups.Add(new Group()
{ {
groups.Add(new Group() Id = LgbConvert.ReadValue(reader[0], 0),
{ GroupName = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
GroupName = (string)reader[1], Checked = (string)reader[3]
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], });
Checked = (string)reader[3]
});
}
} }
return groups; }
}, RetrieveGroupsByRoleIdDataKey); return groups;
} }
/// <summary> /// <summary>
/// 根据角色ID以及选定的部门ID保到角色部门表 /// 根据角色ID以及选定的部门ID保到角色部门表
@ -260,27 +242,23 @@ namespace Bootstrap.DataAccess
/// ///
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <param name="connName"></param>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<string> RetrieveGroupsByUserName(string userName) public virtual IEnumerable<string> RetrieveGroupsByUserName(string userName)
{ {
return CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByUserNameDataKey, userName), r => var entities = new List<string>();
var db = DbAccessManager.DBAccess;
using (DbCommand cmd = db.CreateCommand(CommandType.Text, "select g.GroupName, g.[Description] from Groups g inner join UserGroup ug on g.ID = ug.GroupID inner join Users u on ug.UserID = u.ID where UserName = @UserName"))
{ {
var entities = new List<string>(); cmd.Parameters.Add(db.CreateParameter("@UserName", userName));
var db = DbAccessManager.DBAccess; using (DbDataReader reader = db.ExecuteReader(cmd))
using (DbCommand cmd = db.CreateCommand(CommandType.Text, "select g.GroupName, g.[Description] from Groups g inner join UserGroup ug on g.ID = ug.GroupID inner join Users u on ug.UserID = u.ID where UserName = @UserName"))
{ {
cmd.Parameters.Add(db.CreateParameter("@UserName", userName)); while (reader.Read())
using (DbDataReader reader = db.ExecuteReader(cmd))
{ {
while (reader.Read()) entities.Add((string)reader[0]);
{
entities.Add((string)reader[0]);
}
} }
} }
return entities; }
}, RetrieveGroupsByUserNameDataKey); return entities;
} }
} }
} }

View File

@ -1,4 +1,5 @@
using Bootstrap.Security; using Bootstrap.Security;
using Longbow.Cache;
using Longbow.Data; using Longbow.Data;
using System.Collections.Generic; using System.Collections.Generic;
@ -9,6 +10,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public static class DictHelper public static class DictHelper
{ {
public const string RetrieveCategoryDataKey = "DictHelper-RetrieveDictsCategory";
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -38,7 +40,7 @@ namespace Bootstrap.DataAccess
/// 获取字典分类名称 /// 获取字典分类名称
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<string> RetrieveCategories() => DbAdapterManager.Create<Dict>().RetrieveCategories(); public static IEnumerable<string> RetrieveCategories() => CacheManager.GetOrAdd(RetrieveCategoryDataKey, key => DbAdapterManager.Create<Dict>().RetrieveCategories());
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>

View File

@ -1,4 +1,5 @@
using Longbow.Data; using Longbow.Cache;
using Longbow.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
@ -10,6 +11,10 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public static class ExceptionsHelper public static class ExceptionsHelper
{ {
/// <summary>
///
/// </summary>
public const string RetrieveExceptionsDataKey = "ExceptionHelper-RetrieveExceptions";
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -21,6 +26,6 @@ namespace Bootstrap.DataAccess
/// 查询一周内所有异常 /// 查询一周内所有异常
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Exceptions> RetrieveExceptions() => DbAdapterManager.Create<Exceptions>().RetrieveExceptions(); public static IEnumerable<Exceptions> RetrieveExceptions() => CacheManager.GetOrAdd(RetrieveExceptionsDataKey, key => DbAdapterManager.Create<Exceptions>().RetrieveExceptions());
} }
} }

View File

@ -1,20 +1,30 @@
using Longbow.Data; using Longbow.Cache;
using Longbow.Data;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess namespace Bootstrap.DataAccess
{ {
/// <summary> /// <summary>
/// author:liuchun ///
/// date:2016.10.22
/// </summary> /// </summary>
public static class GroupHelper public static class GroupHelper
{ {
public const string RetrieveGroupsDataKey = "GroupHelper-RetrieveGroups";
public const string RetrieveGroupsByUserIdDataKey = "GroupHelper-RetrieveGroupsByUserId";
public const string RetrieveGroupsByRoleIdDataKey = "GroupHelper-RetrieveGroupsByRoleId";
public const string RetrieveGroupsByUserNameDataKey = "BootstrapAdminGroupMiddleware-RetrieveGroupsByUserName";
/// <summary> /// <summary>
/// 查询所有群组信息 /// 查询所有群组信息
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Group> RetrieveGroups(int id = 0) => DbAdapterManager.Create<Group>().RetrieveGroups(id); public static IEnumerable<Group> RetrieveGroups(int id = 0)
{
var ret = CacheManager.GetOrAdd(RetrieveGroupsDataKey, key => DbAdapterManager.Create<Group>().RetrieveGroups(id));
return id == 0 ? ret : ret.Where(t => id == t.Id);
}
/// <summary> /// <summary>
/// 删除群组信息 /// 删除群组信息
/// </summary> /// </summary>
@ -31,7 +41,8 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="userId"></param> /// <param name="userId"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Group> RetrieveGroupsByUserId(int userId) => DbAdapterManager.Create<Group>().RetrieveGroupsByUserId(userId); public static IEnumerable<Group> RetrieveGroupsByUserId(int userId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByUserIdDataKey, userId), k => DbAdapterManager.Create<Group>().RetrieveGroupsByUserId(userId), RetrieveGroupsByUserIdDataKey);
/// <summary> /// <summary>
/// 保存用户部门关系 /// 保存用户部门关系
/// </summary> /// </summary>
@ -44,7 +55,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="roleId"></param> /// <param name="roleId"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Group> RetrieveGroupsByRoleId(int roleId) => DbAdapterManager.Create<Group>().RetrieveGroupsByRoleId(roleId); public static IEnumerable<Group> RetrieveGroupsByRoleId(int roleId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByRoleIdDataKey, roleId), key => DbAdapterManager.Create<Group>().RetrieveGroupsByRoleId(roleId), RetrieveGroupsByRoleIdDataKey);
/// <summary> /// <summary>
/// 根据角色ID以及选定的部门ID保到角色部门表 /// 根据角色ID以及选定的部门ID保到角色部门表
/// </summary> /// </summary>
@ -52,5 +63,11 @@ namespace Bootstrap.DataAccess
/// <param name="groupIds"></param> /// <param name="groupIds"></param>
/// <returns></returns> /// <returns></returns>
public static bool SaveGroupsByRoleId(int id, IEnumerable<int> groupIds) => DbAdapterManager.Create<Group>().SaveGroupsByRoleId(id, groupIds); public static bool SaveGroupsByRoleId(int id, IEnumerable<int> groupIds) => DbAdapterManager.Create<Group>().SaveGroupsByRoleId(id, groupIds);
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static IEnumerable<string> RetrieveGroupsByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByUserNameDataKey, userName), r => DbAdapterManager.Create<Group>().RetrieveGroupsByUserName(userName), RetrieveGroupsByUserNameDataKey);
} }
} }

View File

@ -1,16 +1,30 @@
using Longbow.Data; using Longbow.Cache;
using Longbow.Data;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess namespace Bootstrap.DataAccess
{ {
/// <summary>
///
/// </summary>
public static class LogHelper public static class LogHelper
{ {
/// <summary>
///
/// </summary>
public const string RetrieveLogsDataKey = "LogHelper-RetrieveLogs";
/// <summary> /// <summary>
/// 查询所有日志信息 /// 查询所有日志信息
/// </summary> /// </summary>
/// <param name="tId"></param> /// <param name="tId"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Log> RetrieveLogs(string tId = null) => DbAdapterManager.Create<Log>().RetrieveLogs(tId); public static IEnumerable<Log> RetrieveLogs(string tId = null)
{
var ret = CacheManager.GetOrAdd(RetrieveLogsDataKey, key => DbAdapterManager.Create<Log>().RetrieveLogs(tId));
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase));
}
/// <summary> /// <summary>
/// 保存新增的日志信息 /// 保存新增的日志信息
/// </summary> /// </summary>

View File

@ -1,4 +1,5 @@
using Bootstrap.Security; using Bootstrap.Security;
using Longbow.Cache;
using Longbow.Data; using Longbow.Data;
using System.Collections.Generic; using System.Collections.Generic;
@ -9,6 +10,18 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public static class MenuHelper public static class MenuHelper
{ {
/// <summary>
///
/// </summary>
public const string RetrieveMenusByRoleIdDataKey = "MenuHelper-RetrieveMenusByRoleId";
/// <summary>
///
/// </summary>
public const string RetrieveMenusDataKey = "BootstrapMenu-RetrieveMenusByUserName";
/// <summary>
///
/// </summary>
public const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus";
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -30,9 +43,12 @@ namespace Bootstrap.DataAccess
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="roleId"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveMenusByRoleId(int id) => DbAdapterManager.Create<Menu>().RetrieveMenusByRoleId(id); public static IEnumerable<BootstrapMenu> RetrieveMenusByRoleId(int roleId)
{
return CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbAdapterManager.Create<Menu>().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey);
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>

View File

@ -1,5 +1,7 @@
using Longbow.Data; using Longbow.Cache;
using Longbow.Data;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess namespace Bootstrap.DataAccess
{ {
@ -8,6 +10,10 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public static class MessageHelper public static class MessageHelper
{ {
/// <summary>
///
/// </summary>
public const string RetrieveMessageDataKey = "MessageHelper-RetrieveMessages";
/// <summary> /// <summary>
/// 收件箱 /// 收件箱
/// </summary> /// </summary>
@ -36,6 +42,6 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Message> RetrieveMessagesHeader(string userName) => DbAdapterManager.Create<Message>().RetrieveMessagesHeader(userName); public static IEnumerable<Message> RetrieveMessagesHeader(string userName) => CacheManager.GetOrAdd(RetrieveMessageDataKey, key => DbAdapterManager.Create<Message>().RetrieveMessagesHeader(userName).OrderByDescending(n => n.SendTime));
} }
} }

View File

@ -1,5 +1,7 @@
using Longbow.Data; using Longbow.Cache;
using Longbow.Data;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess namespace Bootstrap.DataAccess
{ {
@ -8,24 +10,34 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public static class RoleHelper public static class RoleHelper
{ {
public const string RetrieveRolesDataKey = "RoleHelper-RetrieveRoles";
public const string RetrieveRolesByUserIdDataKey = "RoleHelper-RetrieveRolesByUserId";
public const string RetrieveRolesByMenuIdDataKey = "RoleHelper-RetrieveRolesByMenuId";
public const string RetrieveRolesByGroupIdDataKey = "RoleHelper-RetrieveRolesByGroupId";
public const string RetrieveRolesByUserNameDataKey = "BootstrapAdminRoleMiddleware-RetrieveRolesByUserName";
public const string RetrieveRolesByUrlDataKey = "BootstrapAdminAuthorizeFilter-RetrieveRolesByUrl";
/// <summary> /// <summary>
/// 查询所有角色 /// 查询所有角色
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Role> RetrieveRoles(int id = 0) => DbAdapterManager.Create<Role>().RetrieveRoles(id); public static IEnumerable<Role> RetrieveRoles(int id = 0)
{
var ret = CacheManager.GetOrAdd(RetrieveRolesDataKey, key => DbAdapterManager.Create<Role>().RetrieveRoles(id));
return id == 0 ? ret : ret.Where(t => id == t.Id);
}
/// <summary> /// <summary>
/// 保存用户角色关系 /// 保存用户角色关系
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="userId"></param>
/// <param name="roleIds"></param> /// <param name="roleIds"></param>
/// <returns></returns> /// <returns></returns>
public static bool SaveRolesByUserId(int id, IEnumerable<int> roleIds) => DbAdapterManager.Create<Role>().SaveRolesByUserId(id, roleIds); public static bool SaveRolesByUserId(int userId, IEnumerable<int> roleIds) => DbAdapterManager.Create<Role>().SaveRolesByUserId(userId, roleIds);
/// <summary> /// <summary>
/// 查询某个用户所拥有的角色 /// 查询某个用户所拥有的角色
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByUserId(int userId) => DbAdapterManager.Create<Role>().RetrieveRolesByUserId(userId); public static IEnumerable<Role> RetrieveRolesByUserId(int userId) => CacheManager.GetOrAdd($"{RetrieveRolesByUserIdDataKey}-{userId}", key => DbAdapterManager.Create<Role>().RetrieveRolesByUserId(userId), RetrieveRolesByUserIdDataKey);
/// <summary> /// <summary>
/// 删除角色表 /// 删除角色表
/// </summary> /// </summary>
@ -42,7 +54,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="menuId"></param> /// <param name="menuId"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByMenuId(int menuId) => DbAdapterManager.Create<Role>().RetrieveRolesByMenuId(menuId); public static IEnumerable<Role> RetrieveRolesByMenuId(int menuId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByMenuIdDataKey, menuId), key => DbAdapterManager.Create<Role>().RetrieveRolesByMenuId(menuId), RetrieveRolesByMenuIdDataKey);
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -55,7 +67,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="groupId"></param> /// <param name="groupId"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByGroupId(int groupId) => DbAdapterManager.Create<Role>().RetrieveRolesByGroupId(groupId); public static IEnumerable<Role> RetrieveRolesByGroupId(int groupId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByGroupIdDataKey, groupId), key => DbAdapterManager.Create<Role>().RetrieveRolesByGroupId(groupId), RetrieveRolesByGroupIdDataKey);
/// <summary> /// <summary>
/// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息 /// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息
/// </summary> /// </summary>
@ -68,12 +80,12 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<string> RetrieveRolesByUserName(string userName) => DbAdapterManager.Create<Role>().RetrieveRolesByUserName(userName); public static IEnumerable<string> RetrieveRolesByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUserNameDataKey, userName), key => DbAdapterManager.Create<Role>().RetrieveRolesByUserName(userName), RetrieveRolesByUserNameDataKey);
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="url"></param> /// <param name="url"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<string> RetrieveRolesByUrl(string url) => DbAdapterManager.Create<Role>().RetrieveRolesByUrl(url); public static IEnumerable<string> RetrieveRolesByUrl(string url) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUrlDataKey, url), key => DbAdapterManager.Create<Role>().RetrieveRolesByUrl(url), RetrieveRolesByUrlDataKey);
} }
} }

View File

@ -1,14 +1,22 @@
using Longbow.Data; using Longbow.Cache;
using Longbow.Data;
using System.Collections.Generic; using System.Collections.Generic;
namespace Bootstrap.DataAccess namespace Bootstrap.DataAccess
{ {
/// <summary>
///
/// </summary>
public static class TaskHelper public static class TaskHelper
{ {
/// <summary>
///
/// </summary>
public const string RetrieveTasksDataKey = "TaskHelper-RetrieveTasks";
/// <summary> /// <summary>
/// 查询所有任务 /// 查询所有任务
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Task> RetrieveTasks() => DbAdapterManager.Create<Task>().RetrieveTasks(); public static IEnumerable<Task> RetrieveTasks() => CacheManager.GetOrAdd(RetrieveTasksDataKey, key => DbAdapterManager.Create<Task>().RetrieveTasks());
} }
} }

View File

@ -1,4 +1,5 @@
using Bootstrap.Security; using Bootstrap.Security;
using Longbow.Cache;
using Longbow.Data; using Longbow.Data;
using System.Collections.Generic; using System.Collections.Generic;
@ -9,12 +10,17 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public static class UserHelper public static class UserHelper
{ {
public const string RetrieveUsersDataKey = "BootstrapUser-RetrieveUsers";
public const string RetrieveUsersByRoleIdDataKey = "BootstrapUser-RetrieveUsersByRoleId";
public const string RetrieveUsersByGroupIdDataKey = "BootstrapUser-RetrieveUsersByGroupId";
public const string RetrieveNewUsersDataKey = "UserHelper-RetrieveNewUsers";
public const string RetrieveUsersByNameDataKey = "BootstrapUser-RetrieveUsersByName";
/// <summary> /// <summary>
/// 查询所有用户 /// 查询所有用户
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<User> RetrieveUsers() => DbAdapterManager.Create<User>().RetrieveUsers(); public static IEnumerable<User> RetrieveUsers() => CacheManager.GetOrAdd(RetrieveUsersDataKey, key => DbAdapterManager.Create<User>().RetrieveUsers());
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -26,7 +32,7 @@ namespace Bootstrap.DataAccess
/// 查询所有的新注册用户 /// 查询所有的新注册用户
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<User> RetrieveNewUsers() => DbAdapterManager.Create<User>().RetrieveNewUsers(); public static IEnumerable<User> RetrieveNewUsers() => CacheManager.GetOrAdd(RetrieveNewUsersDataKey, key => DbAdapterManager.Create<User>().RetrieveNewUsers());
/// <summary> /// <summary>
/// 删除用户 /// 删除用户
/// </summary> /// </summary>
@ -74,7 +80,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="roleId"></param> /// <param name="roleId"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<User> RetrieveUsersByRoleId(int roleId) => DbAdapterManager.Create<User>().RetrieveUsersByRoleId(roleId); public static IEnumerable<User> RetrieveUsersByRoleId(int roleId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveUsersByRoleIdDataKey, roleId), k => DbAdapterManager.Create<User>().RetrieveUsersByRoleId(roleId), RetrieveUsersByRoleIdDataKey);
/// <summary> /// <summary>
/// 通过角色ID保存当前授权用户插入 /// 通过角色ID保存当前授权用户插入
/// </summary> /// </summary>
@ -87,7 +93,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="groupId"></param> /// <param name="groupId"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<User> RetrieveUsersByGroupId(int groupId) => DbAdapterManager.Create<User>().RetrieveUsersByGroupId(groupId); public static IEnumerable<User> RetrieveUsersByGroupId(int groupId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveUsersByGroupIdDataKey, groupId), k => DbAdapterManager.Create<User>().RetrieveUsersByGroupId(groupId), RetrieveUsersByRoleIdDataKey);
/// <summary> /// <summary>
/// 通过部门ID保存当前授权用户插入 /// 通过部门ID保存当前授权用户插入
/// </summary> /// </summary>
@ -118,8 +124,8 @@ namespace Bootstrap.DataAccess
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="name"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public static BootstrapUser RetrieveUserByUserName(string name) => DbAdapterManager.Create<User>().RetrieveUserByUserName(name); public static BootstrapUser RetrieveUserByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName), k => DbAdapterManager.Create<User>().RetrieveUserByUserName(userName), RetrieveUsersByNameDataKey);
} }
} }

View File

@ -4,7 +4,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
using System.Linq;
namespace Bootstrap.DataAccess namespace Bootstrap.DataAccess
{ {
@ -13,10 +12,6 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public class Log public class Log
{ {
/// <summary>
///
/// </summary>
protected const string RetrieveLogsDataKey = "LogHelper-RetrieveLogs";
/// <summary> /// <summary>
/// 获得/设置 操作日志主键ID /// 获得/设置 操作日志主键ID
/// </summary> /// </summary>
@ -58,30 +53,26 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Log> RetrieveLogs(string tId = null) public virtual IEnumerable<Log> RetrieveLogs(string tId = null)
{ {
var ret = CacheManager.GetOrAdd(RetrieveLogsDataKey, key => string sql = "select * from Logs where DATEDIFF(Week, LogTime, GETDATE()) = 0";
List<Log> logs = new List<Log>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
string sql = "select * from Logs where DATEDIFF(Week, LogTime, GETDATE()) = 0"; while (reader.Read())
List<Log> logs = new List<Log>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) logs.Add(new Log()
{ {
logs.Add(new Log() Id = (int)reader[0],
{ CRUD = (string)reader[1],
Id = (int)reader[0], UserName = (string)reader[2],
CRUD = (string)reader[1], LogTime = (DateTime)reader[3],
UserName = (string)reader[2], ClientIp = (string)reader[4],
LogTime = (DateTime)reader[3], ClientAgent = (string)reader[5],
ClientIp = (string)reader[4], RequestUrl = (string)reader[6]
ClientAgent = (string)reader[5], });
RequestUrl = (string)reader[6]
});
}
} }
return logs; }
}); return logs;
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase));
} }
/// <summary> /// <summary>
/// 删除日志信息 /// 删除日志信息
@ -116,7 +107,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RequestUrl", p.RequestUrl)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RequestUrl", p.RequestUrl));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1; ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
} }
CacheManager.Clear(RetrieveLogsDataKey); CacheManager.Clear(LogHelper.RetrieveLogsDataKey);
DeleteLogAsync(); DeleteLogAsync();
return ret; return ret;
} }

View File

@ -1,7 +1,6 @@
using Bootstrap.Security; using Bootstrap.Security;
using Bootstrap.Security.SQLServer; using Bootstrap.Security.SQLServer;
using Longbow; using Longbow;
using Longbow.Cache;
using Longbow.Data; using Longbow.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -17,18 +16,6 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public class Menu : BootstrapMenu public class Menu : BootstrapMenu
{ {
/// <summary>
///
/// </summary>
public const string RetrieveMenusByRoleIdDataKey = "MenuHelper-RetrieveMenusByRoleId";
/// <summary>
///
/// </summary>
public const string RetrieveMenusDataKey = "BootstrapMenu-RetrieveMenusByUserName";
/// <summary>
///
/// </summary>
public const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus";
/// <summary> /// <summary>
/// 删除菜单信息 /// 删除菜单信息
/// </summary> /// </summary>
@ -84,27 +71,23 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<BootstrapMenu> RetrieveMenusByRoleId(int roleId) public virtual IEnumerable<BootstrapMenu> RetrieveMenusByRoleId(int roleId)
{ {
string key = string.Format("{0}-{1}", RetrieveMenusByRoleIdDataKey, roleId); var menus = new List<BootstrapMenu>();
return CacheManager.GetOrAdd(key, k => string sql = "select NavigationID from NavigationRole where RoleID = @RoleID";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{ {
var menus = new List<BootstrapMenu>(); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RoleID", roleId));
string sql = "select NavigationID from NavigationRole where RoleID = @RoleID"; using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{ {
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RoleID", roleId)); while (reader.Read())
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) menus.Add(new BootstrapMenu()
{ {
menus.Add(new BootstrapMenu() Id = LgbConvert.ReadValue(reader[0], 0)
{ });
Id = LgbConvert.ReadValue(reader[0], 0)
});
}
} }
} }
return menus; }
}, RetrieveMenusByRoleIdDataKey); return menus;
} }
/// <summary> /// <summary>
/// 通过角色ID保存当前授权菜单 /// 通过角色ID保存当前授权菜单

View File

@ -1,5 +1,4 @@
using Longbow; using Longbow;
using Longbow.Cache;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -13,7 +12,6 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public class Message public class Message
{ {
protected const string RetrieveMessageDataKey = "MessageHelper-RetrieveMessages";
/// <summary> /// <summary>
/// 消息主键 数据库自增 /// 消息主键 数据库自增
/// </summary> /// </summary>
@ -77,38 +75,33 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
protected virtual IEnumerable<Message> RetrieveMessages(string userName) protected virtual IEnumerable<Message> RetrieveMessages(string userName)
{ {
var messageRet = CacheManager.GetOrAdd(RetrieveMessageDataKey, key => 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";
List<Message> messages = new List<Message>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserName", userName));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
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"; while (reader.Read())
List<Message> messages = new List<Message>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserName", userName));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) messages.Add(new Message()
{ {
messages.Add(new Message() Id = (int)reader[0],
{ Title = (string)reader[1],
Id = (int)reader[0], Content = (string)reader[2],
Title = (string)reader[1], From = (string)reader[3],
Content = (string)reader[2], To = (string)reader[4],
From = (string)reader[3], SendTime = LgbConvert.ReadValue(reader[5], DateTime.MinValue),
To = (string)reader[4], Status = (string)reader[6],
SendTime = LgbConvert.ReadValue(reader[5], DateTime.MinValue), Mark = (int)reader[7],
Status = (string)reader[6], IsDelete = (int)reader[8],
Mark = (int)reader[7], Label = (string)reader[9],
IsDelete = (int)reader[8], LabelName = LgbConvert.ReadValue(reader[10], string.Empty),
Label = (string)reader[9], FromIcon = (string)reader[11],
LabelName = LgbConvert.ReadValue(reader[10], string.Empty), FromDisplayName = (string)reader[12]
FromIcon = (string)reader[11], });
FromDisplayName = (string)reader[12]
});
}
} }
return messages; }
return messages;
});
return messageRet.OrderByDescending(n => n.SendTime);
} }
/// <summary> /// <summary>
/// 收件箱 /// 收件箱

View File

@ -1,6 +1,5 @@
using Bootstrap.Security.SQLServer; using Bootstrap.Security.SQLServer;
using Longbow; using Longbow;
using Longbow.Cache;
using Longbow.Data; using Longbow.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -16,12 +15,6 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public class Role public class Role
{ {
public const string RetrieveRolesDataKey = "RoleHelper-RetrieveRoles";
public const string RetrieveRolesByUserIdDataKey = "RoleHelper-RetrieveRolesByUserId";
public const string RetrieveRolesByMenuIdDataKey = "RoleHelper-RetrieveRolesByMenuId";
public const string RetrieveRolesByGroupIdDataKey = "RoleHelper-RetrieveRolesByGroupId";
public const string RetrieveRolesByUserNameDataKey = "BootstrapAdminRoleMiddleware-RetrieveRolesByUserName";
public const string RetrieveRolesByUrlDataKey = "BootstrapAdminAuthorizeFilter-RetrieveRolesByUrl";
/// <summary> /// <summary>
/// 获得/设置 角色主键ID /// 获得/设置 角色主键ID
/// </summary> /// </summary>
@ -45,26 +38,22 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Role> RetrieveRoles(int id = 0) public virtual IEnumerable<Role> RetrieveRoles(int id = 0)
{ {
var ret = CacheManager.GetOrAdd(RetrieveRolesDataKey, key => string sql = "select * from Roles";
var roles = new List<Role>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
string sql = "select * from Roles"; while (reader.Read())
var roles = new List<Role>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) roles.Add(new Role()
{ {
roles.Add(new Role() Id = LgbConvert.ReadValue(reader[0], 0),
{ RoleName = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2]
RoleName = (string)reader[1], });
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2]
});
}
} }
return roles; }
}); return roles;
return id == 0 ? ret : ret.Where(t => id == t.Id);
} }
/// <summary> /// <summary>
/// 保存用户角色关系 /// 保存用户角色关系
@ -119,28 +108,24 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Role> RetrieveRolesByUserId(int userId) public virtual IEnumerable<Role> RetrieveRolesByUserId(int userId)
{ {
string key = string.Format("{0}-{1}", RetrieveRolesByUserIdDataKey, userId); List<Role> roles = new List<Role>();
return CacheManager.GetOrAdd(key, k => string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join UserRole ur on r.ID = ur.RoleID and UserID = @UserID";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserID", userId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
List<Role> roles = new List<Role>(); while (reader.Read())
string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join UserRole ur on r.ID = ur.RoleID and UserID = @UserID";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserID", userId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) roles.Add(new Role()
{ {
roles.Add(new Role() Id = LgbConvert.ReadValue(reader[0], 0),
{ RoleName = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
RoleName = (string)reader[1], Checked = (string)reader[3]
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], });
Checked = (string)reader[3]
});
}
} }
return roles; }
}, RetrieveRolesByUserIdDataKey); return roles;
} }
/// <summary> /// <summary>
/// 删除角色表 /// 删除角色表
@ -188,29 +173,24 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Role> RetrieveRolesByMenuId(int menuId) public virtual IEnumerable<Role> RetrieveRolesByMenuId(int menuId)
{ {
string key = string.Format("{0}-{1}", RetrieveRolesByMenuIdDataKey, menuId); string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join NavigationRole ur on r.ID = ur.RoleID and NavigationID = @NavigationID";
var ret = CacheManager.GetOrAdd(key, k => List<Role> roles = new List<Role>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@NavigationID", menuId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join NavigationRole ur on r.ID = ur.RoleID and NavigationID = @NavigationID"; while (reader.Read())
List<Role> roles = new List<Role>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@NavigationID", menuId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) roles.Add(new Role()
{ {
roles.Add(new Role() Id = LgbConvert.ReadValue(reader[0], 0),
{ RoleName = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
RoleName = (string)reader[1], Checked = (string)reader[3]
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], });
Checked = (string)reader[3]
});
}
} }
return roles; }
}, RetrieveRolesByMenuIdDataKey); return roles;
return ret;
} }
/// <summary> /// <summary>
/// ///
@ -265,28 +245,24 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Role> RetrieveRolesByGroupId(int groupId) public virtual IEnumerable<Role> RetrieveRolesByGroupId(int groupId)
{ {
string key = string.Format("{0}-{1}", RetrieveRolesByGroupIdDataKey, groupId); List<Role> roles = new List<Role>();
return CacheManager.GetOrAdd(key, k => string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join RoleGroup ur on r.ID = ur.RoleID and GroupID = @GroupID";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@GroupID", groupId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
List<Role> roles = new List<Role>(); while (reader.Read())
string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join RoleGroup ur on r.ID = ur.RoleID and GroupID = @GroupID";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@GroupID", groupId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) roles.Add(new Role()
{ {
roles.Add(new Role() Id = LgbConvert.ReadValue(reader[0], 0),
{ RoleName = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
RoleName = (string)reader[1], Checked = (string)reader[3]
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2], });
Checked = (string)reader[3]
});
}
} }
return roles; }
}, RetrieveRolesByGroupIdDataKey); return roles;
} }
/// <summary> /// <summary>
/// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息 /// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息

View File

@ -1,5 +1,4 @@
using Longbow.Cache; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
@ -8,7 +7,6 @@ namespace Bootstrap.DataAccess
{ {
public class Task public class Task
{ {
protected const string RetrieveTasksDataKey = "TaskHelper-RetrieveTasks";
/// <summary> /// <summary>
/// 获取/设置 任务ID /// 获取/设置 任务ID
/// </summary> /// </summary>
@ -47,30 +45,27 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Task> RetrieveTasks() public virtual IEnumerable<Task> RetrieveTasks()
{ {
return CacheManager.GetOrAdd(RetrieveTasksDataKey, key => string sql = "select top 1000 t.*, u.DisplayName from Tasks t inner join Users u on t.UserName = u.UserName order by AssignTime desc";
List<Task> tasks = new List<Task>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
string sql = "select top 1000 t.*, u.DisplayName from Tasks t inner join Users u on t.UserName = u.UserName order by AssignTime desc"; while (reader.Read())
List<Task> tasks = new List<Task>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) tasks.Add(new Task()
{ {
tasks.Add(new Task() Id = (int)reader[0],
{ TaskName = (string)reader[1],
Id = (int)reader[0], AssignName = (string)reader[2],
TaskName = (string)reader[1], UserName = (string)reader[3],
AssignName = (string)reader[2], TaskTime = (int)reader[4],
UserName = (string)reader[3], TaskProgress = (double)reader[5],
TaskTime = (int)reader[4], AssignTime = (DateTime)reader[6],
TaskProgress = (double)reader[5], AssignDisplayName = (string)reader[7]
AssignTime = (DateTime)reader[6], });
AssignDisplayName = (string)reader[7]
});
}
} }
return tasks; }
}); return tasks;
} }
} }
} }

View File

@ -1,7 +1,6 @@
using Bootstrap.Security; using Bootstrap.Security;
using Bootstrap.Security.SQLServer; using Bootstrap.Security.SQLServer;
using Longbow; using Longbow;
using Longbow.Cache;
using Longbow.Data; using Longbow.Data;
using Longbow.Security.Cryptography; using Longbow.Security.Cryptography;
using System; using System;
@ -18,11 +17,6 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public class User : BootstrapUser public class User : BootstrapUser
{ {
public const string RetrieveUsersDataKey = "BootstrapUser-RetrieveUsers";
public const string RetrieveUsersByRoleIdDataKey = "BootstrapUser-RetrieveUsersByRoleId";
public const string RetrieveUsersByGroupIdDataKey = "BootstrapUser-RetrieveUsersByGroupId";
public const string RetrieveNewUsersDataKey = "UserHelper-RetrieveNewUsers";
protected const string RetrieveUsersByNameDataKey = "BootstrapUser-RetrieveUsersByName";
/// <summary> /// <summary>
/// 获得/设置 用户主键ID /// 获得/设置 用户主键ID
/// </summary> /// </summary>
@ -126,29 +120,26 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<User> RetrieveUsers() public virtual IEnumerable<User> RetrieveUsers()
{ {
return CacheManager.GetOrAdd(RetrieveUsersDataKey, key => List<User> users = new List<User>();
{ DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, "select ID, UserName, DisplayName, RegisterTime, ApprovedTime, ApprovedBy, Description from Users Where ApprovedTime is not null");
List<User> users = new List<User>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, "select ID, UserName, DisplayName, RegisterTime, ApprovedTime, ApprovedBy, Description from Users Where ApprovedTime is not null");
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd)) using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{
while (reader.Read())
{ {
while (reader.Read()) users.Add(new User()
{ {
users.Add(new User() Id = LgbConvert.ReadValue(reader[0], 0),
{ UserName = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), DisplayName = (string)reader[2],
UserName = (string)reader[1], RegisterTime = LgbConvert.ReadValue(reader[3], DateTime.MinValue),
DisplayName = (string)reader[2], ApprovedTime = LgbConvert.ReadValue(reader[4], DateTime.MinValue),
RegisterTime = LgbConvert.ReadValue(reader[3], DateTime.MinValue), ApprovedBy = reader.IsDBNull(5) ? string.Empty : (string)reader[5],
ApprovedTime = LgbConvert.ReadValue(reader[4], DateTime.MinValue), Description = (string)reader[6]
ApprovedBy = reader.IsDBNull(5) ? string.Empty : (string)reader[5], });
Description = (string)reader[6]
});
}
} }
return users; }
}); return users;
} }
/// <summary> /// <summary>
/// 查询所有的新注册用户 /// 查询所有的新注册用户
@ -156,27 +147,24 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<User> RetrieveNewUsers() public virtual IEnumerable<User> RetrieveNewUsers()
{ {
return CacheManager.GetOrAdd(RetrieveNewUsersDataKey, key => string sql = "select ID, UserName, DisplayName, RegisterTime, [Description] from Users Where ApprovedTime is null order by RegisterTime desc";
List<User> users = new List<User>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
string sql = "select ID, UserName, DisplayName, RegisterTime, [Description] from Users Where ApprovedTime is null order by RegisterTime desc"; while (reader.Read())
List<User> users = new List<User>();
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) users.Add(new User()
{ {
users.Add(new User() Id = LgbConvert.ReadValue(reader[0], 0),
{ UserName = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), DisplayName = (string)reader[2],
UserName = (string)reader[1], RegisterTime = LgbConvert.ReadValue(reader[3], DateTime.MinValue),
DisplayName = (string)reader[2], Description = (string)reader[4]
RegisterTime = LgbConvert.ReadValue(reader[3], DateTime.MinValue), });
Description = (string)reader[4]
});
}
} }
return users; }
}); return users;
} }
/// <summary> /// <summary>
/// 删除用户 /// 删除用户
@ -289,28 +277,24 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<User> RetrieveUsersByRoleId(int roleId) public virtual IEnumerable<User> RetrieveUsersByRoleId(int roleId)
{ {
string key = string.Format("{0}-{1}", RetrieveUsersByRoleIdDataKey, roleId); List<User> users = new List<User>();
return CacheManager.GetOrAdd(key, k => string sql = "select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserRole ur on u.ID = ur.UserID and RoleID = @RoleID where u.ApprovedTime is not null";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RoleID", roleId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
List<User> users = new List<User>(); while (reader.Read())
string sql = "select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserRole ur on u.ID = ur.UserID and RoleID = @RoleID where u.ApprovedTime is not null";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RoleID", roleId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) users.Add(new User()
{ {
users.Add(new User() Id = LgbConvert.ReadValue(reader[0], 0),
{ UserName = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), DisplayName = (string)reader[2],
UserName = (string)reader[1], Checked = (string)reader[3]
DisplayName = (string)reader[2], });
Checked = (string)reader[3]
});
}
} }
return users; }
}, RetrieveUsersByRoleIdDataKey); return users;
} }
/// <summary> /// <summary>
/// 通过角色ID保存当前授权用户插入 /// 通过角色ID保存当前授权用户插入
@ -362,28 +346,24 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<User> RetrieveUsersByGroupId(int groupId) public virtual IEnumerable<User> RetrieveUsersByGroupId(int groupId)
{ {
string key = string.Format("{0}-{1}", RetrieveUsersByGroupIdDataKey, groupId); List<User> users = new List<User>();
return CacheManager.GetOrAdd(key, k => string sql = "select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserGroup ur on u.ID = ur.UserID and GroupID =@groupId where u.ApprovedTime is not null";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@GroupID", groupId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
List<User> users = new List<User>(); while (reader.Read())
string sql = "select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserGroup ur on u.ID = ur.UserID and GroupID =@groupId where u.ApprovedTime is not null";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@GroupID", groupId));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{ {
while (reader.Read()) users.Add(new User()
{ {
users.Add(new User() Id = LgbConvert.ReadValue(reader[0], 0),
{ UserName = (string)reader[1],
Id = LgbConvert.ReadValue(reader[0], 0), DisplayName = (string)reader[2],
UserName = (string)reader[1], Checked = (string)reader[3]
DisplayName = (string)reader[2], });
Checked = (string)reader[3]
});
}
} }
return users; }
}, RetrieveUsersByRoleIdDataKey); return users;
} }
/// <summary> /// <summary>
/// 通过部门ID保存当前授权用户插入 /// 通过部门ID保存当前授权用户插入
@ -444,7 +424,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@iconName", iconName)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@iconName", iconName));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", userName)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", userName));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1; ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{UserHelper.RetrieveUsersDataKey}*");
} }
return ret; return ret;
} }
@ -463,7 +443,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@DisplayName", displayName)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@DisplayName", displayName));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", userName)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", userName));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1; ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{UserHelper.RetrieveUsersDataKey}*");
} }
return ret; return ret;
} }
@ -482,7 +462,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@cssName", DbAdapterManager.ToDBValue(cssName))); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@cssName", DbAdapterManager.ToDBValue(cssName)));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", userName)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", userName));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1; ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{UserHelper.RetrieveUsersDataKey}*");
} }
return ret; return ret;
} }