修改Helper类中与事物相关的方法,及修复角色页面bug

This commit is contained in:
lq_avenue 2016-10-31 15:55:31 +08:00
parent 6ee4048bbd
commit a29edf8a27
6 changed files with 118 additions and 131 deletions

View File

@ -22,7 +22,7 @@
id: 'btn_assignUser',
click: function (row) {
User.getUsersByGroupeId(row.ID, function (data) {
$("#dialogUser .modal-title").text($.format('{0}-指定用户窗口', row.GroupName));
$("#dialogUser .modal-title").text($.format('{0}-用户授权窗口', row.GroupName));
$('#dialogUser form').html(data);
$('#dialogUser').modal('show');
});

View File

@ -13,7 +13,7 @@
id: 'btn_assignUser',
click: function (row) {
User.getUsersByRoleId(row.ID, function (data) {
$("#dialogUser.modal-title").text($.format('{0}-用户授权窗口', row.RoleName));
$("#dialogUser .modal-title").text($.format('{0}-用户授权窗口', row.RoleName));
$('#dialogUser form').html(data);
$('#dialogUser').modal('show');
})
@ -22,7 +22,7 @@
id: 'btn_assignGroup',
click: function (row) {
Group.getGroupsByRoleId(row.ID, function (data) {
$("#dialogGroup.modal-title").text($.format('{0}-部门授权窗口', row.RoleName));
$("#dialogGroup .modal-title").text($.format('{0}-部门授权窗口', row.RoleName));
$('#dialogGroup form').html(data);
$('#dialogGroup').modal('show');
})

View File

@ -36,6 +36,7 @@
<add key="LogData-CodeMenuHelper" interval="600" desc="日志信息缓存" />
<add key="DictData-CodeDictHelper" interval="600" desc="字典信息缓存" />
<add key="UserData-CodeUserHelper-Group-" interval="600" desc="用户信息缓存" />
<add key="GroupData-CodeGroupHelper-" interval="600" desc="用户部门信息缓存"/>
</cacheManager>
<cacheManagerList>

View File

@ -20,7 +20,7 @@ namespace Bootstrap.DataAccess
{
private const string GroupDataKey = "GroupData-CodeGroupHelper";
private const string GroupUserIDDataKey = "GroupData-CodeGroupHelper-";
private const string GroupRoleIDDataKey = "GroupData-CodeGroupHelper-Role-";
private const string GroupRoleIDDataKey = "GroupData-CodeGroupHelper-Role-";
/// <summary>
/// 查询所有群组信息
/// </summary>
@ -150,63 +150,60 @@ namespace Bootstrap.DataAccess
/// <param name="id"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool SaveGroupsByUserId(int id, string value)
public static bool SaveGroupsByUserId(int id, string groupIds)
{
var ret = false;
DataTable dt = new DataTable();
dt.Columns.Add("UserID", typeof(int));
dt.Columns.Add("GroupID", typeof(int));
//判断用户是否选定角色
if (!string.IsNullOrEmpty(value))
if (!string.IsNullOrEmpty(groupIds))
{
string[] groupIDs = value.Split(',');
foreach (string groupID in groupIDs)
groupIds.Split(',').ToList().ForEach(groupId =>
{
DataRow row = dt.NewRow();
row["UserID"] = id;
row["GroupID"] = groupID;
dt.Rows.Add(row);
}
dt.Rows.Add(id, groupId);
});
}
string sql = "delete from UserGroup where UserID=@UserID;";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", id, ParameterDirection.Input));
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
try
{
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction))
//删除用户部门表中该用户所有的部门关系
string sql = "delete from UserGroup where UserID=@UserID;";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
bulk.BatchSize = 1000;
bulk.DestinationTableName = "UserGroup";
bulk.ColumnMappings.Add("UserID", "UserID");
bulk.ColumnMappings.Add("GroupID", "GroupID");
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", id, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction);
bool ret = true;
try
// insert batch data into config table
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction);
bulk.BatchSize = 1000;
bulk.DestinationTableName = "UserGroup";
bulk.ColumnMappings.Add("UserID", "UserID");
bulk.ColumnMappings.Add("GroupID", "GroupID");
bulk.WriteToServer(dt);
transaction.CommitTransaction();
ClearCache();
}
catch (Exception ex)
{
ret = false;
transaction.RollbackTransaction();
}
return ret;
}
ret = true;
ClearCache();
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
transaction.RollbackTransaction();
}
}
return ret;
}
// 更新缓存
private static void ClearCache()
private static void ClearCache(string cacheKey = null)
{
CacheManager.Clear(key => key == GroupDataKey);
CacheManager.Clear(key => key == GroupUserIDDataKey);
CacheManager.Clear(key => key.Contains(GroupRoleIDDataKey));
CacheManager.Clear(key => string.IsNullOrEmpty(cacheKey) || key == cacheKey);
}
/// <summary>
/// <summary>
/// 根据角色ID指派部门
/// </summary>
/// <param name="roleId"></param>
@ -228,9 +225,9 @@ namespace Bootstrap.DataAccess
{
Groups.Add(new Group()
{
ID=(int)reader[0],
GroupName=(string)reader[1],
Description=(string)reader[2],
ID = (int)reader[0],
GroupName = (string)reader[1],
Description = (string)reader[2],
Checked = (string)reader[3]
});
}
@ -238,7 +235,7 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Groups;
},CacheSection.RetrieveDescByKey(GroupRoleIDDataKey));
}, CacheSection.RetrieveDescByKey(GroupRoleIDDataKey));
}
/// <summary>
/// 根据角色ID以及选定的部门ID保到角色部门表
@ -260,21 +257,22 @@ namespace Bootstrap.DataAccess
dt.Rows.Add(groupId, id);
});
}
using(TransactionPackage transaction=DBAccessManager.SqlDBAccess.BeginTransaction()){
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
{
try
{
//删除角色部门表该角色所有的部门
string sql = "delete from RoleGroup where RoleID=@RoleID";
using (DbCommand cmd=DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text,sql))
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", id, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction);
//批插入角色部门表
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction))
{
bulk.BatchSize=1000;
bulk.ColumnMappings.Add("GroupID","GroupID");
bulk.ColumnMappings.Add("RoleID","RoleID");
bulk.BatchSize = 1000;
bulk.ColumnMappings.Add("GroupID", "GroupID");
bulk.ColumnMappings.Add("RoleID", "RoleID");
bulk.DestinationTableName = "RoleGroup";
bulk.WriteToServer(dt);
transaction.CommitTransaction();
@ -283,7 +281,8 @@ namespace Bootstrap.DataAccess
ret = true;
ClearCache();
}
catch (Exception ex) {
catch (Exception ex)
{
ExceptionManager.Publish(ex);
transaction.RollbackTransaction();
}

View File

@ -3,7 +3,6 @@ using Longbow.Caching;
using Longbow.Caching.Configuration;
using Longbow.Data;
using Longbow.ExceptionManagement;
using Longbow.Data;
using System;
using System.Collections.Generic;
using System.Data;
@ -11,7 +10,6 @@ using System.Data.Common;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Data.SqlClient;
namespace Bootstrap.DataAccess
{
@ -110,9 +108,6 @@ namespace Bootstrap.DataAccess
}
return ret;
}
/// <summary>
/// 查询某个用户所拥有的角色
/// </summary>
@ -146,7 +141,7 @@ namespace Bootstrap.DataAccess
return Roles;
}, CacheSection.RetrieveDescByKey(RoleUserIDDataKey));
}
/// <summary>
/// 删除角色表
/// </summary>
@ -239,55 +234,53 @@ namespace Bootstrap.DataAccess
}, CacheSection.RetrieveDescByKey(RoleNavigationIDDataKey));
return ret;
}
public static bool SavaRolesByMenuId(int id, string value)
public static bool SavaRolesByMenuId(int id, string roleIds)
{
var ret = false;
DataTable dt = new DataTable();
dt.Columns.Add("NavigationID", typeof(int));
dt.Columns.Add("RoleID", typeof(int));
//判断用户是否选定角色
if (!string.IsNullOrEmpty(value))
if (!string.IsNullOrEmpty(roleIds))
{
string[] roleIDs = value.Split(',');
foreach (string roleID in roleIDs)
roleIds.Split(',').ToList().ForEach(roleId =>
{
DataRow row = dt.NewRow();
row["NavigationID"] = id;
row["RoleID"] = roleID;
dt.Rows.Add(row);
}
dt.Rows.Add(id, roleId);
});
}
string sql = "delete from NavigationRole where NavigationID=@NavigationID;";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", id, ParameterDirection.Input));
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
try
{
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction))
// delete role from config table
string sql = "delete from NavigationRole where NavigationID=@NavigationID;";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
bulk.BatchSize = 1000;
bulk.DestinationTableName = "NavigationRole";
bulk.ColumnMappings.Add("NavigationID", "NavigationID");
bulk.ColumnMappings.Add("RoleID", "RoleID");
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", id, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction);
bool ret = true;
try
// insert batch data into config table
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction);
bulk.BatchSize = 1000;
bulk.DestinationTableName = "NavigationRole";
bulk.ColumnMappings.Add("NavigationID", "NavigationID");
bulk.ColumnMappings.Add("RoleID", "RoleID");
bulk.WriteToServer(dt);
transaction.CommitTransaction();
ClearCache();
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
ret = false;
transaction.RollbackTransaction();
}
return ret;
}
ret = true;
ClearCache();
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
transaction.RollbackTransaction();
}
}
return ret;
}
// 更新缓存
private static void ClearCache(string cacheKey = null)
@ -335,56 +328,53 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveRolesByGroupId(int gid, string value)
public static bool SaveRolesByGroupId(int id, string roleIds)
{
var ret = false;
//构造表格
DataTable dt = new DataTable();
dt.Columns.Add("RoleID", typeof(int));
dt.Columns.Add("GroupID", typeof(int));
if (!string.IsNullOrEmpty(value))
if (!string.IsNullOrEmpty(roleIds))
{
string[] roles = value.Split(',');
foreach (string role in roles)
roleIds.Split(',').ToList().ForEach(roleId =>
{
DataRow r = dt.NewRow();
r["RoleID"] = role;
r["GroupID"] = gid;
dt.Rows.Add(r);
DataRow row = dt.NewRow();
dt.Rows.Add(roleId,id);
});
}
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
{
try
{
// delete user from config table
string sql = "delete from RoleGroup where GroupID=@GroupID";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupID", id, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction);
// insert batch data into config table
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction))
{
bulk.BatchSize = 1000;
bulk.DestinationTableName = "RoleGroup";
bulk.ColumnMappings.Add("RoleID", "RoleID");
bulk.ColumnMappings.Add("GroupID", "GroupID");
bulk.WriteToServer(dt);
transaction.CommitTransaction();
}
}
ret = true;
ClearCache();
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
transaction.RollbackTransaction();
}
}
var trans = DBAccessManager.SqlDBAccess.BeginTransaction();
try
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, string.Empty))
{
// 执行第一个sql
cmd.CommandText = "delete from RoleGroup where GroupID=@GroupID";
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupID", gid, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, trans);
}
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)trans.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)trans.Transaction))
{
bulk.BatchSize = 1000;
bulk.DestinationTableName = "RoleGroup";
bulk.ColumnMappings.Add("RoleID", "RoleID");
bulk.ColumnMappings.Add("GroupID", "GroupID");
bulk.WriteToServer(dt);
}
trans.CommitTransaction();
ClearCache();
return true;
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
trans.RollbackTransaction();
return false;
}
return ret;
}
}
}

