2016-10-14 17:03:18 +08:00
|
|
|
|
using Infrastructure;
|
|
|
|
|
using OpenAuth.App.ViewModel;
|
2015-12-15 23:04:20 +08:00
|
|
|
|
using OpenAuth.Domain;
|
|
|
|
|
using OpenAuth.Domain.Interface;
|
2016-10-14 17:03:18 +08:00
|
|
|
|
using System;
|
2015-12-15 23:04:20 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace OpenAuth.App
|
|
|
|
|
{
|
|
|
|
|
public class CategoryManagerApp
|
|
|
|
|
{
|
|
|
|
|
private ICategoryRepository _repository;
|
2016-10-14 17:03:18 +08:00
|
|
|
|
private IUnitWork _unitWork;
|
2015-12-15 23:04:20 +08:00
|
|
|
|
|
|
|
|
|
public CategoryManagerApp(ICategoryRepository repository,
|
2016-10-14 17:03:18 +08:00
|
|
|
|
IUnitWork unitWork)
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
|
|
|
|
_repository = repository;
|
2016-10-14 17:03:18 +08:00
|
|
|
|
_unitWork = unitWork;
|
2015-12-15 23:04:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 18:05:17 +08:00
|
|
|
|
public int GetCategoryCntInOrg(Guid orgId)
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
2016-09-02 18:05:17 +08:00
|
|
|
|
if (orgId == Guid.Empty)
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
|
|
|
|
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>
|
2016-10-14 17:03:18 +08:00
|
|
|
|
/// 加载一个分类及子分类全部Categorys
|
2015-12-15 23:04:20 +08:00
|
|
|
|
/// </summary>
|
2016-10-14 17:03:18 +08:00
|
|
|
|
public GridData Load(Guid parentId, int pageindex, int pagesize)
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
2016-10-14 17:03:18 +08:00
|
|
|
|
IQueryable<Category> categories;
|
2015-12-15 23:04:20 +08:00
|
|
|
|
int total = 0;
|
2016-09-02 18:05:17 +08:00
|
|
|
|
if (parentId == Guid.Empty)
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
2016-10-14 17:03:18 +08:00
|
|
|
|
categories = _unitWork.Find<Category>(pageindex, pagesize);
|
2015-12-15 23:04:20 +08:00
|
|
|
|
total = _repository.GetCount();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-05-26 20:10:22 +08:00
|
|
|
|
var ids = GetSubCategories(parentId);
|
2016-10-14 17:03:18 +08:00
|
|
|
|
categories = _unitWork.Find<Category>(pageindex, pagesize, "SortNo", u => ids.Contains(u.Id));
|
2016-05-26 20:10:22 +08:00
|
|
|
|
total = _repository.GetCategoryCntInOrgs(ids);
|
2015-12-15 23:04:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-14 17:03:18 +08:00
|
|
|
|
var query = from c in categories
|
|
|
|
|
join category in _unitWork.Find<Category>(null) on c.ParentId equals category.Id into temp
|
|
|
|
|
from category in temp.DefaultIfEmpty()
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
c.CascadeId,
|
|
|
|
|
c.Name,
|
|
|
|
|
c.ParentId,
|
|
|
|
|
ParentName = category.Name,
|
|
|
|
|
c.SortNo,
|
|
|
|
|
c.RootName,
|
|
|
|
|
c.RootKey,
|
|
|
|
|
c.Status,
|
|
|
|
|
c.Id
|
|
|
|
|
};
|
|
|
|
|
return new GridData()
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
2016-10-14 17:03:18 +08:00
|
|
|
|
records = total,
|
|
|
|
|
total = (int)Math.Ceiling((double)total/pagesize),
|
|
|
|
|
rows = query.ToList(),
|
|
|
|
|
page = pageindex
|
2015-12-15 23:04:20 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取当前组织的所有下级组织
|
|
|
|
|
/// </summary>
|
2016-09-02 18:05:17 +08:00
|
|
|
|
private Guid[] GetSubCategories(Guid orgId)
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
2016-10-14 17:03:18 +08:00
|
|
|
|
var category = Find(orgId);
|
2016-04-15 17:49:21 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2016-09-02 18:05:17 +08:00
|
|
|
|
public Category Find(Guid id)
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
|
|
|
|
var category = _repository.FindSingle(u => u.Id == id);
|
|
|
|
|
if (category == null) return new Category();
|
|
|
|
|
|
|
|
|
|
return category;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-14 17:03:18 +08:00
|
|
|
|
public void Delete(Guid[] ids)
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
2016-10-14 17:03:18 +08:00
|
|
|
|
_repository.Delete(u =>ids.Contains(u.Id));
|
2015-12-15 23:04:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddOrUpdate(Category model)
|
|
|
|
|
{
|
2016-10-14 17:03:18 +08:00
|
|
|
|
Category category = new Category();
|
2015-12-16 22:52:23 +08:00
|
|
|
|
model.CopyTo(category);
|
2016-04-15 17:49:21 +08:00
|
|
|
|
ChangeModuleCascade(category);
|
|
|
|
|
|
2016-09-02 18:05:17 +08:00
|
|
|
|
if (category.Id == Guid.Empty)
|
2015-12-15 23:04:20 +08:00
|
|
|
|
{
|
|
|
|
|
_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)
|
|
|
|
|
{
|
2017-04-15 23:17:02 +08:00
|
|
|
|
int objCascadeId = int.Parse(obj.CascadeId.TrimEnd('.').Split('.').Last());
|
2015-12-16 22:52:23 +08:00
|
|
|
|
if (currentCascadeId <= objCascadeId) currentCascadeId = objCascadeId + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-14 17:03:18 +08:00
|
|
|
|
if (org.ParentId != null && org.ParentId != Guid.Empty)
|
2015-12-16 22:52:23 +08:00
|
|
|
|
{
|
|
|
|
|
var parentOrg = _repository.FindSingle(o => o.Id == org.ParentId);
|
|
|
|
|
if (parentOrg != null)
|
|
|
|
|
{
|
2017-04-15 23:17:02 +08:00
|
|
|
|
cascadeId = parentOrg.CascadeId + currentCascadeId +".";
|
2015-12-16 22:52:23 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("未能找到该组织的父节点信息");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-04-15 23:17:02 +08:00
|
|
|
|
cascadeId = "0." + currentCascadeId +".";
|
2015-12-16 22:52:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
org.CascadeId = cascadeId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion 私有方法
|
2015-12-15 23:04:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|