From 15dc1a6b1b09cf74807a93302760898aa61a91cf Mon Sep 17 00:00:00 2001 From: "liuchun_0206@163.com" Date: Sat, 29 Oct 2016 17:38:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A0=B9=E6=8D=AE=E7=BE=A4?= =?UTF-8?q?=E7=BB=84=E6=8C=87=E5=AE=9A=E7=94=A8=E6=88=B7=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/Content/js/framework.js | 10 ++- .../Controllers/UsersController.cs | 6 ++ Bootstrap.Admin/Scripts/Groups.js | 30 ++++--- Bootstrap.Admin/Views/Admin/Groups.cshtml | 1 + Bootstrap.Admin/Web.config | 1 + Bootstrap.DataAccess/UserHelper.cs | 88 +++++++++++++++++++ 6 files changed, 122 insertions(+), 14 deletions(-) diff --git a/Bootstrap.Admin/Content/js/framework.js b/Bootstrap.Admin/Content/js/framework.js index 6466168f..2a8d9984 100644 --- a/Bootstrap.Admin/Content/js/framework.js +++ b/Bootstrap.Admin/Content/js/framework.js @@ -235,7 +235,7 @@ } if (result) { swal("成功", "授权角色", "success"); } else { swal("失败", "授权角色", "error"); } - data.callback(result); + if ($.isFunction(data.callback)) data.callback(result); }, error: function (XMLHttpRequest, textStatus, errorThrown) { if ($.isFunction(data.callback)) data.callback(false); @@ -284,7 +284,7 @@ } if (result) { swal("成功", "授权用户", "success"); } else { swal("失败", "授权用户", "error"); } - data.callback(result); + if ($.isFunction(data.callback)) data.callback(result); }, error: function (XMLHttpRequest, textStatus, errorThrown) { if ($.isFunction(data.callback)) data.callback(false); @@ -299,6 +299,12 @@ User.saveUsersByRoleId = function (roleId, userIds, callback) { processUsersData({ Id: roleId, callback: callback, method: "PUT", data: { type: "role", userIds: userIds } }); } + User.getUsersByGroupeId = function (groupId, callback) { + processUsersData({ Id: groupId, callback: callback, data: { type: "group" } }); + }; + User.saveUsersByGroupId = function (groupId, userIds, callback) { + processUsersData({ Id: groupId, callback: callback, method: "PUT", data: { type: "group", userIds: userIds } }); + } var processGroupsData = function (options) { var data = $.extend({ data: { type: "" }, method: "POST", Id: "" }, options); diff --git a/Bootstrap.Admin/Controllers/UsersController.cs b/Bootstrap.Admin/Controllers/UsersController.cs index fcce52d4..c29bf872 100644 --- a/Bootstrap.Admin/Controllers/UsersController.cs +++ b/Bootstrap.Admin/Controllers/UsersController.cs @@ -38,6 +38,9 @@ namespace Bootstrap.Admin.Controllers case "role": ret = UserHelper.RetrieveUsersByRoleId(id).ToList(); break; + case "group": + ret = UserHelper.RetrieveUsersByGroupId(id).ToList(); + break; default: break; } @@ -75,6 +78,9 @@ namespace Bootstrap.Admin.Controllers case "role": ret = UserHelper.SaveUsersByRoleId(id, userIds); break; + case "group": + ret = UserHelper.SaveUsersByGroupId(id, userIds); + break; default: break; } diff --git a/Bootstrap.Admin/Scripts/Groups.js b/Bootstrap.Admin/Scripts/Groups.js index 82d9b262..4981c3ff 100644 --- a/Bootstrap.Admin/Scripts/Groups.js +++ b/Bootstrap.Admin/Scripts/Groups.js @@ -19,26 +19,32 @@ }); } }, { - id: 'btn_assignGroup', + id: 'btn_assignUser', click: function (row) { - var userId = row.ID; + User.getUsersByGroupeId(row.ID, function (data) { + $("#dialogUser .modal-title").text($.format('{0}-指定用户窗口', row.GroupName)); + $('#dialogUser form').html(data); + $('#dialogUser').modal('show'); + }); } }, { id: 'btnSubmitUserRole', click: function (row) { - var userId = row.ID; + + var groupId = row.ID; var roleIds = $('#dialogRole :checked').map(function (index, element) { return $(element).val(); }).toArray().join(','); - Role.saveRolesByGroupId(userId, roleIds, function (result) { - if (result) { - $('#dialogRole').modal("hide"); - swal("成功", "修改角色", "success"); - } else { - swal("失败", "修改角色", "error"); - } - - }); + Role.saveRolesByGroupId(groupId, roleIds, { modal: 'dialogRole' }); + } + },{ + id: 'btnSubmitRoleUser', + click: function (row) { + var groupId = row.ID; + var userIds = $('#dialogUser :checked').map(function (index, element) { + return $(element).val(); + }).toArray().join(','); + User.saveUsersByGroupId(groupId, userIds, { modal: 'dialogUser' }); } }] }, diff --git a/Bootstrap.Admin/Views/Admin/Groups.cshtml b/Bootstrap.Admin/Views/Admin/Groups.cshtml index e08bd2cf..ea8cfe5d 100644 --- a/Bootstrap.Admin/Views/Admin/Groups.cshtml +++ b/Bootstrap.Admin/Views/Admin/Groups.cshtml @@ -56,4 +56,5 @@ } @section customModal { @Html.Partial("RoleConfig") + @Html.Partial("UserConfig") } \ No newline at end of file diff --git a/Bootstrap.Admin/Web.config b/Bootstrap.Admin/Web.config index 278c7943..b33e2b92 100644 --- a/Bootstrap.Admin/Web.config +++ b/Bootstrap.Admin/Web.config @@ -35,6 +35,7 @@ + diff --git a/Bootstrap.DataAccess/UserHelper.cs b/Bootstrap.DataAccess/UserHelper.cs index 9ab05294..ff081963 100644 --- a/Bootstrap.DataAccess/UserHelper.cs +++ b/Bootstrap.DataAccess/UserHelper.cs @@ -21,6 +21,7 @@ namespace Bootstrap.DataAccess private const string UserDataKey = "UserData-CodeUserHelper"; private const string UserDisplayNameDataKey = "UserData-CodeUserHelper-"; private const string UserRoleIDDataKey = "UserData-CodeUserHelper-Role-"; + private const string UserGroupIDDataKey = "UserData-CodeUserHelper-Group-"; /// /// 查询所有用户 /// @@ -254,5 +255,92 @@ namespace Bootstrap.DataAccess } return ret; } + + /// + /// 通过groupId获取所有用户 + /// + /// + /// + public static IEnumerable RetrieveUsersByGroupId(int groupId) + { + + string key = string.Format("{0}{1}", UserGroupIDDataKey, groupId); + return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(UserGroupIDDataKey), k => + { + List Users = new List(); + string sql = "select u.ID,u.UserName,u.DisplayName,case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserGroup ur on u.ID=ur.UserID and GroupID =@groupId"; + DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupID", groupId, ParameterDirection.Input)); + try + { + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + { + while (reader.Read()) + { + Users.Add(new User() + { + ID = (int)reader[0], + UserName = (string)reader[1], + DisplayName = (string)reader[2], + Checked = (string)reader[3] + }); + } + } + } + catch (Exception ex) { ExceptionManager.Publish(ex); } + return Users; + }, CacheSection.RetrieveDescByKey(UserRoleIDDataKey)); + } + /// + /// 通过部门ID保存当前授权用户(插入) + /// + /// GroupID + /// 用户ID数组 + /// + public static bool SaveUsersByGroupId(int id, string userIds) + { + bool ret = false; + DataTable dt = new DataTable(); + dt.Columns.Add("UserID", typeof(int)); + dt.Columns.Add("GroupID", typeof(int)); + if (!string.IsNullOrEmpty(userIds)) + { + userIds.Split(',').ToList().ForEach(userId => + { + DataRow dr = dt.NewRow(); + dt.Rows.Add(userId, id); + }); + } + using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction()) + { + try + { + //删除用户角色表该角色所有的用户 + 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)); + DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction); + //批插入用户角色表 + using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) + { + bulk.DestinationTableName = "UserGroup"; + bulk.ColumnMappings.Add("UserID", "UserID"); + bulk.ColumnMappings.Add("GroupID", "GroupID"); + bulk.WriteToServer(dt); + transaction.CommitTransaction(); + } + } + ret = true; + ClearCache(); + } + catch (Exception ex) + { + ExceptionManager.Publish(ex); + transaction.RollbackTransaction(); + } + } + return ret; + } } }