View File

@ -161,14 +161,11 @@ namespace Bootstrap.DataAccess
return user != null && user.Password == LgbCryptography.ComputeHash(password, user.PassSalt);
}
// 更新缓存
private static void ClearCache()
private static void ClearCache(string cacheKey = null)
{
CacheManager.Clear(key => key == UserDataKey);
CacheManager.Clear(key => key.Contains(UserDisplayNameDataKey));
CacheManager.Clear(key => key.Contains(UserRoleIDDataKey));
CacheManager.Clear(key => string.IsNullOrEmpty(cacheKey) || key == cacheKey);
}
/// <summary>
/// 通过roleId获取所有用户
/// </summary>
@ -229,7 +226,7 @@ namespace Bootstrap.DataAccess
try
{
//删除用户角色表该角色所有的用户
string sql = "delete from UserRole where RoleId=@RoleId";
string sql = "delete from UserRole where RoleID=@RoleID";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", id, ParameterDirection.Input));
@ -316,10 +313,10 @@ namespace Bootstrap.DataAccess
try
{
//删除用户角色表该角色所有的用户
string sql = "delete from UserGroup where GroupID=@groupID";
string sql = "delete from UserGroup where GroupID=@GroupID";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@groupID", id, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupID", id, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction);
//批插入用户角色表
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction))