重构代码:删除Bootstrap.DataAccess.SQLite类中可以服用父类的方法,精简代码

This commit is contained in:
Argo-MacBookPro 2018-10-21 10:27:43 +08:00
parent 7ad51a6ecd
commit b0111b8431
12 changed files with 39 additions and 800 deletions

View File

@ -1,168 +1,10 @@
using Bootstrap.Security;
using Longbow.Cache;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Linq;
namespace Bootstrap.DataAccess.SQLite
namespace Bootstrap.DataAccess.SQLite
{
/// <summary>
///
/// </summary>
public class Dict : DataAccess.Dict
{
/// <summary>
/// 删除字典中的数据
/// </summary>
/// <param name="value">需要删除的IDs</param>
/// <returns></returns>
public override bool DeleteDict(IEnumerable<int> value)
{
var ret = false;
var ids = string.Join(",", value);
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Dicts where ID in ({0})", ids);
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == value.Count();
CacheCleanUtility.ClearCache(dictIds: ids);
}
return ret;
}
/// <summary>
/// 保存新建/更新的字典信息
/// </summary>
/// <param name="dict"></param>
/// <returns></returns>
public override bool SaveDict(BootstrapDict dict)
{
bool ret = false;
if (dict.Category.Length > 50) dict.Category = dict.Category.Substring(0, 50);
if (dict.Name.Length > 50) dict.Name = dict.Name.Substring(0, 50);
if (dict.Code.Length > 50) dict.Code = dict.Code.Substring(0, 50);
string sql = dict.Id == 0 ?
"Insert Into Dicts (Category, Name, Code ,Define) Values (@Category, @Name, @Code, @Define)" :
"Update Dicts set Category = @Category, Name = @Name, Code = @Code, Define = @Define where ID = @ID";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ID", dict.Id));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Category", dict.Category));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Name", dict.Name));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Code", dict.Code));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Define", dict.Define));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
}
CacheCleanUtility.ClearCache(dictIds: dict.Id == 0 ? string.Empty : dict.Id.ToString());
return ret;
}
/// <summary>
/// 保存网站个性化设置
/// </summary>
/// <param name="name"></param>
/// <param name="code"></param>
/// <param name="category"></param>
/// <returns></returns>
public override bool SaveSettings(BootstrapDict dict)
{
var ret = false;
string sql = "Update Dicts set Code = @Code where Category = @Category and Name = @Name";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Name", dict.Name));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Code", dict.Code));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Category", dict.Category));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
}
CacheCleanUtility.ClearCache(dictIds: string.Empty);
return ret;
}
/// <summary>
/// 获取字典分类名称
/// </summary>
/// <returns></returns>
public override 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))
{
while (reader.Read())
{
ret.Add((string)reader[0]);
}
}
return ret;
});
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string RetrieveWebTitle()
{
var settings = RetrieveDicts();
return (settings.FirstOrDefault(d => d.Name == "网站标题" && d.Category == "网站设置" && d.Define == 0) ?? new BootstrapDict() { Code = "后台管理系统" }).Code;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string RetrieveWebFooter()
{
var settings = RetrieveDicts();
return (settings.FirstOrDefault(d => d.Name == "网站页脚" && d.Category == "网站设置" && d.Define == 0) ?? new BootstrapDict() { Code = "2016 © 通用后台管理系统" }).Code;
}
/// <summary>
/// 获得系统中配置的可以使用的网站样式
/// </summary>
/// <returns></returns>
public override IEnumerable<BootstrapDict> RetrieveThemes()
{
var data = RetrieveDicts();
return data.Where(d => d.Category == "网站样式");
}
/// <summary>
/// 获得网站设置中的当前样式
/// </summary>
/// <returns></returns>
public override string RetrieveActiveTheme()
{
var data = RetrieveDicts();
var theme = data.Where(d => d.Name == "使用样式" && d.Category == "当前样式" && d.Define == 0).FirstOrDefault();
return theme == null ? string.Empty : (theme.Code.Equals("site.css", StringComparison.OrdinalIgnoreCase) ? string.Empty : theme.Code);
}
/// <summary>
/// 获取头像路径
/// </summary>
/// <returns></returns>
public override BootstrapDict RetrieveIconFolderPath()
{
var data = RetrieveDicts();
return data.FirstOrDefault(d => d.Name == "头像路径" && d.Category == "头像地址" && d.Define == 0) ?? new BootstrapDict() { Code = "~/images/uploader/" };
}
/// <summary>
/// 获得默认的前台首页地址,默认为~/Home/Index
/// </summary>
/// <returns></returns>
public override string RetrieveHomeUrl()
{
var settings = RetrieveDicts();
return (settings.FirstOrDefault(d => d.Name == "前台首页" && d.Category == "网站设置" && d.Define == 0) ?? new BootstrapDict() { Code = "~/Home/Index" }).Code;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override IEnumerable<KeyValuePair<string, string>> RetrieveApps()
{
var settings = RetrieveDicts();
return settings.Where(d => d.Category == "应用程序" && d.Define == 0).Select(d => new KeyValuePair<string, string>(d.Code, d.Name)).OrderBy(d => d.Key);
}
}
}

