mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-11-10 03:14:45 +08:00
把有些东西写到极致
This commit is contained in:
@@ -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;
|
using OpenAuth.Repository.Interface;
|
||||||
|
|
||||||
namespace OpenAuth.App
|
namespace OpenAuth.App
|
||||||
@@ -20,5 +23,54 @@ namespace OpenAuth.App
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The repository.</value>
|
/// <value>The repository.</value>
|
||||||
public IRepository<T> Repository { get; set; }
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using OpenAuth.App.Response;
|
using OpenAuth.App.Response;
|
||||||
using OpenAuth.Repository.Domain;
|
using OpenAuth.Repository.Domain;
|
||||||
using OpenAuth.Repository.Interface;
|
|
||||||
|
|
||||||
namespace OpenAuth.App
|
namespace OpenAuth.App
|
||||||
{
|
{
|
||||||
@@ -99,68 +98,25 @@ namespace OpenAuth.App
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 得到部门的所有子部门
|
/// 得到部门的所有子部门
|
||||||
/// <para>如果orgId为0,表示取得所有部门</para>
|
/// <para>如果orgId为空,表示取得所有部门</para>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public TableData LoadAllChildren(string orgId)
|
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
|
return new TableData
|
||||||
{
|
{
|
||||||
data = query.ToList(),
|
data = query.ToList(),
|
||||||
count = query.Count(),
|
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 私有方法
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,13 +12,11 @@ namespace OpenAuth.App
|
|||||||
{
|
{
|
||||||
public RevelanceManagerApp ReleManagerApp { get; set; }
|
public RevelanceManagerApp ReleManagerApp { get; set; }
|
||||||
|
|
||||||
|
|
||||||
public User Get(string account)
|
public User Get(string account)
|
||||||
{
|
{
|
||||||
return Repository.FindSingle(u => u.Account == account);
|
return Repository.FindSingle(u => u.Account == account);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 加载一个部门及子部门全部用户
|
/// 加载一个部门及子部门全部用户
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -86,11 +84,6 @@ namespace OpenAuth.App
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Delete(string[] ids)
|
|
||||||
{
|
|
||||||
Repository.Delete(u => ids.Contains(u.Id));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddOrUpdate(UserView view)
|
public void AddOrUpdate(UserView view)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(view.OrganizationIds))
|
if (string.IsNullOrEmpty(view.OrganizationIds))
|
||||||
|
|||||||
@@ -70,9 +70,14 @@ namespace OpenAuth.Mvc.Controllers
|
|||||||
return JsonHelper.Instance.Serialize(Result);
|
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>
|
/// <summary>
|
||||||
|
|||||||
30
OpenAuth.Repository/Core/TreeEntity.cs
Normal file
30
OpenAuth.Repository/Core/TreeEntity.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -10,13 +10,14 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using OpenAuth.Repository.Core;
|
||||||
|
|
||||||
namespace OpenAuth.Repository.Domain
|
namespace OpenAuth.Repository.Domain
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 组织表
|
/// 组织表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class Org : Entity
|
public partial class Org : TreeEntity
|
||||||
{
|
{
|
||||||
public Org()
|
public Org()
|
||||||
{
|
{
|
||||||
@@ -36,22 +37,13 @@ namespace OpenAuth.Repository.Domain
|
|||||||
this.TypeId = string.Empty;
|
this.TypeId = string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 节点语义ID
|
|
||||||
/// </summary>
|
|
||||||
public string CascadeId { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 组织名称
|
|
||||||
/// </summary>
|
|
||||||
public string Name { get; set; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 热键
|
/// 热键
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string HotKey { get; set; }
|
public string HotKey { get; set; }
|
||||||
/// <summary>
|
|
||||||
/// 父节点名称
|
|
||||||
/// </summary>
|
|
||||||
public string ParentName { get; set; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否叶子节点
|
/// 是否叶子节点
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -88,10 +80,7 @@ namespace OpenAuth.Repository.Domain
|
|||||||
/// 排序号
|
/// 排序号
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int SortNo { get; set; }
|
public int SortNo { get; set; }
|
||||||
/// <summary>
|
|
||||||
/// 父节点流水号
|
|
||||||
/// </summary>
|
|
||||||
public string ParentId { get; set; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 分类名称
|
/// 分类名称
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -55,10 +55,11 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="Core\TreeEntity.cs" />
|
||||||
<Compile Include="Domain\Application.cs" />
|
<Compile Include="Domain\Application.cs" />
|
||||||
<Compile Include="Domain\Category.cs" />
|
<Compile Include="Domain\Category.cs" />
|
||||||
<Compile Include="Domain\CategoryType.cs" />
|
<Compile Include="Domain\CategoryType.cs" />
|
||||||
<Compile Include="Domain\Entity.cs" />
|
<Compile Include="Core\Entity.cs" />
|
||||||
<Compile Include="Domain\Module.cs" />
|
<Compile Include="Domain\Module.cs" />
|
||||||
<Compile Include="Domain\ModuleElement.cs" />
|
<Compile Include="Domain\ModuleElement.cs" />
|
||||||
<Compile Include="Domain\Org.cs" />
|
<Compile Include="Domain\Org.cs" />
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using Infrastructure;
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
using OpenAuth.App;
|
using OpenAuth.App;
|
||||||
using OpenAuth.Repository.Domain;
|
using OpenAuth.Repository.Domain;
|
||||||
@@ -15,6 +17,13 @@ namespace OpenAuth.UnitTest
|
|||||||
_app = AutofacExt.GetFromFac<OrgManagerApp>();
|
_app = AutofacExt.GetFromFac<OrgManagerApp>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void LoadChildren()
|
||||||
|
{
|
||||||
|
var data= _app.LoadAllChildren("fb086c51-4b41-4aa7-b54f-fb79632aaaf9");
|
||||||
|
Debug.WriteLine(JsonHelper.Instance.Serialize(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Add()
|
public void Add()
|
||||||
|
|||||||
Reference in New Issue
Block a user