private List <ConnectionForConversion> LoadConnectionsToBeImportedFromDatabase()
        {
            List <ConnectionForConversion> connectionsForConversions = new();

            using var dbConn = GetConnection();

            // Load connections
            using var connectionsSelectCmd   = dbConn.CreateCommand();
            connectionsSelectCmd.CommandText = "SELECT external_id, route_node_id, span_equipment_id, span_segment_indexes, terminal_equipment_id,  terminal_indexes FROM " + _segmentToTerminalConnectionsTableName + " WHERE status is null ORDER BY external_id";

            using var connectionstReader = connectionsSelectCmd.ExecuteReader();

            while (connectionstReader.Read())
            {
                var connection = new ConnectionForConversion();

                connection.ExternalId           = connectionstReader.GetString(0).Trim();
                connection.NodeId               = Guid.Parse(connectionstReader.GetString(1));
                connection.SpanEquipmentId      = Guid.Parse(connectionstReader.GetString(2));
                connection.SpanEquipmentIndexes = connectionstReader.GetString(3).Split(',').Select(x => Int32.Parse(x)).ToArray();
                connection.TerminalEquipmentId  = Guid.Parse(connectionstReader.GetString(4));

                List <(int, int)> terminalIndexes = new();

                var idSplit = connectionstReader.GetString(5).Split(',');

                foreach (var id in idSplit)
                {
                    var indexSplit = id.Split('-');
                    terminalIndexes.Add((Int32.Parse(indexSplit[0]) - 1, Int32.Parse(indexSplit[1]) - 1));
                }

                connection.TerminalEquipmentIndexes = terminalIndexes.ToArray();

                connectionsForConversions.Add(connection);
            }

            dbConn.Close();

            return(connectionsForConversions);
        }
        private Result AddConnection(NpgsqlCommand logCmd, ConnectionForConversion connection)
        {
            Guid correlationId = Guid.NewGuid();

            var commandUserContext = new UserContext("conversion", _workTaskId)
            {
                EditingRouteNodeId = connection.NodeId
            };

            if (!_utilityNetwork.TryGetEquipment <SpanEquipment>(connection.SpanEquipmentId, out var spanEquipment))
            {
                return(Result.Fail(new Error($"Connect find span equipment with id: {connection.SpanEquipmentId}")));
            }


            if (!_utilityNetwork.TryGetEquipment <TerminalEquipment>(connection.TerminalEquipmentId, out var terminalEquipment))
            {
                return(Result.Fail(new Error($"Connect find termian equipment with id: {connection.TerminalEquipmentId}")));
            }


            // Find span equipment ids
            List <ConnectSpanSegmentToTerminalOperation> connects = new();

            int terminalIndexPos = 0;

            foreach (var spanIndex in connection.SpanEquipmentIndexes)
            {
                if (spanEquipment.SpanStructures.Length > spanIndex)
                {
                    var spanSegmentId = spanEquipment.SpanStructures[spanIndex].SpanSegments[0].Id;

                    var terminalIndex = connection.TerminalEquipmentIndexes[terminalIndexPos];

                    if (terminalEquipment.TerminalStructures.Length > terminalIndex.Item1)
                    {
                        var structure = terminalEquipment.TerminalStructures[terminalIndex.Item1];

                        if (structure.Terminals.Length > terminalIndex.Item2)
                        {
                            var terminalId = structure.Terminals[terminalIndex.Item2].Id;

                            connects.Add(new ConnectSpanSegmentToTerminalOperation(spanSegmentId, terminalId));
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine($"Terminal equipment {terminalEquipment.Id} has no terminal at structure {terminalIndex.Item1} terminal index {terminalIndex.Item2}");
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine($"Terminal equipment {terminalEquipment.Id} has no structure at index {terminalIndex.Item1}");
                    }
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine($"Span equipment {spanEquipment.Id} has no structure at index {spanIndex}");
                }


                terminalIndexPos++;
            }



            // ACT (do the connect between cable and equipment)
            var connectCmd = new ConnectSpanSegmentsWithTerminalsAtRouteNode(
                correlationId: Guid.NewGuid(),
                userContext: commandUserContext,
                routeNodeId: connection.NodeId,
                connects: connects.ToArray()
                );
            var connectCmdResult = _commandDispatcher.HandleAsync <ConnectSpanSegmentsWithTerminalsAtRouteNode, Result>(connectCmd).Result;


            LogStatus((NpgsqlCommand)logCmd, _segmentToTerminalConnectionsTableName, "external_id", connection.ExternalId, connectCmdResult);

            return(connectCmdResult);
        }