Esempio n. 1
0
        public AccountDto BuildHierarchyTreeRecursive(AccountHierarchy rootNode)
        {
            var dtos = _dbContext.AccountHierarchies
                       .OrderBy(a => a.LevelPath.Length).ThenBy(a => a.LevelPath)
                       .Where(a => a.LevelPath.StartsWith(rootNode.LevelPath))
                       //.Take(10000)   // temp for testing
                       .Select(AccountDto.MapToFromHierarchyEntity).ToList();

            System.Console.WriteLine($"Building for {rootNode.Name}, with {dtos.Count} entities");

            var bottom = dtos.Last();

            System.Console.WriteLine($"Bottom is {bottom.Id} with level {bottom.Level}, hierarchy depth {bottom.Depth}");

            int       counter = 0;
            Stopwatch sw      = Stopwatch.StartNew();

            var levels = bottom.Level.Split("/", StringSplitOptions.RemoveEmptyEntries);
            // System.Console.WriteLine(levels.Length);

            AccountDto root = AccountDto.MapToFromHierarchyEntity(rootNode);

            root.Nodes = SetChildNodesOnDepthRecursive(root.Depth + 1, bottom.Depth, root, dtos, ref counter);

            sw.Stop();
            System.Console.WriteLine($"Hierarchy loop - Total iterations: {counter}. Took {sw.ElapsedMilliseconds} ms");

            return(root);
        }
Esempio n. 2
0
        public void CalculateBonuses(AccountHierarchy rootNode)
        {
            // var entities = new List<AccountHierarchy>()
            // {
            //     new AccountHierarchy(){ Id = 1, Name = "A", PlacementPreference = 3, ParentId = null, UplinkId = null, LevelPath = "/" },
            //     new AccountHierarchy(){ Id = 2, Name = "B", PlacementPreference = 2, ParentId = 1, UplinkId = 1, LevelPath = "/1/" },
            //     new AccountHierarchy(){ Id = 3, Name = "C", PlacementPreference = 3, ParentId = 1, UplinkId = 1, LevelPath = "/2/" },
            //     new AccountHierarchy(){ Id = 4, Name = "D", PlacementPreference = 3, ParentId = 2, UplinkId = 1, LevelPath = "/1/1/" },
            //     new AccountHierarchy(){ Id = 5, Name = "H", PlacementPreference = 1, ParentId = 3, UplinkId = 1, LevelPath = "/2/1/" },
            //     new AccountHierarchy(){ Id = 6, Name = "K", PlacementPreference = 3, ParentId = 3, UplinkId = 1, LevelPath = "/2/2/" },
            //     new AccountHierarchy(){ Id = 7, Name = "F", PlacementPreference = 2, ParentId = 2, UplinkId = 1, LevelPath = "/1/2/" },
            //     new AccountHierarchy(){ Id = 8, Name = "G", PlacementPreference = 3, ParentId = 4, UplinkId = 1, LevelPath = "/1/1/1/" },
            //     new AccountHierarchy(){ Id = 9, Name = "V", PlacementPreference = 3, ParentId = 4, UplinkId = 1, LevelPath = "/1/1/2/" },
            //     new AccountHierarchy(){ Id = 10, Name = "L", PlacementPreference = 1, ParentId = 6, UplinkId = 1, LevelPath = "/2/2/1/" },
            //     new AccountHierarchy(){ Id = 11, Name = "O", PlacementPreference = 3, ParentId = 10, UplinkId = 1, LevelPath = "/2/2/1/1/" }
            // };
            Stopwatch sw       = Stopwatch.StartNew();
            var       entities = _dbContext.AccountHierarchies
                                 .OrderBy(a => a.LevelPath.Length).ThenBy(a => a.LevelPath)
                                 .Where(a => a.LevelPath.StartsWith(rootNode.LevelPath));

            var dtos = entities.Select(AccountDto.MapToFromHierarchyEntity).ToList();

            System.Console.WriteLine($"Start processing {dtos.Count} items.");

            Dictionary <string, long> pathBonus = new Dictionary <string, long>();

            for (int i = dtos.Count - 1; i >= 0; i--)
            {
                var  entity = dtos[i];
                long bonus  = entity.Id;
                if (pathBonus.ContainsKey(entity.Level))
                {
                    bonus += pathBonus[entity.Level];
                }

                var    parentPathNode = SqlHierarchyId.Parse(entity.Level).GetAncestor(1);
                string parentPath     = parentPathNode.ToString();

                if (!pathBonus.ContainsKey(parentPath))
                {
                    pathBonus[parentPath] = 0;
                }

                pathBonus[parentPath] += bonus;

                if (i % 1000 == 0)
                {
                    System.Console.WriteLine($"Processed next 1000 of {i} remaining");
                }

                //System.Console.WriteLine($"Bonus for {entity.Text}: {bonus}");
            }

            System.Console.WriteLine($"Finished in {sw.ElapsedMilliseconds} ms");
        }
