重构代码:根据最新版Longbow.Data组件,更改DbManager属性为方法

This commit is contained in:
Argo-Surface 2019-01-14 12:33:09 +08:00
parent 9aab788ea7
commit 96b52478b9
11 changed files with 61 additions and 62 deletions

View File

@ -7,11 +7,6 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
public static class DbManager public static class DbManager
{ {
/// <summary>
///
/// </summary>
public static IDatabase Db { get { return Create(); } }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>

View File

@ -20,7 +20,7 @@ namespace Bootstrap.DataAccess
{ {
var ids = string.Join(",", value); var ids = string.Join(",", value);
string sql = $"where ID in ({ids})"; string sql = $"where ID in ({ids})";
DbManager.Db.Delete<BootstrapDict>(sql); DbManager.Create().Delete<BootstrapDict>(sql);
return true; return true;
} }
@ -35,7 +35,7 @@ namespace Bootstrap.DataAccess
if (dict.Name.Length > 50) dict.Name = dict.Name.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); if (dict.Code.Length > 50) dict.Code = dict.Code.Substring(0, 50);
DbManager.Db.Save(dict); DbManager.Create().Save(dict);
return true; return true;
} }
@ -48,7 +48,7 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual bool SaveSettings(BootstrapDict dict) public virtual bool SaveSettings(BootstrapDict dict)
{ {
DbManager.Db.Update<BootstrapDict>("set Code = @Code where Category = @Category and Name = @Name", dict); DbManager.Create().Update<BootstrapDict>("set Code = @Code where Category = @Category and Name = @Name", dict);
return true; return true;
} }

View File

@ -65,7 +65,7 @@ namespace Bootstrap.DataAccess
private static void ClearExceptions() => System.Threading.Tasks.Task.Run(() => private static void ClearExceptions() => System.Threading.Tasks.Task.Run(() =>
{ {
DbManager.Db.Execute("delete from Exceptions where LogTime < @0", DateTime.Now.AddMonths(0 - LgbConvert.ReadValue(ConfigurationManager.AppSettings["KeepExceptionsPeriod"], 1))); DbManager.Create().Execute("delete from Exceptions where LogTime < @0", DateTime.Now.AddMonths(0 - LgbConvert.ReadValue(ConfigurationManager.AppSettings["KeepExceptionsPeriod"], 1)));
}); });
/// <summary> /// <summary>
@ -79,7 +79,7 @@ namespace Bootstrap.DataAccess
if (ex == null) throw new ArgumentNullException(nameof(ex)); if (ex == null) throw new ArgumentNullException(nameof(ex));
var errorPage = additionalInfo?["ErrorPage"] ?? (ex.GetType().Name.Length > 50 ? ex.GetType().Name.Substring(0, 50) : ex.GetType().Name); var errorPage = additionalInfo?["ErrorPage"] ?? (ex.GetType().Name.Length > 50 ? ex.GetType().Name.Substring(0, 50) : ex.GetType().Name);
DbManager.Db.Insert(new Exceptions() DbManager.Create().Insert(new Exceptions()
{ {
AppDomainName = AppDomain.CurrentDomain.FriendlyName, AppDomainName = AppDomain.CurrentDomain.FriendlyName,
ErrorPage = errorPage, ErrorPage = errorPage,
@ -98,7 +98,7 @@ namespace Bootstrap.DataAccess
/// 查询一周内所有异常 /// 查询一周内所有异常
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Exceptions> Retrieves() => DbManager.Db.Fetch<Exceptions>("select * from Exceptions where LogTime > @0 order by LogTime desc", DateTime.Now.AddDays(-7)); public virtual IEnumerable<Exceptions> Retrieves() => DbManager.Create().Fetch<Exceptions>("select * from Exceptions where LogTime > @0 order by LogTime desc", DateTime.Now.AddDays(-7));
/// <summary> /// <summary>
/// ///
@ -116,7 +116,7 @@ namespace Bootstrap.DataAccess
if (startTime == null && endTime == null) sql.Append("where LogTime > @0", DateTime.Today.AddDays(-7)); if (startTime == null && endTime == null) sql.Append("where LogTime > @0", DateTime.Today.AddDays(-7));
sql.Append("order by @0", $"{po.Sort} {po.Order}"); sql.Append("order by @0", $"{po.Sort} {po.Order}");
return DbManager.Db.Page<Exceptions>(po.PageIndex, po.Limit, sql); return DbManager.Create().Page<Exceptions>(po.PageIndex, po.Limit, sql);
} }
} }
} }

