using System.Linq;
using Microsoft.EntityFrameworkCore;
using OpenAuth.App.Interface;
using OpenAuth.Repository.Core;
using OpenAuth.Repository.Interface;
namespace OpenAuth.App
{
///
/// 树状结构处理
///
///
/// ///
public class BaseTreeApp :BaseStringApp where T : TreeEntity where TDbContext :DbContext
{
public BaseTreeApp(IUnitWork unitWork, IRepository repository, IAuth auth)
: base(unitWork, repository,auth)
{
}
///
/// 更新树状结构实体
///
///
///
public void UpdateTreeObj(U obj) where U : TreeEntity
{
CaculateCascade(obj);
//获取旧的的CascadeId
var cascadeId = Repository.FirstOrDefault(o => o.Id == obj.Id).CascadeId;
//根据CascadeId查询子部门
var objs = Repository.Find(u => u.CascadeId.Contains(cascadeId) && u.Id != obj.Id)
.OrderBy(u => u.CascadeId).ToList();
//更新操作
UnitWork.Update(obj);
//更新子模块的CascadeId
foreach (var a in objs)
{
a.CascadeId = a.CascadeId.Replace(cascadeId, obj.CascadeId);
if (a.ParentId == obj.Id)
{
a.ParentName = obj.Name;
}
UnitWork.Update(a);
}
UnitWork.Save();
}
}
}