修改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', id: 'btn_assignUser',
click: function (row) { click: function (row) {
User.getUsersByGroupeId(row.ID, function (data) { 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 form').html(data);
$('#dialogUser').modal('show'); $('#dialogUser').modal('show');
}); });

View File

@ -13,7 +13,7 @@
id: 'btn_assignUser', id: 'btn_assignUser',
click: function (row) { click: function (row) {
User.getUsersByRoleId(row.ID, function (data) { 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 form').html(data);
$('#dialogUser').modal('show'); $('#dialogUser').modal('show');
}) })
@ -22,7 +22,7 @@
id: 'btn_assignGroup', id: 'btn_assignGroup',
click: function (row) { click: function (row) {
Group.getGroupsByRoleId(row.ID, function (data) { 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 form').html(data);
$('#dialogGroup').modal('show'); $('#dialogGroup').modal('show');
}) })

View File

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

View File

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

View File

@ -3,7 +3,6 @@ using Longbow.Caching;
using Longbow.Caching.Configuration; using Longbow.Caching.Configuration;
using Longbow.Data; using Longbow.Data;
using Longbow.ExceptionManagement; using Longbow.ExceptionManagement;
using Longbow.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@ -11,7 +10,6 @@ using System.Data.Common;
using System.Data.SqlClient; using System.Data.SqlClient;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Data.SqlClient;
namespace Bootstrap.DataAccess namespace Bootstrap.DataAccess
{ {
@ -110,9 +108,6 @@ namespace Bootstrap.DataAccess
} }
return ret; return ret;
} }
/// <summary> /// <summary>
/// 查询某个用户所拥有的角色 /// 查询某个用户所拥有的角色
/// </summary> /// </summary>
@ -146,7 +141,7 @@ namespace Bootstrap.DataAccess
return Roles; return Roles;
}, CacheSection.RetrieveDescByKey(RoleUserIDDataKey)); }, CacheSection.RetrieveDescByKey(RoleUserIDDataKey));
} }
/// <summary> /// <summary>
/// 删除角色表 /// 删除角色表
/// </summary> /// </summary>
@ -239,55 +234,53 @@ namespace Bootstrap.DataAccess
}, CacheSection.RetrieveDescByKey(RoleNavigationIDDataKey)); }, CacheSection.RetrieveDescByKey(RoleNavigationIDDataKey));
return ret; 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(); DataTable dt = new DataTable();
dt.Columns.Add("NavigationID", typeof(int)); dt.Columns.Add("NavigationID", typeof(int));
dt.Columns.Add("RoleID", typeof(int)); dt.Columns.Add("RoleID", typeof(int));
//判断用户是否选定角色 //判断用户是否选定角色
if (!string.IsNullOrEmpty(value)) if (!string.IsNullOrEmpty(roleIds))
{ {
string[] roleIDs = value.Split(','); roleIds.Split(',').ToList().ForEach(roleId =>
foreach (string roleID in roleIDs)
{ {
DataRow row = dt.NewRow(); DataRow row = dt.NewRow();
row["NavigationID"] = id; dt.Rows.Add(id, roleId);
row["RoleID"] = roleID; });
dt.Rows.Add(row);
}
} }
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
string sql = "delete from NavigationRole where NavigationID=@NavigationID;";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{ {
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", id, ParameterDirection.Input)); try
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
{ {
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; cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", id, ParameterDirection.Input));
bulk.DestinationTableName = "NavigationRole"; DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction);
bulk.ColumnMappings.Add("NavigationID", "NavigationID");
bulk.ColumnMappings.Add("RoleID", "RoleID");
bool ret = true; // insert batch data into config table
try 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); bulk.WriteToServer(dt);
transaction.CommitTransaction(); 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) private static void ClearCache(string cacheKey = null)
@ -335,56 +328,53 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="p"></param> /// <param name="p"></param>
/// <returns></returns> /// <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(); DataTable dt = new DataTable();
dt.Columns.Add("RoleID", typeof(int)); dt.Columns.Add("RoleID", typeof(int));
dt.Columns.Add("GroupID", typeof(int)); dt.Columns.Add("GroupID", typeof(int));
if (!string.IsNullOrEmpty(value)) if (!string.IsNullOrEmpty(roleIds))
{ {
string[] roles = value.Split(','); roleIds.Split(',').ToList().ForEach(roleId =>
foreach (string role in roles)
{ {
DataRow r = dt.NewRow(); DataRow row = dt.NewRow();
r["RoleID"] = role; dt.Rows.Add(roleId,id);
r["GroupID"] = gid; });
dt.Rows.Add(r); }
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();
} }
} }
return ret;
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;
}
} }
} }
} }

View File

@ -161,14 +161,11 @@ namespace Bootstrap.DataAccess
return user != null && user.Password == LgbCryptography.ComputeHash(password, user.PassSalt); 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 => string.IsNullOrEmpty(cacheKey) || key == cacheKey);
CacheManager.Clear(key => key.Contains(UserDisplayNameDataKey));
CacheManager.Clear(key => key.Contains(UserRoleIDDataKey));
} }
/// <summary> /// <summary>
/// 通过roleId获取所有用户 /// 通过roleId获取所有用户
/// </summary> /// </summary>
@ -229,7 +226,7 @@ namespace Bootstrap.DataAccess
try 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)) using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{ {
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", id, ParameterDirection.Input)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", id, ParameterDirection.Input));
@ -316,10 +313,10 @@ namespace Bootstrap.DataAccess
try 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)) 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); DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction);
//批插入用户角色表 //批插入用户角色表
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction))