//public virtual async Task<TEntity> GetEntityWithOffspringAsync(long id) //{ // var parent = await repository.GetAsync(id); // var list = await GetFlattenOffspringAsync(parent.Code); // return parent;//ef自动建立父子关系, //} /// <summary> /// 移动节点 /// </summary> /// <param name="sourceId"></param> /// <param name="targetId"></param> /// <param name="moveType"></param> /// <returns></returns> public virtual async Task <TEntity> MoveAsync(long sourceId, long?targetId, GeneralTreeMoveType moveType) { if (targetId == 0) { targetId = null; } var source = await repository.GetAsync(sourceId); var target = targetId.HasValue ? await repository.GetAsync(targetId.Value) : null; return(await MoveAsync(source, target, moveType)); }
public virtual async Task <TEntity> MoveAsync(TEntity source, TEntity target, GeneralTreeMoveType moveType) { TEntity targetParent = null; IList <TEntity> targetChildren = null; int targetIndex = 0; //if (target == null) //{ // //if (moveType != GeneralTreeMoveType.Append) // // throw new UserFriendlyException(L("不能移动顶级节点的前或后面")); // var all = await AsyncQueryableExecuter.ToListAsync(repository.GetAll().OrderByDescending(c => c.Code)); // targetChildren = all.Where(c => c.ParentId == null).ToList(); // targetIndex = targetChildren.Count; //} //else if (moveType != GeneralTreeMoveType.Append) { var temp1 = await GetBrotherWithOffspringAsync(target); targetParent = temp1.Item1; targetChildren = temp1.Item2; targetIndex = targetChildren.IndexOf(target) + (moveType == GeneralTreeMoveType.Front ? 0 : 1); } else { targetParent = target; if (target != null) { targetChildren = await GetFlattenOffspringAsync(target.Code); targetChildren = targetChildren.Where(c => c.ParentId == target.Id).ToList(); } else { targetChildren = await GetFlattenOffspringAsync(); targetChildren = targetChildren.Where(c => c.ParentId == null).ToList(); } targetIndex = targetChildren.Count; } return(await MoveAsync(source, targetParent, targetChildren, targetIndex)); }