把有些东西写到极致

This commit is contained in:
yubao
2017-12-08 23:31:52 +08:00
parent 9ae57bab41
commit 2c2e5cd179
9 changed files with 136 additions and 101 deletions

View File

@@ -1,4 +1,7 @@
using OpenAuth.Repository.Domain;
using System;
using System.Linq;
using OpenAuth.Repository.Core;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;
namespace OpenAuth.App
@@ -20,5 +23,54 @@ namespace OpenAuth.App
/// </summary>
/// <value>The repository.</value>
public IRepository<T> Repository { get; set; }
/// <summary>
/// 按id批量删除
/// </summary>
/// <param name="ids"></param>
public void Delete(string[] ids)
{
Repository.Delete(u => ids.Contains(u.Id));
}
/// <summary>
/// 如果一个类有层级结构(树状),则修改该节点时,要修改该节点的所有子节点
/// //修改对象的级联ID生成类似XXX.XXX.X.XX
/// </summary>
/// <typeparam name="U">U必须是一个继承TreeEntity的结构</typeparam>
/// <param name="entity"></param>
public void ChangeModuleCascade<U>(U entity) where U:TreeEntity
{
string cascadeId;
int currentCascadeId = 1; //当前结点的级联节点最后一位
var sameLevels = UnitWork.Find<U>(o => o.ParentId == entity.ParentId && o.Id != entity.Id);
foreach (var obj in sameLevels)
{
int objCascadeId = int.Parse(obj.CascadeId.TrimEnd('.').Split('.').Last());
if (currentCascadeId <= objCascadeId) currentCascadeId = objCascadeId + 1;
}
if (!string.IsNullOrEmpty(entity.ParentId))
{
var parentOrg = UnitWork.FindSingle<U>(o => o.Id == entity.ParentId);
if (parentOrg != null)
{
cascadeId = parentOrg.CascadeId + currentCascadeId + ".";
entity.ParentName = parentOrg.Name;
}
else
{
throw new Exception("未能找到该组织的父节点信息");
}
}
else
{
cascadeId = ".0." + currentCascadeId + ".";
entity.ParentName = "根节点";
}
entity.CascadeId = cascadeId;
}
}
}

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;
namespace OpenAuth.App
{
@@ -99,68 +98,25 @@ namespace OpenAuth.App
/// <summary>
/// 得到部门的所有子部门
/// <para>如果orgId为0,表示取得所有部门</para>
/// <para>如果orgId为,表示取得所有部门</para>
/// </summary>
public TableData LoadAllChildren(string orgId)
{
var query = GetSubOrgs(orgId);
string cascadeId = ".0.";
if (!string.IsNullOrEmpty(orgId))
{
var org = Repository.FindSingle(u => u.Id == orgId);
if (org == null)
throw new Exception("未能找到指定对象信息");
cascadeId = org.CascadeId;
}
var query = Repository.Find(u => u.CascadeId.Contains(cascadeId));
return new TableData
{
data = query.ToList(),
count = query.Count(),
};
}
public IEnumerable<Org> GetSubOrgs(string orgId)
{
string cascadeId = "0.";
if (!string.IsNullOrEmpty(orgId))
{
var org = UnitWork.FindSingle<Org>(u => u.Id == orgId);
if (org == null)
throw new Exception("未能找到指定对象信息");
cascadeId = org.CascadeId;
}
return UnitWork.Find<Org>(u => u.CascadeId.Contains(cascadeId));
}
#region
//修改对象的级联ID生成类似XXX.XXX.X.XX
private void ChangeModuleCascade(Org org)
{
string cascadeId;
int currentCascadeId = 1; //当前结点的级联节点最后一位
var sameLevels = UnitWork.Find<Org>(o => o.ParentId == org.ParentId && o.Id != org.Id);
foreach (var obj in sameLevels)
{
int objCascadeId = int.Parse(obj.CascadeId.TrimEnd('.').Split('.').Last());
if (currentCascadeId <= objCascadeId) currentCascadeId = objCascadeId + 1;
}
if (!string.IsNullOrEmpty(org.ParentId))
{
var parentOrg = UnitWork.FindSingle<Org>(o => o.Id == org.ParentId);
if (parentOrg != null)
{
cascadeId = parentOrg.CascadeId + currentCascadeId + ".";
org.ParentName = parentOrg.Name;
}
else
{
throw new Exception("未能找到该组织的父节点信息");
}
}
else
{
cascadeId = ".0." + currentCascadeId + ".";
org.ParentName = "根节点";
}
org.CascadeId = cascadeId;
}
#endregion
}
}

View File

