From dfd4913deb83959c179a4f229a118611ae9bf860 Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Wed, 31 Oct 2018 09:51:28 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=EF=BC=9A?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8Group=E7=BC=93=E5=AD=98=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=88=B0GroupHelper=E7=B1=BB=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/GroupsController.cs | 9 ++- Bootstrap.DataAccess.MongoDB/Group.cs | 76 ++++++++++++++++++- .../MongoDbAccessManager.cs | 17 ++++- Bootstrap.DataAccess.SQLite/Group.cs | 3 - Bootstrap.DataAccess/Group.cs | 4 - Bootstrap.DataAccess/Helper/GroupHelper.cs | 34 ++++++++- 6 files changed, 127 insertions(+), 16 deletions(-) diff --git a/Bootstrap.Admin/Controllers/Api/GroupsController.cs b/Bootstrap.Admin/Controllers/Api/GroupsController.cs index 7de801d6..52fe15da 100644 --- a/Bootstrap.Admin/Controllers/Api/GroupsController.cs +++ b/Bootstrap.Admin/Controllers/Api/GroupsController.cs @@ -23,6 +23,7 @@ namespace Bootstrap.Admin.Controllers.Api { return value.RetrieveData(); } + /// /// /// @@ -33,6 +34,7 @@ namespace Bootstrap.Admin.Controllers.Api { return GroupHelper.RetrieveGroups().FirstOrDefault(t => t.Id == id); } + /// /// /// @@ -42,6 +44,7 @@ namespace Bootstrap.Admin.Controllers.Api { return GroupHelper.SaveGroup(value); } + /// /// /// @@ -51,8 +54,9 @@ namespace Bootstrap.Admin.Controllers.Api { return GroupHelper.DeleteGroup(value); } + /// - /// + /// 获取部门授权 /// /// /// @@ -74,8 +78,9 @@ namespace Bootstrap.Admin.Controllers.Api } return ret; } + /// - /// + /// 保存部门授权 /// /// /// diff --git a/Bootstrap.DataAccess.MongoDB/Group.cs b/Bootstrap.DataAccess.MongoDB/Group.cs index 6d06b39e..8f5e149f 100644 --- a/Bootstrap.DataAccess.MongoDB/Group.cs +++ b/Bootstrap.DataAccess.MongoDB/Group.cs @@ -1,5 +1,6 @@ using MongoDB.Driver; using System.Collections.Generic; +using System.Linq; namespace Bootstrap.DataAccess.MongoDB { @@ -14,8 +15,79 @@ namespace Bootstrap.DataAccess.MongoDB /// public override IEnumerable RetrieveGroups() { - var groups = MongoDbAccessManager.DBAccess.GetCollection("Groups"); - return groups.Find(FilterDefinition.Empty).ToList(); + return MongoDbAccessManager.Groups.Find(FilterDefinition.Empty).ToList(); + } + + /// + /// + /// + /// + /// + public override bool SaveGroup(DataAccess.Group p) + { + if (p.Id == "0") + { + p.Id = null; + MongoDbAccessManager.Groups.InsertOne(p); + return true; + } + else + { + MongoDbAccessManager.Groups.UpdateOne(md => md.Id == p.Id, Builders.Update.Set(md => md.GroupName, p.GroupName).Set(md => md.Description, p.Description)); + return true; + } + } + + /// + /// + /// + /// + /// + public override bool DeleteGroup(IEnumerable value) + { + var list = new List>(); + foreach (var id in value) + { + list.Add(new DeleteOneModel(Builders.Filter.Eq(g => g.Id, id))); + } + MongoDbAccessManager.Groups.BulkWrite(list); + return true; + } + + /// + /// + /// + /// + /// + public override IEnumerable RetrieveGroupsByUserId(string userId) + { + var groups = GroupHelper.RetrieveGroups(); + var user = UserHelper.RetrieveUsers().Cast().FirstOrDefault(u => u.Id == userId); + groups.ToList().ForEach(g => g.Checked = user.Groups.Any(id => id == g.Id) ? "checked" : ""); + return groups; + } + + /// + /// + /// + /// + /// + /// + public override bool SaveGroupsByUserId(string userId, IEnumerable groupIds) + { + MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders.Update.Set(u => u.Groups, groupIds)); + return true; + } + + /// + /// + /// + /// + /// + /// + public override bool SaveGroupsByRoleId(string roleId, IEnumerable groupIds) + { + return base.SaveGroupsByRoleId(roleId, groupIds); } } } diff --git a/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs b/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs index 8f7d5a02..47d0413e 100644 --- a/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs +++ b/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs @@ -16,6 +16,7 @@ namespace Bootstrap.DataAccess.MongoDB { private static IMongoDatabase _db = null; private static bool _register = false; + /// /// /// @@ -36,6 +37,7 @@ namespace Bootstrap.DataAccess.MongoDB return _db; } } + /// /// /// @@ -46,6 +48,7 @@ namespace Bootstrap.DataAccess.MongoDB return DBAccess.GetCollection("Logs"); } } + /// /// /// @@ -66,6 +69,7 @@ namespace Bootstrap.DataAccess.MongoDB return DBAccess.GetCollection("Dicts"); } } + /// /// /// @@ -76,7 +80,18 @@ namespace Bootstrap.DataAccess.MongoDB return DBAccess.GetCollection("Users"); } } - + + /// + /// + /// + public static IMongoCollection Groups + { + get + { + return DBAccess.GetCollection("Groups"); + } + } + private static void InitDb() { var connectString = DbAdapterManager.GetConnectionString("ba"); diff --git a/Bootstrap.DataAccess.SQLite/Group.cs b/Bootstrap.DataAccess.SQLite/Group.cs index 31b19895..65e03ada 100644 --- a/Bootstrap.DataAccess.SQLite/Group.cs +++ b/Bootstrap.DataAccess.SQLite/Group.cs @@ -36,7 +36,6 @@ namespace Bootstrap.DataAccess.SQLite DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); transaction.CommitTransaction(); - CacheCleanUtility.ClearCache(groupIds: value); ret = true; } catch (Exception ex) @@ -76,7 +75,6 @@ namespace Bootstrap.DataAccess.SQLite }); transaction.CommitTransaction(); } - CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: new List() { userId }); ret = true; } catch (Exception ex) @@ -113,7 +111,6 @@ namespace Bootstrap.DataAccess.SQLite }); transaction.CommitTransaction(); } - CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: new List() { roleId }); ret = true; } catch (Exception ex) diff --git a/Bootstrap.DataAccess/Group.cs b/Bootstrap.DataAccess/Group.cs index e6058f7c..2ec38996 100644 --- a/Bootstrap.DataAccess/Group.cs +++ b/Bootstrap.DataAccess/Group.cs @@ -66,7 +66,6 @@ namespace Bootstrap.DataAccess cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ids", ids)); ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1; } - CacheCleanUtility.ClearCache(groupIds: value); return ret; } /// @@ -89,7 +88,6 @@ namespace Bootstrap.DataAccess cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Description", DbAdapterManager.ToDBValue(p.Description))); ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1; } - CacheCleanUtility.ClearCache(groupIds: string.IsNullOrEmpty(p.Id) ? new List() : new List() { p.Id }); return ret; } /// @@ -153,7 +151,6 @@ namespace Bootstrap.DataAccess transaction.CommitTransaction(); } } - CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: new List() { userId }); ret = true; } catch (Exception ex) @@ -223,7 +220,6 @@ namespace Bootstrap.DataAccess transaction.CommitTransaction(); } } - CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: new List() { roleId }); ret = true; } catch (Exception ex) diff --git a/Bootstrap.DataAccess/Helper/GroupHelper.cs b/Bootstrap.DataAccess/Helper/GroupHelper.cs index 704214ee..a2c5224d 100644 --- a/Bootstrap.DataAccess/Helper/GroupHelper.cs +++ b/Bootstrap.DataAccess/Helper/GroupHelper.cs @@ -13,23 +13,36 @@ namespace Bootstrap.DataAccess public const string RetrieveGroupsByUserIdDataKey = "GroupHelper-RetrieveGroupsByUserId"; public const string RetrieveGroupsByRoleIdDataKey = "GroupHelper-RetrieveGroupsByRoleId"; public const string RetrieveGroupsByUserNameDataKey = "GroupHelper-RetrieveGroupsByUserName"; + /// /// 查询所有群组信息 /// /// /// public static IEnumerable RetrieveGroups() => CacheManager.GetOrAdd(RetrieveGroupsDataKey, key => DbAdapterManager.Create().RetrieveGroups()); + /// /// 删除群组信息 /// /// - public static bool DeleteGroup(IEnumerable value) => DbAdapterManager.Create().DeleteGroup(value); + public static bool DeleteGroup(IEnumerable value) + { + var ret = DbAdapterManager.Create().DeleteGroup(value); + if (ret) CacheCleanUtility.ClearCache(groupIds: value); + return ret; + } + /// /// 保存新建/更新的群组信息 /// /// /// - public static bool SaveGroup(Group p) => DbAdapterManager.Create().SaveGroup(p); + public static bool SaveGroup(Group p) + { + var ret = DbAdapterManager.Create().SaveGroup(p); + if (ret) CacheCleanUtility.ClearCache(groupIds: string.IsNullOrEmpty(p.Id) ? new List() : new List() { p.Id }); + return ret; + } /// /// 根据用户查询部门信息 /// @@ -43,20 +56,33 @@ namespace Bootstrap.DataAccess /// /// /// - public static bool SaveGroupsByUserId(string userId, IEnumerable groupIds) => DbAdapterManager.Create().SaveGroupsByUserId(userId, groupIds); + public static bool SaveGroupsByUserId(string userId, IEnumerable groupIds) + { + var ret = DbAdapterManager.Create().SaveGroupsByUserId(userId, groupIds); + if (ret) CacheCleanUtility.ClearCache(groupIds: groupIds, userIds: new List() { userId }); + return ret; + } + /// /// 根据角色ID指派部门 /// /// /// public static IEnumerable RetrieveGroupsByRoleId(string roleId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveGroupsByRoleIdDataKey, roleId), key => DbAdapterManager.Create().RetrieveGroupsByRoleId(roleId), RetrieveGroupsByRoleIdDataKey); + /// /// 根据角色ID以及选定的部门ID,保到角色部门表 /// /// /// /// - public static bool SaveGroupsByRoleId(string roleId, IEnumerable groupIds) => DbAdapterManager.Create().SaveGroupsByRoleId(roleId, groupIds); + public static bool SaveGroupsByRoleId(string roleId, IEnumerable groupIds) + { + var ret = DbAdapterManager.Create().SaveGroupsByRoleId(roleId, groupIds); + if (ret) CacheCleanUtility.ClearCache(groupIds: groupIds, roleIds: new List() { roleId }); + return ret; + } + /// /// ///