Esempio n. 3
0
 public static AccountDto MapToFromHierarchyEntity(AccountHierarchy acc)
 {
     return(new AccountDto()
     {
         Id = acc.Id,
         Text = acc.Name,
         PlacementPreference = acc.PlacementPreference,
         Leg = acc.Leg,
         ParentId = acc.ParentId,
         UplinkId = acc.UplinkId,
         Selectable = false,
         Level = acc.LevelPath,
         Depth = acc.LevelPath != "/" ? (int)SqlHierarchyId.Parse(acc.LevelPath).GetLevel() : 0,
         Nodes = new List <AccountDto>()
     });
 }
Esempio n. 4
0
        public async Task <IActionResult> CreateHierarchy(int sponsorId, string name, int placement)
        {
            var newAccountHierarchy = new AccountHierarchy
            {
                Name = name,
                PlacementPreference = placement,
                UplinkId            = sponsorId
            };
            var hierarchyRepo = new HierarchyRepository(_dbContext);

            var entity = await hierarchyRepo.AddNode(newAccountHierarchy);

            System.Console.WriteLine($"Created hierarchy: {entity.Name}, parent: {entity.ParentId}, level: {entity.LevelPath}");

            return(RedirectToAction("Index"));
        }
Esempio n. 5
0
        public async Task <IActionResult> GetHierarchy()
        {
            Logger.LogInformation(nameof(GetHierarchy));

            var customers = await _cwsAccountClient.GetMyAccounts(new Guid(UserId), customHeaders);

            var result = new AccountHierarchy
            {
                UserUid   = UserId,
                Customers = customers.Accounts.Select(c => AutoMapperUtility.Automapper.Map <AccountHierarchyCustomer>(c)).ToList()
            };

            // The previous customer endpoint was PascalCase, and calling code (eg GQL) expects that.
            return(Json(result, new JsonSerializerSettings
            {
                ContractResolver = new DefaultContractResolver()
            }));
        }
Esempio n. 6
0
        }//---------------------

        //this fucntion will get details chart of account hierarchy
        private AccountHierarchy GetAccountHierarchyDetails(String accountSysId)
        {
            AccountHierarchy accountHierarchyInfo = new AccountHierarchy();

            String strFilter = "sysid_account = '" + accountSysId + "'";

            DataRow[] selectRow = _chartOfAccountsTable.Select(strFilter);

            foreach (DataRow accountElementRow in selectRow)
            {
                accountHierarchyInfo.AccountCodeName = RemoteServerLib.ProcStatic.DataRowConvert(accountElementRow, "account_code", String.Empty) + "  (" +
                                                       RemoteServerLib.ProcStatic.DataRowConvert(accountElementRow, "account_name", String.Empty) + ")";
                accountHierarchyInfo.SystemIdAccount         = RemoteServerLib.ProcStatic.DataRowConvert(accountElementRow, "sysid_account", String.Empty);
                accountHierarchyInfo.AccountingCategorySysId = RemoteServerLib.ProcStatic.DataRowConvert(accountElementRow, "accounting_category_id", String.Empty);
            }

            return(accountHierarchyInfo);
        }//------------------
Esempio n. 7
0
        public async Task <IActionResult> GetHierarchyTree(int?parentId)
        {
            AccountHierarchy root = parentId.HasValue ?
                                    await _dbContext.AccountHierarchies.FindAsync((long)parentId) :
                                    await _dbContext.AccountHierarchies.OrderBy(a => a.Id).FirstOrDefaultAsync();

            TreeBuilder treeBuilder = new TreeBuilder(_dbContext);
            //var account = treeBuilder.BuildHierarchyTreeRecursive(root);
            var account = treeBuilder.BuildHierarchyTreeRecursive(root);

            DefaultContractResolver contractResolver = new DefaultContractResolver
            {
                NamingStrategy = new CamelCaseNamingStrategy()
            };
            string json = JsonConvert.SerializeObject(account, new JsonSerializerSettings
            {
                ContractResolver = contractResolver,
                Formatting       = Formatting.Indented
            });

            //Console.WriteLine(json);
            return(View("GetTree", json));
        }