View File

@ -1,4 +1,5 @@
using Bootstrap.Security.DataAccess; using Bootstrap.Security.DataAccess;
using Longbow.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -35,7 +36,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Group> Retrieves() => DbManager.Db.Fetch<Group>("select * from Groups"); public virtual IEnumerable<Group> Retrieves() => DbManager.Create().Fetch<Group>("select * from Groups");
/// <summary> /// <summary>
/// 删除群组信息 /// 删除群组信息
@ -45,7 +46,7 @@ namespace Bootstrap.DataAccess
{ {
bool ret = false; bool ret = false;
var ids = string.Join(",", value); var ids = string.Join(",", value);
var db = DbManager.Db; var db = DbManager.Create();
try try
{ {
db.BeginTransaction(); db.BeginTransaction();
@ -70,7 +71,7 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual bool Save(Group p) public virtual bool Save(Group p)
{ {
DbManager.Db.Save(p); DbManager.Create().Save(p);
return !p.Id.IsNullOrEmpty(); return !p.Id.IsNullOrEmpty();
} }
@ -79,14 +80,14 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="userId"></param> /// <param name="userId"></param>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Group> RetrievesByUserId(string userId) => DbManager.Db.Fetch<Group>("select g.ID, g.GroupName, g.Description, case ug.GroupID when g.ID then 'checked' else '' end Checked from Groups g left join UserGroup ug on g.ID = ug.GroupID and UserID = @0", userId); public virtual IEnumerable<Group> RetrievesByUserId(string userId) => DbManager.Create().Fetch<Group>("select g.ID, g.GroupName, g.Description, case ug.GroupID when g.ID then 'checked' else '' end Checked from Groups g left join UserGroup ug on g.ID = ug.GroupID and UserID = @0", userId);
/// <summary> /// <summary>
/// 根据角色ID指派部门 /// 根据角色ID指派部门
/// </summary> /// </summary>
/// <param name="roleId"></param> /// <param name="roleId"></param>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Group> RetrievesByRoleId(string roleId) => DbManager.Db.Fetch<Group>("select g.ID, g.GroupName, g.Description, case rg.GroupID when g.ID then 'checked' else '' end Checked from Groups g left join RoleGroup rg on g.ID = rg.GroupID and RoleID = @0", roleId); public virtual IEnumerable<Group> RetrievesByRoleId(string roleId) => DbManager.Create().Fetch<Group>("select g.ID, g.GroupName, g.Description, case rg.GroupID when g.ID then 'checked' else '' end Checked from Groups g left join RoleGroup rg on g.ID = rg.GroupID and RoleID = @0", roleId);
/// <summary> /// <summary>
/// 保存用户部门关系 /// 保存用户部门关系
@ -97,13 +98,13 @@ namespace Bootstrap.DataAccess
public virtual bool SaveByUserId(string userId, IEnumerable<string> groupIds) public virtual bool SaveByUserId(string userId, IEnumerable<string> groupIds)
{ {
var ret = false; var ret = false;
var db = DbManager.Db; var db = DbManager.Create();
try try
{ {
db.BeginTransaction(); db.BeginTransaction();
//删除用户部门表中该用户所有的部门关系 //删除用户部门表中该用户所有的部门关系
db.Execute("delete from UserGroup where UserID = @0", userId); db.Execute("delete from UserGroup where UserID = @0", userId);
db.InsertBulk("UserGroup", groupIds.Select(g => new { UserID = userId, GroupID = g })); db.InsertBatch("UserGroup", groupIds.Select(g => new { UserID = userId, GroupID = g }));
db.CompleteTransaction(); db.CompleteTransaction();
ret = true; ret = true;
} }
@ -124,13 +125,13 @@ namespace Bootstrap.DataAccess
public virtual bool SaveByRoleId(string roleId, IEnumerable<string> groupIds) public virtual bool SaveByRoleId(string roleId, IEnumerable<string> groupIds)
{ {
bool ret = false; bool ret = false;
var db = DbManager.Db; var db = DbManager.Create();
try try
{ {
db.BeginTransaction(); db.BeginTransaction();
//删除角色部门表该角色所有的部门 //删除角色部门表该角色所有的部门
db.Execute("delete from RoleGroup where RoleID = @0", roleId); db.Execute("delete from RoleGroup where RoleID = @0", roleId);
db.InsertBulk("RoleGroup", groupIds.Select(g => new { RoleID = roleId, GroupID = g })); db.InsertBatch("RoleGroup", groupIds.Select(g => new { RoleID = roleId, GroupID = g }));
db.CompleteTransaction(); db.CompleteTransaction();
ret = true; ret = true;
} }

View File

@ -42,6 +42,6 @@ namespace Bootstrap.DataAccess
/// <param name="startTime"></param> /// <param name="startTime"></param>
/// <param name="endTime"></param> /// <param name="endTime"></param>
/// <returns></returns> /// <returns></returns>
public static Page<Exceptions> RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime) => CacheManager.GetOrAdd(RetrieveExceptionsDataKey, key => DbContextManager.Create<Exceptions>().RetrievePages(po, startTime, endTime)); public static Page<Exceptions> RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime) => DbContextManager.Create<Exceptions>().RetrievePages(po, startTime, endTime);
} }
} }

View File

@ -50,7 +50,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="tId"></param> /// <param name="tId"></param>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Log> Retrieves() => DbManager.Db.Fetch<Log>(); public virtual IEnumerable<Log> Retrieves() => DbManager.Create().Fetch<Log>("select * from Logs where LogTime > @0 order by LogTime desc", DateTime.Now.AddDays(-7));
/// <summary> /// <summary>
/// 删除日志信息 /// 删除日志信息
@ -62,7 +62,7 @@ namespace Bootstrap.DataAccess
System.Threading.Tasks.Task.Run(() => System.Threading.Tasks.Task.Run(() =>
{ {
var dtm = DateTime.Now.AddMonths(0 - LgbConvert.ReadValue(ConfigurationManager.AppSettings["KeepLogsPeriod"], 1)); var dtm = DateTime.Now.AddMonths(0 - LgbConvert.ReadValue(ConfigurationManager.AppSettings["KeepLogsPeriod"], 1));
DbManager.Db.Execute("delete from Logs where LogTime < @0", dtm); DbManager.Create().Execute("delete from Logs where LogTime < @0", dtm);
}); });
} }
@ -76,7 +76,7 @@ namespace Bootstrap.DataAccess
if (p == null) throw new ArgumentNullException(nameof(p)); if (p == null) throw new ArgumentNullException(nameof(p));
DeleteLogAsync(); DeleteLogAsync();
p.LogTime = DateTime.Now; p.LogTime = DateTime.Now;
DbManager.Db.Save(p); DbManager.Create().Save(p);
return true; return true;
} }
} }

