public virtual async Task MoveAfterAsync(TPrimaryKey id, TPrimaryKey afterId, Action <TTree> childrenAction = null, CancellationToken cancellationToken = default) { var sourceTree = await _generalTreeRepository.GetAsync(id, cancellationToken : cancellationToken); var destTree = await _generalTreeRepository.GetAsync(afterId, cancellationToken : cancellationToken); var parent = destTree.ParentId.HasValue ? await _generalTreeRepository.GetAsync(destTree.ParentId.Value, cancellationToken : cancellationToken) : null; var oldChildren = await _generalTreeRepository.GetAllChildrenAsync(id, cancellationToken); var oldCode = sourceTree.Code; var oldFullName = sourceTree.FullName; sourceTree.Code = destTree.Code; sourceTree.Level = sourceTree.Code.Split('.').Length; sourceTree.ParentId = destTree.ParentId; sourceTree.FullName = parent != null ? parent.Name + _generalTreeOptions.Hyphen + sourceTree.Name : sourceTree.Name; await CheckSameName(sourceTree); var children = await _generalTreeRepository.GetChildrenAsync(parent?.Id, cancellationToken); var allNext = children.SkipWhile(x => x.Code != destTree.Code); await _generalTreeRepository.UpdateAsync(sourceTree, cancellationToken : cancellationToken); foreach (var child in oldChildren) { child.Code = _generalTreeCodeGenerator.MergeCode(sourceTree.Code, _generalTreeCodeGenerator.RemoveParentCode(child.Code, oldCode)); child.FullName = _generalTreeCodeGenerator.MergeFullName( sourceTree.FullName, _generalTreeCodeGenerator.RemoveParentCode(child.FullName, oldFullName), _generalTreeOptions.Hyphen); child.Level = child.Code.Split('.').Length; childrenAction?.Invoke(child); await _generalTreeRepository.UpdateAsync(child, cancellationToken : cancellationToken); } foreach (var child in allNext) { child.Code = _generalTreeCodeGenerator.GetNextCode(child.Code); childrenAction?.Invoke(child); await _generalTreeRepository.UpdateAsync(child, cancellationToken : cancellationToken); } }
public async Task MoveAfterAsync() { //Act var beijing = await CreateRegion("beijing"); await CreateRegion("dongcheng", beijing.Id); var xicheng = await CreateRegion("xicheng", beijing.Id); var hebei = await CreateRegion("hebei"); await CreateRegion("shijiazhuang", hebei.Id); var chengde = await CreateRegion("chengde", hebei.Id); await CreateRegion("shaungqiao", chengde.Id); await CreateRegion("shaungluan", chengde.Id); await _generalTreeManager.MoveAfterAsync(chengde.Id, xicheng.Id); //Assert var cd = await GetRegion(chengde.Name); cd.ShouldNotBeNull(); cd.FullName.ShouldBe(beijing.FullName + "-" + chengde.Name); cd.ParentId.ShouldBe(beijing.Id); cd.Level.ShouldBe(beijing.Level + 1); cd.Code.ShouldBe(xicheng.Code); xicheng = await GetRegion("xicheng"); xicheng.ShouldNotBeNull(); xicheng.FullName.ShouldBe(beijing.FullName + "-" + xicheng.Name); xicheng.ParentId.ShouldBe(beijing.Id); xicheng.Level.ShouldBe(beijing.Level + 1); xicheng.Code.ShouldBe(_generalTreeCodeGenerator.GetNextCode(cd.Code)); var shaungqiao = await GetRegion("shaungqiao"); shaungqiao.ShouldNotBeNull(); shaungqiao.FullName.ShouldBe(cd.FullName + "-" + shaungqiao.Name); shaungqiao.ParentId.ShouldBe(cd.Id); shaungqiao.Level.ShouldBe(cd.Level + 1); shaungqiao.Code.ShouldBe(_generalTreeCodeGenerator.MergeCode(cd.Code, _generalTreeCodeGenerator.GetLastCode(shaungqiao.Code))); }