From bd7842d5be2c78b5060e540f719c5ee73f841209 Mon Sep 17 00:00:00 2001 From: yubaolee Date: Sat, 31 Oct 2015 23:04:01 +0800 Subject: [PATCH] Routine Update --- OpenAuth.App/OrgManagerApp.cs | 66 ++++++++++---- .../Controllers/OrgManagerController.cs | 2 +- OpenAuth.Mvc/OpenAuth.Mvc.csproj | 16 ++-- OpenAuth.Mvc/Web.config | 4 +- OpenAuth.Mvc/packages.config | 91 +++++++++---------- OpenAuth.UnitTest/TestOrgApp.cs | 26 ++++++ 6 files changed, 127 insertions(+), 78 deletions(-) diff --git a/OpenAuth.App/OrgManagerApp.cs b/OpenAuth.App/OrgManagerApp.cs index 35d0454e..066450fd 100644 --- a/OpenAuth.App/OrgManagerApp.cs +++ b/OpenAuth.App/OrgManagerApp.cs @@ -1,8 +1,8 @@ -using System; +using OpenAuth.Domain; +using OpenAuth.Domain.Interface; +using System; using System.Collections.Generic; using System.Linq; -using OpenAuth.Domain; -using OpenAuth.Domain.Interface; namespace OpenAuth.App { @@ -15,7 +15,6 @@ namespace OpenAuth.App _repository = repository; } - public IEnumerable GetAll() { return _repository.LoadOrgs(); @@ -30,24 +29,14 @@ namespace OpenAuth.App public int AddOrg(Org org) { string cascadeId; - int currentCascadeId = 1; - + int currentCascadeId = GetMaxCascadeId(org.ParentId); if (org.ParentId != 0) { - //根据同一级中最大的语义ID - var maxCascadeIdOrg = _repository.Find(o => o.ParentId == org.ParentId) - .OrderByDescending(o =>o.CascadeId).FirstOrDefault(); - if (maxCascadeIdOrg != null) - { - var cascades = maxCascadeIdOrg.CascadeId.Split('.'); - currentCascadeId = int.Parse(cascades[cascades.Length - 1]) + 1; - } - var parentOrg = _repository.FindSingle(o => o.Id == org.ParentId); if (parentOrg != null) { - cascadeId = parentOrg.CascadeId +"." + currentCascadeId; + cascadeId = parentOrg.CascadeId + "." + currentCascadeId; org.ParentName = parentOrg.Name; } else @@ -70,17 +59,54 @@ namespace OpenAuth.App /// /// 部门的直接子部门 + /// TODO:可以根据用户的喜好决定选择LoadAllChildren或LoadDirectChildren /// /// The org unique identifier. /// IEnumerable{Org}. - public IEnumerable LoadChildren(int orgId) + public IEnumerable LoadDirectChildren(int orgId) { return _repository.Find(u => u.ParentId == orgId); } - public void DelOrg(int Id) + //得到部门的所有子部门 + public IEnumerable LoadAllChildren(int orgId) { - _repository.Delete(u =>u.Id ==Id); + var org = _repository.FindSingle(u => u.Id == orgId); + if (org == null) + throw new Exception("未能找到指定对象信息"); + + return _repository.Find(u => u.CascadeId.Contains(org.CascadeId) && u.Id != orgId); } + + /// + /// 删除指定ID的部门及其所有子部门 + /// + public void DelOrg(int id) + { + var delOrg = _repository.FindSingle(u => u.Id == id); + if (delOrg == null) + throw new Exception("未能找到要删除的对象"); + + _repository.Delete(u => u.CascadeId.Contains(delOrg.CascadeId)); + } + + #region 私有方法 + + //根据同一级中最大的语义ID + private int GetMaxCascadeId(int parentId) + { + int currentCascadeId = 1; + + var maxCascadeIdOrg = _repository.Find(o => o.ParentId == parentId) + .OrderByDescending(o => o.CascadeId).FirstOrDefault(); + if (maxCascadeIdOrg != null) + { + var cascades = maxCascadeIdOrg.CascadeId.Split('.'); + currentCascadeId = int.Parse(cascades[cascades.Length - 1]) + 1; + } + return currentCascadeId; + } + + #endregion 私有方法 } -} +} \ No newline at end of file diff --git a/OpenAuth.Mvc/Controllers/OrgManagerController.cs b/OpenAuth.Mvc/Controllers/OrgManagerController.cs index f4ec9440..ceabfa21 100644 --- a/OpenAuth.Mvc/Controllers/OrgManagerController.cs +++ b/OpenAuth.Mvc/Controllers/OrgManagerController.cs @@ -32,7 +32,7 @@ namespace OpenAuth.Mvc.Controllers public string LoadChildren(int id) { - return JsonHelper.Instance.Serialize(_orgApp.LoadChildren(id)); + return JsonHelper.Instance.Serialize(_orgApp.LoadAllChildren(id)); } public void DelOrg(string json) diff --git a/OpenAuth.Mvc/OpenAuth.Mvc.csproj b/OpenAuth.Mvc/OpenAuth.Mvc.csproj index 2c93fde3..d42aede2 100644 --- a/OpenAuth.Mvc/OpenAuth.Mvc.csproj +++ b/OpenAuth.Mvc/OpenAuth.Mvc.csproj @@ -45,6 +45,14 @@ ..\packages\Autofac.Mvc5.3.3.4\lib\net45\Autofac.Integration.Mvc.dll + + False + ..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll + + + False + ..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll + False @@ -108,14 +116,6 @@ ..\packages\WebGrease.1.5.2\lib\WebGrease.dll - - - ..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll - - - ..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll - - diff --git a/OpenAuth.Mvc/Web.config b/OpenAuth.Mvc/Web.config index 28973d2e..4fed6541 100644 --- a/OpenAuth.Mvc/Web.config +++ b/OpenAuth.Mvc/Web.config @@ -5,11 +5,11 @@ --> - +
- + diff --git a/OpenAuth.Mvc/packages.config b/OpenAuth.Mvc/packages.config index f3846668..c4218b8a 100644 --- a/OpenAuth.Mvc/packages.config +++ b/OpenAuth.Mvc/packages.config @@ -1,48 +1,45 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OpenAuth.UnitTest/TestOrgApp.cs b/OpenAuth.UnitTest/TestOrgApp.cs index 91049c6e..277e676d 100644 --- a/OpenAuth.UnitTest/TestOrgApp.cs +++ b/OpenAuth.UnitTest/TestOrgApp.cs @@ -90,6 +90,32 @@ namespace OpenAuth.UnitTest Assert.IsTrue(id != 0); } + [TestMethod] + public void TestDelOrg() + { + int rootId = _app.AddOrg(new Org + { + Name = "即将被删除", + ParentId = 0 + }); + Assert.IsTrue(rootId != 0); + + int id = _app.AddOrg(new Org + { + Name = "即将被删除1", + ParentId = rootId + }); + id = _app.AddOrg(new Org + { + Name = "即将被删除2", + ParentId = id + }); + + _app.DelOrg(rootId); + } + + + [TestMethod] public void TestLoadOrg() {