using Longbow.Data; using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Linq; namespace Bootstrap.DataAccess.SQLite { /// /// /// public class Role : DataAccess.Role { /// /// /// 保存用户角色关系 /// /// /// /// public override bool SaveRolesByUserId(string userId, IEnumerable roleIds) { var ret = false; //判断用户是否选定角色 using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { try { // delete user from config table string sql = $"delete from UserRole where UserID = {userId}"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); roleIds.ToList().ForEach(rId => { cmd.CommandText = $"Insert Into UserRole (UserID, RoleID) Values ( {userId}, {rId})"; DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); }); transaction.CommitTransaction(); } ret = true; } catch (Exception ex) { transaction.RollbackTransaction(); throw ex; } } return ret; } /// /// 删除角色表 /// /// public override bool DeleteRole(IEnumerable value) { bool ret = false; var ids = string.Join(",", value); using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, $"delete from UserRole where RoleID in ({ids})")) { try { DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); cmd.CommandText = $"delete from RoleGroup where RoleID in ({ids})"; DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); cmd.CommandText = $"delete from NavigationRole where RoleID in ({ids})"; DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); cmd.CommandText = $"delete from Roles 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 SavaRolesByMenuId(string menuId, IEnumerable roleIds) { var ret = false; //判断用户是否选定角色 using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { try { // delete role from config table string sql = $"delete from NavigationRole where NavigationID = {menuId}"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); // insert batch data into config table roleIds.ToList().ForEach(rId => { cmd.CommandText = $"Insert Into NavigationRole (NavigationID, RoleID) Values ( {menuId}, {rId})"; DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); }); transaction.CommitTransaction(); } ret = true; } catch (Exception ex) { transaction.RollbackTransaction(); throw ex; } } return ret; } /// /// 根据GroupId更新Roles信息,删除旧的Roles信息,插入新的Roles信息 /// /// /// /// public override bool SaveRolesByGroupId(string groupId, IEnumerable roleIds) { var ret = false; //构造表格 using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) { try { // delete user from config table string sql = $"delete from RoleGroup where GroupID = {groupId}"; using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) { DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); // insert batch data into config table roleIds.ToList().ForEach(rId => { cmd.CommandText = $"Insert Into RoleGroup (GroupID, RoleID) Values ( {groupId}, {rId})"; DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); }); transaction.CommitTransaction(); } ret = true; } catch (Exception ex) { transaction.RollbackTransaction(); throw ex; } } return ret; } } }