View File

@ -1,5 +1,6 @@
using Bootstrap.Security; using Bootstrap.Security;
using Bootstrap.Security.DataAccess; using Bootstrap.Security.DataAccess;
using Longbow.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -18,7 +19,7 @@ namespace Bootstrap.DataAccess
public virtual bool Delete(IEnumerable<string> value) public virtual bool Delete(IEnumerable<string> value)
{ {
var ret = false; var ret = false;
var db = DbManager.Db; var db = DbManager.Create();
try try
{ {
var ids = string.Join(",", value); var ids = string.Join(",", value);
@ -47,7 +48,7 @@ namespace Bootstrap.DataAccess
if (p.Name.Length > 50) p.Name = p.Name.Substring(0, 50); 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.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); if (p.Url != null && p.Url.Length > 4000) p.Url = p.Url.Substring(0, 4000);
DbManager.Db.Save(p); DbManager.Create().Save(p);
return true; return true;
} }
/// <summary> /// <summary>
@ -57,7 +58,7 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<object> RetrieveMenusByRoleId(string roleId) public virtual IEnumerable<object> RetrieveMenusByRoleId(string roleId)
{ {
var menus = DbManager.Db.Fetch<BootstrapMenu>("select NavigationID as Id from NavigationRole where RoleID = @0", roleId); var menus = DbManager.Create().Fetch<BootstrapMenu>("select NavigationID as Id from NavigationRole where RoleID = @0", roleId);
return menus.Select(m => new { m.Id }); return menus.Select(m => new { m.Id });
} }
/// <summary> /// <summary>
@ -69,12 +70,12 @@ namespace Bootstrap.DataAccess
public virtual bool SaveMenusByRoleId(string roleId, IEnumerable<string> menuIds) public virtual bool SaveMenusByRoleId(string roleId, IEnumerable<string> menuIds)
{ {
bool ret = false; bool ret = false;
var db = DbManager.Db; var db = DbManager.Create();
try try
{ {
db.BeginTransaction(); db.BeginTransaction();
db.Execute("delete from NavigationRole where RoleID = @0", roleId); db.Execute("delete from NavigationRole where RoleID = @0", roleId);
db.InsertBulk("NavigationRole", menuIds.Select(g => new { NavigationID = g, RoleID = roleId })); db.InsertBatch("NavigationRole", menuIds.Select(g => new { NavigationID = g, RoleID = roleId }));
db.CompleteTransaction(); db.CompleteTransaction();
ret = true; ret = true;
} }

View File

@ -87,7 +87,7 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
protected virtual IEnumerable<Message> Retrieves(string userName) protected virtual IEnumerable<Message> Retrieves(string userName)
{ {
var db = DbManager.Db; var db = DbManager.Create();
var t = db.Provider.EscapeSqlIdentifier("To"); var t = db.Provider.EscapeSqlIdentifier("To");
var f = db.Provider.EscapeSqlIdentifier("From"); var f = db.Provider.EscapeSqlIdentifier("From");
return db.Fetch<Message>($"select m.*, d.Name, u.DisplayName from [Messages] m left join Dicts d on m.Label = d.Code and d.Category = @Category and d.Define = 0 inner join Users u on m.{f} = u.UserName where {t} = @UserName or {f} = @UserName order by SendTime desc", new { UserName = userName, Category = "消息标签" }); return db.Fetch<Message>($"select m.*, d.Name, u.DisplayName from [Messages] m left join Dicts d on m.Label = d.Code and d.Category = @Category and d.Define = 0 inner join Users u on m.{f} = u.UserName where {t} = @UserName or {f} = @UserName order by SendTime desc", new { UserName = userName, Category = "消息标签" });

View File

@ -1,4 +1,5 @@
using Bootstrap.Security.DataAccess; using Bootstrap.Security.DataAccess;
using Longbow.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -34,7 +35,7 @@ namespace Bootstrap.DataAccess
/// 查询所有角色 /// 查询所有角色
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Role> Retrieves() => DbManager.Db.Fetch<Role>(); public virtual IEnumerable<Role> Retrieves() => DbManager.Create().Fetch<Role>();
/// <summary> /// <summary>
/// 保存用户角色关系 /// 保存用户角色关系
@ -45,13 +46,13 @@ namespace Bootstrap.DataAccess
public virtual bool SaveByUserId(string userId, IEnumerable<string> roleIds) public virtual bool SaveByUserId(string userId, IEnumerable<string> roleIds)
{ {
var ret = false; var ret = false;
var db = DbManager.Db; var db = DbManager.Create();
try try
{ {
db.BeginTransaction(); db.BeginTransaction();
// delete user from config table // delete user from config table
db.Execute("delete from UserRole where UserID = @0", userId); db.Execute("delete from UserRole where UserID = @0", userId);
db.InsertBulk("UserRole", roleIds.Select(g => new { UserID = userId, RoleID = g })); db.InsertBatch("UserRole", roleIds.Select(g => new { UserID = userId, RoleID = g }));
db.CompleteTransaction(); db.CompleteTransaction();
ret = true; ret = true;
} }
@ -67,7 +68,7 @@ namespace Bootstrap.DataAccess
/// 查询某个用户所拥有的角色 /// 查询某个用户所拥有的角色
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Role> RetrievesByUserId(string userId) => DbManager.Db.Fetch<Role>("select r.ID, r.RoleName, r.Description, case ur.RoleID when r.ID then 'checked' else '' end Checked from Roles r left join UserRole ur on r.ID = ur.RoleID and UserID = @0", userId); public virtual IEnumerable<Role> RetrievesByUserId(string userId) => DbManager.Create().Fetch<Role>("select r.ID, r.RoleName, r.Description, case ur.RoleID when r.ID then 'checked' else '' end Checked from Roles r left join UserRole ur on r.ID = ur.RoleID and UserID = @0", userId);
/// <summary> /// <summary>
/// 删除角色表 /// 删除角色表
@ -77,7 +78,7 @@ namespace Bootstrap.DataAccess
{ {
bool ret = false; bool ret = false;
var ids = string.Join(",", value); var ids = string.Join(",", value);
var db = DbManager.Db; var db = DbManager.Create();
try try
{ {
db.BeginTransaction(); db.BeginTransaction();
@ -106,7 +107,7 @@ namespace Bootstrap.DataAccess
if (!string.IsNullOrEmpty(p.RoleName) && p.RoleName.Length > 50) p.RoleName = p.RoleName.Substring(0, 50); 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); if (!string.IsNullOrEmpty(p.Description) && p.Description.Length > 50) p.Description = p.Description.Substring(0, 500);
DbManager.Db.Save(p); DbManager.Create().Save(p);
return true; return true;
} }
@ -115,7 +116,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="menuId"></param> /// <param name="menuId"></param>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Role> RetrievesByMenuId(string menuId) => DbManager.Db.Fetch<Role>("select r.ID, r.RoleName, r.Description, case ur.RoleID when r.ID then 'checked' else '' end Checked from Roles r left join NavigationRole ur on r.ID = ur.RoleID and NavigationID = @0", menuId); public virtual IEnumerable<Role> RetrievesByMenuId(string menuId) => DbManager.Create().Fetch<Role>("select r.ID, r.RoleName, r.Description, case ur.RoleID when r.ID then 'checked' else '' end Checked from Roles r left join NavigationRole ur on r.ID = ur.RoleID and NavigationID = @0", menuId);
/// <summary> /// <summary>
/// ///
@ -126,13 +127,13 @@ namespace Bootstrap.DataAccess
public virtual bool SavaByMenuId(string menuId, IEnumerable<string> roleIds) public virtual bool SavaByMenuId(string menuId, IEnumerable<string> roleIds)
{ {
var ret = false; var ret = false;
var db = DbManager.Db; var db = DbManager.Create();
db.BeginTransaction(); db.BeginTransaction();
try try
{ {
// delete role from config table // delete role from config table
db.Execute("delete from NavigationRole where NavigationID = @0", menuId); db.Execute("delete from NavigationRole where NavigationID = @0", menuId);
db.InsertBulk("NavigationRole", roleIds.Select(g => new { NavigationID = menuId, RoleID = g })); db.InsertBatch("NavigationRole", roleIds.Select(g => new { NavigationID = menuId, RoleID = g }));
db.CompleteTransaction(); db.CompleteTransaction();
ret = true; ret = true;
} }
@ -149,7 +150,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="groupId"></param> /// <param name="groupId"></param>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Role> RetrievesByGroupId(string groupId) => DbManager.Db.Fetch<Role>("select r.ID, r.RoleName, r.Description, case ur.RoleID when r.ID then 'checked' else '' end Checked from Roles r left join RoleGroup ur on r.ID = ur.RoleID and GroupID = @0", groupId); public virtual IEnumerable<Role> RetrievesByGroupId(string groupId) => DbManager.Create().Fetch<Role>("select r.ID, r.RoleName, r.Description, case ur.RoleID when r.ID then 'checked' else '' end Checked from Roles r left join RoleGroup ur on r.ID = ur.RoleID and GroupID = @0", groupId);
/// <summary> /// <summary>
/// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息 /// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息
@ -160,12 +161,12 @@ namespace Bootstrap.DataAccess
public virtual bool SaveByGroupId(string groupId, IEnumerable<string> roleIds) public virtual bool SaveByGroupId(string groupId, IEnumerable<string> roleIds)
{ {
var ret = false; var ret = false;
var db = DbManager.Db; var db = DbManager.Create();
try try
{ {
// delete user from config table // delete user from config table
db.Execute("delete from RoleGroup where GroupID = @0", groupId); db.Execute("delete from RoleGroup where GroupID = @0", groupId);
db.InsertBulk("RoleGroup", roleIds.Select(g => new { GroupID = groupId, RoleID = g })); db.InsertBatch("RoleGroup", roleIds.Select(g => new { GroupID = groupId, RoleID = g }));
db.CompleteTransaction(); db.CompleteTransaction();
ret = true; ret = true;
} }

View File

@ -52,6 +52,6 @@ namespace Bootstrap.DataAccess
/// 查询所有任务 /// 查询所有任务
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<Task> Retrieves() => DbManager.Db.SkipTake<Task>(0, 1000, "select t.*, u.DisplayName AssignDisplayName from Tasks t inner join Users u on t.UserName = u.UserName order by AssignTime desc"); public virtual IEnumerable<Task> Retrieves() => DbManager.Create().SkipTake<Task>(0, 1000, "select t.*, u.DisplayName AssignDisplayName from Tasks t inner join Users u on t.UserName = u.UserName order by AssignTime desc");
} }
} }

View File

@ -1,5 +1,6 @@
using Bootstrap.Security; using Bootstrap.Security;
using Bootstrap.Security.DataAccess; using Bootstrap.Security.DataAccess;
using Longbow.Data;
using Longbow.Security.Cryptography; using Longbow.Security.Cryptography;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -75,7 +76,7 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual bool Authenticate(string userName, string password) public virtual bool Authenticate(string userName, string password)
{ {
var user = DbManager.Db.SingleOrDefault<User>("select Password, PassSalt from Users where ApprovedTime is not null and UserName = @0", userName); var user = DbManager.Create().SingleOrDefault<User>("select Password, PassSalt from Users where ApprovedTime is not null and UserName = @0", userName);
return !string.IsNullOrEmpty(user.PassSalt) && user.Password == LgbCryptography.ComputeHash(password, user.PassSalt); return !string.IsNullOrEmpty(user.PassSalt) && user.Password == LgbCryptography.ComputeHash(password, user.PassSalt);
} }
@ -95,7 +96,7 @@ namespace Bootstrap.DataAccess
string sql = "set Password = @0, PassSalt = @1 where UserName = @2"; string sql = "set Password = @0, PassSalt = @1 where UserName = @2";
var passSalt = LgbCryptography.GenerateSalt(); var passSalt = LgbCryptography.GenerateSalt();
var newPassword = LgbCryptography.ComputeHash(newPass, passSalt); var newPassword = LgbCryptography.ComputeHash(newPass, passSalt);
ret = DbManager.Db.Update<User>(sql, newPassword, passSalt, userName) == 1; ret = DbManager.Create().Update<User>(sql, newPassword, passSalt, userName) == 1;
} }
return ret; return ret;
} }
@ -105,13 +106,13 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<User> Retrieves() => DbManager.Db.Fetch<User>("select ID, UserName, DisplayName, RegisterTime, ApprovedTime, ApprovedBy, Description from Users Where ApprovedTime is not null"); public virtual IEnumerable<User> Retrieves() => DbManager.Create().Fetch<User>("select ID, UserName, DisplayName, RegisterTime, ApprovedTime, ApprovedBy, Description from Users Where ApprovedTime is not null");
/// <summary> /// <summary>
/// 查询所有的新注册用户 /// 查询所有的新注册用户
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<User> RetrieveNewUsers() => DbManager.Db.Fetch<User>("select ID, UserName, DisplayName, RegisterTime, Description from Users Where ApprovedTime is null order by RegisterTime desc"); public virtual IEnumerable<User> RetrieveNewUsers() => DbManager.Create().Fetch<User>("select ID, UserName, DisplayName, RegisterTime, Description from Users Where ApprovedTime is null order by RegisterTime desc");
/// <summary> /// <summary>
/// 删除用户 /// 删除用户
@ -121,7 +122,7 @@ namespace Bootstrap.DataAccess
{ {
if (!value.Any()) return true; if (!value.Any()) return true;
bool ret = false; bool ret = false;
var db = DbManager.Db; var db = DbManager.Create();
try try
{ {
var ids = string.Join(",", value); var ids = string.Join(",", value);
@ -154,7 +155,7 @@ namespace Bootstrap.DataAccess
p.Password = LgbCryptography.ComputeHash(p.Password, p.PassSalt); p.Password = LgbCryptography.ComputeHash(p.Password, p.PassSalt);
p.RegisterTime = DateTime.Now; p.RegisterTime = DateTime.Now;
var db = DbManager.Db; var db = DbManager.Create();
try try
{ {
db.BeginTransaction(); db.BeginTransaction();
@ -185,7 +186,7 @@ namespace Bootstrap.DataAccess
{ {
var passSalt = LgbCryptography.GenerateSalt(); var passSalt = LgbCryptography.GenerateSalt();
var newPassword = LgbCryptography.ComputeHash(password, passSalt); var newPassword = LgbCryptography.ComputeHash(password, passSalt);
return DbManager.Db.Update<User>("set Password = @1, PassSalt = @2, DisplayName = @3 where ID = @0", id, newPassword, passSalt, displayName) == 1; return DbManager.Create().Update<User>("set Password = @1, PassSalt = @2, DisplayName = @3 where ID = @0", id, newPassword, passSalt, displayName) == 1;
} }
/// <summary> /// <summary>
@ -194,7 +195,7 @@ namespace Bootstrap.DataAccess
/// <param name="id"></param> /// <param name="id"></param>
/// <param name="approvedBy"></param> /// <param name="approvedBy"></param>
/// <returns></returns> /// <returns></returns>
public virtual bool Approve(string id, string approvedBy) => DbManager.Db.Update<User>("set ApprovedTime = @1, ApprovedBy = @2 where ID = @0", id, DateTime.Now, approvedBy) == 1; public virtual bool Approve(string id, string approvedBy) => DbManager.Create().Update<User>("set ApprovedTime = @1, ApprovedBy = @2 where ID = @0", id, DateTime.Now, approvedBy) == 1;
/// <summary> /// <summary>
/// ///
@ -205,7 +206,7 @@ namespace Bootstrap.DataAccess
public virtual bool Reject(string id, string rejectBy) public virtual bool Reject(string id, string rejectBy)
{ {
var ret = false; var ret = false;
var db = DbManager.Db; var db = DbManager.Create();
try try
{ {
db.BeginTransaction(); db.BeginTransaction();
@ -229,7 +230,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="roleId"></param> /// <param name="roleId"></param>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<User> RetrievesByRoleId(string roleId) => DbManager.Db.Fetch<User>("select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end Checked from Users u left join UserRole ur on u.ID = ur.UserID and RoleID = @0 where u.ApprovedTime is not null", roleId); public virtual IEnumerable<User> RetrievesByRoleId(string roleId) => DbManager.Create().Fetch<User>("select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end Checked from Users u left join UserRole ur on u.ID = ur.UserID and RoleID = @0 where u.ApprovedTime is not null", roleId);
/// <summary> /// <summary>
/// 通过角色ID保存当前授权用户插入 /// 通过角色ID保存当前授权用户插入
@ -240,13 +241,13 @@ namespace Bootstrap.DataAccess
public virtual bool SaveByRoleId(string roleId, IEnumerable<string> userIds) public virtual bool SaveByRoleId(string roleId, IEnumerable<string> userIds)
{ {
bool ret = false; bool ret = false;
var db = DbManager.Db; var db = DbManager.Create();
try try
{ {
db.BeginTransaction(); db.BeginTransaction();
//删除用户角色表该角色所有的用户 //删除用户角色表该角色所有的用户
db.Execute("delete from UserRole where RoleID = @0", roleId); db.Execute("delete from UserRole where RoleID = @0", roleId);
db.InsertBulk("UserRole", userIds.Select(g => new { UserID = g, RoleID = roleId })); db.InsertBatch("UserRole", userIds.Select(g => new { UserID = g, RoleID = roleId }));
db.CompleteTransaction(); db.CompleteTransaction();
ret = true; ret = true;
} }
@ -263,7 +264,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="groupId"></param> /// <param name="groupId"></param>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<User> RetrievesByGroupId(string groupId) => DbManager.Db.Fetch<User>("select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end Checked from Users u left join UserGroup ur on u.ID = ur.UserID and GroupID = @0 where u.ApprovedTime is not null", groupId); public virtual IEnumerable<User> RetrievesByGroupId(string groupId) => DbManager.Create().Fetch<User>("select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end Checked from Users u left join UserGroup ur on u.ID = ur.UserID and GroupID = @0 where u.ApprovedTime is not null", groupId);
/// <summary> /// <summary>
/// 通过部门ID保存当前授权用户插入 /// 通过部门ID保存当前授权用户插入
@ -274,13 +275,13 @@ namespace Bootstrap.DataAccess
public virtual bool SaveByGroupId(string groupId, IEnumerable<string> userIds) public virtual bool SaveByGroupId(string groupId, IEnumerable<string> userIds)
{ {
bool ret = false; bool ret = false;
var db = DbManager.Db; var db = DbManager.Create();
try try
{ {
db.BeginTransaction(); db.BeginTransaction();
//删除用户角色表该角色所有的用户 //删除用户角色表该角色所有的用户
db.Execute("delete from UserGroup where GroupID = @0", groupId); db.Execute("delete from UserGroup where GroupID = @0", groupId);
db.InsertBulk("UserGroup", userIds.Select(g => new { UserID = g, GroupID = groupId })); db.InsertBatch("UserGroup", userIds.Select(g => new { UserID = g, GroupID = groupId }));
db.CompleteTransaction(); db.CompleteTransaction();
ret = true; ret = true;
} }
@ -298,7 +299,7 @@ namespace Bootstrap.DataAccess
/// <param name="userName"></param> /// <param name="userName"></param>
/// <param name="iconName"></param> /// <param name="iconName"></param>
/// <returns></returns> /// <returns></returns>
public virtual bool SaveUserIconByName(string userName, string iconName) => DbManager.Db.Update<User>("set Icon = @1 where UserName = @0", userName, iconName) == 1; public virtual bool SaveUserIconByName(string userName, string iconName) => DbManager.Create().Update<User>("set Icon = @1 where UserName = @0", userName, iconName) == 1;
/// <summary> /// <summary>
/// ///
@ -306,7 +307,7 @@ namespace Bootstrap.DataAccess
/// <param name="userName"></param> /// <param name="userName"></param>
/// <param name="displayName"></param> /// <param name="displayName"></param>
/// <returns></returns> /// <returns></returns>
public virtual bool SaveDisplayName(string userName, string displayName) => DbManager.Db.Update<User>("set DisplayName = @1 where UserName = @0", userName, displayName) == 1; public virtual bool SaveDisplayName(string userName, string displayName) => DbManager.Create().Update<User>("set DisplayName = @1 where UserName = @0", userName, displayName) == 1;
/// <summary> /// <summary>
/// 根据用户名更改用户皮肤 /// 根据用户名更改用户皮肤
@ -314,7 +315,7 @@ namespace Bootstrap.DataAccess
/// <param name="userName"></param> /// <param name="userName"></param>
/// <param name="cssName"></param> /// <param name="cssName"></param>
/// <returns></returns> /// <returns></returns>
public virtual bool SaveUserCssByName(string userName, string cssName) => DbManager.Db.Update<User>("set Css = @1 where UserName = @0", userName, cssName) == 1; public virtual bool SaveUserCssByName(string userName, string cssName) => DbManager.Create().Update<User>("set Css = @1 where UserName = @0", userName, cssName) == 1;
/// <summary> /// <summary>
/// ///