2016-01-05 17:14:10 +08:00
|
|
|
|
using System;
|
2015-12-15 23:04:20 +08:00
|
|
|
|
using OpenAuth.Domain;
|
|
|
|
|
using OpenAuth.Domain.Interface;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2015-12-16 22:52:23 +08:00
|
|
|
|
using Infrastructure;
|
2015-12-15 23:04:20 +08:00
|
|
|
|
|
|
|
|
|
namespace OpenAuth.App
|
|
|
|
|
{
|
|
|
|
|
public class CategoryManagerApp
|
|
|
|
|
{
|
|
|
|
|
private ICategoryRepository _repository;
|
|
|
|
|
private IOrgRepository _orgRepository;
|
|
|
|
|
|
|
|
|
|
public CategoryManagerApp(ICategoryRepository repository,
|
|
|
|
|
IOrgRepository orgRepository)
|
|
|
|
|
{
|
|
|
|
|
_repository = repository;
|
|
|
|
|
_orgRepository = orgRepository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetCategoryCntInOrg(int orgId)
|
|
|
|
|
{
|
|
|
|
|
if (orgId == 0)
|
|
|
|
|
{
|
|
|
|
|
return _repository.Find(null).Count();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-04-15 17:49:21 +08:00
|
|
|
|
return _repository.GetCategoryCntInOrgs(GetSubCategories(orgId));
|
2015-12-15 23:04:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-16 22:52:23 +08:00
|
|
|
|
public List<Category> LoadAll()
|
|
|
|
|
{
|
|
|
|
|
return _repository.Find(null).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-15 23:04:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 加载一个部门及子部门全部Categorys
|
|
|
|
|
/// </summary>
|
2016-04-15 17:49:21 +08:00
|
|
|
|
public dynamic Load(int parentId, int pageindex, int pagesize)
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
|
|
|
|
IEnumerable<Category> Categorys;
|
|
|
|
|
int total = 0;
|
2016-04-15 17:49:21 +08:00
|
|
|
|
if (parentId == 0)
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
|
|
|
|
Categorys = _repository.LoadCategorys(pageindex, pagesize);
|
|
|
|
|
total = _repository.GetCount();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-05-26 20:10:22 +08:00
|
|
|
|
var ids = GetSubCategories(parentId);
|
|
|
|
|
Categorys = _repository.LoadInOrgs(pageindex, pagesize, ids);
|
|
|
|
|
total = _repository.GetCategoryCntInOrgs(ids);
|
2015-12-15 23:04:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-16 22:52:23 +08:00
|
|
|
|
return new
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
|
|
|
|
total = total,
|
|
|
|
|
list = Categorys,
|
|
|
|
|
pageCurrent = pageindex
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取当前组织的所有下级组织
|
|
|
|
|
/// </summary>
|
2016-04-15 17:49:21 +08:00
|
|
|
|
private int[] GetSubCategories(int orgId)
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
2016-04-15 17:49:21 +08:00
|
|
|
|
var category = Find(orgId);
|
|
|
|
|
var categories = _repository.Find(u => u.CascadeId.Contains(category.CascadeId)).Select(u => u.Id).ToArray();
|
|
|
|
|
return categories;
|
2015-12-15 23:04:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Category Find(int id)
|
|
|
|
|
{
|
|
|
|
|
var category = _repository.FindSingle(u => u.Id == id);
|
|
|
|
|
if (category == null) return new Category();
|
|
|
|
|
|
|
|
|
|
return category;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Delete(int id)
|
|
|
|
|
{
|
|
|
|
|
_repository.Delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddOrUpdate(Category model)
|
|
|
|
|
{
|
2015-12-16 22:52:23 +08:00
|
|
|
|
Category category = new Category();
|
|
|
|
|
model.CopyTo(category);
|
2016-04-15 17:49:21 +08:00
|
|
|
|
ChangeModuleCascade(category);
|
|
|
|
|
|
2015-12-15 23:04:20 +08:00
|
|
|
|
if (category.Id == 0)
|
|
|
|
|
{
|
|
|
|
|
_repository.Add(category);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_repository.Update(category);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-16 22:52:23 +08:00
|
|
|
|
#region 私有方法
|
|
|
|
|
|
|
|
|
|
//修改对象的级联ID,生成类似XXX.XXX.X.XX
|
|
|
|
|
private void ChangeModuleCascade(Category org)
|
|
|
|
|
{
|
|
|
|
|
string cascadeId;
|
|
|
|
|
int currentCascadeId = 1; //当前结点的级联节点最后一位
|
|
|
|
|
var sameLevels = _repository.Find(o => o.ParentId == org.ParentId && o.Id != org.Id);
|
|
|
|
|
foreach (var obj in sameLevels)
|
|
|
|
|
{
|
|
|
|
|
int objCascadeId = int.Parse(obj.CascadeId.Split('.').Last());
|
|
|
|
|
if (currentCascadeId <= objCascadeId) currentCascadeId = objCascadeId + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (org.ParentId != 0)
|
|
|
|
|
{
|
|
|
|
|
var parentOrg = _repository.FindSingle(o => o.Id == org.ParentId);
|
|
|
|
|
if (parentOrg != null)
|
|
|
|
|
{
|
|
|
|
|
cascadeId = parentOrg.CascadeId + "." + currentCascadeId;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("未能找到该组织的父节点信息");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cascadeId = "0." + currentCascadeId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
org.CascadeId = cascadeId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion 私有方法
|
2015-12-15 23:04:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|