diff --git a/Bootstrap.DataAccess.SQLite/Group.cs b/Bootstrap.DataAccess.SQLite/Group.cs index 65f3ee39..8b45216d 100644 --- a/Bootstrap.DataAccess.SQLite/Group.cs +++ b/Bootstrap.DataAccess.SQLite/Group.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.Data; using System.Data.Common; -using System.Data.SqlClient; using System.Linq; namespace Bootstrap.DataAccess.SQLite @@ -22,51 +21,62 @@ namespace Bootstrap.DataAccess.SQLite { bool ret = false; var ids = string.Join(",", value); - using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteGroups")) + using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ids", ids)); - ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1; + using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, $"delete from UserGroup where GroupID in ({ids})")) + { + try + { + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + + cmd.CommandText = $"delete from RoleGroup where GroupID in ({ids})"; + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + + cmd.CommandText = $"delete from Groups where ID in ({ids})"; + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + + transaction.CommitTransaction(); + CacheCleanUtility.ClearCache(groupIds: value); + ret = true; + } + catch (Exception ex) + { + transaction.RollbackTransaction(); + throw ex; + } + } } - CacheCleanUtility.ClearCache(groupIds: value); return ret; } /// /// 保存用户部门关系 /// - /// + /// /// /// - public override bool SaveGroupsByUserId(int id, IEnumerable groupIds) + public override bool SaveGroupsByUserId(int userId, IEnumerable groupIds) { var ret = false; - DataTable dt = new DataTable(); - dt.Columns.Add("UserID", typeof(int)); - dt.Columns.Add("GroupID", typeof(int)); + //判断用户是否选定角色 - groupIds.ToList().ForEach(groupId => dt.Rows.Add(id, groupId)); using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { try { //删除用户部门表中该用户所有的部门关系 - string sql = "delete from UserGroup where UserID=@UserID;"; + string sql = $"delete from UserGroup where UserID = {userId}"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserID", id)); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); - // insert batch data into config table - using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) + groupIds.ToList().ForEach(gId => { - bulk.BatchSize = 1000; - bulk.DestinationTableName = "UserGroup"; - bulk.ColumnMappings.Add("UserID", "UserID"); - bulk.ColumnMappings.Add("GroupID", "GroupID"); - bulk.WriteToServer(dt); - transaction.CommitTransaction(); - } + cmd.CommandText = $"Insert Into UserGroup (UserID, GroupID) Values ({userId}, {gId})"; + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + }); + transaction.CommitTransaction(); } - CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: new List() { id }); + CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: new List() { userId }); ret = true; } catch (Exception ex) @@ -80,38 +90,30 @@ namespace Bootstrap.DataAccess.SQLite /// /// 根据角色ID以及选定的部门ID,保到角色部门表 /// - /// + /// /// /// - public override bool SaveGroupsByRoleId(int id, IEnumerable groupIds) + public override bool SaveGroupsByRoleId(int roleId, IEnumerable groupIds) { bool ret = false; - DataTable dt = new DataTable(); - dt.Columns.Add("GroupID", typeof(int)); - dt.Columns.Add("RoleID", typeof(int)); - groupIds.ToList().ForEach(groupId => dt.Rows.Add(groupId, id)); using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { try { //删除角色部门表该角色所有的部门 - string sql = "delete from RoleGroup where RoleID=@RoleID"; + string sql = $"delete from RoleGroup where RoleID = {roleId}"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RoleID", id)); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); //批插入角色部门表 - using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) + groupIds.ToList().ForEach(gId => { - bulk.BatchSize = 1000; - bulk.ColumnMappings.Add("GroupID", "GroupID"); - bulk.ColumnMappings.Add("RoleID", "RoleID"); - bulk.DestinationTableName = "RoleGroup"; - bulk.WriteToServer(dt); - transaction.CommitTransaction(); - } + cmd.CommandText = $"Insert Into RoleGroup (RoleID, GroupID) Values ({roleId}, {gId})"; + DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); + }); + transaction.CommitTransaction(); } - CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: new List() { id }); + CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: new List() { roleId }); ret = true; } catch (Exception ex) diff --git a/Bootstrap.DataAccess/Group.cs b/Bootstrap.DataAccess/Group.cs index eacbd355..dd465c30 100644 --- a/Bootstrap.DataAccess/Group.cs +++ b/Bootstrap.DataAccess/Group.cs @@ -139,26 +139,25 @@ namespace Bootstrap.DataAccess /// /// 保存用户部门关系 /// - /// + /// /// /// - public virtual bool SaveGroupsByUserId(int id, IEnumerable groupIds) + public virtual bool SaveGroupsByUserId(int userId, IEnumerable groupIds) { var ret = false; DataTable dt = new DataTable(); dt.Columns.Add("UserID", typeof(int)); dt.Columns.Add("GroupID", typeof(int)); //判断用户是否选定角色 - groupIds.ToList().ForEach(groupId => dt.Rows.Add(id, groupId)); + groupIds.ToList().ForEach(groupId => dt.Rows.Add(userId, groupId)); using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { try { //删除用户部门表中该用户所有的部门关系 - string sql = "delete from UserGroup where UserID=@UserID;"; + string sql = $"delete from UserGroup where UserID = {userId}"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@UserID", id)); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); // insert batch data into config table @@ -172,7 +171,7 @@ namespace Bootstrap.DataAccess transaction.CommitTransaction(); } } - CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: new List() { id }); + CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: new List() { userId }); ret = true; } catch (Exception ex) @@ -216,25 +215,24 @@ namespace Bootstrap.DataAccess /// /// 根据角色ID以及选定的部门ID,保到角色部门表 /// - /// + /// /// /// - public virtual bool SaveGroupsByRoleId(int id, IEnumerable groupIds) + public virtual bool SaveGroupsByRoleId(int roleId, IEnumerable groupIds) { bool ret = false; DataTable dt = new DataTable(); dt.Columns.Add("GroupID", typeof(int)); dt.Columns.Add("RoleID", typeof(int)); - groupIds.ToList().ForEach(groupId => dt.Rows.Add(groupId, id)); + groupIds.ToList().ForEach(groupId => dt.Rows.Add(groupId, roleId)); using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { try { //删除角色部门表该角色所有的部门 - string sql = "delete from RoleGroup where RoleID=@RoleID"; + string sql = $"delete from RoleGroup where RoleID = {roleId}"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { - cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RoleID", id)); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); //批插入角色部门表 using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) @@ -247,7 +245,7 @@ namespace Bootstrap.DataAccess transaction.CommitTransaction(); } } - CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: new List() { id }); + CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: new List() { roleId }); ret = true; } catch (Exception ex)