@@ -12,13 +12,11 @@ namespace OpenAuth.App
{
public RevelanceManagerApp ReleManagerApp { get; set; }
public User Get(string account)
{
return Repository.FindSingle(u => u.Account == account);
}
/// <summary>
/// 加载一个部门及子部门全部用户
/// </summary>
@@ -86,11 +84,6 @@ namespace OpenAuth.App
return view;
}
public void Delete(string[] ids)
{
Repository.Delete(u => ids.Contains(u.Id));
}
public void AddOrUpdate(UserView view)
{
if (string.IsNullOrEmpty(view.OrganizationIds))

View File

@@ -70,9 +70,14 @@ namespace OpenAuth.Mvc.Controllers
return JsonHelper.Instance.Serialize(Result);
}
public string LoadChildren(string id)
/// <summary>
/// 加载机构的全部下级机构
/// </summary>
/// <param name="orgId">机构ID</param>
/// <returns></returns>
public string LoadChildren(string orgId)
{
return JsonHelper.Instance.Serialize(OrgApp.LoadAllChildren(id));
return JsonHelper.Instance.Serialize(OrgApp.LoadAllChildren(orgId));
}
/// <summary>

View File

@@ -0,0 +1,30 @@
using OpenAuth.Repository.Domain;
namespace OpenAuth.Repository.Core
{
/// <summary>
/// 树状结构实体
/// </summary>
public abstract class TreeEntity: Entity
{
/// <summary>
/// 父节点名称
/// </summary>
public string ParentId { get; set; }
/// <summary>
/// 父节点名称
/// </summary>
public string ParentName { get; set; }
/// <summary>
/// 节点语义ID
/// </summary>
public string CascadeId { get; set; }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
}
}

View File

@@ -10,51 +10,43 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenAuth.Repository.Core;
namespace OpenAuth.Repository.Domain
{
/// <summary>
/// 组织表
/// </summary>
public partial class Org : Entity
public partial class Org : TreeEntity
{
public Org()
{
this.CascadeId= string.Empty;
this.Name= string.Empty;
this.HotKey= string.Empty;
this.ParentName= string.Empty;
this.IconName= string.Empty;
this.Status= 0;
this.BizCode= string.Empty;
this.CustomCode= string.Empty;
this.CreateTime= DateTime.Now;
this.CreateId= 0;
this.SortNo= 0;
this.ParentId= string.Empty;
this.TypeName= string.Empty;
this.TypeId= string.Empty;
this.CascadeId = string.Empty;
this.Name = string.Empty;
this.HotKey = string.Empty;
this.ParentName = string.Empty;
this.IconName = string.Empty;
this.Status = 0;
this.BizCode = string.Empty;
this.CustomCode = string.Empty;
this.CreateTime = DateTime.Now;
this.CreateId = 0;
this.SortNo = 0;
this.ParentId = string.Empty;
this.TypeName = string.Empty;
this.TypeId = string.Empty;
}
/// <summary>
/// 节点语义ID
/// </summary>
public string CascadeId { get; set; }
/// <summary>
/// 组织名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 热键
/// </summary>
public string HotKey { get; set; }
/// <summary>
/// 父节点名称
/// </summary>
public string ParentName { get; set; }
/// <summary>
/// 是否叶子节点
/// </summary>
/// 是否叶子节点
/// </summary>
public bool IsLeaf { get; set; }
/// <summary>
/// 是否自动展开
@@ -88,13 +80,10 @@ namespace OpenAuth.Repository.Domain
/// 排序号
/// </summary>
public int SortNo { get; set; }
/// <summary>
/// 父节点流水号
/// </summary>
public string ParentId { get; set; }
/// <summary>
/// 分类名称
/// </summary>
/// 分类名称
/// </summary>
public string TypeName { get; set; }
/// <summary>
/// 分类ID

View File

@@ -55,10 +55,11 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Core\TreeEntity.cs" />
<Compile Include="Domain\Application.cs" />
<Compile Include="Domain\Category.cs" />
<Compile Include="Domain\CategoryType.cs" />
<Compile Include="Domain\Entity.cs" />
<Compile Include="Core\Entity.cs" />
<Compile Include="Domain\Module.cs" />
<Compile Include="Domain\ModuleElement.cs" />
<Compile Include="Domain\Org.cs" />

View File

@@ -1,4 +1,6 @@
using System;
using System.Diagnostics;
using Infrastructure;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenAuth.App;
using OpenAuth.Repository.Domain;
@@ -15,6 +17,13 @@ namespace OpenAuth.UnitTest
_app = AutofacExt.GetFromFac<OrgManagerApp>();
}
[TestMethod]
public void LoadChildren()
{
var data= _app.LoadAllChildren("fb086c51-4b41-4aa7-b54f-fb79632aaaf9");
Debug.WriteLine(JsonHelper.Instance.Serialize(data));
}
[TestMethod]
public void Add()