From 2cd0ca2f8ff3ba78a3c2baee0c7d88ad85d161fc Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Fri, 2 Nov 2018 19:35:55 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=EF=BC=9AUs?= =?UTF-8?q?er=E7=A7=BB=E9=99=A4=E5=AD=98=E5=82=A8=E8=BF=87=E7=A8=8BProc=5F?= =?UTF-8?q?DeleteUsers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.DataAccess.MySQL/User.cs | 101 ---------------------------- Bootstrap.DataAccess.SQLite/User.cs | 101 ---------------------------- Bootstrap.DataAccess/User.cs | 70 +++++++++++++++---- 3 files changed, 57 insertions(+), 215 deletions(-) diff --git a/Bootstrap.DataAccess.MySQL/User.cs b/Bootstrap.DataAccess.MySQL/User.cs index b9a2dfd4..2a7f89b3 100644 --- a/Bootstrap.DataAccess.MySQL/User.cs +++ b/Bootstrap.DataAccess.MySQL/User.cs @@ -14,107 +14,6 @@ namespace Bootstrap.DataAccess.MySQL /// public class User : DataAccess.User { - /// - /// - /// 删除用户 - /// - /// - public override bool DeleteUser(IEnumerable value) - { - bool ret = false; - var ids = string.Join(",", value); - using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) - { - try - { - using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, $"Delete from UserRole where UserID in ({ids})")) - { - DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); - - cmd.CommandText = $"delete from UserGroup where UserID in ({ids})"; - DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); - - cmd.CommandText = $"delete from Users where ID in ({ids})"; - DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); - - transaction.CommitTransaction(); - ret = true; - } - } - catch (Exception ex) - { - transaction.RollbackTransaction(); - throw ex; - } - } - return ret; - } - /// - /// 保存新建 - /// - /// - /// - public override bool SaveUser(DataAccess.User p) - { - var ret = false; - if (string.IsNullOrEmpty(p.Id) && p.Description.Length > 500) p.Description = p.Description.Substring(0, 500); - if (p.UserName.Length > 50) p.UserName = p.UserName.Substring(0, 50); - p.PassSalt = LgbCryptography.GenerateSalt(); - p.Password = LgbCryptography.ComputeHash(p.Password, p.PassSalt); - - using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) - { - try - { - using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, "select UserName from Users Where UserName = @userName")) - { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", p.UserName)); - var un = DbAccessManager.DBAccess.ExecuteScalar(cmd, transaction); - if (DbAdapterManager.ToObjectValue(un) == null) - { - cmd.CommandText = "Insert Into Users (UserName, Password, PassSalt, DisplayName, RegisterTime, ApprovedBy, ApprovedTime, Description) values (@userName, @password, @passSalt, @displayName, datetime('now', 'localtime'), @approvedBy, now(), @description)"; - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@password", p.Password)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@passSalt", p.PassSalt)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@displayName", p.DisplayName)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@approvedBy", DbAdapterManager.ToDBValue(p.ApprovedBy))); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@description", p.Description)); - DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); - - cmd.CommandText = $"insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = '{p.UserName}'"; - cmd.Parameters.Clear(); - DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); - - transaction.CommitTransaction(); - ret = true; - } - } - } - catch (Exception ex) - { - transaction.RollbackTransaction(); - throw ex; - } - } - return ret; - } - /// - /// - /// - /// - /// - /// - public override bool ApproveUser(string id, string approvedBy) - { - var ret = false; - var sql = "update Users set ApprovedTime = now(), ApprovedBy = @approvedBy where ID = @id"; - using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) - { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@id", id)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@approvedBy", approvedBy)); - ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1; - } - return ret; - } /// /// /// diff --git a/Bootstrap.DataAccess.SQLite/User.cs b/Bootstrap.DataAccess.SQLite/User.cs index 3509568e..bf8ea11d 100644 --- a/Bootstrap.DataAccess.SQLite/User.cs +++ b/Bootstrap.DataAccess.SQLite/User.cs @@ -14,107 +14,6 @@ namespace Bootstrap.DataAccess.SQLite /// public class User : DataAccess.User { - /// - /// - /// 删除用户 - /// - /// - public override bool DeleteUser(IEnumerable value) - { - bool ret = false; - var ids = string.Join(",", value); - using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) - { - try - { - using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, $"Delete from UserRole where UserID in ({ids})")) - { - DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); - - cmd.CommandText = $"delete from UserGroup where UserID in ({ids})"; - DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); - - cmd.CommandText = $"delete from Users where ID in ({ids})"; - DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); - - transaction.CommitTransaction(); - ret = true; - } - } - catch (Exception ex) - { - transaction.RollbackTransaction(); - throw ex; - } - } - return ret; - } - /// - /// 保存新建 - /// - /// - /// - public override bool SaveUser(DataAccess.User p) - { - var ret = false; - if (string.IsNullOrEmpty(p.Id) && p.Description.Length > 500) p.Description = p.Description.Substring(0, 500); - if (p.UserName.Length > 50) p.UserName = p.UserName.Substring(0, 50); - p.PassSalt = LgbCryptography.GenerateSalt(); - p.Password = LgbCryptography.ComputeHash(p.Password, p.PassSalt); - - using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) - { - try - { - using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, "select UserName from Users Where UserName = @userName")) - { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", p.UserName)); - var un = DbAccessManager.DBAccess.ExecuteScalar(cmd, transaction); - if (DbAdapterManager.ToObjectValue(un) == null) - { - cmd.CommandText = "Insert Into Users (UserName, [Password], PassSalt, DisplayName, RegisterTime, ApprovedBy, ApprovedTime, [Description]) values (@userName, @password, @passSalt, @displayName, datetime('now', 'localtime'), @approvedBy, datetime('now', 'localtime'), @description)"; - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@password", p.Password)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@passSalt", p.PassSalt)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@displayName", p.DisplayName)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@approvedBy", DbAdapterManager.ToDBValue(p.ApprovedBy))); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@description", p.Description)); - DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); - - cmd.CommandText = $"insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = '{p.UserName}'"; - cmd.Parameters.Clear(); - DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); - - transaction.CommitTransaction(); - ret = true; - } - } - } - catch (Exception ex) - { - transaction.RollbackTransaction(); - throw ex; - } - } - return ret; - } - /// - /// - /// - /// - /// - /// - public override bool ApproveUser(string id, string approvedBy) - { - var ret = false; - var sql = "update Users set ApprovedTime = datetime('now', 'localtime'), ApprovedBy = @approvedBy where ID = @id"; - using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) - { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@id", id)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@approvedBy", approvedBy)); - ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1; - } - return ret; - } /// /// /// diff --git a/Bootstrap.DataAccess/User.cs b/Bootstrap.DataAccess/User.cs index aef108b4..efbe78fd 100644 --- a/Bootstrap.DataAccess/User.cs +++ b/Bootstrap.DataAccess/User.cs @@ -174,10 +174,29 @@ namespace Bootstrap.DataAccess { bool ret = false; var ids = string.Join(",", value); - using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteUsers")) + using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ids", ids)); - ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1; + try + { + using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, $"Delete from UserRole where UserID in ({ids})")) + { + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + + cmd.CommandText = $"delete from UserGroup where UserID in ({ids})"; + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + + cmd.CommandText = $"delete from Users where ID in ({ids})"; + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + + transaction.CommitTransaction(); + ret = true; + } + } + catch (Exception ex) + { + transaction.RollbackTransaction(); + throw ex; + } } return ret; } @@ -189,19 +208,43 @@ namespace Bootstrap.DataAccess public virtual bool SaveUser(User p) { var ret = false; - if (p.Description.Length > 500) p.Description = p.Description.Substring(0, 500); + if (string.IsNullOrEmpty(p.Id) && p.Description.Length > 500) p.Description = p.Description.Substring(0, 500); if (p.UserName.Length > 50) p.UserName = p.UserName.Substring(0, 50); p.PassSalt = LgbCryptography.GenerateSalt(); p.Password = LgbCryptography.ComputeHash(p.Password, p.PassSalt); - using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_SaveUsers")) + + using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", p.UserName)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@password", p.Password)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@passSalt", p.PassSalt)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@displayName", p.DisplayName)); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@approvedBy", DbAdapterManager.ToDBValue(p.ApprovedBy))); - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@description", p.Description)); - ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1; + try + { + using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, "select UserName from Users Where UserName = @userName")) + { + cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", p.UserName)); + var un = DbAccessManager.DBAccess.ExecuteScalar(cmd, transaction); + if (DbAdapterManager.ToObjectValue(un) == null) + { + cmd.CommandText = "Insert Into Users (UserName, Password, PassSalt, DisplayName, RegisterTime, ApprovedBy, ApprovedTime, Description) values (@userName, @password, @passSalt, @displayName, datetime('now', 'localtime'), @approvedBy, now(), @description)"; + cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@password", p.Password)); + cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@passSalt", p.PassSalt)); + cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@displayName", p.DisplayName)); + cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@approvedBy", DbAdapterManager.ToDBValue(p.ApprovedBy))); + cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@description", p.Description)); + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + + cmd.CommandText = $"insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = '{p.UserName}'"; + cmd.Parameters.Clear(); + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + + transaction.CommitTransaction(); + ret = true; + } + } + } + catch (Exception ex) + { + transaction.RollbackTransaction(); + throw ex; + } } return ret; } @@ -237,10 +280,11 @@ namespace Bootstrap.DataAccess public virtual bool ApproveUser(string id, string approvedBy) { var ret = false; - var sql = "update Users set ApprovedTime = GETDATE(), ApprovedBy = @approvedBy where ID = @id"; + var sql = "update Users set ApprovedTime = @ApproveTime, ApprovedBy = @approvedBy where ID = @id"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@id", id)); + cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ApproveTime", DateTime.Now, DbType.DateTime)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@approvedBy", approvedBy)); ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1; }