2015-11-23 18:15:29 +08:00
|
|
|
|
using OpenAuth.App.ViewModel;
|
2015-11-21 23:56:39 +08:00
|
|
|
|
using OpenAuth.Domain;
|
|
|
|
|
using OpenAuth.Domain.Interface;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2015-11-23 18:15:29 +08:00
|
|
|
|
using Infrastructure;
|
2015-11-21 23:56:39 +08:00
|
|
|
|
|
|
|
|
|
namespace OpenAuth.App
|
|
|
|
|
{
|
|
|
|
|
public class ModuleManagerApp
|
|
|
|
|
{
|
|
|
|
|
private IModuleRepository _repository;
|
|
|
|
|
|
2015-11-22 15:55:26 +08:00
|
|
|
|
public ModuleManagerApp(IModuleRepository repository)
|
2015-11-21 23:56:39 +08:00
|
|
|
|
{
|
|
|
|
|
_repository = repository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2015-11-23 18:15:29 +08:00
|
|
|
|
/// 加载一个节点下面的所有
|
2015-11-21 23:56:39 +08:00
|
|
|
|
/// </summary>
|
2015-11-23 16:41:52 +08:00
|
|
|
|
public dynamic Load(int parentId, int pageindex, int pagesize)
|
2015-11-21 23:56:39 +08:00
|
|
|
|
{
|
|
|
|
|
IEnumerable<Module> Modules;
|
|
|
|
|
int total = 0;
|
2015-11-23 16:41:52 +08:00
|
|
|
|
if (parentId == 0)
|
2015-11-21 23:56:39 +08:00
|
|
|
|
{
|
|
|
|
|
Modules = _repository.LoadModules(pageindex, pagesize);
|
|
|
|
|
total = _repository.GetCount();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-11-23 16:41:52 +08:00
|
|
|
|
Modules = _repository.LoadInOrgs(pageindex, pagesize, GetSubOrgIds(parentId));
|
|
|
|
|
total = _repository.GetModuleCntInOrgs(parentId);
|
2015-11-21 23:56:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-23 18:15:29 +08:00
|
|
|
|
return new
|
2015-11-21 23:56:39 +08:00
|
|
|
|
{
|
|
|
|
|
total = total,
|
|
|
|
|
list = Modules,
|
|
|
|
|
pageCurrent = pageindex
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-22 22:26:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 为树型结构提供数据
|
|
|
|
|
/// </summary>
|
2015-11-22 15:55:26 +08:00
|
|
|
|
public List<Module> LoadForTree(bool bAll)
|
2015-11-21 23:56:39 +08:00
|
|
|
|
{
|
2015-11-22 15:55:26 +08:00
|
|
|
|
if (bAll)
|
|
|
|
|
return _repository.Find(null).ToList();
|
|
|
|
|
return _repository.Find(u => u.ParentId == 0).ToList();
|
2015-11-21 23:56:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-22 22:26:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 以组合的方式显示所有的模块信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="parentId">The parent unique identifier.</param>
|
|
|
|
|
/// <returns>List{ModuleView}.</returns>
|
2015-11-22 21:32:38 +08:00
|
|
|
|
public List<ModuleView> LoadByParent(int parentId)
|
|
|
|
|
{
|
|
|
|
|
var modules = new List<ModuleView>();
|
|
|
|
|
var roots = _repository.Find(u => u.ParentId == parentId);
|
|
|
|
|
foreach (var module in roots)
|
|
|
|
|
{
|
|
|
|
|
ModuleView mv = module;
|
|
|
|
|
mv.Childern = LoadByParent(module.Id);
|
|
|
|
|
modules.Add(mv);
|
|
|
|
|
}
|
|
|
|
|
return modules;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-21 23:56:39 +08:00
|
|
|
|
public Module Find(int id)
|
|
|
|
|
{
|
|
|
|
|
var module = _repository.FindSingle(u => u.Id == id);
|
|
|
|
|
if (module == null) return new Module();
|
|
|
|
|
|
|
|
|
|
return module;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Delete(int id)
|
|
|
|
|
{
|
2015-11-22 15:55:26 +08:00
|
|
|
|
var del = _repository.FindSingle(u => u.Id == id);
|
|
|
|
|
if (del == null) return;
|
|
|
|
|
|
|
|
|
|
_repository.Delete(u => u.CascadeId.Contains(del.CascadeId));
|
2015-11-21 23:56:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-23 18:15:29 +08:00
|
|
|
|
public void AddOrUpdate(Module vm)
|
2015-11-21 23:56:39 +08:00
|
|
|
|
{
|
2015-11-23 18:15:29 +08:00
|
|
|
|
Module model = new Module();
|
|
|
|
|
vm.CopyTo(model); //copy一次,防止成员为null的情况
|
|
|
|
|
ChangeModuleCascade(model);
|
|
|
|
|
if (model.Id == 0)
|
2015-11-21 23:56:39 +08:00
|
|
|
|
{
|
2015-11-23 18:15:29 +08:00
|
|
|
|
_repository.Add(model);
|
2015-11-21 23:56:39 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-11-23 18:15:29 +08:00
|
|
|
|
_repository.Update(model);
|
2015-11-21 23:56:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-22 15:55:26 +08:00
|
|
|
|
#region 私有方法
|
|
|
|
|
|
|
|
|
|
//根据同一级中最大的语义ID
|
|
|
|
|
|
2015-11-24 15:49:00 +08:00
|
|
|
|
private int[] GetSubOrgIds(int parentId)
|
2015-11-22 15:55:26 +08:00
|
|
|
|
{
|
2015-11-24 15:49:00 +08:00
|
|
|
|
var parent = _repository.FindSingle(u => u.Id == parentId);
|
2015-11-23 16:41:52 +08:00
|
|
|
|
var orgs = _repository.Find(u => u.CascadeId.Contains(parent.CascadeId)).Select(u => u.Id).ToArray();
|
2015-11-22 15:55:26 +08:00
|
|
|
|
return orgs;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-23 16:41:52 +08:00
|
|
|
|
//修改对象的级联ID
|
|
|
|
|
private void ChangeModuleCascade(Module module)
|
|
|
|
|
{
|
|
|
|
|
string cascadeId;
|
2015-11-24 15:49:00 +08:00
|
|
|
|
int currentCascadeId = 1; //当前结点的级联节点最后一位
|
|
|
|
|
var sameLevels = _repository.Find(o => o.ParentId == module.ParentId && o.Id != module.Id);
|
|
|
|
|
foreach (var obj in sameLevels)
|
|
|
|
|
{
|
|
|
|
|
int objCascadeId = int.Parse(obj.CascadeId.Split('.').Last());
|
|
|
|
|
if (currentCascadeId <= objCascadeId) currentCascadeId = objCascadeId + 1;
|
|
|
|
|
}
|
2015-11-23 16:41:52 +08:00
|
|
|
|
|
|
|
|
|
if (module.ParentId != 0)
|
|
|
|
|
{
|
|
|
|
|
var parentOrg = _repository.FindSingle(o => o.Id == module.ParentId);
|
|
|
|
|
if (parentOrg != null)
|
|
|
|
|
{
|
|
|
|
|
cascadeId = parentOrg.CascadeId + "." + currentCascadeId;
|
|
|
|
|
module.ParentName = parentOrg.Name;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("未能找到该组织的父节点信息");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cascadeId = "0." + currentCascadeId;
|
2015-11-23 18:15:29 +08:00
|
|
|
|
module.ParentName = "根节点";
|
2015-11-23 16:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.CascadeId = cascadeId;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-22 15:55:26 +08:00
|
|
|
|
#endregion 私有方法
|
2015-11-21 23:56:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|