Пример #1
0
        public byte[] GetNextSonHid(String mae)
        // Obtém hid do próximo novo filho
        {
            byte[] b = null;
            try
            {
                // obter hid da mãe
                if (string.IsNullOrEmpty(mae))
                {
                    b = Conversions.HierarchyId2Bytes(SqlHierarchyId.GetRoot());
                }
                else
                {
                    SqlHierarchyId maeHid = Conversions.Bytes2HierarchyId(repo.Find(new object[] { long.Parse(mae) }).hid);
                    long           m      = long.Parse(mae);

                    // byte[] LastChildHid = null; // = repo.Get.Where(p => p.Parent == m).Select(p => p.hid).Max();
                    SqlHierarchyId lastChHid = SqlHierarchyId.Null;

                    foreach (T r  in repo.Get)
                    {
                        SqlHierarchyId h = Conversions.Bytes2HierarchyId(r.hid);
                        if (h.GetAncestor(1) == maeHid)
                        {
                            if (lastChHid.IsNull || h > lastChHid)
                            {
                                lastChHid = h;
                            }
                        }
                    }

                    if (lastChHid == SqlHierarchyId.Null)
                    {
                        b = Conversions.HierarchyId2Bytes(maeHid.GetDescendant(SqlHierarchyId.Null, SqlHierarchyId.Null));
                    }
                    else
                    {
                        b = Conversions.HierarchyId2Bytes(maeHid.GetDescendant(lastChHid, SqlHierarchyId.Null));
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Erro em GetNextSonHid: " + ex.Message);
            }
            return(b);
        }
Пример #2
0
 public static SqlHierarchyId FirstChild(this SqlHierarchyId parent)
 {
     return(parent.GetDescendant(SqlHierarchyId.Null, SqlHierarchyId.Null));
 }
Пример #3
0
        private async Task <AccountHierarchy> AddToParent(AccountHierarchy entity, long?parentId, bool autoCommit = true)
        {
            if (!parentId.HasValue)
            {
                SqlHierarchyId parentLevel = SqlHierarchyId.GetRoot();
                entity.ParentId  = null;
                entity.UplinkId  = null;
                entity.LevelPath = parentLevel.ToString();
            }
            else
            {
                var parent = await _dbContext.AccountHierarchies.Where(x => x.Id == parentId)
                             .Include(x => x.Children)
                             .FirstOrDefaultAsync();

                SqlHierarchyId parentLevel = SqlHierarchyId.Parse(parent.LevelPath);


                var lastSibling = parent.Children.OrderByDescending(x => x.LevelPath).FirstOrDefault();

                string levelPath = null;

                if (entity.Leg == 2)
                {
                    if (lastSibling == null)
                    {
                        levelPath = parent.LevelPath + "2/";
                    }
                    else
                    {
                        SqlHierarchyId newLevel = parentLevel.GetDescendant(SqlHierarchyId.Parse(lastSibling.LevelPath), SqlHierarchyId.Null);
                        levelPath = newLevel.ToString();
                    }
                }
                else if (entity.Leg == 1)   // in any other case - put it on the left
                {
                    if (lastSibling == null)
                    {
                        levelPath = parent.LevelPath + "1/";
                    }
                    else
                    {
                        SqlHierarchyId newLevel = parentLevel.GetDescendant(SqlHierarchyId.Null, SqlHierarchyId.Parse(lastSibling.LevelPath));
                        levelPath = newLevel.ToString();
                    }
                }
                else
                {
                    throw new ArgumentOutOfRangeException("Leg", "Invalid value for leg: " + entity.Leg);
                }

                entity.LevelPath = levelPath;
                entity.ParentId  = parentId;
            }

            var result = (await _dbContext.AccountHierarchies.AddAsync(entity)).Entity;

            if (autoCommit)
            {
                await _dbContext.SaveChangesAsync();
            }

            return(result);
        }