BootstrapAdmin/Bootstrap.DataAccess.SQLite/Group.cs

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