View File

@ -15,6 +15,18 @@ namespace Bootstrap.DataAccess.SQLite
/// </summary>
public class Exceptions : DataAccess.Exceptions
{
/// <summary>
///
/// </summary>
private static void ClearExceptions()
{
System.Threading.Tasks.Task.Run(() =>
{
string sql = $"delete from Exceptions where LogTime < datetime('now', 'localtime', '-{ConfigurationManager.AppSettings["KeepExceptionsPeriod"]} month')";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
DbAccessManager.DBAccess.ExecuteNonQuery(cmd);
});
}
/// <summary>
///
/// </summary>
@ -33,7 +45,7 @@ namespace Bootstrap.DataAccess.SQLite
};
}
var errorPage = additionalInfo["ErrorPage"] ?? (nameof(ex).Length > 50 ? nameof(ex).Substring(0, 50) : nameof(ex));
var sql = "insert into Exceptions (ID, AppDomainName, ErrorPage, UserID, UserIp, ExceptionType, Message, StackTrace, LogTime) values (NULL, @AppDomainName, @ErrorPage, @UserID, @UserIp, @ExceptionType, @Message, @StackTrace, datetime('now', 'localtime'))";
var sql = "insert into Exceptions (AppDomainName, ErrorPage, UserID, UserIp, ExceptionType, Message, StackTrace, LogTime) values (@AppDomainName, @ErrorPage, @UserID, @UserIp, @ExceptionType, @Message, @StackTrace, datetime('now', 'localtime'))";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@AppDomainName", AppDomain.CurrentDomain.FriendlyName));
@ -49,18 +61,6 @@ namespace Bootstrap.DataAccess.SQLite
}
}
/// <summary>
///
/// </summary>
private static void ClearExceptions()
{
System.Threading.Tasks.Task.Run(() =>
{
string sql = $"delete from Exceptions where LogTime < datetime('now', 'localtime', '-{ConfigurationManager.AppSettings["KeepExceptionsPeriod"]} month')";
DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
DbAccessManager.DBAccess.ExecuteNonQuery(cmd);
});
}
/// <summary>
/// 查询一周内所有异常
/// </summary>
/// <returns></returns>

View File

