BootstrapAdmin/Bootstrap.DataAccess.SQLite/Role.cs

168 lines
6.4 KiB
C#
Raw Normal View History

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