private void TraverseTree(TTree parent, ICollection <TTree> children) { if (children == null || !children.Any()) { return; } children.ForEach((tree, index) => { //CheckSameName if (children.Count(x => x.Name == tree.Name) > 1) { throw new UserFriendlyException(_generalTreeConfiguration.ExceptionMessageFactory.Invoke(tree)); } tree.Code = index == 0 ? GeneralTreeCodeGenerate.MergeCode(parent.Code, GeneralTreeCodeGenerate.CreateCode(1)) : GeneralTreeCodeGenerate.GetNextCode(children.ElementAt(index - 1).Code); tree.Level = tree.Code.Split('.').Length; tree.FullName = parent.FullName + _generalTreeConfiguration.Hyphen + tree.Name; }); children.ForEach(tree => { TraverseTree(tree, tree.Children); }); }
/// <summary> /// Get next child code /// </summary> /// <param name="parentId"></param> /// <returns></returns> private async Task <string> GetNextChildCodeAsync(TPrimaryKey?parentId) { var lastChild = _generalTreeRepository.GetAll() .Where(Equal(parentId, "ParentId")) .OrderByDescending(x => x.Code) .FirstOrDefault(); if (lastChild != null) { //Get the next code return(GeneralTreeCodeGenerate.GetNextCode(lastChild.Code)); } //Generate a code var parentCode = parentId != null ? await GetCodeAsync(parentId.Value) : null; return(GeneralTreeCodeGenerate.MergeCode(parentCode, GeneralTreeCodeGenerate.CreateCode(1))); }