@ -1,6 +1,4 @@
using Longbow;
using Longbow.Cache;
using Longbow.Data;
using Longbow.Data;
using System;
using System.Collections.Generic;
using System.Data;
@ -16,33 +14,6 @@ namespace Bootstrap.DataAccess.SQLite
public class Group : DataAccess.Group
{
/// <summary>
/// 查询所有群组信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.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))
{
while (reader.Read())
{
groups.Add(new Group()
{
Id = LgbConvert.ReadValue(reader[0], 0),
GroupName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2]
});
}
}
return groups;
});
return id == 0 ? ret : ret.Where(t => id == t.Id);
}
/// <summary>
/// 删除群组信息
/// </summary>
@ -60,60 +31,6 @@ namespace Bootstrap.DataAccess.SQLite
return ret;
}
/// <summary>
/// 保存新建/更新的群组信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public override bool SaveGroup(DataAccess.Group p)
{
bool ret = false;
if (p.GroupName.Length > 50) p.GroupName = p.GroupName.Substring(0, 50);
if (!string.IsNullOrEmpty(p.Description) && p.Description.Length > 500) p.Description = p.Description.Substring(0, 500);
string sql = p.Id == 0 ?
"Insert Into Groups (GroupName, Description) Values (@GroupName, @Description)" :
"Update Groups set GroupName = @GroupName, Description = @Description where ID = @ID";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ID", p.Id));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@GroupName", p.GroupName));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Description", DbAccessFactory.ToDBValue(p.Description)));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
}
CacheCleanUtility.ClearCache(groupIds: p.Id == 0 ? new List<int>() : new List<int>() { p.Id });
return ret;
}
/// <summary>
/// 根据用户查询部门信息
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Group> RetrieveGroupsByUserId(int userId)
{
string key = string.Format("{0}-{1}", RetrieveGroupsByUserIdDataKey, userId);
var ret = CacheManager.GetOrAdd(key, k =>
{
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";
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()
{
Id = LgbConvert.ReadValue(reader[0], 0),
GroupName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
Checked = (string)reader[3]
});
}
}
return groups;
}, RetrieveGroupsByUserIdDataKey);
return ret;
}
/// <summary>
/// 保存用户部门关系
/// </summary>
/// <param name="id"></param>
@ -161,36 +78,6 @@ namespace Bootstrap.DataAccess.SQLite
return ret;
}
/// <summary>
/// 根据角色ID指派部门
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Group> RetrieveGroupsByRoleId(int roleId)
{
string k = string.Format("{0}-{1}", RetrieveGroupsByRoleIdDataKey, roleId);
return CacheManager.GetOrAdd(k, key =>
{
List<Group> groups = new List<Group>();
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()
{
Id = LgbConvert.ReadValue(reader[0], 0),
GroupName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
Checked = (string)reader[3]
});
}
}
return groups;
}, RetrieveGroupsByRoleIdDataKey);
}
/// <summary>
/// 根据角色ID以及选定的部门ID保到角色部门表
/// </summary>
/// <param name="id"></param>

View File

@ -1,7 +1,4 @@
using Bootstrap.Security;
using Longbow;
using Longbow.Cache;
using Longbow.Data;
using Longbow.Data;
using System;
using System.Collections.Generic;
using System.Data;
@ -33,67 +30,6 @@ namespace Bootstrap.DataAccess.SQLite
return ret;
}
/// <summary>
/// 保存新建/更新的菜单信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public override bool SaveMenu(BootstrapMenu p)
{
if (string.IsNullOrEmpty(p.Name)) return false;
bool ret = false;
if (p.Name.Length > 50) p.Name = p.Name.Substring(0, 50);
if (p.Icon != null && p.Icon.Length > 50) p.Icon = p.Icon.Substring(0, 50);
if (p.Url != null && p.Url.Length > 4000) p.Url = p.Url.Substring(0, 4000);
string sql = p.Id == 0 ?
"Insert Into Navigations (ParentId, Name, [Order], Icon, Url, Category, Target, IsResource, [Application]) Values (@ParentId, @Name, @Order, @Icon, @Url, @Category, @Target, @IsResource, @ApplicationCode)" :
"Update Navigations set ParentId = @ParentId, Name = @Name, [Order] = @Order, Icon = @Icon, Url = @Url, Category = @Category, Target = @Target, IsResource = @IsResource, Application = @ApplicationCode where ID = @ID";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ID", p.Id));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ParentId", p.ParentId));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Name", p.Name));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Order", p.Order));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Icon", DbAccessFactory.ToDBValue(p.Icon)));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Url", DbAccessFactory.ToDBValue(p.Url)));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Category", p.Category));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Target", p.Target));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@IsResource", p.IsResource));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ApplicationCode", p.ApplicationCode));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
}
CacheCleanUtility.ClearCache(menuIds: p.Id == 0 ? new List<int>() : new List<int>() { p.Id });
return ret;
}
/// <summary>
/// 查询某个角色所配置的菜单
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public override IEnumerable<BootstrapMenu> RetrieveMenusByRoleId(int roleId)
{
string key = string.Format("{0}-{1}", RetrieveMenusByRoleIdDataKey, roleId);
return CacheManager.GetOrAdd(key, k =>
{
var menus = new List<BootstrapMenu>();
string sql = "select NavigationID from NavigationRole where RoleID = @RoleID";
using (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())
{
menus.Add(new BootstrapMenu()
{
Id = LgbConvert.ReadValue(reader[0], 0)
});
}
}
}
return menus;
}, RetrieveMenusByRoleIdDataKey);
}
/// <summary>
/// 通过角色ID保存当前授权菜单
/// </summary>
@ -138,33 +74,5 @@ namespace Bootstrap.DataAccess.SQLite
}
return ret;
}
///// <summary>
/////
///// </summary>
///// <param name="userName"></param>
///// <param name="activeUrl"></param>
///// <returns></returns>
//public override IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName, string activeUrl = null) => RetrieveAllMenus(DBAccessManager.DBAccess, userName, activeUrl);
///// <summary>
/////
///// </summary>
///// <param name="userName"></param>
///// <param name="activeUrl"></param>
///// <returns></returns>
//public override IEnumerable<BootstrapMenu> RetrieveAppMenus(string userName, string activeUrl = null) => RetrieveAppMenus(DBAccessManager.DBAccess, userName, activeUrl);
///// <summary>
/////
///// </summary>
///// <param name="userName"></param>
///// <param name="activeUrl"></param>
///// <returns></returns>
//public override IEnumerable<BootstrapMenu> RetrieveMenusByUserName(string userName, string activeUrl = null) => RetrieveMenusByUserName(DBAccessManager.DBAccess, userName, activeUrl);
///// <summary>
/////
///// </summary>
///// <param name="userName"></param>
///// <param name="activeUrl"></param>
///// <returns></returns>
//public override IEnumerable<BootstrapMenu> RetrieveSystemMenus(string userName, string activeUrl = null) => RetrieveSystemMenus(DBAccessManager.DBAccess, userName, activeUrl);
}
}

