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 Group : DataAccess.Group
{
///
///
/// 删除群组信息
///
///
public override bool DeleteGroup(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 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;
}
}
}
return ret;
}
///
/// 保存用户部门关系
///
///
///
///
public override bool SaveGroupsByUserId(int userId, IEnumerable groupIds)
{
var ret = false;
//判断用户是否选定角色
using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
{
try
{
//删除用户部门表中该用户所有的部门关系
string sql = $"delete from UserGroup where UserID = {userId}";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
groupIds.ToList().ForEach(gId =>
{
cmd.CommandText = $"Insert Into UserGroup (UserID, GroupID) Values ({userId}, {gId})";
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
});
transaction.CommitTransaction();
}
CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: new List() { userId });
ret = true;
}
catch (Exception ex)
{
transaction.RollbackTransaction();
throw ex;
}
}
return ret;
}
///
/// 根据角色ID以及选定的部门ID,保到角色部门表
///
///
///
///
public override bool SaveGroupsByRoleId(int roleId, IEnumerable groupIds)
{
bool ret = false;
using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
{
try
{
//删除角色部门表该角色所有的部门
string sql = $"delete from RoleGroup where RoleID = {roleId}";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
//批插入角色部门表
groupIds.ToList().ForEach(gId =>
{
cmd.CommandText = $"Insert Into RoleGroup (RoleID, GroupID) Values ({roleId}, {gId})";
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
});
transaction.CommitTransaction();
}
CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: new List() { roleId });
ret = true;
}
catch (Exception ex)
{
transaction.RollbackTransaction();
throw ex;
}
}
return ret;
}
}
}