View File

@ -18,7 +18,7 @@ namespace Bootstrap.DataAccess.SQLite
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
private static IEnumerable<DataAccess.Message> RetrieveMessages(string userName)
protected override IEnumerable<DataAccess.Message> RetrieveMessages(string userName)
{
var messageRet = CacheManager.GetOrAdd(RetrieveMessageDataKey, key =>
{
@ -53,62 +53,5 @@ namespace Bootstrap.DataAccess.SQLite
});
return messageRet.OrderByDescending(n => n.SendTime);
}
/// <summary>
/// 收件箱
/// </summary>
/// <param name="userName"></param>
public override IEnumerable<DataAccess.Message> Inbox(string userName)
{
var messageRet = RetrieveMessages(userName);
return messageRet.Where(n => n.To.Equals(userName, StringComparison.OrdinalIgnoreCase));
}
/// <summary>
/// 发件箱
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Message> SendMail(string userName)
{
var messageRet = RetrieveMessages(userName);
return messageRet.Where(n => n.From.Equals(userName, StringComparison.OrdinalIgnoreCase));
}
/// <summary>
/// 垃圾箱
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Message> Trash(string userName)
{
var messageRet = RetrieveMessages(userName);
return messageRet.Where(n => n.IsDelete == 1);
}
/// <summary>
/// 标旗
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Message> Flag(string userName)
{
var messageRet = RetrieveMessages(userName);
return messageRet.Where(n => n.Mark == 1);
}
/// <summary>
/// 获取Header处显示的消息列表
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Message> RetrieveMessagesHeader(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;
}
}
}

View File

@ -1,6 +1,4 @@
using Longbow;
using Longbow.Cache;
using Longbow.Data;
using Longbow.Data;
using System;
using System.Collections.Generic;
using System.Data;
@ -16,33 +14,6 @@ namespace Bootstrap.DataAccess.SQLite
public class Role : DataAccess.Role
{
/// <summary>
/// 查询所有角色
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.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))
{
while (reader.Read())
{
roles.Add(new Role()
{
Id = LgbConvert.ReadValue(reader[0], 0),
RoleName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2]
});
}
}
return roles;
});
return id == 0 ? ret : ret.Where(t => id == t.Id);
}
/// <summary>
/// 保存用户角色关系
/// </summary>
@ -92,35 +63,6 @@ namespace Bootstrap.DataAccess.SQLite
return ret;
}
/// <summary>
/// 查询某个用户所拥有的角色
/// </summary>
/// <returns></returns>
public override IEnumerable<DataAccess.Role> RetrieveRolesByUserId(int userId)
{
string key = string.Format("{0}-{1}", RetrieveRolesByUserIdDataKey, userId);
return CacheManager.GetOrAdd(key, k =>
{
List<Role> roles = new List<Role>();
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()
{
Id = LgbConvert.ReadValue(reader[0], 0),
RoleName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
Checked = (string)reader[3]
});
}
}
return roles;
}, RetrieveRolesByUserIdDataKey);
}
/// <summary>
/// 删除角色表
/// </summary>
/// <param name="value"></param>
@ -137,60 +79,6 @@ namespace Bootstrap.DataAccess.SQLite
return ret;
}
/// <summary>
/// 保存新建/更新的角色信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public override bool SaveRole(DataAccess.Role p)
{
bool ret = false;
if (!string.IsNullOrEmpty(p.RoleName) && p.RoleName.Length > 50) p.RoleName = p.RoleName.Substring(0, 50);
if (!string.IsNullOrEmpty(p.Description) && p.Description.Length > 50) p.Description = p.Description.Substring(0, 500);
string sql = p.Id == 0 ?
"Insert Into Roles (RoleName, Description) Values (@RoleName, @Description)" :
"Update Roles set RoleName = @RoleName, Description = @Description where ID = @ID";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ID", p.Id));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RoleName", p.RoleName));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Description", DbAccessFactory.ToDBValue(p.Description)));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
}
CacheCleanUtility.ClearCache(roleIds: p.Id == 0 ? new List<int>() : new List<int> { p.Id });
return ret;
}
/// <summary>
/// 查询某个菜单所拥有的角色
/// </summary>
/// <param name="menuId"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Role> RetrieveRolesByMenuId(int menuId)
{
string key = string.Format("{0}-{1}", RetrieveRolesByMenuIdDataKey, menuId);
var ret = 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 NavigationRole ur on r.ID = ur.RoleID and NavigationID = @NavigationID";
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()
{
Id = LgbConvert.ReadValue(reader[0], 0),
RoleName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
Checked = (string)reader[3]
});
}
}
return roles;
}, RetrieveRolesByMenuIdDataKey);
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
@ -237,37 +125,6 @@ namespace Bootstrap.DataAccess.SQLite
}
return ret;
}
/// <summary>
/// 根据GroupId查询和该Group有关的所有Roles
/// </summary>
/// <param name="groupId"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Role> RetrieveRolesByGroupId(int groupId)
{
string key = string.Format("{0}-{1}", RetrieveRolesByGroupIdDataKey, groupId);
return CacheManager.GetOrAdd(key, k =>
{
List<Role> roles = new List<Role>();
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()
{
Id = LgbConvert.ReadValue(reader[0], 0),
RoleName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
Checked = (string)reader[3]
});
}
}
return roles;
}, RetrieveRolesByGroupIdDataKey);
}
/// <summary>
/// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息
/// </summary>

View File

@ -1,5 +1,4 @@
using Bootstrap.Security;
using Longbow;
using Longbow.Cache;
using Longbow.Data;
using Longbow.Security;
@ -19,64 +18,6 @@ namespace Bootstrap.DataAccess.SQLite
public class User : DataAccess.User
{
/// <summary>
/// 查询所有用户
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.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");
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
{
while (reader.Read())
{
users.Add(new User()
{
Id = LgbConvert.ReadValue(reader[0], 0),
UserName = (string)reader[1],
DisplayName = (string)reader[2],
RegisterTime = LgbConvert.ReadValue(reader[3], DateTime.MinValue),
ApprovedTime = LgbConvert.ReadValue(reader[4], DateTime.MinValue),
ApprovedBy = reader.IsDBNull(5) ? string.Empty : (string)reader[5],
Description = (string)reader[6]
});
}
}
return users;
});
}
/// <summary>
/// 查询所有的新注册用户
/// </summary>
/// <returns></returns>
public override IEnumerable<DataAccess.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))
{
while (reader.Read())
{
users.Add(new User()
{
Id = LgbConvert.ReadValue(reader[0], 0),
UserName = (string)reader[1],
DisplayName = (string)reader[2],
RegisterTime = LgbConvert.ReadValue(reader[3], DateTime.MinValue),
Description = (string)reader[4]
});
}
}
return users;
});
}
/// <summary>
/// 删除用户
/// </summary>
@ -122,30 +63,6 @@ namespace Bootstrap.DataAccess.SQLite
///
/// </summary>
/// <param name="id"></param>
/// <param name="password"></param>
/// <param name="displayName"></param>
/// <returns></returns>
public override bool UpdateUser(int id, string password, string displayName)
{
bool ret = false;
string sql = "Update Users set Password = @Password, PassSalt = @PassSalt, DisplayName = @DisplayName where ID = @id";
var passSalt = LgbCryptography.GenerateSalt();
var newPassword = LgbCryptography.ComputeHash(password, passSalt);
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@id", id));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@DisplayName", displayName));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Password", newPassword));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@PassSalt", passSalt));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(userIds: id == 0 ? new List<int>() : new List<int>() { id });
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="approvedBy"></param>
/// <returns></returns>
public override bool ApproveUser(int id, string approvedBy)
@ -182,36 +99,6 @@ namespace Bootstrap.DataAccess.SQLite
return ret;
}
/// <summary>
/// 通过roleId获取所有用户
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.User> RetrieveUsersByRoleId(int roleId)
{
string key = string.Format("{0}-{1}", RetrieveUsersByRoleIdDataKey, roleId);
return CacheManager.GetOrAdd(key, k =>
{
List<User> users = new List<User>();
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()
{
Id = LgbConvert.ReadValue(reader[0], 0),
UserName = (string)reader[1],
DisplayName = (string)reader[2],
Checked = (string)reader[3]
});
}
}
return users;
}, RetrieveUsersByRoleIdDataKey);
}
/// <summary>
/// 通过角色ID保存当前授权用户插入
/// </summary>
/// <param name="id">角色ID</param>
@ -256,36 +143,6 @@ namespace Bootstrap.DataAccess.SQLite
return ret;
}
/// <summary>
/// 通过groupId获取所有用户
/// </summary>
/// <param name="groupId"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.User> RetrieveUsersByGroupId(int groupId)
{
string key = string.Format("{0}-{1}", RetrieveUsersByGroupIdDataKey, groupId);
return CacheManager.GetOrAdd(key, k =>
{
List<User> users = new List<User>();
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()
{
Id = LgbConvert.ReadValue(reader[0], 0),
UserName = (string)reader[1],
DisplayName = (string)reader[2],
Checked = (string)reader[3]
});
}
}
return users;
}, RetrieveUsersByRoleIdDataKey);
}
/// <summary>
/// 通过部门ID保存当前授权用户插入
/// </summary>
/// <param name="id">GroupID</param>
@ -330,66 +187,9 @@ namespace Bootstrap.DataAccess.SQLite
return ret;
}
/// <summary>
/// 根据用户名修改用户头像
/// </summary>
/// <param name="userName"></param>
/// <param name="iconName"></param>
/// <returns></returns>
public override bool SaveUserIconByName(string userName, string iconName)
{
bool ret = false;
string sql = "Update Users set Icon = @iconName where UserName = @userName";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@iconName", iconName));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", userName));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*");
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="displayName"></param>
/// <returns></returns>
public override bool SaveDisplayName(string userName, string displayName)
{
bool ret = false;
string sql = "Update Users set DisplayName = @DisplayName where UserName = @userName";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@DisplayName", displayName));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", userName));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*");
}
return ret;
}
/// <summary>
/// 根据用户名更改用户皮肤
/// </summary>
/// <param name="userName"></param>
/// <param name="cssName"></param>
/// <returns></returns>
public override bool SaveUserCssByName(string userName, string cssName)
{
bool ret = false;
string sql = "Update Users set Css = @cssName where UserName = @userName";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@cssName", DbAccessFactory.ToDBValue(cssName)));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", userName));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*");
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public override BootstrapUser RetrieveUserByUserName(string userName)
{
@ -398,9 +198,10 @@ namespace Bootstrap.DataAccess.SQLite
{
BootstrapUser user = null;
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 cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserName", userName));
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
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())
{

View File

@ -1,4 +1,5 @@
using Longbow.Cache;
using Longbow;
using Longbow.Cache;
using Longbow.Data;
using System;
using System.Collections.Generic;
@ -55,7 +56,7 @@ namespace Bootstrap.DataAccess
{
groups.Add(new Group()
{
Id = (int)reader[0],
Id = LgbConvert.ReadValue(reader[0], 0),
GroupName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2]
});
@ -124,7 +125,7 @@ namespace Bootstrap.DataAccess
{
groups.Add(new Group()
{
Id = (int)reader[0],
Id = LgbConvert.ReadValue(reader[0], 0),
GroupName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
Checked = (string)reader[3]
@ -202,7 +203,7 @@ namespace Bootstrap.DataAccess
{
groups.Add(new Group()
{
Id = (int)reader[0],
Id = LgbConvert.ReadValue(reader[0], 0),
GroupName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
Checked = (string)reader[3]

View File

@ -93,7 +93,7 @@ namespace Bootstrap.DataAccess
{
menus.Add(new BootstrapMenu()
{
Id = (int)reader[0]
Id = LgbConvert.ReadValue(reader[0], 0)
});
}
}

View File

@ -75,7 +75,7 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
private static IEnumerable<Message> RetrieveMessages(string userName)
protected virtual IEnumerable<Message> RetrieveMessages(string userName)
{
var messageRet = CacheManager.GetOrAdd(RetrieveMessageDataKey, key =>
{

View File

@ -56,7 +56,7 @@ namespace Bootstrap.DataAccess
{
roles.Add(new Role()
{
Id = (int)reader[0],
Id = LgbConvert.ReadValue(reader[0], 0),
RoleName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2]
});
@ -133,7 +133,7 @@ namespace Bootstrap.DataAccess
{
roles.Add(new Role()
{
Id = (int)reader[0],
Id = LgbConvert.ReadValue(reader[0], 0),
RoleName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
Checked = (string)reader[3]
@ -202,7 +202,7 @@ namespace Bootstrap.DataAccess
{
roles.Add(new Role()
{
Id = (int)reader[0],
Id = LgbConvert.ReadValue(reader[0], 0),
RoleName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
Checked = (string)reader[3]
@ -280,7 +280,7 @@ namespace Bootstrap.DataAccess
{
roles.Add(new Role()
{
Id = (int)reader[0],
Id = LgbConvert.ReadValue(reader[0], 0),
RoleName = (string)reader[1],
Description = reader.IsDBNull(2) ? string.Empty : (string)reader[2],
Checked = (string)reader[3]

View File

@ -136,10 +136,10 @@ namespace Bootstrap.DataAccess
{
users.Add(new User()
{
Id = (int)reader[0],
Id = LgbConvert.ReadValue(reader[0], 0),
UserName = (string)reader[1],
DisplayName = (string)reader[2],
RegisterTime = (DateTime)reader[3],
RegisterTime = LgbConvert.ReadValue(reader[3], DateTime.MinValue),
ApprovedTime = LgbConvert.ReadValue(reader[4], DateTime.MinValue),
ApprovedBy = reader.IsDBNull(5) ? string.Empty : (string)reader[5],
Description = (string)reader[6]
@ -166,10 +166,10 @@ namespace Bootstrap.DataAccess
{
users.Add(new User()
{
Id = (int)reader[0],
Id = LgbConvert.ReadValue(reader[0], 0),
UserName = (string)reader[1],
DisplayName = (string)reader[2],
RegisterTime = (DateTime)reader[3],
RegisterTime = LgbConvert.ReadValue(reader[3], DateTime.MinValue),
Description = (string)reader[4]
});
}
@ -301,7 +301,7 @@ namespace Bootstrap.DataAccess
{
users.Add(new User()
{
Id = (int)reader[0],
Id = LgbConvert.ReadValue(reader[0], 0),
UserName = (string)reader[1],
DisplayName = (string)reader[2],
Checked = (string)reader[3]
@ -375,7 +375,7 @@ namespace Bootstrap.DataAccess
{
users.Add(new User()
{
Id = (int)reader[0],
Id = LgbConvert.ReadValue(reader[0], 0),
UserName = (string)reader[1],
DisplayName = (string)reader[2],
Checked = (string)reader[3]